11'use strict'
22
3+ const path = require ( 'path' )
4+ const os = require ( 'os' )
5+ const fs = require ( 'fs' )
6+ const { assert } = require ( 'chai' )
37const { prepareTestServerForIast } = require ( '../utils' )
48const { storage } = require ( '../../../../../datadog-core' )
59const iastContextFunctions = require ( '../../../../src/appsec/iast/iast-context' )
@@ -9,6 +13,7 @@ const vulnerabilityReporter = require('../../../../src/appsec/iast/vulnerability
913describe ( 'sql-injection-analyzer with mysql2' , ( ) => {
1014 let mysql2
1115 let connection
16+
1217 withVersions ( 'mysql2' , 'mysql2' , version => {
1318 prepareTestServerForIast ( 'mysql2' , ( testThatRequestHasVulnerability , testThatRequestHasNoVulnerability ) => {
1419 beforeEach ( ( ) => {
@@ -26,22 +31,43 @@ describe('sql-injection-analyzer with mysql2', () => {
2631 connection . end ( done )
2732 } )
2833
29- describe ( 'has vulnerability' , ( ) => {
34+ describe ( 'has vulnerability in the right file/line' , ( ) => {
35+ let tmpFilePath
36+ const vulnerableMethodFilename = 'mysql2-vulnerable-method.js'
37+
38+ beforeEach ( ( ) => {
39+ tmpFilePath = path . join ( os . tmpdir ( ) , vulnerableMethodFilename )
40+
41+ try {
42+ fs . unlinkSync ( tmpFilePath )
43+ } catch ( e ) {
44+ // ignore the error
45+ }
46+ const src = path . join ( __dirname , 'resources' , vulnerableMethodFilename )
47+
48+ fs . copyFileSync ( src , tmpFilePath )
49+ } )
50+
51+ afterEach ( ( ) => {
52+ try {
53+ fs . unlinkSync ( tmpFilePath )
54+ } catch ( e ) {
55+ // ignore the error
56+ }
57+ } )
58+
3059 testThatRequestHasVulnerability ( ( ) => {
31- return new Promise ( ( resolve , reject ) => {
32- const store = storage ( 'legacy' ) . getStore ( )
33- const iastCtx = iastContextFunctions . getIastContext ( store )
34- let sql = 'SELECT 1'
35- sql = newTaintedString ( iastCtx , sql , 'param' , 'Request' )
36- connection . query ( sql , function ( err ) {
37- if ( err ) {
38- reject ( err )
39- } else {
40- resolve ( )
41- }
42- } )
43- } )
44- } , 'SQL_INJECTION' )
60+ const store = storage ( 'legacy' ) . getStore ( )
61+ const iastCtx = iastContextFunctions . getIastContext ( store )
62+ let sql = 'SELECT 1'
63+ sql = newTaintedString ( iastCtx , sql , 'param' , 'Request' )
64+ const vulnerableMethod = require ( tmpFilePath )
65+
66+ return vulnerableMethod ( connection , sql )
67+ } , 'SQL_INJECTION' , 1 , function ( [ vulnerability ] ) {
68+ assert . isTrue ( vulnerability . location . path . endsWith ( vulnerableMethodFilename ) )
69+ assert . equal ( vulnerability . location . line , 5 )
70+ } )
4571 } )
4672
4773 describe ( 'has no vulnerability' , ( ) => {
0 commit comments