Skip to content

Commit 2811e30

Browse files
committed
Add more tests
1 parent 3956d95 commit 2811e30

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

library/vulnerabilities/sql-injection/detectSQLInjection.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { basename, join } from "path";
22
import * as t from "tap";
33
import { readFileSync } from "fs";
44
import { escapeStringRegexp } from "../../helpers/escapeStringRegexp";
5-
import { SQL_DANGEROUS_IN_STRING } from "./config";
5+
import { SQL_DANGEROUS_IN_STRING, SQL_KEYWORDS } from "./config";
66
import { detectSQLInjection } from "./detectSQLInjection";
77
import { SQLDialectMySQL } from "./dialects/SQLDialectMySQL";
88
import { SQLDialectPostgres } from "./dialects/SQLDialectPostgres";
@@ -361,6 +361,36 @@ t.test("It does not match VIEW keyword", async () => {
361361
isNotSqlInjection(query2, "view");
362362
});
363363

364+
t.test("It does not flag SQL keyword if part of another word", async () => {
365+
SQL_KEYWORDS.forEach((keyword) => {
366+
isNotSqlInjection(
367+
`
368+
SELECT id,
369+
business_id,
370+
name,
371+
created_at,
372+
updated_at
373+
FROM ${keyword}
374+
WHERE business_id = ?
375+
`,
376+
keyword
377+
);
378+
379+
isNotSqlInjection(
380+
`
381+
SELECT id,
382+
business_id,
383+
name,
384+
created_at,
385+
updated_at
386+
FROM ${keyword.toLowerCase()}
387+
WHERE business_id = ?
388+
`,
389+
keyword
390+
);
391+
});
392+
});
393+
364394
const files = [
365395
// Taken from https://github.com/payloadbox/sql-injection-payload-list/tree/master
366396
join(__dirname, "payloads", "Auth_Bypass.txt"),

library/vulnerabilities/sql-injection/userInputContainsSQLSyntax.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { readFileSync } from "fs";
22
import { join } from "path";
33
import * as t from "tap";
4+
import { SQL_KEYWORDS } from "./config";
45
import { SQLDialectMySQL } from "./dialects/SQLDialectMySQL";
56
import { SQLDialectPostgres } from "./dialects/SQLDialectPostgres";
67
import { userInputContainsSQLSyntax } from "./userInputContainsSQLSyntax";
@@ -13,6 +14,29 @@ t.test("it does not flag common SQL keywords", async () => {
1314
t.same(userInputContainsSQLSyntax("SELECT", new SQLDialectMySQL()), false);
1415
});
1516

17+
t.test("it ignores alphanumeric input", async () => {
18+
t.same(userInputContainsSQLSyntax("1", new SQLDialectMySQL()), false);
19+
t.same(userInputContainsSQLSyntax("123", new SQLDialectMySQL()), false);
20+
t.same(userInputContainsSQLSyntax("1313", new SQLDialectMySQL()), false);
21+
t.same(userInputContainsSQLSyntax("0", new SQLDialectMySQL()), false);
22+
t.same(userInputContainsSQLSyntax("abc", new SQLDialectMySQL()), false);
23+
t.same(userInputContainsSQLSyntax("ABC", new SQLDialectMySQL()), false);
24+
});
25+
26+
t.test("it does not flag SQL keyword as dangerous", async () => {
27+
// They just contain alpha characters
28+
SQL_KEYWORDS.forEach((keyword) => {
29+
t.same(
30+
userInputContainsSQLSyntax(keyword.toLowerCase(), new SQLDialectMySQL()),
31+
false
32+
);
33+
t.same(
34+
userInputContainsSQLSyntax(keyword.toUpperCase(), new SQLDialectMySQL()),
35+
false
36+
);
37+
});
38+
});
39+
1640
const files = [
1741
// Taken from https://github.com/payloadbox/sql-injection-payload-list/tree/master
1842
join(__dirname, "payloads", "Auth_Bypass.txt"),

0 commit comments

Comments
 (0)