Skip to content

Commit 86f6e2a

Browse files
committed
Run tests for supported dialects
`#` comments are only supported in MySQL and generic.
1 parent 5849a19 commit 86f6e2a

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

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

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as t from "tap";
33
import { readFileSync } from "fs";
44
import { escapeStringRegexp } from "../../helpers/escapeStringRegexp";
55
import { detectSQLInjection } from "./detectSQLInjection";
6+
import { SQLDialectClickHouse } from "./dialects/SQLDialectClickHouse";
7+
import { SQLDialectGeneric } from "./dialects/SQLDialectGeneric";
68
import { SQLDialectMySQL } from "./dialects/SQLDialectMySQL";
79
import { SQLDialectPostgres } from "./dialects/SQLDialectPostgres";
810

@@ -96,7 +98,8 @@ t.test("User input is multiline", async () => {
9698
`SELECT * FROM users WHERE id = 'a'
9799
OR 1=1#'`,
98100
`a'
99-
OR 1=1#`
101+
OR 1=1#`,
102+
[new SQLDialectGeneric(), new SQLDialectMySQL()]
100103
);
101104

102105
isNotSqlInjection(
@@ -314,28 +317,46 @@ for (const file of files) {
314317
}
315318
}
316319

317-
function isSqlInjection(sql: string, input: string) {
318-
t.same(
319-
detectSQLInjection(sql, input, new SQLDialectMySQL()),
320-
true,
321-
`${sql} (mysql)`
322-
);
323-
t.same(
324-
detectSQLInjection(sql, input, new SQLDialectPostgres()),
325-
true,
326-
`${sql} (postgres)`
327-
);
320+
function isSqlInjection(
321+
sql: string,
322+
input: string,
323+
dialects = [
324+
new SQLDialectGeneric(),
325+
new SQLDialectMySQL(),
326+
new SQLDialectPostgres(),
327+
new SQLDialectClickHouse(),
328+
new SQLDialectClickHouse(),
329+
]
330+
) {
331+
if (dialects.length === 0) {
332+
throw new Error("No dialects provided");
333+
}
334+
335+
for (const dialect of dialects) {
336+
t.same(
337+
detectSQLInjection(sql, input, dialect),
338+
true,
339+
`${sql} (${dialect.constructor.name})`
340+
);
341+
}
328342
}
329343

330-
function isNotSqlInjection(sql: string, input: string) {
331-
t.same(
332-
detectSQLInjection(sql, input, new SQLDialectMySQL()),
333-
false,
334-
`${sql} (mysql)`
335-
);
336-
t.same(
337-
detectSQLInjection(sql, input, new SQLDialectPostgres()),
338-
false,
339-
`${sql} (postgres)`
340-
);
344+
function isNotSqlInjection(
345+
sql: string,
346+
input: string,
347+
dialects = [
348+
new SQLDialectGeneric(),
349+
new SQLDialectMySQL(),
350+
new SQLDialectPostgres(),
351+
new SQLDialectClickHouse(),
352+
new SQLDialectClickHouse(),
353+
]
354+
) {
355+
for (const dialect of dialects) {
356+
t.same(
357+
detectSQLInjection(sql, input, dialect),
358+
false,
359+
`${sql} (${dialect.constructor.name})`
360+
);
361+
}
341362
}

0 commit comments

Comments
 (0)