Skip to content

Commit 8262737

Browse files
committed
fix(patterns): resolve constructed joins via AST with per-line PE3 dedup
The PE3 constructed-path recognizer missed multiline and imported/aliased os.path.join spellings, and emitted a second PE3 for lines the pattern loop already covered. Resolve literal join calls from the Python AST (handling from/import aliases), and keep the best-confidence PE3 per line. Signed-off-by: Deepak Jain <[email protected]>
1 parent 1782279 commit 8262737

2 files changed

Lines changed: 105 additions & 24 deletions

File tree

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

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
from __future__ import annotations
1919

20+
import ast
2021
import posixpath
2122
import re
2223
import sys
2324
from bisect import bisect_right
2425

2526
from skillspector.logging_config import get_logger
2627
from skillspector.models import AnalyzerFinding, Location, Severity
28+
from skillspector.python_ast import parse_python_source
2729
from skillspector.state import AnalyzerNodeResponse, SkillspectorState
2830

2931
from . import static_runner
@@ -35,6 +37,7 @@
3537
get_context,
3638
get_context_from_lines,
3739
get_line_number,
40+
resolve_call_name,
3841
)
3942
from .pattern_defaults import PatternCategory
4043

@@ -660,26 +663,37 @@ def _is_qualified_benign_access_requirement(
660663
return heading_index >= 0 and lines[heading_index].strip() == "## Access Requirements"
661664

662665

663-
def _constructed_sensitive_paths(content: str) -> list[tuple[int, str, float]]:
666+
def _constructed_sensitive_paths(content: str, file_path: str) -> list[tuple[int, str, float]]:
664667
"""Return literal sensitive paths assembled with ``os.path.join`` in Python.
665668
666-
Keep this expression-level recognizer regex-based: Python AST parsing is shared
667-
across the analyzer graph, so a second parse here would defeat that cache.
669+
Resolved from the Python AST so calls split across lines and supported
670+
import spellings (``import os.path as p``, ``from os.path import join``,
671+
``from os import path``) are recognized without reparsing tricks. Only
672+
fully-literal positional argument lists are resolved; anything dynamic is
673+
left to the existing pattern loop. Parsing stays local to this module so
674+
its runner contract (lexical scanning, windowed views) does not change;
675+
unparseable content simply yields no findings here.
668676
"""
669-
call_pattern = re.compile(r"os\.path\.join\((?P<args>[^()\n]+)\)")
670-
string_pattern = re.compile(r"(['\"])(?P<value>[^'\"]*)\1")
671-
677+
parsed = parse_python_source(content, file_path)
678+
tree = parsed.tree
679+
if tree is None:
680+
return []
681+
aliases = parsed.import_aliases
672682
resolved: list[tuple[int, str, float]] = []
673-
for match in call_pattern.finditer(content):
674-
args = match.group("args")
675-
parts = [item.group("value") for item in string_pattern.finditer(args)]
676-
residual = string_pattern.sub("", args).replace(",", "").strip()
677-
if len(parts) < 2 or residual:
683+
for node in ast.walk(tree):
684+
if not isinstance(node, ast.Call):
685+
continue
686+
if resolve_call_name(node, aliases) != "os.path.join":
687+
continue
688+
if node.keywords or len(node.args) < 2:
689+
continue
690+
parts = [arg.value for arg in node.args if isinstance(arg, ast.Constant)]
691+
if len(parts) != len(node.args) or not all(isinstance(part, str) for part in parts):
678692
continue
679693
value = posixpath.join(*parts)
680694
for pattern, confidence in PE3_PATTERNS:
681695
if re.search(pattern, value, re.IGNORECASE):
682-
resolved.append((get_line_number(content, match.start()), value, confidence))
696+
resolved.append((node.lineno, value, confidence))
683697
break
684698
return resolved
685699

@@ -834,19 +848,33 @@ def context_at(offset: int) -> str:
834848
)
835849
)
836850
if file_type == "python":
837-
for line_num, path, confidence in _constructed_sensitive_paths(content):
838-
findings.append(
839-
AnalyzerFinding(
840-
rule_id="PE3",
841-
message="Credential Access",
842-
severity=Severity.HIGH,
843-
location=loc(line_num),
844-
confidence=confidence,
845-
tags=list(tag),
846-
context=get_context(content, line_starts[line_num - 1]),
847-
matched_text=path,
848-
)
851+
for line_num, path, confidence in _constructed_sensitive_paths(content, file_path):
852+
constructed = AnalyzerFinding(
853+
rule_id="PE3",
854+
message="Credential Access",
855+
severity=Severity.HIGH,
856+
location=loc(line_num),
857+
confidence=confidence,
858+
tags=list(tag),
859+
context=get_context(content, line_starts[line_num - 1]),
860+
matched_text=path,
861+
)
862+
# One PE3 per source occurrence: the pattern loop above may already
863+
# have fired on this line's raw text (for example the literal
864+
# '.ssh/id_rsa' inside the join call). Keep the best-confidence
865+
# finding, mirroring the PE4/PE5 per-line aggregation below.
866+
duplicate = next(
867+
(
868+
existing
869+
for existing in findings
870+
if existing.rule_id == "PE3" and existing.location.start_line == line_num
871+
),
872+
None,
849873
)
874+
if duplicate is None:
875+
findings.append(constructed)
876+
elif confidence > duplicate.confidence:
877+
findings[findings.index(duplicate)] = constructed
850878
# Collect best-confidence PE4 finding per line to avoid double-counting lines
851879
# that match multiple patterns (e.g. DockerClient(base_url=".../docker.sock")).
852880
pe4_best: dict[int, AnalyzerFinding] = {}

‎tests/unit/test_patterns.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,59 @@ def test_pe3_constant_os_path_join_passwd(self) -> None:
303303
for finding in findings
304304
)
305305

