Repository navigation
fix(validator): reject findings inside JS block comments - #40
harshitgavita-07 wants to merge 1 commit into
Conversation
The static pre-validator's Layer A checks whether a reported sink sits in a comment rather than actual code, but _is_comment_line only handled // line comments for JS/TS. _JS_BLOCK_COMMENT_START/END were defined but never used, so a finding pointing inside a /* ... */ block comment passed both validation layers even though the sink is provably not live code. Track block-comment state up to the reported line and reject findings whose target line opens or sits inside a block comment. Adds tests/test_static_validator.py (5 tests: multi-line block comment, single-line block comment, pipeline end-to-end, plus controls showing real sinks after a closed comment and // line comments behave as before).
[Medium impact] The change affects which findings continue through one validation workflow. Do not merge until the block-comment check stops dropping findings on executable code.
|
| start = raw.find("/*", i) | ||
| if start == -1: | ||
| break | ||
| in_block = True | ||
| i = start + 2 |
There was a problem hiding this comment.
Strings can hide real findings
The scan treats /* inside strings and // comments as a real comment opening. For example, const pattern = "/*"; followed by eval(req.query.code); makes the executable eval line fail validate_finding_basic. run_static_validation then drops that finding from passed. Track strings and line comments before changing block-comment state.
How this was verified: The preceding-line scan uses an unconditional raw.find("/*", i), and the new rejection gate removes matching findings before further checks.
Prompt To Fix With AI
This is a comment left during a code review.
Path: openhack/static_validator.py
Line: 204-208
Comment:
**Strings can hide real findings**
The scan treats `/*` inside strings and `//` comments as a real comment opening. For example, `const pattern = "/*";` followed by `eval(req.query.code);` makes the executable `eval` line fail `validate_finding_basic`. `run_static_validation` then drops that finding from `passed`. Track strings and line comments before changing block-comment state.
**How this was verified:** The preceding-line scan uses an unconditional `raw.find("/*", i)`, and the new rejection gate removes matching findings before further checks.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| if in_block: | ||
| return True | ||
| if 0 < line_number <= len(lines): | ||
| target = lines[line_number - 1] | ||
| # Strip line-number prefix if present (from read_file format) | ||
| if "\t" in target: | ||
| target = target.split("\t", 1)[1] | ||
| return bool(_JS_BLOCK_COMMENT_START.match(target.strip())) |
There was a problem hiding this comment.
Code after comments gets ignored
The target line is rejected without checking for executable code after */. Both /* note */ eval(req.query.code); and */ eval(req.query.code); after an earlier comment opening contain executable calls, but this helper returns True for both. That drops real security findings. Inspect the target line through its closing delimiter, and keep findings when executable code remains.
How this was verified: The helper returns immediately when the preceding lines leave a comment open; otherwise it rejects any target line starting with /*, without checking where that comment ends.
Prompt To Fix With AI
This is a comment left during a code review.
Path: openhack/static_validator.py
Line: 209-216
Comment:
**Code after comments gets ignored**
The target line is rejected without checking for executable code after `*/`. Both `/* note */ eval(req.query.code);` and `*/ eval(req.query.code);` after an earlier comment opening contain executable calls, but this helper returns `True` for both. That drops real security findings. Inspect the target line through its closing delimiter, and keep findings when executable code remains.
**How this was verified:** The helper returns immediately when the preceding lines leave a comment open; otherwise it rejects any target line starting with `/*`, without checking where that comment ends.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Problem
openhack/static_validator.pyis the deterministic pre-validator whose stated job (module docstring) is to filter out findings that are provably wrong before the LLM validator runs — including "Is the pattern in a comment or string literal, not actual code?"For JS/TS files,
_is_comment_lineonly detects//line comments._JS_BLOCK_COMMENT_START/_JS_BLOCK_COMMENT_ENDare defined at module level but never used, so a finding whose reported line sits inside a/* ... */block comment passes Layer A (the line is not recognized as a comment, and the sink window does contain the claimed pattern) and can also pass the tree-sitter layer, since the comment text inside an enclosing function still matches user-input accessor patterns. A "provably wrong" finding therefore survives both layers and consumes LLM validation budget.Reproduction
A finding
{file_path, line_number: 4, category: "rce"}(theevalline, inside the block comment) is currently accepted byvalidate_finding_basicand byrun_static_validation.Fix
Add
_is_in_block_comment(lines, line_number), which tracks/*/*/state across the lines preceding the reported line and also treats a target line that itself opens a block comment as commented.validate_finding_basicrejects the finding when the reported line is inside a block comment (non-Python files), using the previously unused module-level regexes. Behavior for//comments, Python comments/docstrings, and real sinks after a closed comment block is unchanged.Tests
New
tests/test_static_validator.py(5 tests, all failing-before/passing-after where applicable):/* ... */comment is rejectedrun_static_validationdrops a block-comment finding//line comments are still rejectedFull suite: 516 passed (511 existing + 5 new).