Repository navigation
fix(validator): reject findings inside JS block comments #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,6 +184,39 @@ def _is_comment_line(line: str, file_path: str) -> bool: | |
| return bool(_JS_LINE_COMMENT.match(stripped)) | ||
|
|
||
|
|
||
| def _is_in_block_comment(lines: list[str], line_number: int) -> bool: | ||
| """Check if a 1-based line sits inside a JS-style /* ... */ block comment. | ||
|
|
||
| Tracks block-comment state across the lines preceding the target, and | ||
| also treats a target line that itself opens a block comment as commented. | ||
| """ | ||
| in_block = False | ||
| for raw in lines[: line_number - 1]: | ||
| i = 0 | ||
| while i < len(raw): | ||
| if in_block: | ||
| end = raw.find("*/", i) | ||
| if end == -1: | ||
| break | ||
| in_block = False | ||
| i = end + 2 | ||
| else: | ||
| start = raw.find("/*", i) | ||
| if start == -1: | ||
| break | ||
| in_block = True | ||
| i = start + 2 | ||
| 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())) | ||
|
Comment on lines
+209
to
+216
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The target line is rejected without checking for executable code after How this was verified: The helper returns immediately when the preceding lines leave a comment open; otherwise it rejects any target line starting with Prompt To Fix With AIThis 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. |
||
| return False | ||
|
|
||
|
|
||
| def _line_has_sink_pattern(line: str, category: str) -> bool: | ||
| """Check if a line contains a sink pattern for the given vuln category.""" | ||
| normalized_cat = category.lower().replace(" ", "_").replace("-", "_") | ||
|
|
@@ -228,6 +261,8 @@ def validate_finding_basic(finding: dict, fs_tools) -> tuple[bool, str]: | |
| # Check 2a: Is this line a comment? | ||
| if _is_comment_line(target_line, file_path): | ||
| return False, f"Reported line {line_number} is a comment" | ||
| if not file_path.endswith(".py") and _is_in_block_comment(lines, line_number): | ||
| return False, f"Reported line {line_number} is inside a block comment" | ||
|
|
||
| # Check 2b: Does the line contain anything related to the claimed category? | ||
| # Only reject if we have specific sink patterns AND the line is clearly unrelated | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| """Tests for the deterministic static pre-validator.""" | ||
|
|
||
| from openhack.static_validator import run_static_validation, validate_finding_basic | ||
|
|
||
|
|
||
| def _write(fs_tools, rel_path, content): | ||
| target = fs_tools.jail_dir / rel_path | ||
| target.parent.mkdir(parents=True, exist_ok=True) | ||
| target.write_text(content) | ||
|
|
||
|
|
||
| def test_finding_inside_js_block_comment_is_rejected(fs_tools): | ||
| """A sink reported inside a /* ... */ block comment is not real code. | ||
|
|
||
| The validator's stated purpose is to reject findings where the pattern | ||
| sits in a comment rather than actual code. Line comments (//) are | ||
| handled, but block comments were not, so a finding pointing inside one | ||
| passed every layer. | ||
| """ | ||
| _write( | ||
| fs_tools, | ||
| "app/handler.js", | ||
| "function handler(req, res) {\n" | ||
| " /*\n" | ||
| " * Legacy debug path, disabled:\n" | ||
| " * eval(req.query.code)\n" | ||
| " */\n" | ||
| " const q = sanitize(req.query.code);\n" | ||
| " res.send(q);\n" | ||
| "}\n", | ||
| ) | ||
| finding = { | ||
| "file_path": "app/handler.js", | ||
| "line_number": 4, # the eval(...) line, inside the block comment | ||
| "category": "rce", | ||
| } | ||
| is_valid, reason = validate_finding_basic(finding, fs_tools) | ||
| assert not is_valid | ||
| assert "comment" in reason.lower() | ||
|
|
||
|
|
||
| def test_finding_on_single_line_block_comment_is_rejected(fs_tools): | ||
| _write( | ||
| fs_tools, | ||
| "app/util.js", | ||
| "/* eval(userInput) -- kept for reference, do not enable */\n" | ||
| "export function util() { return 1; }\n", | ||
| ) | ||
| finding = { | ||
| "file_path": "app/util.js", | ||
| "line_number": 1, | ||
| "category": "rce", | ||
| } | ||
| is_valid, reason = validate_finding_basic(finding, fs_tools) | ||
| assert not is_valid | ||
| assert "comment" in reason.lower() | ||
|
|
||
|
|
||
| def test_real_sink_after_a_block_comment_still_passes(fs_tools): | ||
| """The block-comment state must reset at */ — real code after a comment | ||
| block is not a comment.""" | ||
| _write( | ||
| fs_tools, | ||
| "app/handler.js", | ||
| "function handler(req, res) {\n" | ||
| " /* disabled: eval(req.query.old) */\n" | ||
| " eval(req.query.code);\n" | ||
| "}\n", | ||
| ) | ||
| finding = { | ||
| "file_path": "app/handler.js", | ||
| "line_number": 3, | ||
| "category": "rce", | ||
| } | ||
| is_valid, reason = validate_finding_basic(finding, fs_tools) | ||
| assert is_valid, reason | ||
|
|
||
|
|
||
| def test_line_comment_still_rejected(fs_tools): | ||
| _write( | ||
| fs_tools, | ||
| "app/handler.js", | ||
| "function handler(req) {\n" | ||
| " // eval(req.query.code)\n" | ||
| " return 1;\n" | ||
| "}\n", | ||
| ) | ||
| finding = { | ||
| "file_path": "app/handler.js", | ||
| "line_number": 2, | ||
| "category": "rce", | ||
| } | ||
| is_valid, reason = validate_finding_basic(finding, fs_tools) | ||
| assert not is_valid | ||
| assert "comment" in reason.lower() | ||
|
|
||
|
|
||
| def test_run_static_validation_drops_block_comment_finding(fs_tools): | ||
| """End-to-end: a block-comment finding must not survive the pipeline.""" | ||
| _write( | ||
| fs_tools, | ||
| "app/handler.js", | ||
| "function handler(req, res) {\n" | ||
| " /*\n" | ||
| " * eval(req.query.code)\n" | ||
| " */\n" | ||
| " res.send('ok');\n" | ||
| "}\n", | ||
| ) | ||
| findings = [ | ||
| { | ||
| "file_path": "app/handler.js", | ||
| "line_number": 3, | ||
| "category": "rce", | ||
| } | ||
| ] | ||
| passed, rejected, stats = run_static_validation(findings, fs_tools) | ||
| assert passed == [] | ||
| assert len(rejected) == 1 | ||
| assert stats["basic_rejected"] == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scan treats
/*inside strings and//comments as a real comment opening. For example,const pattern = "/*";followed byeval(req.query.code);makes the executableevalline failvalidate_finding_basic.run_static_validationthen drops that finding frompassed. 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