@@ -3,8 +3,11 @@ import * as t from "tap";
3
3
import { readFileSync } from "fs" ;
4
4
import { escapeStringRegexp } from "../../helpers/escapeStringRegexp" ;
5
5
import { detectSQLInjection } from "./detectSQLInjection" ;
6
+ import { SQLDialectClickHouse } from "./dialects/SQLDialectClickHouse" ;
7
+ import { SQLDialectGeneric } from "./dialects/SQLDialectGeneric" ;
6
8
import { SQLDialectMySQL } from "./dialects/SQLDialectMySQL" ;
7
9
import { SQLDialectPostgres } from "./dialects/SQLDialectPostgres" ;
10
+ import { SQLDialectSQLite } from "./dialects/SQLDialectSQLite" ;
8
11
9
12
t . test ( "It ignores invalid queries" , async ( ) => {
10
13
isNotSqlInjection ( "SELECT * FROM users WHERE id = 'users\\'" , "users\\" ) ;
@@ -96,7 +99,8 @@ t.test("User input is multiline", async () => {
96
99
`SELECT * FROM users WHERE id = 'a'
97
100
OR 1=1#'` ,
98
101
`a'
99
- OR 1=1#`
102
+ OR 1=1#` ,
103
+ [ new SQLDialectGeneric ( ) , new SQLDialectMySQL ( ) ]
100
104
) ;
101
105
102
106
isNotSqlInjection (
@@ -314,28 +318,46 @@ for (const file of files) {
314
318
}
315
319
}
316
320
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
- ) ;
321
+ function isSqlInjection (
322
+ sql : string ,
323
+ input : string ,
324
+ dialects = [
325
+ new SQLDialectGeneric ( ) ,
326
+ new SQLDialectMySQL ( ) ,
327
+ new SQLDialectPostgres ( ) ,
328
+ new SQLDialectSQLite ( ) ,
329
+ new SQLDialectClickHouse ( ) ,
330
+ ]
331
+ ) {
332
+ if ( dialects . length === 0 ) {
333
+ throw new Error ( "No dialects provided" ) ;
334
+ }
335
+
336
+ for ( const dialect of dialects ) {
337
+ t . same (
338
+ detectSQLInjection ( sql , input , dialect ) ,
339
+ true ,
340
+ `${ sql } (${ dialect . constructor . name } )`
341
+ ) ;
342
+ }
328
343
}
329
344
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
- ) ;
345
+ function isNotSqlInjection (
346
+ sql : string ,
347
+ input : string ,
348
+ dialects = [
349
+ new SQLDialectGeneric ( ) ,
350
+ new SQLDialectMySQL ( ) ,
351
+ new SQLDialectPostgres ( ) ,
352
+ new SQLDialectSQLite ( ) ,
353
+ new SQLDialectClickHouse ( ) ,
354
+ ]
355
+ ) {
356
+ for ( const dialect of dialects ) {
357
+ t . same (
358
+ detectSQLInjection ( sql , input , dialect ) ,
359
+ false ,
360
+ `${ sql } (${ dialect . constructor . name } )`
361
+ ) ;
362
+ }
341
363
}
0 commit comments