patchproof-core 0.3.1__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.
- patchproof/__init__.py +44 -0
- patchproof/__main__.py +8 -0
- patchproof/benchmarks/__init__.py +17 -0
- patchproof/benchmarks/swe_bench.py +159 -0
- patchproof/cli.py +1254 -0
- patchproof/core/__init__.py +33 -0
- patchproof/core/agent_init.py +103 -0
- patchproof/core/anti_cheat.py +396 -0
- patchproof/core/applier.py +159 -0
- patchproof/core/ast_analyzer.py +254 -0
- patchproof/core/detector.py +78 -0
- patchproof/core/git_provider.py +298 -0
- patchproof/core/healer.py +104 -0
- patchproof/core/mutator.py +223 -0
- patchproof/core/normalizer.py +338 -0
- patchproof/core/polyglot_guard.py +167 -0
- patchproof/core/runtime_guard.py +188 -0
- patchproof/core/search_replace.py +222 -0
- patchproof/core/slicer.py +172 -0
- patchproof/core/sparse_tree.py +95 -0
- patchproof/docker/runner.Dockerfile +42 -0
- patchproof/exceptions.py +37 -0
- patchproof/factory/__init__.py +22 -0
- patchproof/factory/blast_radius.py +120 -0
- patchproof/factory/engine.py +134 -0
- patchproof/factory/models.py +75 -0
- patchproof/mcp.py +532 -0
- patchproof/models/__init__.py +57 -0
- patchproof/models/ast.py +78 -0
- patchproof/models/patch.py +106 -0
- patchproof/models/sandbox.py +417 -0
- patchproof/reporting/__init__.py +19 -0
- patchproof/reporting/asymmetric.py +88 -0
- patchproof/reporting/badge.py +86 -0
- patchproof/reporting/certificate.py +230 -0
- patchproof/reporting/diagnostic.py +330 -0
- patchproof/reporting/html_report.py +367 -0
- patchproof/reporting/in_toto.py +188 -0
- patchproof/reporting/markdown.py +179 -0
- patchproof/reporting/sbom.py +152 -0
- patchproof/sandbox/__init__.py +37 -0
- patchproof/sandbox/classifier.py +289 -0
- patchproof/sandbox/comparative.py +478 -0
- patchproof/sandbox/docker_cli.py +384 -0
- patchproof/sandbox/doctor.py +605 -0
- patchproof/sandbox/flakiness.py +87 -0
- patchproof/sandbox/network_bridge.py +145 -0
- patchproof/sandbox/parser.py +172 -0
- patchproof/sandbox/runner.py +424 -0
- patchproof/sandbox/setup_runner.py +261 -0
- patchproof_core-0.3.1.dist-info/METADATA +212 -0
- patchproof_core-0.3.1.dist-info/RECORD +56 -0
- patchproof_core-0.3.1.dist-info/WHEEL +5 -0
- patchproof_core-0.3.1.dist-info/entry_points.txt +2 -0
- patchproof_core-0.3.1.dist-info/licenses/LICENSE +13 -0
- patchproof_core-0.3.1.dist-info/top_level.txt +1 -0
patchproof/models/ast.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Immutable models representing AST inspection and blast-radius results."""
|
|
2
|
+
|
|
3
|
+
from enum import StrEnum
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class EntityType(StrEnum):
|
|
9
|
+
"""Types of syntactic code structures."""
|
|
10
|
+
|
|
11
|
+
FUNCTION = "function"
|
|
12
|
+
METHOD = "method"
|
|
13
|
+
CLASS = "class"
|
|
14
|
+
MODULE = "module"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ASTEntity(BaseModel):
|
|
18
|
+
"""Represents a specific syntactic code structure located within a source file."""
|
|
19
|
+
|
|
20
|
+
model_config = ConfigDict(frozen=True)
|
|
21
|
+
|
|
22
|
+
name: str = Field(description="Identifier name of the function, method, or class")
|
|
23
|
+
entity_type: EntityType = Field(description="Category of code entity")
|
|
24
|
+
parent_class: str | None = Field(default=None, description="Enclosing class name if a method")
|
|
25
|
+
start_line: int = Field(description="1-based starting line number in source")
|
|
26
|
+
end_line: int = Field(description="1-based ending line number in source")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class FileASTReport(BaseModel):
|
|
30
|
+
"""Syntactic report for an individual source file affected by a patch."""
|
|
31
|
+
|
|
32
|
+
model_config = ConfigDict(frozen=True)
|
|
33
|
+
|
|
34
|
+
path: str = Field(description="Relative path of the analyzed source file")
|
|
35
|
+
modified_entities: tuple[ASTEntity, ...] = Field(
|
|
36
|
+
default_factory=tuple,
|
|
37
|
+
description="Functions, methods, or classes directly intersected by modified lines",
|
|
38
|
+
)
|
|
39
|
+
imported_modules: tuple[str, ...] = Field(
|
|
40
|
+
default_factory=tuple,
|
|
41
|
+
description="Static imports discovered in this file",
|
|
42
|
+
)
|
|
43
|
+
syntactic_calls: tuple[str, ...] = Field(
|
|
44
|
+
default_factory=tuple,
|
|
45
|
+
description="Syntactic call sites invoked within the modified line ranges",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class ASTAnalysisReport(BaseModel):
|
|
50
|
+
"""Aggregated deterministic AST analysis across all modified files."""
|
|
51
|
+
|
|
52
|
+
model_config = ConfigDict(frozen=True)
|
|
53
|
+
|
|
54
|
+
file_reports: tuple[FileASTReport, ...] = Field(default_factory=tuple)
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def all_modified_entities(self) -> tuple[ASTEntity, ...]:
|
|
58
|
+
"""Flattened tuple of all modified entities across all files."""
|
|
59
|
+
entities: list[ASTEntity] = []
|
|
60
|
+
for rep in self.file_reports:
|
|
61
|
+
entities.extend(rep.modified_entities)
|
|
62
|
+
return tuple(entities)
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def all_imports(self) -> tuple[str, ...]:
|
|
66
|
+
"""Unique sorted list of all imported modules across modified files."""
|
|
67
|
+
imports: set[str] = set()
|
|
68
|
+
for rep in self.file_reports:
|
|
69
|
+
imports.update(rep.imported_modules)
|
|
70
|
+
return tuple(sorted(imports))
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def all_calls(self) -> tuple[str, ...]:
|
|
74
|
+
"""Unique sorted list of all syntactic calls made in modified lines."""
|
|
75
|
+
calls: set[str] = set()
|
|
76
|
+
for rep in self.file_reports:
|
|
77
|
+
calls.update(rep.syntactic_calls)
|
|
78
|
+
return tuple(sorted(calls))
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Immutable models representing canonical patch representations."""
|
|
2
|
+
|
|
3
|
+
from enum import StrEnum
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class PatchType(StrEnum):
|
|
9
|
+
"""Supported patch format types."""
|
|
10
|
+
|
|
11
|
+
UNIFIED_DIFF = "unified_diff"
|
|
12
|
+
SEARCH_REPLACE = "search_replace"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ChangeType(StrEnum):
|
|
16
|
+
"""Type of file alteration within a patch."""
|
|
17
|
+
|
|
18
|
+
ADD = "add"
|
|
19
|
+
DELETE = "delete"
|
|
20
|
+
MODIFY = "modify"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class PatchHunk(BaseModel):
|
|
24
|
+
"""Represents an individual unified diff hunk."""
|
|
25
|
+
|
|
26
|
+
model_config = ConfigDict(frozen=True)
|
|
27
|
+
|
|
28
|
+
old_start: int = Field(description="Starting line in original file")
|
|
29
|
+
old_lines: int = Field(description="Number of lines in original file hunk")
|
|
30
|
+
new_start: int = Field(description="Starting line in new file")
|
|
31
|
+
new_lines: int = Field(description="Number of lines in new file hunk")
|
|
32
|
+
header: str = Field(description="Raw @@ hunk header line")
|
|
33
|
+
lines: tuple[str, ...] = Field(description="Hunk body lines including prefix (+, -, ' ', \\)")
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def added_lines_count(self) -> int:
|
|
37
|
+
"""Count of added lines starting with '+'."""
|
|
38
|
+
return sum(1 for line in self.lines if line.startswith("+"))
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def deleted_lines_count(self) -> int:
|
|
42
|
+
"""Count of deleted lines starting with '-'."""
|
|
43
|
+
return sum(1 for line in self.lines if line.startswith("-"))
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class FileDelta(BaseModel):
|
|
47
|
+
"""Represents changes applied to a single file."""
|
|
48
|
+
|
|
49
|
+
model_config = ConfigDict(frozen=True)
|
|
50
|
+
|
|
51
|
+
path: str = Field(description="Canonical repository relative path")
|
|
52
|
+
change_type: ChangeType = Field(description="Type of modification (add/delete/modify)")
|
|
53
|
+
old_path: str | None = Field(default=None, description="Original path from diff header")
|
|
54
|
+
new_path: str | None = Field(default=None, description="New path from diff header")
|
|
55
|
+
hunks: tuple[PatchHunk, ...] = Field(default_factory=tuple, description="Ordered patch hunks")
|
|
56
|
+
is_binary: bool = Field(default=False, description="Flag indicating binary file change")
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def is_new_file(self) -> bool:
|
|
60
|
+
"""Return True if this file is newly created."""
|
|
61
|
+
return self.change_type == ChangeType.ADD
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def is_deleted_file(self) -> bool:
|
|
65
|
+
"""Return True if this file was removed."""
|
|
66
|
+
return self.change_type == ChangeType.DELETE
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def total_added_lines(self) -> int:
|
|
70
|
+
"""Sum of all additions across all hunks for this file."""
|
|
71
|
+
return sum(hunk.added_lines_count for hunk in self.hunks)
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def total_deleted_lines(self) -> int:
|
|
75
|
+
"""Sum of all deletions across all hunks for this file."""
|
|
76
|
+
return sum(hunk.deleted_lines_count for hunk in self.hunks)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class CanonicalPatch(BaseModel):
|
|
80
|
+
"""Immutable root model representing a normalized patch."""
|
|
81
|
+
|
|
82
|
+
model_config = ConfigDict(frozen=True)
|
|
83
|
+
|
|
84
|
+
patch_type: PatchType = Field(default=PatchType.UNIFIED_DIFF)
|
|
85
|
+
file_deltas: tuple[FileDelta, ...] = Field(default_factory=tuple)
|
|
86
|
+
raw_diff: str = Field(description="Normalized raw diff string (LF normalized)")
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def modified_paths(self) -> tuple[str, ...]:
|
|
90
|
+
"""All file paths touched by this patch."""
|
|
91
|
+
return tuple(delta.path for delta in self.file_deltas)
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def total_files(self) -> int:
|
|
95
|
+
"""Total number of modified, added, or deleted files."""
|
|
96
|
+
return len(self.file_deltas)
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def total_additions(self) -> int:
|
|
100
|
+
"""Total additions across all files in the patch."""
|
|
101
|
+
return sum(delta.total_added_lines for delta in self.file_deltas)
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def total_deletions(self) -> int:
|
|
105
|
+
"""Total deletions across all files in the patch."""
|
|
106
|
+
return sum(delta.total_deleted_lines for delta in self.file_deltas)
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
"""Domain models for isolated sandbox configuration, execution, and regression analysis."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from enum import StrEnum
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
|
9
|
+
|
|
10
|
+
from patchproof.exceptions import SandboxSecurityError
|
|
11
|
+
|
|
12
|
+
DISALLOWED_ENV_PATTERNS = (
|
|
13
|
+
"TOKEN",
|
|
14
|
+
"KEY",
|
|
15
|
+
"SECRET",
|
|
16
|
+
"PASSWORD",
|
|
17
|
+
"AUTH",
|
|
18
|
+
"CREDENTIAL",
|
|
19
|
+
"AWS",
|
|
20
|
+
"GITHUB",
|
|
21
|
+
"GH_",
|
|
22
|
+
"DOCKER",
|
|
23
|
+
"SSH",
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
ALLOWLISTED_EXACT_VARS = {
|
|
27
|
+
"PATH",
|
|
28
|
+
"LANG",
|
|
29
|
+
"LC_ALL",
|
|
30
|
+
"PYTHONUNBUFFERED",
|
|
31
|
+
"PYTHONDONTWRITEBYTECODE",
|
|
32
|
+
"PYTHONPATH",
|
|
33
|
+
"CI",
|
|
34
|
+
"TERM",
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
ALLOWLISTED_PREFIXES = ("PYTEST_",)
|
|
38
|
+
|
|
39
|
+
FORBIDDEN_HOST_PATHS = (
|
|
40
|
+
".ssh",
|
|
41
|
+
".aws",
|
|
42
|
+
"docker.sock",
|
|
43
|
+
"docker_engine",
|
|
44
|
+
"/etc/shadow",
|
|
45
|
+
"/etc/passwd",
|
|
46
|
+
"system32",
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
BROAD_ROOT_PATHS = (
|
|
50
|
+
"",
|
|
51
|
+
"/",
|
|
52
|
+
"c:",
|
|
53
|
+
"c:/windows",
|
|
54
|
+
"c:/users",
|
|
55
|
+
"/home",
|
|
56
|
+
"/root",
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class NetworkMode(StrEnum):
|
|
61
|
+
"""Allowed network isolation modes."""
|
|
62
|
+
|
|
63
|
+
NONE = "none"
|
|
64
|
+
INTERNAL_BRIDGE = "internal"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class SandboxNetworkConfig(BaseModel):
|
|
68
|
+
"""Configuration for ephemeral network topology and intra-sandbox multi-service sidecars."""
|
|
69
|
+
|
|
70
|
+
model_config = ConfigDict(frozen=True)
|
|
71
|
+
|
|
72
|
+
mode: NetworkMode = Field(default=NetworkMode.NONE)
|
|
73
|
+
network_name: str | None = Field(default=None)
|
|
74
|
+
sidecars: tuple[str, ...] = Field(default=())
|
|
75
|
+
|
|
76
|
+
@field_validator("network_name")
|
|
77
|
+
@classmethod
|
|
78
|
+
def _validate_network_name(cls, v: str | None) -> str | None:
|
|
79
|
+
if v is not None and not v.startswith("patchproof-net-"):
|
|
80
|
+
raise SandboxSecurityError(
|
|
81
|
+
f"Custom network must begin with 'patchproof-net-' prefix: {v}"
|
|
82
|
+
)
|
|
83
|
+
return v
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class TestOutcome(StrEnum):
|
|
87
|
+
"""Result of an individual test case execution."""
|
|
88
|
+
|
|
89
|
+
__test__ = False
|
|
90
|
+
|
|
91
|
+
PASS = "pass"
|
|
92
|
+
FAIL = "fail"
|
|
93
|
+
ERROR = "error"
|
|
94
|
+
SKIPPED = "skipped"
|
|
95
|
+
XFAIL = "xfail"
|
|
96
|
+
XPASS = "xpass"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class RegressionClassification(StrEnum):
|
|
100
|
+
"""Classification of test outcome evolution between baseline and patched code."""
|
|
101
|
+
|
|
102
|
+
CLEAN_PRESERVATION = "clean_preservation"
|
|
103
|
+
INTRODUCED_REGRESSION = "introduced_regression"
|
|
104
|
+
RESOLVED_DEFECT = "resolved_defect"
|
|
105
|
+
PRE_EXISTING_FAILURE = "pre_existing_failure"
|
|
106
|
+
SUPPRESSION_ALERT = "suppression_alert"
|
|
107
|
+
PRE_EXISTING_SKIP = "pre_existing_skip"
|
|
108
|
+
NEW_TEST_PASSING = "new_test_passing"
|
|
109
|
+
NEW_TEST_FAILING = "new_test_failing"
|
|
110
|
+
NEW_TEST_SKIPPED = "new_test_skipped"
|
|
111
|
+
REMOVED_TEST = "removed_test"
|
|
112
|
+
FLAKY_REGRESSION = "flaky_regression"
|
|
113
|
+
TEST_TAMPERING = "test_tampering"
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class PatchVerdict(StrEnum):
|
|
117
|
+
"""High-level evaluation verdict for a patch."""
|
|
118
|
+
|
|
119
|
+
ACCEPTED = "accepted"
|
|
120
|
+
REJECTED = "rejected"
|
|
121
|
+
SUSPICIOUS = "suspicious"
|
|
122
|
+
INCONCLUSIVE = "inconclusive"
|
|
123
|
+
INFRASTRUCTURE_FAILURE = "infrastructure_failure"
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class SandboxFailureReason(StrEnum):
|
|
127
|
+
"""Explicit taxonomy of sandbox and execution failures."""
|
|
128
|
+
|
|
129
|
+
NONE = "none"
|
|
130
|
+
TIMEOUT = "timeout"
|
|
131
|
+
OOM_KILLED = "oom_killed"
|
|
132
|
+
COLLECTION_FAILURE = "collection_failure"
|
|
133
|
+
DOCKER_UNAVAILABLE = "docker_unavailable"
|
|
134
|
+
UNSUPPORTED_ENGINE = "unsupported_engine"
|
|
135
|
+
SETUP_ERROR = "setup_error"
|
|
136
|
+
EXECUTION_ERROR = "execution_error"
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class ResourceLimits(BaseModel):
|
|
140
|
+
"""Hardware resource limits and execution quotas enforced on test containers."""
|
|
141
|
+
|
|
142
|
+
model_config = ConfigDict(frozen=True)
|
|
143
|
+
|
|
144
|
+
cpu_count: float = Field(default=1.0, gt=0.0, le=4.0)
|
|
145
|
+
memory_limit_mb: int = Field(default=1024, ge=128, le=4096)
|
|
146
|
+
pids_limit: int = Field(default=128, ge=16, le=512)
|
|
147
|
+
timeout_seconds: float = Field(default=15.0, gt=0.0, le=300.0)
|
|
148
|
+
workspace_tmpfs_size_mb: int = Field(default=256, ge=64, le=1024)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class SecurityPolicy(BaseModel):
|
|
152
|
+
"""Strict security invariants enforced on every container execution."""
|
|
153
|
+
|
|
154
|
+
model_config = ConfigDict(frozen=True)
|
|
155
|
+
|
|
156
|
+
network_mode: NetworkMode = Field(default=NetworkMode.NONE)
|
|
157
|
+
network_name: str | None = Field(default=None)
|
|
158
|
+
user: str = Field(default="10001:10001")
|
|
159
|
+
drop_capabilities: tuple[str, ...] = Field(default=("ALL",))
|
|
160
|
+
no_new_privileges: bool = Field(default=True)
|
|
161
|
+
mask_git_dir: bool = Field(default=True)
|
|
162
|
+
read_only_root: bool = Field(default=True)
|
|
163
|
+
allow_host_fallback: bool = Field(default=False)
|
|
164
|
+
|
|
165
|
+
@field_validator("allow_host_fallback")
|
|
166
|
+
@classmethod
|
|
167
|
+
def _validate_host_fallback(cls, v: bool) -> bool:
|
|
168
|
+
if v:
|
|
169
|
+
raise SandboxSecurityError(
|
|
170
|
+
"Host fallback execution is strictly prohibited by security policy"
|
|
171
|
+
)
|
|
172
|
+
return v
|
|
173
|
+
|
|
174
|
+
@field_validator("network_mode", mode="before")
|
|
175
|
+
@classmethod
|
|
176
|
+
def _validate_network_mode(cls, v: Any) -> Any:
|
|
177
|
+
if v in (NetworkMode.NONE, "none", NetworkMode.INTERNAL_BRIDGE, "internal"):
|
|
178
|
+
return v
|
|
179
|
+
raise SandboxSecurityError(
|
|
180
|
+
f"Invalid network mode '{v}'. Only NetworkMode.NONE is permitted "
|
|
181
|
+
"(or NetworkMode.INTERNAL_BRIDGE for intra-sandbox sidecar bridge)"
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
@field_validator("network_name")
|
|
185
|
+
@classmethod
|
|
186
|
+
def _validate_network_name(cls, v: str | None) -> str | None:
|
|
187
|
+
if v is not None and not v.startswith("patchproof-net-"):
|
|
188
|
+
raise SandboxSecurityError(
|
|
189
|
+
f"Custom network must begin with 'patchproof-net-' prefix: {v}"
|
|
190
|
+
)
|
|
191
|
+
return v
|
|
192
|
+
|
|
193
|
+
@field_validator("user")
|
|
194
|
+
@classmethod
|
|
195
|
+
def _validate_user(cls, v: str) -> str:
|
|
196
|
+
cleaned = v.strip().lower()
|
|
197
|
+
uid = cleaned.split(":", 1)[0].strip()
|
|
198
|
+
if uid == "root" or (uid.isdigit() and int(uid) == 0):
|
|
199
|
+
raise SandboxSecurityError("Root container execution is prohibited")
|
|
200
|
+
return v
|
|
201
|
+
|
|
202
|
+
@field_validator("drop_capabilities")
|
|
203
|
+
@classmethod
|
|
204
|
+
def _validate_capabilities(cls, v: tuple[str, ...]) -> tuple[str, ...]:
|
|
205
|
+
if "ALL" not in v:
|
|
206
|
+
raise SandboxSecurityError("drop_capabilities must contain 'ALL'")
|
|
207
|
+
return v
|
|
208
|
+
|
|
209
|
+
@field_validator("no_new_privileges")
|
|
210
|
+
@classmethod
|
|
211
|
+
def _validate_no_new_privileges(cls, v: bool) -> bool:
|
|
212
|
+
if not v:
|
|
213
|
+
raise SandboxSecurityError("no_new_privileges must be True")
|
|
214
|
+
return v
|
|
215
|
+
|
|
216
|
+
@field_validator("mask_git_dir")
|
|
217
|
+
@classmethod
|
|
218
|
+
def _validate_mask_git_dir(cls, v: bool) -> bool:
|
|
219
|
+
if not v:
|
|
220
|
+
raise SandboxSecurityError("mask_git_dir must be True to prevent credential leakage")
|
|
221
|
+
return v
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
class ContainerEnvironment(BaseModel):
|
|
225
|
+
"""Sanitized, allowlist-only environment variables for container execution."""
|
|
226
|
+
|
|
227
|
+
model_config = ConfigDict(frozen=True)
|
|
228
|
+
|
|
229
|
+
env_vars: tuple[tuple[str, str], ...] = Field(default=())
|
|
230
|
+
|
|
231
|
+
@field_validator("env_vars")
|
|
232
|
+
@classmethod
|
|
233
|
+
def _validate_env_vars(cls, v: tuple[tuple[str, str], ...]) -> tuple[tuple[str, str], ...]:
|
|
234
|
+
for key, _ in v:
|
|
235
|
+
key_upper = key.upper()
|
|
236
|
+
for pattern in DISALLOWED_ENV_PATTERNS:
|
|
237
|
+
if pattern in key_upper:
|
|
238
|
+
raise SandboxSecurityError(
|
|
239
|
+
f"Environment variable '{key}' violates credential isolation policy"
|
|
240
|
+
)
|
|
241
|
+
if key not in ALLOWLISTED_EXACT_VARS and not any(
|
|
242
|
+
key.startswith(p) for p in ALLOWLISTED_PREFIXES
|
|
243
|
+
):
|
|
244
|
+
raise SandboxSecurityError(
|
|
245
|
+
f"Environment variable '{key}' violates credential isolation policy "
|
|
246
|
+
"(not in allowlist)"
|
|
247
|
+
)
|
|
248
|
+
return v
|
|
249
|
+
|
|
250
|
+
def as_dict(self) -> dict[str, str]:
|
|
251
|
+
"""Convert tuple of key-value pairs into a standard dictionary."""
|
|
252
|
+
return dict(self.env_vars)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
class ContainerConfig(BaseModel):
|
|
256
|
+
"""Immutable specifications for launching an isolated execution container."""
|
|
257
|
+
|
|
258
|
+
model_config = ConfigDict(frozen=True)
|
|
259
|
+
|
|
260
|
+
image: str = Field(default="patchproof-runner-python:3.11")
|
|
261
|
+
resources: ResourceLimits = Field(default_factory=ResourceLimits)
|
|
262
|
+
security: SecurityPolicy = Field(default_factory=SecurityPolicy)
|
|
263
|
+
network: SandboxNetworkConfig = Field(default_factory=SandboxNetworkConfig)
|
|
264
|
+
environment: ContainerEnvironment = Field(default_factory=ContainerEnvironment)
|
|
265
|
+
host_repo_path: str
|
|
266
|
+
container_workspace_path: str = Field(default="/workspace")
|
|
267
|
+
container_repo_base_path: str = Field(default="/repo_base")
|
|
268
|
+
entrypoint_command: tuple[str, ...] = Field(default=("pytest", "-q"))
|
|
269
|
+
|
|
270
|
+
@field_validator("host_repo_path")
|
|
271
|
+
@classmethod
|
|
272
|
+
def _validate_host_repo_path(cls, v: str) -> str:
|
|
273
|
+
normalized = v.replace("\\", "/").lower().rstrip("/")
|
|
274
|
+
if normalized in BROAD_ROOT_PATHS:
|
|
275
|
+
raise SandboxSecurityError(f"Broad root host path prohibited from mount: {v}")
|
|
276
|
+
for forbidden in FORBIDDEN_HOST_PATHS:
|
|
277
|
+
if forbidden in normalized:
|
|
278
|
+
raise SandboxSecurityError(f"Sensitive host path prohibited from mount: {v}")
|
|
279
|
+
return v
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
class TestCaseResult(BaseModel):
|
|
283
|
+
"""Syntactic outcome and execution measurements of an individual test case."""
|
|
284
|
+
|
|
285
|
+
__test__ = False
|
|
286
|
+
model_config = ConfigDict(frozen=True)
|
|
287
|
+
|
|
288
|
+
name: str = Field(description="Fully-qualified test identifier")
|
|
289
|
+
outcome: TestOutcome = Field(description="Result of test execution")
|
|
290
|
+
duration_seconds: float = Field(ge=0.0, description="Execution duration in seconds")
|
|
291
|
+
failure_message: str | None = Field(default=None, description="Captured failure traceback")
|
|
292
|
+
stdout: str | None = Field(default=None, description="Captured stdout for this test")
|
|
293
|
+
stderr: str | None = Field(default=None, description="Captured stderr for this test")
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
class TestRunReport(BaseModel):
|
|
297
|
+
"""Consolidated test suite report extracted from a container run."""
|
|
298
|
+
|
|
299
|
+
__test__ = False
|
|
300
|
+
model_config = ConfigDict(frozen=True)
|
|
301
|
+
|
|
302
|
+
test_cases: tuple[TestCaseResult, ...] = Field(default=())
|
|
303
|
+
exit_code: int = Field(description="Process exit code from test runner")
|
|
304
|
+
duration_seconds: float = Field(ge=0.0, description="Total duration of test runner execution")
|
|
305
|
+
total_collected: int = Field(ge=0, description="Total number of tests discovered")
|
|
306
|
+
collection_failed: bool = Field(default=False, description="True if test discovery failed")
|
|
307
|
+
collection_error: str | None = Field(default=None, description="Captured collection error")
|
|
308
|
+
|
|
309
|
+
@property
|
|
310
|
+
def passed_count(self) -> int:
|
|
311
|
+
"""Count of passing test cases."""
|
|
312
|
+
return sum(1 for t in self.test_cases if t.outcome == TestOutcome.PASS)
|
|
313
|
+
|
|
314
|
+
@property
|
|
315
|
+
def failed_count(self) -> int:
|
|
316
|
+
"""Count of failed or errored test cases."""
|
|
317
|
+
return sum(1 for t in self.test_cases if t.outcome in (TestOutcome.FAIL, TestOutcome.ERROR))
|
|
318
|
+
|
|
319
|
+
@property
|
|
320
|
+
def skipped_count(self) -> int:
|
|
321
|
+
"""Count of skipped test cases."""
|
|
322
|
+
return sum(1 for t in self.test_cases if t.outcome == TestOutcome.SKIPPED)
|
|
323
|
+
|
|
324
|
+
@property
|
|
325
|
+
def test_map(self) -> dict[str, TestCaseResult]:
|
|
326
|
+
"""Lookup dictionary mapping test name to TestCaseResult."""
|
|
327
|
+
return {t.name: t for t in self.test_cases}
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
class ExecutionResult(BaseModel):
|
|
331
|
+
"""Low-level execution result from container execution."""
|
|
332
|
+
|
|
333
|
+
model_config = ConfigDict(frozen=True)
|
|
334
|
+
|
|
335
|
+
exit_code: int = Field(description="Container process exit code")
|
|
336
|
+
stdout: str = Field(default="", description="Captured stdout from container")
|
|
337
|
+
stderr: str = Field(default="", description="Captured stderr from container")
|
|
338
|
+
duration_seconds: float = Field(ge=0.0, description="Total elapsed container execution time")
|
|
339
|
+
failure_reason: SandboxFailureReason = Field(
|
|
340
|
+
default=SandboxFailureReason.NONE,
|
|
341
|
+
description="Explicit category of failure if execution did not complete normally",
|
|
342
|
+
)
|
|
343
|
+
oom_killed: bool = Field(default=False, description="True if container was OOM killed")
|
|
344
|
+
timeout_triggered: bool = Field(default=False, description="True if container timed out")
|
|
345
|
+
test_report: TestRunReport | None = Field(default=None, description="Parsed test suite report")
|
|
346
|
+
|
|
347
|
+
@property
|
|
348
|
+
def is_success(self) -> bool:
|
|
349
|
+
"""Return True if execution completed with exit code 0 and no failure signals."""
|
|
350
|
+
return (
|
|
351
|
+
self.exit_code == 0
|
|
352
|
+
and self.failure_reason == SandboxFailureReason.NONE
|
|
353
|
+
and not self.oom_killed
|
|
354
|
+
and not self.timeout_triggered
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
class ComparativeTestCase(BaseModel):
|
|
359
|
+
"""Comparison of a single test case between baseline and patched execution."""
|
|
360
|
+
|
|
361
|
+
model_config = ConfigDict(frozen=True)
|
|
362
|
+
|
|
363
|
+
test_name: str = Field(description="Test case identifier")
|
|
364
|
+
baseline_outcome: TestOutcome | None = Field(default=None, description="Baseline result")
|
|
365
|
+
patched_outcome: TestOutcome | None = Field(default=None, description="Patched result")
|
|
366
|
+
classification: RegressionClassification = Field(description="Behavioral categorization")
|
|
367
|
+
message: str | None = Field(default=None, description="Explanatory notes on classification")
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
class ComparativeExecutionResult(BaseModel):
|
|
371
|
+
"""Complete reconciliation report between baseline and patched container runs."""
|
|
372
|
+
|
|
373
|
+
model_config = ConfigDict(frozen=True)
|
|
374
|
+
|
|
375
|
+
baseline_result: ExecutionResult = Field(description="Execution result from pre-patch state")
|
|
376
|
+
patched_result: ExecutionResult = Field(description="Execution result from post-patch state")
|
|
377
|
+
verdict: PatchVerdict = Field(description="Final empirical evaluation verdict for the patch")
|
|
378
|
+
test_comparisons: tuple[ComparativeTestCase, ...] = Field(default=())
|
|
379
|
+
introduced_regressions_count: int = Field(ge=0, default=0)
|
|
380
|
+
flaky_tests_count: int = Field(ge=0, default=0)
|
|
381
|
+
resolved_defects_count: int = Field(ge=0, default=0)
|
|
382
|
+
pre_existing_failures_count: int = Field(ge=0, default=0)
|
|
383
|
+
suppressed_tests_count: int = Field(ge=0, default=0)
|
|
384
|
+
removed_tests_count: int = Field(ge=0, default=0)
|
|
385
|
+
test_tampering_detected: bool = Field(default=False)
|
|
386
|
+
tampering_details: tuple[str, ...] = Field(default=())
|
|
387
|
+
collection_failure_occurred: bool = Field(default=False)
|
|
388
|
+
summary: str = Field(default="")
|
|
389
|
+
|
|
390
|
+
@model_validator(mode="after")
|
|
391
|
+
def _validate_verdict_consistency(self) -> ComparativeExecutionResult:
|
|
392
|
+
if self.introduced_regressions_count > 0 and self.verdict in (
|
|
393
|
+
PatchVerdict.ACCEPTED,
|
|
394
|
+
PatchVerdict.INCONCLUSIVE,
|
|
395
|
+
):
|
|
396
|
+
raise SandboxSecurityError(
|
|
397
|
+
"Verdict cannot be ACCEPTED when introduced regressions exist"
|
|
398
|
+
)
|
|
399
|
+
if self.test_tampering_detected and self.verdict == PatchVerdict.ACCEPTED:
|
|
400
|
+
raise SandboxSecurityError(
|
|
401
|
+
"Verdict cannot be ACCEPTED when test tampering is detected"
|
|
402
|
+
)
|
|
403
|
+
if self.removed_tests_count > 0 and self.verdict == PatchVerdict.ACCEPTED:
|
|
404
|
+
raise SandboxSecurityError(
|
|
405
|
+
"Verdict cannot be ACCEPTED when tests were removed or uncollected"
|
|
406
|
+
)
|
|
407
|
+
if self.collection_failure_occurred and self.verdict != PatchVerdict.REJECTED:
|
|
408
|
+
raise SandboxSecurityError("Verdict must be REJECTED when collection failure occurs")
|
|
409
|
+
if (
|
|
410
|
+
self.verdict == PatchVerdict.ACCEPTED
|
|
411
|
+
and self.patched_result.test_report is not None
|
|
412
|
+
and self.patched_result.test_report.passed_count == 0
|
|
413
|
+
):
|
|
414
|
+
raise SandboxSecurityError(
|
|
415
|
+
"Verdict cannot be ACCEPTED when zero tests passed in patched execution"
|
|
416
|
+
)
|
|
417
|
+
return self
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Reporting and visualization subsystem for PatchProof."""
|
|
2
|
+
|
|
3
|
+
from patchproof.reporting.html_report import generate_html_report
|
|
4
|
+
from patchproof.reporting.in_toto import (
|
|
5
|
+
build_dsse_envelope,
|
|
6
|
+
build_in_toto_statement,
|
|
7
|
+
build_slsa_provenance_predicate,
|
|
8
|
+
verify_dsse_envelope,
|
|
9
|
+
)
|
|
10
|
+
from patchproof.reporting.sbom import diff_declared_dependencies
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"build_dsse_envelope",
|
|
14
|
+
"build_in_toto_statement",
|
|
15
|
+
"build_slsa_provenance_predicate",
|
|
16
|
+
"diff_declared_dependencies",
|
|
17
|
+
"generate_html_report",
|
|
18
|
+
"verify_dsse_envelope",
|
|
19
|
+
]
|