Skip to content

Commit 56d121c

Browse files
committed
fix(patterns): alias-aware join gate, shared AST reuse, span dedup
Address rng1995's review on constructed sensitive-path detection: - Broaden the join pre-check to cover imported local names: the gate now matches every name the file binds to os.path or os.path.join, so 'from os.path import join as j' followed by j('/etc', 'passwd') reaches alias resolution instead of returning early on the missing literal join(. - Route the supplemental analysis through the scan's shared AST: node() publishes the runner's python_ast_cache_key via a contextvar and _constructed_sensitive_paths consults it with the new peek_python_ast helper (exact-content hit only, never parses or writes). The module stays lexical so windowed and normalized views keep running; windowed view fragments are skipped and standalone callers keep one on-demand parse. - Deduplicate across the call's whole source span: hits now carry (start_line, end_line), so a join call wrapped over several lines no longer produces a second PE3 when the lexical loop fires on a wrapped argument line. Tests: extend test_pe3_aliased_join_imports_are_detected with the renamed import case, add test_pe3_multiline_constructed_join_is_deduplicated, and extend test_graph_scan_parses_python_once_before_parallel_analyzers with a literal os.path.join call to verify the graph still performs one parse. Signed-off-by: Deepak Jain <[email protected]>
1 parent 40cea88 commit 56d121c

4 files changed

Lines changed: 142 additions & 27 deletions

File tree

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

Lines changed: 90 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import re
2323
import sys
2424
from bisect import bisect_right
25+
from contextvars import ContextVar
2526

2627
from skillspector.logging_config import get_logger
2728
from skillspector.models import AnalyzerFinding, Location, Severity
28-
from skillspector.python_ast import parse_python_source
29+
from skillspector.python_ast import ParsedPythonFile, parse_python_source, peek_python_ast
2930
from skillspector.state import AnalyzerNodeResponse, SkillspectorState
3031

3132
from . import static_runner
@@ -45,6 +46,15 @@
4546

4647
ANALYZER_ID = "static_patterns_privilege_escalation"
4748

