Skip to content

Commit d1f9339

Browse files
committed
fix(patterns): evaluate windowed join calls on the whole-file AST
The windowed fallback parsed each fragment as a standalone module, so a slice starting mid-block (for example inside a function body) failed parsing and its constructed-path PE3 findings were silently dropped. Evaluate the supplemental constructed calls against the whole file's cached tree instead, reusing the existing cache peek: call spans map onto fragment-relative lines and only calls starting inside the fragment are owned by it, preserving full-file syntax, scope and import aliases as well as the runner's source-coordinate and dedupe behavior. Fragments that are not slices of the whole file (normalized views) keep the standalone-parse fallback. Add direct and graph regressions using the enclosing-function fixture: a mid-function window now reports exactly one PE3 at the call site. Signed-off-by: Deepak Jain <[email protected]>
1 parent fb9d631 commit d1f9339

2 files changed

Lines changed: 144 additions & 16 deletions

File tree

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

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,21 @@ def _join_call_hint(aliases: dict[str, str]) -> re.Pattern[str]:
712712
return re.compile(r"\b(?:" + "|".join(sorted(re.escape(name) for name in names)) + r")\s*\(")
713713

714714

715+
def _fragment_whole_file_line_offset(fragment: str, whole_content: str) -> int | None:
716+
"""Return how many whole-file lines precede a windowed view fragment.
717+
718+
Windowed view fragments are contiguous slices of the scanned file, so a
719+
whole-file AST line number maps onto a fragment-relative line by
720+
subtracting this offset. Return ``None`` when the fragment is not a
721+
slice of the whole file (for example a normalized view), so callers keep
722+
the standalone-parse fallback.
723+
"""
724+
start = whole_content.find(fragment)
725+
if start < 0:
726+
return None
727+
return whole_content.count("\n", 0, start)
728+
729+
715730
def _constructed_sensitive_paths(
716731
content: str,
717732
file_path: str,
@@ -728,45 +743,77 @@ def _constructed_sensitive_paths(
728743
without reparsing tricks. The scan's shared parse is reused whenever this
729744
runs inside the runner (the cache key published by node()); standalone
730745
callers get a single on-demand parse. Windowed view fragments under a scan
731-
miss the whole-file cache entry and are parsed directly behind the same
732-
textual join-or-import gate, so large files keep their findings instead of
733-
silently dropping them. On such a fragment the whole file's import-alias
746+
miss the whole-file cache entry; behind the same textual join-or-import
747+
gate they are evaluated against the whole file's cached tree instead of
748+
being parsed standalone, so a fragment starting mid-block (for example
749+
inside a function body, which is not a valid module on its own) keeps its
750+
findings instead of silently dropping them. The whole file's import-alias
734751
map is carried in from the scan cache for the join gate and call
735752
resolution, so a renamed ``os.path.join`` spelling (``from os.path import
736753
join as j``) is recognized even when the import lives in an earlier window
737-
than the call. Only fully-literal positional argument lists are resolved;
738-
anything dynamic is left to the existing pattern loop. Unparseable content
739-
simply yields no findings here.
754+
than the call. Call spans map onto fragment-relative lines and only
755+
calls starting inside the fragment are owned by it, preserving the
756+
runner's source-coordinate and dedupe behavior. When the whole-file tree
757+
is unavailable, or the fragment is not a slice of the whole file
758+
(normalized views), the fragment is parsed directly as before. Only
759+
fully-literal positional argument lists are resolved; anything dynamic is
760+
left to the existing pattern loop. Unparseable content simply yields no
761+
findings here.
740762
"""
741763
whole_file_aliases: dict[str, str] | None = None
764+
whole_file_tree: ast.Module | None = None
765+
whole_file_source: str | None = None
766+
whole_file_line_offset: int | None = None
742767
if python_ast is None:
743768
cache_key = _scan_python_ast_cache_key.get()
744769
if cache_key is not None:
745770
python_ast = peek_python_ast(cache_key, content, file_path)
746771
if python_ast is None:
747772
# A windowed view fragment, not the scan's whole file: the
748773
# shared tree does not cover this slice. Carry the whole
749-
# file's import-alias map from the scan cache first (a
750-
# fragment is a slice of the same scanned file), so the
774+
# file's import-alias map and tree from the scan cache first
775+
# (a fragment is a slice of the same scanned file), so the
751776
# textual gate below also fires on renamed call spellings
752777
# like ``j(`` when the ``from os.path import join as j``
753778
# import lives in an earlier window. Fragments without a
754779
# plausible join call or join import never parse.
755780
whole_file = peek_python_ast_any_content(cache_key, file_path)
756781
if whole_file is not None and whole_file.tree is not None:
757782
whole_file_aliases = whole_file.import_aliases
783+
whole_file_tree = whole_file.tree
784+
whole_file_source = whole_file.content
758785
if _join_call_hint(whole_file_aliases or {}).search(
759786
content
760787
) or _JOIN_IMPORT_HINT.search(content):
761-
python_ast = parse_python_source(content, file_path)
788+
if whole_file_tree is not None and whole_file_source is not None:
789+
# Evaluate the whole-file tree rather than parsing the
790+
# fragment standalone: a slice starting mid-block is
791+
# not a valid module, so its standalone parse fails
792+
# and the finding would be silently dropped.
793+
whole_file_line_offset = _fragment_whole_file_line_offset(
794+
content, whole_file_source
795+
)
796+
if whole_file_line_offset is None:
797+
python_ast = parse_python_source(content, file_path)
762798
else:
763799
python_ast = parse_python_source(content, file_path)
764-
if python_ast is None:
765-
return []
766-
tree = python_ast.tree
767-
if tree is None:
768-
return []
769-
aliases = whole_file_aliases if whole_file_aliases is not None else python_ast.import_aliases
800+
if whole_file_tree is not None and whole_file_line_offset is not None:
801+
tree = whole_file_tree
802+
aliases = whole_file_aliases or {}
803+
owned_span: tuple[int, int] | None = (
804+
whole_file_line_offset,
805+
whole_file_line_offset + len(content.splitlines()),
806+
)
807+
else:
808+
if python_ast is None:
809+
return []
810+
tree = python_ast.tree
811+
if tree is None:
812+
return []
813+
aliases = (
814+
whole_file_aliases if whole_file_aliases is not None else python_ast.import_aliases
815+
)
816+
owned_span = None
770817
if not _join_call_hint(aliases).search(content):
771818
return []
772819
resolved: list[tuple[int, int, str, float]] = []
@@ -781,9 +828,19 @@ def _constructed_sensitive_paths(
781828
if len(parts) != len(node.args) or not all(isinstance(part, str) for part in parts):
782829
continue
783830
value = posixpath.join(*parts)
831+
start_line = node.lineno
832+
end_line = node.end_lineno or node.lineno
833+
if owned_span is not None:
834+
# Only calls starting inside this fragment are owned by it; the
835+
# runner's own owned-range filter and cross-window dedupe keep
836+
# exactly one finding per call at whole-file coordinates.
837+
if not owned_span[0] < start_line <= owned_span[1]:
838+
continue
839+
start_line -= owned_span[0]
840+
end_line -= owned_span[0]
784841
for pattern, confidence in PE3_PATTERNS:
785842
if re.search(pattern, value, re.IGNORECASE):
786-
resolved.append((node.lineno, node.end_lineno or node.lineno, value, confidence))
843+
resolved.append((start_line, end_line, value, confidence))
787844
break
788845
return resolved
789846

‎tests/unit/test_patterns.py‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,77 @@ def test_pe3_graph_windowed_import_and_call_across_windows(self) -> None:
497497
]
498498
assert len(constructed) == 1
499499

500+
def test_pe3_windowed_fragment_inside_function_body_keeps_coverage(self) -> None:
501+
"""A fragment starting inside a function body keeps PE3 coverage.
502+
503+
Regression test: the fallback parsed each windowed fragment as a
504+
standalone module, so a slice starting mid-block (indented, with the
505+
enclosing ``def`` in an earlier window) failed parsing and silently
506+
dropped its findings. The fallback now evaluates the whole-file
507+
tree with spans mapped onto fragment lines, so the constructed
508+
sensitive path is reported exactly once at its fragment location.
509+
"""
510+
padding = (" # " + "x" * 118 + "\n") * 40
511+
whole = (
512+
"from os.path import join as j\n"
513+
"def load():\n"
514+
" pass\n" + padding + " credential = j('/etc', 'passwd')\n"
515+
)
516+
lines = whole.splitlines(keepends=True)
517+
# A later window slice as the runner would hand it: starts inside the
518+
# function body, so it cannot parse as a standalone module, and the
519+
# import plus the ``def`` line live in the earlier window.
520+
fragment = "".join(lines[10:])
521+
cache_key = prewarm_python_ast_cache(["exploit.py"], {"exploit.py": whole})
522+
token = privilege_escalation_module._scan_python_ast_cache_key.set(cache_key)
523+
try:
524+
findings = privilege_escalation_module.analyze(fragment, "exploit.py", "python")
525+
finally:
526+
privilege_escalation_module._scan_python_ast_cache_key.reset(token)
527+
528+
constructed = [
529+
finding
530+
for finding in findings
531+
if finding.rule_id == "PE3" and finding.matched_text == "/etc/passwd"
532+
]
533+
assert len(constructed) == 1
534+
assert constructed[0].location.start_line == len(lines) - 10
535+
536+
def test_pe3_graph_windowed_call_inside_function_keeps_coverage(self) -> None:
537+
"""The full node path keeps PE3 coverage for a call inside a function.
538+
539+
Graph regression for the reviewer finding on the current head: the
540+
source is large enough that the runner splits it into two raw
541+
windows, and the second window starts inside the ``load`` function
542+
body, so parsing that slice as a standalone module fails. The
543+
constructed sensitive path must still be reported exactly once, at
544+
the call's original whole-file location.
545+
"""
546+
padding_line = " # " + "x" * 118 + "\n"
547+
pad_lines = static_runner.SECURITY_VIEW_WINDOW_CHARS // len(padding_line) + 10
548+
content = (
549+
"from os.path import join as j\n"
550+
"def load():\n"
551+
" pass\n" + padding_line * pad_lines + " credential = j('/etc', 'passwd')\n"
552+
)
553+
assert len(content) > static_runner.SECURITY_VIEW_WINDOW_CHARS
554+
call_line = 3 + pad_lines + 1
555+
cache_key = prewarm_python_ast_cache(["exploit.py"], {"exploit.py": content})
556+
response = privilege_escalation_module.node(
557+
{
558+
"components": ["exploit.py"],
559+
"file_cache": {"exploit.py": content},
560+
"python_ast_cache_key": cache_key,
561+
}
562+
)
563+
constructed = [
564+
finding
565+
for finding in response["findings"]
566+
if finding.rule_id == "PE3" and finding.matched_text == "/etc/passwd"
567+
]
568+
assert len(constructed) == 1
569+
assert constructed[0].start_line == call_line
570+
500571
def test_pe3_windowed_fragment_without_join_call_does_not_parse(self, monkeypatch) -> None:
501572
"""Fragments without a plausible join call must not pay for a parse."""
502573
whole = "import os\npath = os.path.join('/etc', 'passwd')\n"

0 commit comments

Comments
 (0)