|
17 | 17 |
|
18 | 18 | from __future__ import annotations |
19 | 19 |
|
| 20 | +import ast |
20 | 21 | import posixpath |
21 | 22 | import re |
22 | 23 | import sys |
23 | 24 | from bisect import bisect_right |
24 | 25 |
|
25 | 26 | from skillspector.logging_config import get_logger |
26 | 27 | from skillspector.models import AnalyzerFinding, Location, Severity |
| 28 | +from skillspector.python_ast import parse_python_source |
27 | 29 | from skillspector.state import AnalyzerNodeResponse, SkillspectorState |
28 | 30 |
|
29 | 31 | from . import static_runner |
|
35 | 37 | get_context, |
36 | 38 | get_context_from_lines, |
37 | 39 | get_line_number, |
| 40 | + resolve_call_name, |
38 | 41 | ) |
39 | 42 | from .pattern_defaults import PatternCategory |
40 | 43 |
|
@@ -660,26 +663,37 @@ def _is_qualified_benign_access_requirement( |
660 | 663 | return heading_index >= 0 and lines[heading_index].strip() == "## Access Requirements" |
661 | 664 |
|
662 | 665 |
|
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]]: |
664 | 667 | """Return literal sensitive paths assembled with ``os.path.join`` in Python. |
665 | 668 |
|
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. |
668 | 676 | """ |
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 |
672 | 682 | 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): |
678 | 692 | continue |
679 | 693 | value = posixpath.join(*parts) |
680 | 694 | for pattern, confidence in PE3_PATTERNS: |
681 | 695 | 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)) |
683 | 697 | break |
684 | 698 | return resolved |
685 | 699 |
|
@@ -834,19 +848,33 @@ def context_at(offset: int) -> str: |
834 | 848 | ) |
835 | 849 | ) |
836 | 850 | 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, |
849 | 873 | ) |
| 874 | + if duplicate is None: |
| 875 | + findings.append(constructed) |
| 876 | + elif confidence > duplicate.confidence: |
| 877 | + findings[findings.index(duplicate)] = constructed |
850 | 878 | # Collect best-confidence PE4 finding per line to avoid double-counting lines |
851 | 879 | # that match multiple patterns (e.g. DockerClient(base_url=".../docker.sock")). |
852 | 880 | pe4_best: dict[int, AnalyzerFinding] = {} |
|
0 commit comments