@@ -59,6 +59,7 @@ const SCANNABLE_EXTENSIONS = new Set([
5959
6060const DEFAULT_MAX_SCAN_FILES = 500 ;
6161const DEFAULT_MAX_FILE_BYTES = 1024 * 1024 ;
62+ const MAX_LINE_RULE_FINDINGS_PER_RULE = 32 ;
6263const FILE_SCAN_CACHE_MAX = 5000 ;
6364const DIR_ENTRY_CACHE_MAX = 5000 ;
6465const TEST_DIRECTORY_NAMES = new Set ( [ "__fixtures__" , "__mocks__" , "__tests__" , "test" , "tests" ] ) ;
@@ -392,47 +393,65 @@ export function scanSource(source: string, filePath: string): SkillScanFinding[]
392393 const lines = source . split ( "\n" ) ;
393394 const heuristicSource = stripCommentsForHeuristics ( source ) ;
394395 const heuristicLines = heuristicSource . split ( "\n" ) ;
395- const matchedLineRules = new Set < string > ( ) ;
396396
397397 // --- Line rules ---
398398 for ( const rule of LINE_RULES ) {
399- if ( matchedLineRules . has ( rule . ruleId ) ) {
400- continue ;
401- }
402-
403399 // Skip rule entirely if context requirement not met
404400 if ( rule . requiresContext && ! rule . requiresContext . test ( source ) ) {
405401 continue ;
406402 }
407403
404+ let acceptedMatches = 0 ;
405+ let omittedMatches = 0 ;
406+ let lastOmittedLine : number | undefined ;
408407 for ( const [ i , line ] of lines . entries ( ) ) {
409- const match = rule . pattern . exec ( line ) ;
410- if ( ! match ) {
411- continue ;
412- }
408+ const matches = line . matchAll (
409+ new RegExp (
410+ rule . pattern . source ,
411+ rule . pattern . flags . includes ( "g" ) ? rule . pattern . flags : `${ rule . pattern . flags } g` ,
412+ ) ,
413+ ) ;
414+ for ( const match of matches ) {
415+ if ( rule . ruleId === "dangerous-exec" && isBenignMemberExecMatch ( line , match ) ) {
416+ continue ;
417+ }
413418
414- if ( rule . ruleId === "dangerous-exec" && isBenignMemberExecMatch ( line , match ) ) {
415- continue ;
416- }
419+ // Special handling for suspicious-network: check port
420+ if ( rule . ruleId === "suspicious-network" ) {
421+ const port = Number . parseInt ( expectDefined ( match [ 1 ] , "scanner regex capture 1" ) , 10 ) ;
422+ if ( STANDARD_PORTS . has ( port ) ) {
423+ continue ;
424+ }
425+ }
417426
418- // Special handling for suspicious-network: check port
419- if ( rule . ruleId === "suspicious-network" ) {
420- const port = Number . parseInt ( expectDefined ( match [ 1 ] , "scanner regex capture 1" ) , 10 ) ;
421- if ( STANDARD_PORTS . has ( port ) ) {
427+ if ( acceptedMatches >= MAX_LINE_RULE_FINDINGS_PER_RULE ) {
428+ omittedMatches += 1 ;
429+ lastOmittedLine = i + 1 ;
422430 continue ;
423431 }
424- }
425432
433+ // Retain distinct calls up to the cap, then aggregate every remaining match.
434+ // This keeps hostile output bounded without hiding that later sites exist.
435+ findings . push ( {
436+ ruleId : rule . ruleId ,
437+ severity : rule . severity ,
438+ file : filePath ,
439+ line : i + 1 ,
440+ message : rule . message ,
441+ evidence : formatScanEvidence ( line ) ,
442+ } ) ;
443+ acceptedMatches += 1 ;
444+ }
445+ }
446+ if ( lastOmittedLine !== undefined ) {
426447 findings . push ( {
427- ruleId : rule . ruleId ,
448+ ruleId : ` ${ rule . ruleId } -truncated` ,
428449 severity : rule . severity ,
429450 file : filePath ,
430- line : i + 1 ,
431- message : rule . message ,
432- evidence : formatScanEvidence ( line ) ,
451+ line : lastOmittedLine ,
452+ message : ` ${ omittedMatches } additional ${ rule . ruleId } matches omitted after ${ MAX_LINE_RULE_FINDINGS_PER_RULE } findings` ,
453+ evidence : `[ ${ omittedMatches } additional matches omitted after ${ MAX_LINE_RULE_FINDINGS_PER_RULE } findings]` ,
433454 } ) ;
434- matchedLineRules . add ( rule . ruleId ) ;
435- break ; // one finding per line-rule per file
436455 }
437456 }
438457
0 commit comments