306+
def test_pe3_constructed_join_is_deduplicated_per_line(self) -> None:
307+
"""One source occurrence must not produce two PE3 findings for a line."""
308+
content = (
309+
"import os\n"
310+
"path = os.path.join('.ssh/id_rsa', 'x')\n"
311+
"with open(path) as source:\n"
312+
" data = source.read()\n"
313+
)
314+
315+
findings = privilege_escalation_module.analyze(content, "exploit.py", "python")
316+
317+
line_two = [f for f in findings if f.rule_id == "PE3" and f.location.start_line == 2]
318+
assert len(line_two) == 1
319+
320+
def test_pe3_multiline_os_path_join_is_detected(self) -> None:
321+
"""A join call wrapped across lines must retain PE3 coverage."""
322+
content = (
323+
"import os\n"
324+
"path = os.path.join(\n"
325+
" '/etc', 'passwd'\n"
326+
")\n"
327+
"with open(path) as source:\n"
328+
" data = source.read()\n"
329+
)
330+
331+
findings = privilege_escalation_module.analyze(content, "exploit.py", "python")
332+
333+
assert any(
334+
finding.rule_id == "PE3" and finding.matched_text == "/etc/passwd"
335+
for finding in findings
336+
)
337+
338+
def test_pe3_aliased_join_imports_are_detected(self) -> None:
339+
"""Supported import spellings of os.path.join must retain PE3 coverage."""
340+
for header in (
341+
"from os.path import join\n",
342+
"import os.path as p\n",
343+
"from os import path\n",
344+
):
345+
call = {
346+
"from os.path import join\n": "join('/etc', 'passwd')\n",
347+
"import os.path as p\n": "p.join('/etc', 'passwd')\n",
348+
"from os import path\n": "path.join('/etc', 'passwd')\n",
349+
}[header]
350+
content = header + "target = " + call
351+
352+
findings = privilege_escalation_module.analyze(content, "exploit.py", "python")
353+
354+
assert any(
355+
finding.rule_id == "PE3" and finding.matched_text == "/etc/passwd"
356+
for finding in findings
357+
), header
358+
306359
# -- PE3 false-positive prevention --
307360

308361
def test_pe3_gitlab_settings_access_tokens_is_contextualized(self) -> None:

0 commit comments

Comments
 (0)