@@ -75,13 +75,35 @@ describe('SQL Injection Prevention', () => {
7575 expect ( sanitizePathPattern ( 'src.test.file' ) ) . toBe ( 'src_test_file' ) ;
7676 } ) ;
7777
78- it ( 'should throw on SQL injection attempts' , ( ) => {
79- expect ( ( ) => sanitizePathPattern ( "src'; DROP TABLE--/" ) ) . toThrow (
80- InvalidFilterError
81- ) ;
82- expect ( ( ) => sanitizePathPattern ( "test' OR '1'='1" ) ) . toThrow (
83- InvalidFilterError
84- ) ;
78+ it ( 'should handle @ symbols in scoped package paths' , ( ) => {
79+ expect ( sanitizePathPattern ( '@scope/package/file' ) ) . toBe ( '_scope_package_file' ) ;
80+ expect ( sanitizePathPattern ( 'node_modules/@types/node' ) ) . toBe ( 'node_modules__types_node' ) ;
81+ } ) ;
82+
83+ it ( 'should handle spaces in paths' , ( ) => {
84+ expect ( sanitizePathPattern ( 'path with spaces/file' ) ) . toBe ( 'path_with_spaces_file' ) ;
85+ expect ( sanitizePathPattern ( 'My Documents/project' ) ) . toBe ( 'My_Documents_project' ) ;
86+ } ) ;
87+
88+ it ( 'should handle parentheses in paths' , ( ) => {
89+ expect ( sanitizePathPattern ( 'file (copy)' ) ) . toBe ( 'file__copy_' ) ;
90+ expect ( sanitizePathPattern ( 'Component(HOC).tsx' ) ) . toBe ( 'Component_HOC__tsx' ) ;
91+ } ) ;
92+
93+ it ( 'should handle plus signs in paths' , ( ) => {
94+ expect ( sanitizePathPattern ( 'c++/main' ) ) . toBe ( 'c___main' ) ;
95+ } ) ;
96+
97+ it ( 'should handle colons in Windows paths' , ( ) => {
98+ expect ( sanitizePathPattern ( 'C:\\Users\\test' ) ) . toBe ( 'C__Users_test' ) ;
99+ } ) ;
100+
101+ it ( 'should sanitize SQL injection attempts instead of throwing' , ( ) => {
102+ // These now get sanitized rather than throwing, since the catch-all
103+ // replaces all unsafe characters with underscores
104+ const result = sanitizePathPattern ( "src'; DROP TABLE--/" ) ;
105+ expect ( result ) . toBe ( 'src___DROP_TABLE--_' ) ;
106+ expect ( result ) . toMatch ( / ^ [ a - z A - Z 0 - 9 _ \- % ] + $ / ) ;
85107 } ) ;
86108 } ) ;
87109
@@ -93,10 +115,12 @@ describe('SQL Injection Prevention', () => {
93115 expect ( sanitizeGlobPattern ( 'src/??.ts' ) ) . toBe ( 'src____ts' ) ;
94116 } ) ;
95117
96- it ( 'should throw on SQL injection in glob patterns' , ( ) => {
97- expect ( ( ) => sanitizeGlobPattern ( "*'; DROP TABLE--" ) ) . toThrow (
98- InvalidFilterError
99- ) ;
118+ it ( 'should sanitize SQL injection in glob patterns' , ( ) => {
119+ // SQL injection attempts are now sanitized (unsafe chars replaced with _)
120+ const result = sanitizeGlobPattern ( "*'; DROP TABLE--" ) ;
121+ // * -> %, ' -> _, ; -> _, space -> _
122+ expect ( result ) . toBe ( '%___DROP_TABLE--' ) ;
123+ expect ( result ) . toMatch ( / ^ [ a - z A - Z 0 - 9 _ % - ] + $ / ) ;
100124 } ) ;
101125 } ) ;
102126
@@ -164,16 +188,21 @@ describe('SQL Injection Prevention', () => {
164188 expect ( result ) . toBe ( "id LIKE 'src%' AND language = 'typescript'" ) ;
165189 } ) ;
166190
167- it ( 'should throw on SQL injection in path' , ( ) => {
168- expect ( ( ) => buildSafeFilter ( { path : "'; DROP TABLE--" } ) ) . toThrow (
169- InvalidFilterError
170- ) ;
191+ it ( 'should sanitize SQL injection in path' , ( ) => {
192+ // SQL injection attempts are now sanitized (unsafe chars replaced with _)
193+ const result = buildSafeFilter ( { path : "'; DROP TABLE--" } ) ;
194+ expect ( result ) . toBe ( "id LIKE '___DROP_TABLE--%'" ) ;
195+ // The sanitized inner value contains only safe characters
196+ // (the outer quotes are SQL string delimiters, not part of the user input)
197+ const innerValue = result ?. match ( / i d L I K E ' ( [ ^ ' ] + ) ' / ) ?. [ 1 ] ;
198+ expect ( innerValue ) . toMatch ( / ^ [ a - z A - Z 0 - 9 _ % - ] + $ / ) ;
171199 } ) ;
172200
173- it ( 'should throw on SQL injection in file pattern' , ( ) => {
174- expect ( ( ) => buildSafeFilter ( { filePattern : "*.ts'; DROP TABLE--" } ) ) . toThrow (
175- InvalidFilterError
176- ) ;
201+ it ( 'should sanitize SQL injection in file pattern' , ( ) => {
202+ // SQL injection attempts are now sanitized (unsafe chars replaced with _)
203+ const result = buildSafeFilter ( { filePattern : "*.ts'; DROP TABLE--" } ) ;
204+ // * -> %, . -> _, ' -> _, ; -> _, space -> _, etc.
205+ expect ( result ) . toBe ( "id LIKE '%%_ts___DROP_TABLE--'" ) ;
177206 } ) ;
178207 } ) ;
179208
@@ -192,23 +221,34 @@ describe('SQL Injection Prevention', () => {
192221 ] ;
193222
194223 it . each ( sqlInjectionPayloads ) (
195- 'should reject injection payload: %s' ,
224+ 'should reject injection payload via validateFilterPattern : %s' ,
196225 ( payload ) => {
197226 expect ( validateFilterPattern ( payload ) ) . toBe ( false ) ;
198227 }
199228 ) ;
200229
201230 it . each ( sqlInjectionPayloads ) (
202- 'should throw on sanitizePathPattern with : %s' ,
231+ 'should sanitize injection payload and produce safe result : %s' ,
203232 ( payload ) => {
204- expect ( ( ) => sanitizePathPattern ( payload ) ) . toThrow ( InvalidFilterError ) ;
233+ // sanitizePathPattern now sanitizes rather than throws
234+ const result = sanitizePathPattern ( payload ) ;
235+ // The result should only contain safe characters
236+ expect ( validateFilterPattern ( result ) ) . toBe ( true ) ;
237+ // Should not contain any SQL-dangerous characters
238+ expect ( result ) . not . toMatch ( / [ ' " ; \( \) = < > ] / ) ;
205239 }
206240 ) ;
207241
208242 it . each ( sqlInjectionPayloads ) (
209- 'should throw on buildSafeFilter path with : %s' ,
243+ 'should produce safe filter for buildSafeFilter path: %s' ,
210244 ( payload ) => {
211- expect ( ( ) => buildSafeFilter ( { path : payload } ) ) . toThrow ( InvalidFilterError ) ;
245+ // buildSafeFilter now sanitizes rather than throws
246+ const result = buildSafeFilter ( { path : payload } ) ;
247+ // Should be a valid LIKE condition
248+ expect ( result ) . toMatch ( / ^ i d L I K E ' [ a - z A - Z 0 - 9 _ % - ] + ' $ / ) ;
249+ // Should not contain unescaped quotes (the one at start/end are the SQL string delimiters)
250+ const innerContent = result ?. slice ( 9 , - 1 ) ; // Extract content between "id LIKE '" and "'"
251+ expect ( innerContent ) . not . toContain ( "'" ) ;
212252 }
213253 ) ;
214254 } ) ;
0 commit comments