Skip to content

Commit c8d201b

Browse files
committed
fix(patterns): restore constructed-path findings on windowed views
Above SECURITY_VIEW_WINDOW_CHARS the runner hands lexical modules window slices instead of the whole file, so peek_python_ast never hits the scan's whole-file cache entry and the constructed-path analysis silently dropped its findings (regression introduced in 56d121c; 40cea88 reported them). On a peek miss, parse the provided content directly behind the same plain textual join gate, restoring the 40cea88 behavior for window slices while keeping the 17:45 improvements for whole files: alias-aware gate, shared AST reuse, multiline span dedup, and scan-count discipline (fragments without a plausible join call never parse). Regression tests: - unit: a windowed fragment under a scan keeps PE3 coverage, and a join-free fragment triggers no parse - graph: a ~376k-char file keeps its constructed-path PE3 through the full scan Signed-off-by: Deepak Jain <[email protected]>
1 parent 56d121c commit c8d201b

3 files changed

Lines changed: 92 additions & 9 deletions

File tree

‎src/skillspector/nodes/analyzers/static_patterns_privilege_escalation.py‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -714,23 +714,29 @@ def _constructed_sensitive_paths(
714714
``from os.path import join as j``, ``from os import path``) are recognized
715715
without reparsing tricks. The scan's shared parse is reused whenever this
716716
runs inside the runner (the cache key published by node()); standalone
717-
callers get a single on-demand parse, while windowed view fragments under
718-
a scan are skipped because the shared tree only covers whole files.
719-
Only fully-literal positional argument lists are resolved; anything
720-
dynamic is left to the existing pattern loop. Unparseable content simply
721-
yields no findings here.
717+
callers get a single on-demand parse. Windowed view fragments under a scan
718+
miss the whole-file cache entry and are parsed directly behind the same
719+
textual join gate, so large files keep their findings instead of silently
720+
dropping them. Only fully-literal positional argument lists are resolved;
721+
anything dynamic is left to the existing pattern loop. Unparseable content
722+
simply yields no findings here.
722723
"""
723724
if python_ast is None:
724725
cache_key = _scan_python_ast_cache_key.get()
725726
if cache_key is not None:
726727
python_ast = peek_python_ast(cache_key, content, file_path)
727-
if python_ast is None:
728+
if python_ast is None and _join_call_hint({}).search(content):
728729
# A windowed view fragment, not the scan's whole file: the
729-
# shared tree does not cover it, and parsing fragments here
730-
# would break the runner's parse-once invariant.
731-
return []
730+
# shared tree does not cover this slice, so parse the
731+
# fragment directly to avoid silently dropping large-file
732+
# findings. The plain textual gate keeps the runner's
733+
# scan-count discipline for fragments without a plausible
734+
# join call.
735+
python_ast = parse_python_source(content, file_path)
732736
else:
733737
python_ast = parse_python_source(content, file_path)
738+
if python_ast is None:
739+
return []
734740
tree = python_ast.tree
735741
if tree is None:
736742
return []

‎tests/nodes/analyzers/test_shared_python_ast.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
behavioral_taint_tracking,
1717
static_patterns_data_exfiltration,
1818
static_patterns_output_handling,
19+
static_runner,
1920
)
2021
from skillspector.nodes.build_context import build_context
2122
from skillspector.nodes.deduplicate import deduplicate
@@ -163,3 +164,27 @@ def count_parse(*args, **kwargs):
163164
)
164165
assert parse_calls == 1
165166
assert JsonPlusSerializer().dumps_typed(result)
167+
168+
169+
def test_graph_scan_reports_constructed_path_above_view_window_chars(tmp_path) -> None:
170+
"""Windowed lexical scans keep constructed-path PE3 above the view window.
171+
172+
Regression test: routing the constructed-path analysis through
173+
``peek_python_ast`` dropped findings once the runner sliced content into
174+
window views (above ``SECURITY_VIEW_WINDOW_CHARS``), because a slice never
175+
matches the scan's whole-file cache entry. The fragment fallback parses
176+
the slice directly so large files keep their findings.
177+
"""
178+
filler_line = "# " + "x" * 118 + "\n"
179+
body = "import os\ncredential = os.path.join('/etc', 'passwd')\n"
180+
target_chars = static_runner.SECURITY_VIEW_WINDOW_CHARS + 120_000
181+
source = body + filler_line * ((target_chars - len(body)) // len(filler_line))
182+
assert len(source) > static_runner.SECURITY_VIEW_WINDOW_CHARS
183+
(tmp_path / "large_script.py").write_text(source, encoding="utf-8")
184+
185+
result = graph.invoke({"skill_path": str(tmp_path), "use_llm": False})
186+
187+
assert any(
188+
finding.rule_id == "PE3" and finding.matched_text == "/etc/passwd"
189+
for finding in result["findings"]
190+
)

‎tests/unit/test_patterns.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import pytest
2121

22+
import skillspector.python_ast as python_ast_module
2223
from skillspector.models import Severity
2324
from skillspector.nodes.analyzers import (
2425
static_patterns_data_exfiltration as data_exfiltration_module,
@@ -36,6 +37,7 @@
3637
static_patterns_supply_chain as supply_chain_module,
3738
)
3839
from skillspector.nodes.analyzers import static_runner
40+
from skillspector.python_ast import prewarm_python_ast_cache
3941

4042

4143
def _assert_contextual_pe3(findings) -> None:
@@ -376,6 +378,56 @@ def test_pe3_aliased_join_imports_are_detected(self) -> None:
376378
for finding in findings
377379
), header
378380

381+
def test_pe3_windowed_fragment_keeps_constructed_path_coverage(self) -> None:
382+
"""A windowed view fragment under a scan must retain PE3 coverage.
383+
384+
Regression test: above SECURITY_VIEW_WINDOW_CHARS the runner hands
385+
lexical modules window slices, which never match the scan's
386+
whole-file AST cache entry. The constructed-path analysis must parse
387+
the fragment directly instead of silently dropping its findings.
388+
The fragment omits the import line, as a later window would, to pin
389+
that the dotted ``os.path.join`` spelling resolves without aliases.
390+
"""
391+
whole = "import os\npath = os.path.join('/etc', 'passwd')\nprint(path)\n"
392+
# A later window slice as the runner would hand it: complete and
393+
# parseable, but missing the import line and unable to match the
394+
# whole-file cache entry.
395+
fragment = "".join(whole.splitlines(keepends=True)[1:])
396+
cache_key = prewarm_python_ast_cache(["exploit.py"], {"exploit.py": whole})
397+
token = privilege_escalation_module._scan_python_ast_cache_key.set(cache_key)
398+
try:
399+
findings = privilege_escalation_module.analyze(fragment, "exploit.py", "python")
400+
finally:
401+
privilege_escalation_module._scan_python_ast_cache_key.reset(token)
402+
403+
assert any(
404+
finding.rule_id == "PE3" and finding.matched_text == "/etc/passwd"
405+
for finding in findings
406+
)
407+
408+
def test_pe3_windowed_fragment_without_join_call_does_not_parse(self, monkeypatch) -> None:
409+
"""Fragments without a plausible join call must not pay for a parse."""
410+
whole = "import os\npath = os.path.join('/etc', 'passwd')\n"
411+
fragment = "# just a comment line\nx = 1\n"
412+
cache_key = prewarm_python_ast_cache(["exploit.py"], {"exploit.py": whole})
413+
414+
parse_calls = 0
415+
original_parse = python_ast_module.ast.parse
416+
417+
def count_parse(*args, **kwargs):
418+
nonlocal parse_calls
419+
parse_calls += 1
420+
return original_parse(*args, **kwargs)
421+
422+
monkeypatch.setattr(python_ast_module.ast, "parse", count_parse)
423+
token = privilege_escalation_module._scan_python_ast_cache_key.set(cache_key)
424+
try:
425+
privilege_escalation_module.analyze(fragment, "exploit.py", "python")
426+
finally:
427+
privilege_escalation_module._scan_python_ast_cache_key.reset(token)
428+
429+
assert parse_calls == 0
430+
379431
# -- PE3 false-positive prevention --
380432

381433
def test_pe3_gitlab_settings_access_tokens_is_contextualized(self) -> None:

0 commit comments

Comments
 (0)