openfix-cli 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
openfix/sandbox.py ADDED
@@ -0,0 +1,230 @@
1
+ """Where repository code (tests, setup, scanners) runs.
2
+
3
+ The model never gets a shell. It can only ask for the commands the policy
4
+ defines (tests, rescan), and those run here: in a throwaway container with no
5
+ network by default, or - only when explicitly chosen - as a local process with
6
+ a scrubbed environment.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import os
12
+ import shutil
13
+ import signal
14
+ import subprocess
15
+ import time
16
+ from dataclasses import dataclass
17
+ from pathlib import Path
18
+ from typing import Protocol
19
+
20
+ from .redact import redact_text
21
+
22
+ MAX_OUTPUT = 200_000
23
+
24
+ _SAFE_ENV = ("PATH", "HOME", "LANG", "LC_ALL", "TERM", "TMPDIR", "USER", "SHELL", "SYSTEMROOT", "COMSPEC")
25
+ _SECRETY = ("KEY", "TOKEN", "SECRET", "PASSWORD", "CREDENTIAL", "AUTH", "SESSION", "COOKIE")
26
+
27
+
28
+ @dataclass
29
+ class CommandResult:
30
+ command: str
31
+ exit_code: int
32
+ stdout: str
33
+ stderr: str
34
+ duration_s: float
35
+ timed_out: bool = False
36
+
37
+ @property
38
+ def ok(self) -> bool:
39
+ return self.exit_code == 0 and not self.timed_out
40
+
41
+ def tail(self, limit: int = 6000) -> str:
42
+ text = (self.stdout + ("\n" + self.stderr if self.stderr else "")).strip()
43
+ return text if len(text) <= limit else "...(truncated)...\n" + text[-limit:]
44
+
45
+
46
+ class Sandbox(Protocol):
47
+ kind: str
48
+
49
+ def run(
50
+ self, command: str, cwd: Path, timeout: int = 600, network: bool = False, image: str | None = None
51
+ ) -> CommandResult: ...
52
+
53
+
54
+ def scrubbed_env(extra: dict[str, str] | None = None) -> dict[str, str]:
55
+ env = {k: v for k, v in os.environ.items() if k in _SAFE_ENV}
56
+ env.update({"CI": "1", "NO_COLOR": "1", "OPENFIX_SANDBOX": "1"})
57
+ for k, v in (extra or {}).items():
58
+ if not any(s in k.upper() for s in _SECRETY):
59
+ env[k] = v
60
+ return env
61
+
62
+
63
+ def _clip(s: str) -> str:
64
+ s = redact_text(s)
65
+ return s if len(s) <= MAX_OUTPUT else s[: MAX_OUTPUT // 2] + "\n...(clipped)...\n" + s[-MAX_OUTPUT // 2 :]
66
+
67
+
68
+ class LocalSandbox:
69
+ """Runs commands on this machine with secrets removed from the environment.
70
+
71
+ This is NOT isolation. Use it on ephemeral CI runners or for repositories you trust.
72
+ """
73
+
74
+ kind = "local"
75
+
76
+ def run(
77
+ self,
78
+ command: str,
79
+ cwd: Path,
80
+ timeout: int = 600,
81
+ network: bool = False,
82
+ image: str | None = None,
83
+ passthrough: tuple[str, ...] = (),
84
+ ) -> CommandResult:
85
+ start = time.monotonic()
86
+ env = scrubbed_env()
87
+ env.update({k: os.environ[k] for k in passthrough if k in os.environ})
88
+ proc = subprocess.Popen(
89
+ ["/bin/sh", "-c", command] if os.name != "nt" else ["cmd", "/c", command],
90
+ cwd=str(cwd),
91
+ env=env,
92
+ stdout=subprocess.PIPE,
93
+ stderr=subprocess.PIPE,
94
+ stdin=subprocess.DEVNULL,
95
+ text=True,
96
+ errors="replace",
97
+ start_new_session=os.name != "nt",
98
+ )
99
+ timed_out = False
100
+ try:
101
+ out, err = proc.communicate(timeout=timeout)
102
+ except subprocess.TimeoutExpired:
103
+ timed_out = True
104
+ if os.name != "nt":
105
+ os.killpg(proc.pid, signal.SIGKILL)
106
+ else: # pragma: no cover
107
+ proc.kill()
108
+ out, err = proc.communicate()
109
+ return CommandResult(
110
+ command,
111
+ proc.returncode if not timed_out else -9,
112
+ _clip(out),
113
+ _clip(err),
114
+ round(time.monotonic() - start, 2),
115
+ timed_out,
116
+ )
117
+
118
+
119
+ class DockerSandbox:
120
+ """Runs each command in a fresh container: workspace mounted at /work, no network by default,
121
+ all capabilities dropped, no privilege escalation, memory/CPU/PID limits."""
122
+
123
+ kind = "docker"
124
+
125
+ def __init__(self, image: str, memory: str = "2g", cpus: str = "2", binary: str = "docker") -> None:
126
+ self.image = image
127
+ self.memory = memory
128
+ self.cpus = cpus
129
+ self.binary = binary
130
+
131
+ def run(
132
+ self, command: str, cwd: Path, timeout: int = 600, network: bool = False, image: str | None = None
133
+ ) -> CommandResult:
134
+ argv = [
135
+ self.binary,
136
+ "run",
137
+ "--rm",
138
+ "-i",
139
+ "--network",
140
+ "bridge" if network else "none",
141
+ "--memory",
142
+ self.memory,
143
+ "--cpus",
144
+ self.cpus,
145
+ "--pids-limit",
146
+ "1024",
147
+ "--cap-drop",
148
+ "ALL",
149
+ "--security-opt",
150
+ "no-new-privileges",
151
+ "-e",
152
+ "CI=1",
153
+ "-e",
154
+ "HOME=/tmp",
155
+ "-e",
156
+ "OPENFIX_SANDBOX=1",
157
+ "-v",
158
+ f"{Path(cwd).resolve()}:/work",
159
+ "-w",
160
+ "/work",
161
+ ]
162
+ if hasattr(os, "getuid"):
163
+ argv += ["--user", f"{os.getuid()}:{os.getgid()}"]
164
+ argv += [image or self.image, "sh", "-c", command]
165
+ start = time.monotonic()
166
+ try:
167
+ res = subprocess.run(
168
+ argv,
169
+ capture_output=True,
170
+ text=True,
171
+ errors="replace",
172
+ timeout=timeout + 30,
173
+ stdin=subprocess.DEVNULL,
174
+ env=scrubbed_env(),
175
+ )
176
+ return CommandResult(
177
+ command, res.returncode, _clip(res.stdout), _clip(res.stderr), round(time.monotonic() - start, 2)
178
+ )
179
+ except subprocess.TimeoutExpired as exc:
180
+ return CommandResult(
181
+ command,
182
+ -9,
183
+ _clip(str(exc.stdout or "")),
184
+ _clip(str(exc.stderr or "")),
185
+ round(time.monotonic() - start, 2),
186
+ timed_out=True,
187
+ )
188
+
189
+
190
+ def default_image(repo: Path) -> str:
191
+ if (repo / "package.json").exists():
192
+ return "node:22-bookworm"
193
+ if any((repo / f).exists() for f in ("pyproject.toml", "requirements.txt", "setup.py")):
194
+ return "python:3.12-bookworm"
195
+ if (repo / "go.mod").exists():
196
+ return "golang:1.23-bookworm"
197
+ if (repo / "Cargo.toml").exists():
198
+ return "rust:1-bookworm"
199
+ if (repo / "pom.xml").exists() or (repo / "build.gradle").exists():
200
+ return "eclipse-temurin:21-jdk"
201
+ return "debian:bookworm"
202
+
203
+
204
+ def docker_available() -> bool:
205
+ if not shutil.which("docker"):
206
+ return False
207
+ try:
208
+ return subprocess.run(["docker", "info"], capture_output=True, timeout=15).returncode == 0
209
+ except (OSError, subprocess.TimeoutExpired):
210
+ return False
211
+
212
+
213
+ class SandboxUnavailable(RuntimeError):
214
+ pass
215
+
216
+
217
+ def make_sandbox(backend: str, repo: Path, image: str | None = None, memory: str = "2g", cpus: str = "2") -> Sandbox:
218
+ if backend == "local":
219
+ return LocalSandbox()
220
+ if backend in ("docker", "auto"):
221
+ if docker_available():
222
+ return DockerSandbox(image or default_image(repo), memory=memory, cpus=cpus)
223
+ if backend == "docker":
224
+ raise SandboxUnavailable("sandbox.backend is 'docker' but Docker is not available")
225
+ raise SandboxUnavailable(
226
+ "Docker is not available, so OpenFix cannot isolate the repository's tests. "
227
+ "Start Docker, or pass --sandbox local to run them on this machine (only for trusted code "
228
+ "or ephemeral CI runners)."
229
+ )
230
+ raise SandboxUnavailable(f"Unknown sandbox backend '{backend}'")
openfix/sarif_out.py ADDED
@@ -0,0 +1,74 @@
1
+ """Write findings back out as SARIF 2.1.0 with ``fixes[]`` populated, so code-scanning UIs and other tools
2
+ can show (and apply) the proposed change next to the original alert."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import difflib
7
+ from typing import Any
8
+
9
+ from . import __version__
10
+ from .findings import Finding
11
+
12
+
13
+ def replacements(before: str, after: str) -> list[dict[str, Any]]:
14
+ a, b = before.splitlines(keepends=True), after.splitlines(keepends=True)
15
+ out = []
16
+ for tag, i1, i2, j1, j2 in difflib.SequenceMatcher(a=a, b=b, autojunk=False).get_opcodes():
17
+ if tag == "equal":
18
+ continue
19
+ if i1 == i2: # pure insertion: empty region at the start of line i1+1
20
+ region = {"startLine": i1 + 1, "startColumn": 1, "endLine": i1 + 1, "endColumn": 1}
21
+ else:
22
+ region = {"startLine": i1 + 1, "startColumn": 1, "endLine": i2 + 1, "endColumn": 1}
23
+ out.append({"deletedRegion": region, "insertedContent": {"text": "".join(b[j1:j2])}})
24
+ return out
25
+
26
+
27
+ def to_sarif(entries: list[dict[str, Any]]) -> dict[str, Any]:
28
+ """``entries``: dicts with ``finding``, ``status``, ``summary`` and ``files`` ({path: (before, after)})."""
29
+ results = []
30
+ for e in entries:
31
+ f: Finding = e["finding"]
32
+ result: dict[str, Any] = {
33
+ "ruleId": f.rule_id,
34
+ "level": {"critical": "error", "high": "error", "medium": "warning"}.get(f.severity.value, "note"),
35
+ "message": {"text": f.title},
36
+ "properties": {
37
+ "openfix": {"id": f.id, "status": e.get("status"), "tool": f.tool, "receipt": e.get("receipt")}
38
+ },
39
+ }
40
+ if f.location:
41
+ region = {"startLine": f.location.start_line} if f.location.start_line else {}
42
+ result["locations"] = [
43
+ {
44
+ "physicalLocation": {
45
+ "artifactLocation": {"uri": f.location.path},
46
+ **({"region": region} if region else {}),
47
+ }
48
+ }
49
+ ]
50
+ changes = [
51
+ {"artifactLocation": {"uri": path}, "replacements": replacements(before, after)}
52
+ for path, (before, after) in (e.get("files") or {}).items()
53
+ ]
54
+ if changes:
55
+ result["fixes"] = [
56
+ {"description": {"text": e.get("summary") or "OpenFix proposed fix"}, "artifactChanges": changes}
57
+ ]
58
+ results.append(result)
59
+ return {
60
+ "$schema": "https://json.schemastore.org/sarif-2.1.0.json",
61
+ "version": "2.1.0",
62
+ "runs": [
63
+ {
64
+ "tool": {
65
+ "driver": {
66
+ "name": "OpenFix",
67
+ "version": __version__,
68
+ "informationUri": "https://github.com/markusweldon/openfix",
69
+ }
70
+ },
71
+ "results": results,
72
+ }
73
+ ],
74
+ }
openfix/scanners.py ADDED
@@ -0,0 +1,187 @@
1
+ """Re-running the original scanner to prove a finding is reproduced before and gone after a fix.
2
+
3
+ Static scanners read source; they do not execute it. They therefore run as local
4
+ processes (with a scrubbed environment) rather than inside the test sandbox, and
5
+ must be installed where OpenFix runs.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import difflib
11
+ import json
12
+ import shutil
13
+ from dataclasses import dataclass
14
+ from pathlib import Path
15
+
16
+ from .findings import Category, Finding
17
+ from .ingest import load_document
18
+ from .sandbox import CommandResult, LocalSandbox
19
+
20
+ SCAN_DIR = ".openfix-scan"
21
+
22
+
23
+ @dataclass(frozen=True)
24
+ class Preset:
25
+ name: str
26
+ binary: str
27
+ command: str # {output} -> SARIF path, {targets} -> space separated paths
28
+ passthrough: tuple[str, ...] = ()
29
+ per_file: bool = False
30
+
31
+
32
+ PRESETS: dict[str, Preset] = {
33
+ "semgrep": Preset(
34
+ "semgrep",
35
+ "semgrep",
36
+ "semgrep scan --config ${OPENFIX_SEMGREP_CONFIG:-p/default} --sarif --output {output} "
37
+ "--metrics off --quiet --disable-version-check {targets}",
38
+ passthrough=("SEMGREP_APP_TOKEN", "OPENFIX_SEMGREP_CONFIG"),
39
+ per_file=True,
40
+ ),
41
+ "snyk-code": Preset(
42
+ "snyk-code",
43
+ "snyk",
44
+ "snyk code test --sarif-file-output={output} . ; test -s {output}",
45
+ passthrough=("SNYK_TOKEN", "SNYK_API"),
46
+ ),
47
+ "snyk": Preset(
48
+ "snyk",
49
+ "snyk",
50
+ "snyk test --sarif-file-output={output} . ; test -s {output}",
51
+ passthrough=("SNYK_TOKEN", "SNYK_API"),
52
+ ),
53
+ "trivy": Preset("trivy", "trivy", "trivy fs --quiet --scanners vuln,misconfig --format sarif --output {output} ."),
54
+ "osv-scanner": Preset(
55
+ "osv-scanner", "osv-scanner", "osv-scanner --format sarif --output {output} -r . ; test -s {output}"
56
+ ),
57
+ "gitleaks": Preset(
58
+ "gitleaks", "gitleaks", "gitleaks dir . --report-format sarif --report-path {output} --exit-code 0"
59
+ ),
60
+ "bandit": Preset("bandit", "bandit", "bandit -q -f sarif -o {output} --exit-zero {targets}", per_file=True),
61
+ }
62
+
63
+
64
+ def preset_for(f: Finding) -> str | None:
65
+ tool = f.tool.lower()
66
+ if "semgrep" in tool:
67
+ return "semgrep"
68
+ if "snyk" in tool:
69
+ return "snyk" if f.category is Category.sca else "snyk-code"
70
+ for name in ("trivy", "osv-scanner", "gitleaks", "bandit"):
71
+ if name.split("-")[0] in tool:
72
+ return name
73
+ return None
74
+
75
+
76
+ @dataclass
77
+ class ScanResult:
78
+ findings: list[Finding]
79
+ command: CommandResult | None
80
+ error: str | None = None
81
+
82
+
83
+ class Scanner:
84
+ def __init__(self, spec: str | dict | None, finding: Finding, timeout: int = 900) -> None:
85
+ self.timeout = timeout
86
+ self.preset: Preset | None = None
87
+ self.command: str | None = None
88
+ if isinstance(spec, dict): # {command, timeout}
89
+ self.command = spec["command"]
90
+ self.timeout = int(spec.get("timeout", timeout))
91
+ elif isinstance(spec, str) and spec in PRESETS:
92
+ self.preset = PRESETS[spec]
93
+ elif isinstance(spec, str) and spec:
94
+ self.command = spec
95
+ else:
96
+ name = preset_for(finding)
97
+ self.preset = PRESETS[name] if name else None
98
+ self.finding = finding
99
+
100
+ @property
101
+ def available(self) -> bool:
102
+ if self.command:
103
+ return True
104
+ return bool(self.preset and shutil.which(self.preset.binary))
105
+
106
+ @property
107
+ def description(self) -> str:
108
+ if self.command:
109
+ return self.command
110
+ return self.preset.name if self.preset else "none"
111
+
112
+ def scan(self, work: Path) -> ScanResult:
113
+ if not self.available:
114
+ why = f"{self.preset.binary} is not installed" if self.preset else "no scanner configured for this tool"
115
+ return ScanResult([], None, why)
116
+ out_dir = work / SCAN_DIR
117
+ out_dir.mkdir(exist_ok=True)
118
+ output = out_dir / "result.sarif"
119
+ output.unlink(missing_ok=True)
120
+ targets = "."
121
+ loc = self.finding.location
122
+ if self.preset and self.preset.per_file and loc and loc.path and (work / loc.path).exists():
123
+ targets = _q(loc.path)
124
+ template = self.command or (self.preset.command if self.preset else "")
125
+ cmd = template.replace("{output}", _q(f"{SCAN_DIR}/result.sarif")).replace("{targets}", targets)
126
+ res = LocalSandbox().run(
127
+ cmd, work, timeout=self.timeout, passthrough=self.preset.passthrough if self.preset else ()
128
+ )
129
+ try:
130
+ if not output.exists():
131
+ return ScanResult([], res, f"scanner produced no SARIF (exit {res.exit_code}): {res.tail(800)}")
132
+ doc = json.loads(output.read_text(encoding="utf-8"))
133
+ return ScanResult(load_document(doc), res)
134
+ except (ValueError, json.JSONDecodeError) as exc:
135
+ return ScanResult([], res, f"could not read scanner output: {exc}")
136
+ finally:
137
+ shutil.rmtree(out_dir, ignore_errors=True)
138
+
139
+
140
+ def _q(s: str) -> str:
141
+ return "'" + s.replace("'", "'\\''") + "'"
142
+
143
+
144
+ # ----------------------------------------------------------------------------- matching
145
+
146
+
147
+ def _same_rule(a: str, b: str) -> bool:
148
+ if a == b:
149
+ return True
150
+ # Semgrep prefixes rule ids with the config path; compare the dotted tail.
151
+ ta, tb = a.split(".")[-1], b.split(".")[-1]
152
+ return ta == tb and (a.endswith(b) or b.endswith(a) or len(ta) > 12)
153
+
154
+
155
+ def matches(target: Finding, candidate: Finding, line_map: dict[int, int] | None = None, tolerance: int = 3) -> bool:
156
+ """Does ``candidate`` (from a rescan) report the same problem as ``target``?"""
157
+ if target.package and candidate.package:
158
+ if target.package.name.lower() != candidate.package.name.lower():
159
+ return False
160
+ adv_t = set(a.upper() for a in target.advisories)
161
+ adv_c = set(a.upper() for a in candidate.advisories) | {candidate.rule_id.upper()}
162
+ return bool(adv_t & adv_c) or _same_rule(target.rule_id, candidate.rule_id)
163
+ if not _same_rule(target.rule_id, candidate.rule_id):
164
+ return False
165
+ if not target.location or not candidate.location:
166
+ return target.location is None and candidate.location is None
167
+ if target.location.path != candidate.location.path:
168
+ return False
169
+ t_line, c_line = target.location.start_line, candidate.location.start_line
170
+ if t_line is None or c_line is None:
171
+ return True
172
+ expected = line_map.get(t_line, t_line) if line_map else t_line
173
+ return abs(expected - c_line) <= tolerance
174
+
175
+
176
+ def line_map(before: str, after: str) -> dict[int, int]:
177
+ """Map 1-based line numbers in ``before`` to their position in ``after`` (deleted lines map to the
178
+ nearest surviving position) so a finding can be followed across an edit."""
179
+ a, b = before.splitlines(), after.splitlines()
180
+ mapping: dict[int, int] = {}
181
+ for tag, i1, i2, j1, j2 in difflib.SequenceMatcher(a=a, b=b, autojunk=False).get_opcodes():
182
+ for k in range(i1, i2):
183
+ if tag == "equal":
184
+ mapping[k + 1] = j1 + (k - i1) + 1
185
+ else:
186
+ mapping[k + 1] = min(j1 + (k - i1), max(j2 - 1, j1)) + 1
187
+ return mapping