From 55bf24136acf56646e54da56a441cf94857ce066 Mon Sep 17 00:00:00 2001 From: harshitgavita-07 Date: Fri, 13 Nov 2026 14:58:15 +0530 Subject: [PATCH] fix(validator): reject findings inside JS block comments 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). --- openhack/static_validator.py | 35 ++++++++++ tests/test_static_validator.py | 120 +++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 tests/test_static_validator.py diff --git a/openhack/static_validator.py b/openhack/static_validator.py index a9c2c00..0ff511c 100644 --- a/openhack/static_validator.py +++ b/openhack/static_validator.py @@ -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())) + 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 diff --git a/tests/test_static_validator.py b/tests/test_static_validator.py new file mode 100644 index 0000000..e783299 --- /dev/null +++ b/tests/test_static_validator.py @@ -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