1
1
'use strict'
2
2
3
+ const path = require ( 'path' )
4
+ const os = require ( 'os' )
5
+ const fs = require ( 'fs' )
6
+ const { assert } = require ( 'chai' )
3
7
const { prepareTestServerForIast } = require ( '../utils' )
4
8
const { storage } = require ( '../../../../../datadog-core' )
5
9
const iastContextFunctions = require ( '../../../../src/appsec/iast/iast-context' )
@@ -9,6 +13,7 @@ const vulnerabilityReporter = require('../../../../src/appsec/iast/vulnerability
9
13
describe ( 'sql-injection-analyzer with mysql2' , ( ) => {
10
14
let mysql2
11
15
let connection
16
+
12
17
withVersions ( 'mysql2' , 'mysql2' , version => {
13
18
prepareTestServerForIast ( 'mysql2' , ( testThatRequestHasVulnerability , testThatRequestHasNoVulnerability ) => {
14
19
beforeEach ( ( ) => {
@@ -26,22 +31,43 @@ describe('sql-injection-analyzer with mysql2', () => {
26
31
connection . end ( done )
27
32
} )
28
33
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
+
30
59
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
+ } )
45
71
} )
46
72
47
73
describe ( 'has no vulnerability' , ( ) => {
0 commit comments