@@ -476,35 +476,53 @@ describe('Database.sql (async)', () => {
476476 }
477477 } )
478478
479- it ( 'should sanitize SQL Injection as table name' , async ( ) => {
480- const database = await getTestingDatabaseAsync ( )
481-
482- const databaseName = sanitizeSQLiteIdentifier ( 'people.sqlite; SELECT * FROM people; -- ' )
483- await expect ( database . sql ( `USE DATABASE ${ databaseName } ` ) ) . rejects . toThrow ( 'Database name contains invalid characters (people.sqlite; SELECT * FROM people; --).' )
484-
485- const table = sanitizeSQLiteIdentifier ( 'people; -- ' )
486- await expect ( database . sql ( `SELECT * FROM ${ table } WHERE people = 1` ) ) . rejects . toThrow ( 'no such table: people; --' )
479+ describe ( 'should sanitize identifiers' , ( ) => {
480+ it ( 'should sanitize database name and run the query' , async ( ) => {
481+ const database = await getTestingDatabaseAsync ( )
482+
483+ const databaseName = sanitizeSQLiteIdentifier ( 'people.sqlite' )
484+ await expect ( database . sql ( `USE DATABASE ${ databaseName } ` ) ) . resolves . toBe ( 'OK' )
485+ } )
486+
487+ it ( 'should sanitize table name and run the query' , async ( ) => {
488+ const database = await getTestingDatabaseAsync ( )
489+
490+ const table = sanitizeSQLiteIdentifier ( 'people' )
491+ await expect ( database . sql ( `USE DATABASE people.sqlite; SELECT id FROM ${ table } LIMIT 1` ) ) . resolves . toMatchObject ( [ { id : 1 } ] )
492+ } )
493+
494+ it ( 'should sanitize SQL Injection as table name' , async ( ) => {
495+ const database = await getTestingDatabaseAsync ( )
496+
497+ const databaseName = sanitizeSQLiteIdentifier ( 'people.sqlite; SELECT * FROM people; -- ' )
498+ await expect ( database . sql ( `USE DATABASE ${ databaseName } ` ) ) . rejects . toThrow (
499+ 'Database name contains invalid characters (people.sqlite; SELECT * FROM people; --).'
500+ )
501+
502+ const table = sanitizeSQLiteIdentifier ( 'people; -- ' )
503+ await expect ( database . sql ( `SELECT * FROM ${ table } WHERE people = 1` ) ) . rejects . toThrow ( 'no such table: people; --' )
504+ } )
487505 } )
488506
489507 it ( 'should throw exception when using table name as binding' , async ( ) => {
490- const database = await getTestingDatabaseAsync ( )
508+ const database = await getTestingDatabaseAsync ( )
491509 const table = 'people'
492510 await expect ( database . sql `USE DATABASE people.sqlite; SELECT * FROM ${ table } ` ) . rejects . toThrow ( 'near "?": syntax error' )
493511 } )
494512
495513 it ( 'should built in commands accept bindings' , async ( ) => {
496514 const database = await getTestingDatabaseAsync ( )
497-
515+
498516 let databaseName = 'people.sqlite'
499517 await expect ( database . sql `USE DATABASE ${ databaseName } ` ) . resolves . toBe ( 'OK' )
500-
518+
501519 databaseName = 'people.sqlite; SELECT * FROM people'
502520 await expect ( database . sql `USE DATABASE ${ databaseName } ` ) . rejects . toThrow ( 'Database name contains invalid characters (people.sqlite; SELECT * FROM people).' )
503-
521+
504522 let key = 'logo_level'
505523 let value = 'debug'
506524 await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
507-
525+
508526 key = 'logo_level'
509527 value = 'debug; DROP TABLE people'
510528 await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
0 commit comments