49+
# Scan-scoped handle on the runner's shared Python AST cache, published by
50+
# node() for the analyze() calls the runner makes on its behalf. This module
51+
# stays lexical (no USES_PYTHON_AST opt-in) so its windowed and normalized
52+
# views keep running; the constructed-path analysis below consults the shared
53+
# cache through this key instead of reparsing.
54+
_scan_python_ast_cache_key: ContextVar[str | None] = ContextVar(
55+
"privilege_escalation_python_ast_cache_key", default=None
56+
)
57+
4858
PE1_CODE_PATTERNS = [
4959
(r"permissions?\s*:\s*\[?\s*['\"]?\*['\"]?\s*\]?", 0.8),
5060
(
@@ -663,32 +673,71 @@ def _is_qualified_benign_access_requirement(
663673
return heading_index >= 0 and lines[heading_index].strip() == "## Access Requirements"
664674

665675

666-
# Cheap pre-check before parsing for constructed join calls: the AST walk
676+
# Cheap pre-check before walking for constructed join calls: the AST walk
667677
# below is only worthwhile when the text plausibly contains a join() call.
668-
# This keeps the module lexical (windowed and normalized-view scans keep
669-
# working for every file type) while preserving the runner's parse-once
678+
# The gate covers spelling aliases collected from the imports (``from
679+
# os.path import join as j`` binds ``j`` to ``os.path.join``, so the call
680+
# site reads ``j(`` with no literal ``join`` in sight). Keeping the module
681+
# lexical plus this alias-aware gate preserves the runner's parse-once
670682
# invariant for files without any join call.
671-
_JOIN_CALL_HINT = re.compile(r"\bjoin\s*\(")
683+
_JOIN_CALL_BASE_NAMES = {"join"}
684+
_JOIN_CALL_ALIAS_TARGETS = {"os.path", "os.path.join"}
685+
686+
687+
def _join_call_hint(aliases: dict[str, str]) -> re.Pattern[str]:
688+
"""Return a pre-check pattern matching ``join(`` and imported join aliases.
689+
690+
Every local name the file binds to ``os.path`` or ``os.path.join`` is a
691+
possible call spelling (``j(`` for ``from os.path import join as j``),
692+
alongside the plain ``join(`` used by ``os.path.join(``, ``p.join(``,
693+
and direct ``join(`` imports.
694+
"""
695+
names = set(_JOIN_CALL_BASE_NAMES)
696+
for local, qualified in aliases.items():
697+
if qualified in _JOIN_CALL_ALIAS_TARGETS:
698+
names.add(local)
699+
return re.compile(r"\b(?:" + "|".join(sorted(re.escape(name) for name in names)) + r")\s*\(")
672700

673701

674-
def _constructed_sensitive_paths(content: str, file_path: str) -> list[tuple[int, str, float]]:
702+
def _constructed_sensitive_paths(
703+
content: str,
704+
file_path: str,
705+
python_ast: ParsedPythonFile | None = None,
706+
) -> list[tuple[int, int, str, float]]:
675707
"""Return literal sensitive paths assembled with ``os.path.join`` in Python.
676708
709+
Each hit is a ``(start_line, end_line, path, confidence)`` tuple anchored
710+
to the call's source span so callers can deduplicate raw findings across
711+
the whole occurrence, including calls wrapped over several lines.
677712
Resolved from the Python AST so calls split across lines and supported
678713
import spellings (``import os.path as p``, ``from os.path import join``,
679-
``from os import path``) are recognized without reparsing tricks. Only
680-
fully-literal positional argument lists are resolved; anything dynamic is
681-
left to the existing pattern loop. Unparseable content simply yields no
682-
findings here.
714+
``from os.path import join as j``, ``from os import path``) are recognized
715+
without reparsing tricks. The scan's shared parse is reused whenever this
716+
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.
683722
"""
684-
if not _JOIN_CALL_HINT.search(content):
685-
return []
686-
parsed = parse_python_source(content, file_path)
687-
tree = parsed.tree
723+
if python_ast is None:
724+
cache_key = _scan_python_ast_cache_key.get()
725+
if cache_key is not None:
726+
python_ast = peek_python_ast(cache_key, content, file_path)
727+
if python_ast is None:
728+
# 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 []
732+
else:
733+
python_ast = parse_python_source(content, file_path)
734+
tree = python_ast.tree
688735
if tree is None:
689736
return []
690-
aliases = parsed.import_aliases
691-
resolved: list[tuple[int, str, float]] = []
737+
aliases = python_ast.import_aliases
738+
if not _join_call_hint(aliases).search(content):
739+
return []
740+
resolved: list[tuple[int, int, str, float]] = []
692741
for node in ast.walk(tree):
693742
if not isinstance(node, ast.Call):
694743
continue
@@ -702,12 +751,17 @@ def _constructed_sensitive_paths(content: str, file_path: str) -> list[tuple[int
702751
value = posixpath.join(*parts)
703752
for pattern, confidence in PE3_PATTERNS:
704753
if re.search(pattern, value, re.IGNORECASE):
705-
resolved.append((node.lineno, value, confidence))
754+
resolved.append((node.lineno, node.end_lineno or node.lineno, value, confidence))
706755
break
707756
return resolved
708757

709758

710-
def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFinding]:
759+
def analyze(
760+
content: str,
761+
file_path: str,
762+
file_type: str,
763+
python_ast: ParsedPythonFile | None = None,
764+
) -> list[AnalyzerFinding]:
711765
"""Analyze content for privilege escalation patterns (PE1–PE5)."""
712766
findings: list[AnalyzerFinding] = []
713767
line_starts, line_ends = _source_line_metadata(content)
@@ -857,26 +911,32 @@ def context_at(offset: int) -> str:
857911
)
858912
)
859913
if file_type == "python":
860-
for line_num, path, confidence in _constructed_sensitive_paths(content, file_path):
914+
for start_line, end_line, path, confidence in _constructed_sensitive_paths(
915+
content, file_path, python_ast
916+
):
861917
constructed = AnalyzerFinding(
862918
rule_id="PE3",
863919
message="Credential Access",
864920
severity=Severity.HIGH,
865-
location=loc(line_num),
921+
location=loc(start_line),
866922
confidence=confidence,
867923
tags=list(tag),
868-
context=get_context(content, line_starts[line_num - 1]),
924+
context=get_context(content, line_starts[start_line - 1]),
869925
matched_text=path,
870926
)
871927
# One PE3 per source occurrence: the pattern loop above may already
872-
# have fired on this line's raw text (for example the literal
873-
# '.ssh/id_rsa' inside the join call). Keep the best-confidence
874-
# finding, mirroring the PE4/PE5 per-line aggregation below.
928+
# have fired inside the join call's line span (for example the
929+
# literal '.ssh/id_rsa' on a wrapped argument line, while this
930+
# finding anchors to the call's first line). Keep the
931+
# best-confidence finding per call span, mirroring the PE4/PE5
932+
# per-line aggregation below; unrelated occurrences on other
933+
# lines are preserved.
875934
duplicate = next(
876935
(
877936
existing
878937
for existing in findings
879-
if existing.rule_id == "PE3" and existing.location.start_line == line_num
938+
if existing.rule_id == "PE3"
939+
and start_line <= existing.location.start_line <= end_line
880940
),
881941
None,
882942
)
@@ -1031,6 +1091,10 @@ def _is_negated_safety_constraint(
10311091

10321092
def node(state: SkillspectorState) -> AnalyzerNodeResponse:
10331093
"""Run privilege_escalation patterns and return findings."""
1034-
response = static_runner.run_static_patterns_with_ledger(state, [sys.modules[__name__]])
1094+
token = _scan_python_ast_cache_key.set(state.get("python_ast_cache_key"))
1095+
try:
1096+
response = static_runner.run_static_patterns_with_ledger(state, [sys.modules[__name__]])
1097+
finally:
1098+
_scan_python_ast_cache_key.reset(token)
10351099
logger.info("%s: %d findings", ANALYZER_ID, len(response["findings"]))
10361100
return response

‎src/skillspector/python_ast.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,27 @@ def get_python_ast(cache_key: str | None, content: str, filename: str) -> Parsed
337337
return parsed
338338

339339

340+
def peek_python_ast(cache_key: str | None, content: str, filename: str) -> ParsedPythonFile | None:
341+
"""Return the scan's cached parse for *filename* without parsing anything.
342+
343+
Only an exact content match hits. Anything else (a windowed view fragment,
344+
an uncached file, an unknown cache key) returns ``None`` so the caller can
345+
apply its own fallback without disturbing the shared cache: unlike
346+
:func:`get_python_ast`, this never parses and never stores.
347+
"""
348+
if cache_key is None:
349+
return None
350+
with _runtime_ast_cache_lock:
351+
cache = _runtime_ast_caches.get(cache_key)
352+
if cache is None:
353+
return None
354+
cached = cache.entries.get(filename)
355+
if cached is not None and cached.content == content:
356+
cache.entries.move_to_end(filename)
357+
return cached
358+
return None
359+
360+
340361
def clear_python_ast_cache(cache_key: str | None) -> None:
341362
"""Release one scan's process-local parsed trees after its analyzer phase."""
342363
if cache_key is None:

‎tests/nodes/analyzers/test_shared_python_ast.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,18 @@ def count_parse(*args, **kwargs):
128128

129129

130130
def test_graph_scan_parses_python_once_before_parallel_analyzers(tmp_path, monkeypatch) -> None:
131-
"""The runtime cache shares one parse across the graph's analyzer fan-out."""
131+
"""The runtime cache shares one parse across the graph's analyzer fan-out.
132+
133+
The fixture includes a literal ``os.path.join`` call so the test proves
134+
the supplemental constructed-path analysis reuses the shared parse rather
135+
than parsing again.
136+
"""
132137
(tmp_path / "script.py").write_text(
133138
"import os\n"
134139
"import subprocess\n"
135140
"payload = input()\n"
136141
"environment = os.environ.copy()\n"
142+
"credential = os.path.join('/etc', 'passwd')\n"
137143
"subprocess.run(output)\n"
138144
"exec(payload)\n",
139145
encoding="utf-8",
@@ -151,5 +157,9 @@ def count_parse(*args, **kwargs):
151157
result = graph.invoke({"skill_path": str(tmp_path), "use_llm": False})
152158

153159
assert {"E2", "OH1", "AST1", "TT5"} <= {finding.rule_id for finding in result["findings"]}
160+
assert any(
161+
finding.rule_id == "PE3" and finding.matched_text == "/etc/passwd"
162+
for finding in result["findings"]
163+
)
154164
assert parse_calls == 1
155165
assert JsonPlusSerializer().dumps_typed(result)

‎tests/unit/test_patterns.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,24 @@ def test_pe3_constructed_join_is_deduplicated_per_line(self) -> None:
317317
line_two = [f for f in findings if f.rule_id == "PE3" and f.location.start_line == 2]
318318
assert len(line_two) == 1
319319

320+
def test_pe3_multiline_constructed_join_is_deduplicated(self) -> None:
321+
"""A join call wrapped across lines must yield one PE3 for its span."""
322+
content = (
323+
"import os\n"
324+
"p = os.path.join(\n"
325+
" '.ssh/id_rsa', 'x'\n"
326+
")\n"
327+
"with open(p) as source:\n"
328+
" data = source.read()\n"
329+
)
330+
331+
findings = privilege_escalation_module.analyze(content, "exploit.py", "python")
332+
333+
span_pe3 = [f for f in findings if f.rule_id == "PE3" and 2 <= f.location.start_line <= 4]
334+
assert len(span_pe3) == 1
335+
assert span_pe3[0].message == "Credential Access"
336+
assert span_pe3[0].severity == Severity.HIGH
337+
320338
def test_pe3_multiline_os_path_join_is_detected(self) -> None:
321339
"""A join call wrapped across lines must retain PE3 coverage."""
322340
content = (
@@ -339,11 +357,13 @@ def test_pe3_aliased_join_imports_are_detected(self) -> None:
339357
"""Supported import spellings of os.path.join must retain PE3 coverage."""
340358
for header in (
341359
"from os.path import join\n",
360+
"from os.path import join as j\n",
342361
"import os.path as p\n",
343362
"from os import path\n",
344363
):
345364
call = {
346365
"from os.path import join\n": "join('/etc', 'passwd')\n",
366+
"from os.path import join as j\n": "j('/etc', 'passwd')\n",
347367
"import os.path as p\n": "p.join('/etc', 'passwd')\n",
348368
"from os import path\n": "path.join('/etc', 'passwd')\n",
349369
}[header]

0 commit comments

Comments
 (0)