ayentra 1.0.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.
- ayentra/__init__.py +63 -0
- ayentra/ast_analyzer.py +460 -0
- ayentra/cli.py +1864 -0
- ayentra/cloud.py +308 -0
- ayentra/concurrency.py +333 -0
- ayentra/contracts.py +303 -0
- ayentra/dogfood.py +278 -0
- ayentra/engine.py +1205 -0
- ayentra/py.typed +0 -0
- ayentra/regression.py +278 -0
- ayentra/repair.py +1217 -0
- ayentra/report.py +276 -0
- ayentra/sarif.py +153 -0
- ayentra/site/app.js +1387 -0
- ayentra/site/favicon.png +0 -0
- ayentra/site/index.html +1622 -0
- ayentra/site/logo.png +0 -0
- ayentra/site/styles.css +1842 -0
- ayentra/studio.py +2628 -0
- ayentra-1.0.0.dist-info/METADATA +260 -0
- ayentra-1.0.0.dist-info/RECORD +25 -0
- ayentra-1.0.0.dist-info/WHEEL +5 -0
- ayentra-1.0.0.dist-info/entry_points.txt +2 -0
- ayentra-1.0.0.dist-info/licenses/LICENSE +181 -0
- ayentra-1.0.0.dist-info/top_level.txt +1 -0
ayentra/__init__.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from ayentra.ast_analyzer import ASTAnalysisResult, CandidateSuggestion, analyze_source
|
|
4
|
+
from ayentra.cloud import (
|
|
5
|
+
UPGRADE_URL,
|
|
6
|
+
is_gating_enforced,
|
|
7
|
+
load_credentials,
|
|
8
|
+
resolve_credentials,
|
|
9
|
+
save_credentials,
|
|
10
|
+
)
|
|
11
|
+
from ayentra.contracts import (
|
|
12
|
+
InvariantSpec,
|
|
13
|
+
SecurityPrincipal,
|
|
14
|
+
StateSnapshot,
|
|
15
|
+
TransactionPolicy,
|
|
16
|
+
TransitionSpec,
|
|
17
|
+
domain,
|
|
18
|
+
invariant,
|
|
19
|
+
load_toml_invariants,
|
|
20
|
+
load_toml_transitions,
|
|
21
|
+
transition_invariant,
|
|
22
|
+
)
|
|
23
|
+
from ayentra.engine import EngineCheckResult, InvariantViolation, run_verification_engine
|
|
24
|
+
from ayentra.repair import PatchVerificationResult, generate_and_verify_patch
|
|
25
|
+
from ayentra.report import VerificationReport, build_verification_report
|
|
26
|
+
from ayentra.sarif import build_sarif_report, to_sarif
|
|
27
|
+
from ayentra.studio import StudioHandler, create_studio_server, execute_full_check, run_studio_server
|
|
28
|
+
|
|
29
|
+
__version__ = "1.0.0"
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"ASTAnalysisResult",
|
|
33
|
+
"CandidateSuggestion",
|
|
34
|
+
"EngineCheckResult",
|
|
35
|
+
"InvariantSpec",
|
|
36
|
+
"InvariantViolation",
|
|
37
|
+
"PatchVerificationResult",
|
|
38
|
+
"SecurityPrincipal",
|
|
39
|
+
"StateSnapshot",
|
|
40
|
+
"TransactionPolicy",
|
|
41
|
+
"TransitionSpec",
|
|
42
|
+
"VerificationReport",
|
|
43
|
+
"analyze_source",
|
|
44
|
+
"build_verification_report",
|
|
45
|
+
"StudioHandler",
|
|
46
|
+
"create_studio_server",
|
|
47
|
+
"domain",
|
|
48
|
+
"execute_full_check",
|
|
49
|
+
"run_studio_server",
|
|
50
|
+
"generate_and_verify_patch",
|
|
51
|
+
"invariant",
|
|
52
|
+
"load_toml_invariants",
|
|
53
|
+
"load_toml_transitions",
|
|
54
|
+
"run_verification_engine",
|
|
55
|
+
"transition_invariant",
|
|
56
|
+
"build_sarif_report",
|
|
57
|
+
"to_sarif",
|
|
58
|
+
"resolve_credentials",
|
|
59
|
+
"load_credentials",
|
|
60
|
+
"save_credentials",
|
|
61
|
+
"is_gating_enforced",
|
|
62
|
+
"UPGRADE_URL",
|
|
63
|
+
]
|
ayentra/ast_analyzer.py
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import ast
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True)
|
|
9
|
+
class CandidateSuggestion:
|
|
10
|
+
target_name: str
|
|
11
|
+
kind: str
|
|
12
|
+
suggested_expression: str
|
|
13
|
+
rationale: str
|
|
14
|
+
lineno: int
|
|
15
|
+
label: str = "Candidate Suggestion (Requires Developer Approval)"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class ASTAnalysisResult:
|
|
20
|
+
filepath: str
|
|
21
|
+
explicit_decorator_lines: dict[tuple[str, str], int] = field(default_factory=dict)
|
|
22
|
+
mutated_attributes: dict[str, set[str]] = field(default_factory=dict)
|
|
23
|
+
has_lock_synchronization: dict[str, bool] = field(default_factory=dict)
|
|
24
|
+
suggestions: list[CandidateSuggestion] = field(default_factory=list)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def analyze_source(filepath: str | Path) -> ASTAnalysisResult:
|
|
28
|
+
path = Path(filepath)
|
|
29
|
+
source = path.read_text(encoding="utf-8")
|
|
30
|
+
tree = ast.parse(source, filename=str(path))
|
|
31
|
+
result = ASTAnalysisResult(filepath=str(path))
|
|
32
|
+
|
|
33
|
+
for node in tree.body:
|
|
34
|
+
if isinstance(node, ast.ClassDef):
|
|
35
|
+
_analyze_class(node, result)
|
|
36
|
+
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
|
37
|
+
_analyze_function(node, result, parent_class=None)
|
|
38
|
+
|
|
39
|
+
return result
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _extract_invariant_decorators(
|
|
43
|
+
node: ast.ClassDef | ast.FunctionDef | ast.AsyncFunctionDef,
|
|
44
|
+
target_name: str,
|
|
45
|
+
result: ASTAnalysisResult,
|
|
46
|
+
) -> set[str]:
|
|
47
|
+
found: set[str] = set()
|
|
48
|
+
for dec in node.decorator_list:
|
|
49
|
+
if isinstance(dec, ast.Call):
|
|
50
|
+
func_name = ""
|
|
51
|
+
if isinstance(dec.func, ast.Name):
|
|
52
|
+
func_name = dec.func.id
|
|
53
|
+
elif isinstance(dec.func, ast.Attribute):
|
|
54
|
+
func_name = dec.func.attr
|
|
55
|
+
if func_name in ("invariant", "transition_invariant") and dec.args:
|
|
56
|
+
first_arg = dec.args[0]
|
|
57
|
+
if isinstance(first_arg, ast.Constant) and isinstance(first_arg.value, str):
|
|
58
|
+
expr = first_arg.value.strip()
|
|
59
|
+
result.explicit_decorator_lines[(target_name, expr)] = dec.lineno
|
|
60
|
+
found.add(expr)
|
|
61
|
+
return found
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _analyze_class(node: ast.ClassDef, result: ASTAnalysisResult) -> None:
|
|
65
|
+
explicit = _extract_invariant_decorators(node, node.name, result)
|
|
66
|
+
attrs_read: set[str] = set()
|
|
67
|
+
attrs_mutated: set[str] = set()
|
|
68
|
+
all_attrs: set[str] = set()
|
|
69
|
+
has_lock = False
|
|
70
|
+
|
|
71
|
+
for child in ast.walk(node):
|
|
72
|
+
if isinstance(child, ast.Attribute) and isinstance(child.value, ast.Name) and child.value.id == "self":
|
|
73
|
+
all_attrs.add(child.attr)
|
|
74
|
+
if isinstance(child.ctx, ast.Store):
|
|
75
|
+
attrs_mutated.add(child.attr)
|
|
76
|
+
elif isinstance(child.ctx, ast.Load):
|
|
77
|
+
attrs_read.add(child.attr)
|
|
78
|
+
if "lock" in child.attr.lower():
|
|
79
|
+
has_lock = True
|
|
80
|
+
elif isinstance(child, ast.With):
|
|
81
|
+
for item in child.items:
|
|
82
|
+
if "lock" in ast.unparse(item.context_expr).lower():
|
|
83
|
+
has_lock = True
|
|
84
|
+
|
|
85
|
+
result.mutated_attributes[node.name] = attrs_mutated
|
|
86
|
+
result.has_lock_synchronization[node.name] = has_lock
|
|
87
|
+
|
|
88
|
+
for method in node.body:
|
|
89
|
+
if isinstance(method, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
|
90
|
+
_analyze_function(method, result, parent_class=node.name)
|
|
91
|
+
|
|
92
|
+
for attr in sorted(attrs_mutated):
|
|
93
|
+
if attr in {"allocated", "available", "reserved", "stock", "balance", "count"}:
|
|
94
|
+
expr = f"self.{attr} >= 0"
|
|
95
|
+
if not any(expr in e for e in explicit):
|
|
96
|
+
result.suggestions.append(
|
|
97
|
+
CandidateSuggestion(
|
|
98
|
+
target_name=node.name,
|
|
99
|
+
kind="class",
|
|
100
|
+
suggested_expression=expr,
|
|
101
|
+
rationale=f"Attribute 'self.{attr}' is mutated across methods; non-negativity guard may apply.",
|
|
102
|
+
lineno=node.lineno,
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
if "allocated" in all_attrs and "capacity" in all_attrs:
|
|
107
|
+
expr = "self.allocated <= self.capacity"
|
|
108
|
+
if not any(expr in e for e in explicit):
|
|
109
|
+
result.suggestions.append(
|
|
110
|
+
CandidateSuggestion(
|
|
111
|
+
target_name=node.name,
|
|
112
|
+
kind="class",
|
|
113
|
+
suggested_expression=expr,
|
|
114
|
+
rationale="Detected paired 'allocated' and 'capacity' state attributes.",
|
|
115
|
+
lineno=node.lineno,
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
if {"available", "reserved", "initial_total"}.issubset(all_attrs):
|
|
120
|
+
expr = "self.available + self.reserved == self.initial_total"
|
|
121
|
+
if not any(expr in e for e in explicit):
|
|
122
|
+
result.suggestions.append(
|
|
123
|
+
CandidateSuggestion(
|
|
124
|
+
target_name=node.name,
|
|
125
|
+
kind="class",
|
|
126
|
+
suggested_expression=expr,
|
|
127
|
+
rationale="Detected conservation triplet ('available', 'reserved', 'initial_total').",
|
|
128
|
+
lineno=node.lineno,
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
rmw_attrs = attrs_read.intersection(attrs_mutated) - {"_lock", "lock"}
|
|
133
|
+
if rmw_attrs and not has_lock:
|
|
134
|
+
result.suggestions.append(
|
|
135
|
+
CandidateSuggestion(
|
|
136
|
+
target_name=node.name,
|
|
137
|
+
kind="class",
|
|
138
|
+
suggested_expression=f"@invariant(..., check_concurrency=True) on {node.name}",
|
|
139
|
+
rationale=f"Read-modify-write mutations on {sorted(rmw_attrs)} without lock synchronization.",
|
|
140
|
+
lineno=node.lineno,
|
|
141
|
+
)
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _analyze_function(
|
|
146
|
+
node: ast.FunctionDef | ast.AsyncFunctionDef,
|
|
147
|
+
result: ASTAnalysisResult,
|
|
148
|
+
parent_class: str | None,
|
|
149
|
+
) -> None:
|
|
150
|
+
qualname = f"{parent_class}.{node.name}" if parent_class else node.name
|
|
151
|
+
explicit = _extract_invariant_decorators(node, qualname, result)
|
|
152
|
+
param_names = {a.arg for a in node.args.args if a.arg != "self"}
|
|
153
|
+
optional_params: set[str] = set()
|
|
154
|
+
str_params: set[str] = set()
|
|
155
|
+
decimal_params: set[str] = set()
|
|
156
|
+
for param in node.args.args:
|
|
157
|
+
if param.arg == "self" or param.annotation is None:
|
|
158
|
+
continue
|
|
159
|
+
ann_src = ast.unparse(param.annotation)
|
|
160
|
+
if "None" in ann_src or "Optional" in ann_src:
|
|
161
|
+
optional_params.add(param.arg)
|
|
162
|
+
if ann_src.strip() == "str" or "str" in ann_src:
|
|
163
|
+
str_params.add(param.arg)
|
|
164
|
+
if "Decimal" in ann_src:
|
|
165
|
+
decimal_params.add(param.arg)
|
|
166
|
+
|
|
167
|
+
uses_float_cast = False
|
|
168
|
+
checks_isfinite = False
|
|
169
|
+
has_value_error_handler = False
|
|
170
|
+
checks_isdigit = False
|
|
171
|
+
uses_decimal_or_round = bool(decimal_params)
|
|
172
|
+
decimal_line = node.lineno
|
|
173
|
+
int_cast_params: list[tuple[str, int]] = []
|
|
174
|
+
uuid_cast_params: list[tuple[str, int]] = []
|
|
175
|
+
fromhex_cast_params: list[tuple[str, int]] = []
|
|
176
|
+
validated_uuid_params: set[str] = set()
|
|
177
|
+
validated_fromhex_params: set[str] = set()
|
|
178
|
+
divisors: list[tuple[str, int]] = []
|
|
179
|
+
subscripts: list[tuple[str, str, int]] = []
|
|
180
|
+
checked_pairs: set[tuple[str, str]] = set()
|
|
181
|
+
get_vars: set[str] = set()
|
|
182
|
+
none_checked_params: set[str] = set()
|
|
183
|
+
attr_accesses: list[tuple[str, str, int]] = []
|
|
184
|
+
mass_assignment_nodes: list[tuple[str, int]] = []
|
|
185
|
+
|
|
186
|
+
for child in ast.walk(node):
|
|
187
|
+
if isinstance(child, ast.ExceptHandler):
|
|
188
|
+
if child.type is None:
|
|
189
|
+
has_value_error_handler = True
|
|
190
|
+
elif isinstance(child.type, ast.Name) and child.type.id in {"ValueError", "Exception", "BaseException"}:
|
|
191
|
+
has_value_error_handler = True
|
|
192
|
+
elif isinstance(child.type, ast.Tuple) and any(
|
|
193
|
+
isinstance(e, ast.Name) and e.id in {"ValueError", "Exception", "BaseException"}
|
|
194
|
+
for e in child.type.elts
|
|
195
|
+
):
|
|
196
|
+
has_value_error_handler = True
|
|
197
|
+
elif isinstance(child, ast.Call):
|
|
198
|
+
if isinstance(child.func, ast.Name):
|
|
199
|
+
if child.func.id == "float":
|
|
200
|
+
uses_float_cast = True
|
|
201
|
+
elif child.func.id == "Decimal":
|
|
202
|
+
uses_decimal_or_round = True
|
|
203
|
+
decimal_line = child.lineno
|
|
204
|
+
elif child.func.id == "round" and len(child.args) >= 2:
|
|
205
|
+
uses_decimal_or_round = True
|
|
206
|
+
decimal_line = child.lineno
|
|
207
|
+
elif child.func.id == "int" and child.args:
|
|
208
|
+
first_arg = child.args[0]
|
|
209
|
+
if isinstance(first_arg, ast.Name) and first_arg.id in str_params:
|
|
210
|
+
int_cast_params.append((first_arg.id, child.lineno))
|
|
211
|
+
elif child.func.id == "UUID" and (child.args or child.keywords):
|
|
212
|
+
target_arg = child.args[0] if child.args else (child.keywords[0].value if child.keywords else None)
|
|
213
|
+
if isinstance(target_arg, ast.Name) and target_arg.id in param_names:
|
|
214
|
+
uuid_cast_params.append((target_arg.id, child.lineno))
|
|
215
|
+
elif child.func.id == "setattr" and child.args:
|
|
216
|
+
if isinstance(child.args[0], ast.Name) and child.args[0].id == "self":
|
|
217
|
+
if len(child.args) >= 2 and not (
|
|
218
|
+
isinstance(child.args[1], ast.Constant) and isinstance(child.args[1].value, str)
|
|
219
|
+
):
|
|
220
|
+
mass_assignment_nodes.append(("setattr", child.lineno))
|
|
221
|
+
elif isinstance(child.func, ast.Attribute):
|
|
222
|
+
if child.func.attr in {"isfinite", "isnan", "isinf"}:
|
|
223
|
+
checks_isfinite = True
|
|
224
|
+
elif child.func.attr == "isdigit":
|
|
225
|
+
checks_isdigit = True
|
|
226
|
+
elif child.func.attr == "quantize":
|
|
227
|
+
uses_decimal_or_round = True
|
|
228
|
+
decimal_line = child.lineno
|
|
229
|
+
elif child.func.attr == "get" and isinstance(child.func.value, ast.Name):
|
|
230
|
+
get_vars.add(child.func.value.id)
|
|
231
|
+
elif child.func.attr == "update":
|
|
232
|
+
val = child.func.value
|
|
233
|
+
if (
|
|
234
|
+
isinstance(val, ast.Attribute)
|
|
235
|
+
and val.attr == "__dict__"
|
|
236
|
+
and isinstance(val.value, ast.Name)
|
|
237
|
+
and val.value.id == "self"
|
|
238
|
+
):
|
|
239
|
+
mass_assignment_nodes.append(("__dict__.update", child.lineno))
|
|
240
|
+
elif (
|
|
241
|
+
child.func.attr == "UUID"
|
|
242
|
+
and isinstance(child.func.value, ast.Name)
|
|
243
|
+
and child.func.value.id == "uuid"
|
|
244
|
+
and (child.args or child.keywords)
|
|
245
|
+
):
|
|
246
|
+
target_arg = child.args[0] if child.args else (child.keywords[0].value if child.keywords else None)
|
|
247
|
+
if isinstance(target_arg, ast.Name) and target_arg.id in param_names:
|
|
248
|
+
uuid_cast_params.append((target_arg.id, child.lineno))
|
|
249
|
+
elif (
|
|
250
|
+
child.func.attr == "fromhex"
|
|
251
|
+
and isinstance(child.func.value, ast.Name)
|
|
252
|
+
and child.func.value.id in {"bytes", "bytearray"}
|
|
253
|
+
and (child.args or child.keywords)
|
|
254
|
+
):
|
|
255
|
+
target_arg = child.args[0] if child.args else (child.keywords[0].value if child.keywords else None)
|
|
256
|
+
if isinstance(target_arg, ast.Name) and target_arg.id in param_names:
|
|
257
|
+
fromhex_cast_params.append((target_arg.id, child.lineno))
|
|
258
|
+
elif isinstance(child, ast.BinOp) and isinstance(child.op, (ast.Div, ast.FloorDiv, ast.Mod)):
|
|
259
|
+
if not isinstance(child.right, ast.Constant):
|
|
260
|
+
divisors.append((ast.unparse(child.right), child.lineno))
|
|
261
|
+
elif isinstance(child, ast.Compare):
|
|
262
|
+
for name_node in ast.walk(child):
|
|
263
|
+
if isinstance(name_node, ast.Name) and name_node.id in param_names:
|
|
264
|
+
p_id = name_node.id
|
|
265
|
+
cmp_src = ast.unparse(child)
|
|
266
|
+
if "len(" in cmp_src and any(k in cmp_src for k in ("32", "36", "37")):
|
|
267
|
+
validated_uuid_params.add(p_id)
|
|
268
|
+
if "% 2" in cmp_src:
|
|
269
|
+
validated_fromhex_params.add(p_id)
|
|
270
|
+
cur = child.left
|
|
271
|
+
for op, comp in zip(child.ops, child.comparators):
|
|
272
|
+
if isinstance(op, (ast.In, ast.NotIn)) and isinstance(comp, ast.Name):
|
|
273
|
+
checked_pairs.add((comp.id, ast.unparse(cur)))
|
|
274
|
+
elif isinstance(op, (ast.Is, ast.IsNot, ast.Eq, ast.NotEq)):
|
|
275
|
+
if isinstance(cur, ast.Name) and isinstance(comp, ast.Constant) and comp.value is None:
|
|
276
|
+
none_checked_params.add(cur.id)
|
|
277
|
+
elif isinstance(comp, ast.Name) and isinstance(cur, ast.Constant) and cur.value is None:
|
|
278
|
+
none_checked_params.add(comp.id)
|
|
279
|
+
cur = comp
|
|
280
|
+
elif (
|
|
281
|
+
isinstance(child, ast.Subscript)
|
|
282
|
+
and isinstance(child.ctx, ast.Load)
|
|
283
|
+
and isinstance(child.value, ast.Name)
|
|
284
|
+
and child.value.id in param_names
|
|
285
|
+
):
|
|
286
|
+
subscripts.append((child.value.id, ast.unparse(child.slice), child.lineno))
|
|
287
|
+
elif (
|
|
288
|
+
isinstance(child, ast.Attribute)
|
|
289
|
+
and isinstance(child.ctx, ast.Load)
|
|
290
|
+
and isinstance(child.value, ast.Name)
|
|
291
|
+
and child.value.id in param_names
|
|
292
|
+
):
|
|
293
|
+
attr_accesses.append((child.value.id, child.attr, child.lineno))
|
|
294
|
+
|
|
295
|
+
# Detect split-commit leaks: mutations on self preceding raise statements without rollback
|
|
296
|
+
split_commit_nodes: list[tuple[str, int, int]] = []
|
|
297
|
+
has_payload_param = any(p in {"payload", "updates", "data", "body"} or "dict" in p.lower() for p in param_names)
|
|
298
|
+
has_transition_dec = any("transition_invariant" in e or "TransactionPolicy" in e for e in explicit)
|
|
299
|
+
if (has_payload_param or has_transition_dec) and parent_class:
|
|
300
|
+
mutations_before_raise: list[tuple[str, int]] = []
|
|
301
|
+
for stmt in node.body:
|
|
302
|
+
if isinstance(stmt, (ast.Assign, ast.AugAssign)):
|
|
303
|
+
targets = [stmt.target] if isinstance(stmt, ast.AugAssign) else stmt.targets
|
|
304
|
+
for t in targets:
|
|
305
|
+
if isinstance(t, ast.Attribute) and isinstance(t.value, ast.Name) and t.value.id == "self":
|
|
306
|
+
mutations_before_raise.append((t.attr, stmt.lineno))
|
|
307
|
+
elif isinstance(stmt, ast.If):
|
|
308
|
+
raises_in_if = [n for n in ast.walk(stmt) if isinstance(n, ast.Raise)]
|
|
309
|
+
if raises_in_if and mutations_before_raise:
|
|
310
|
+
for attr_m, m_line in mutations_before_raise:
|
|
311
|
+
if m_line < stmt.lineno:
|
|
312
|
+
split_commit_nodes.append((attr_m, m_line, raises_in_if[0].lineno))
|
|
313
|
+
mutations_before_raise.clear()
|
|
314
|
+
elif isinstance(stmt, ast.Raise) and mutations_before_raise:
|
|
315
|
+
for attr_m, m_line in mutations_before_raise:
|
|
316
|
+
if m_line < stmt.lineno:
|
|
317
|
+
split_commit_nodes.append((attr_m, m_line, stmt.lineno))
|
|
318
|
+
mutations_before_raise.clear()
|
|
319
|
+
elif isinstance(stmt, ast.Try):
|
|
320
|
+
mutations_before_raise.clear()
|
|
321
|
+
|
|
322
|
+
if uses_float_cast and not checks_isfinite:
|
|
323
|
+
expr = "all(math.isfinite(v) for v in result.values())"
|
|
324
|
+
if not any("isfinite" in e for e in explicit):
|
|
325
|
+
result.suggestions.append(
|
|
326
|
+
CandidateSuggestion(
|
|
327
|
+
target_name=qualname,
|
|
328
|
+
kind="function",
|
|
329
|
+
suggested_expression=expr,
|
|
330
|
+
rationale="Function parses floats with float() without checking math.isfinite() against NaN/Inf.",
|
|
331
|
+
lineno=node.lineno,
|
|
332
|
+
)
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
if uses_decimal_or_round:
|
|
336
|
+
expr = "result >= 0"
|
|
337
|
+
if not any("result >= 0" in e for e in explicit):
|
|
338
|
+
result.suggestions.append(
|
|
339
|
+
CandidateSuggestion(
|
|
340
|
+
target_name=qualname,
|
|
341
|
+
kind="function",
|
|
342
|
+
suggested_expression=expr,
|
|
343
|
+
rationale="Financial Decimal or round(..., 2) calculation detected; non-negative quantized postcondition may apply.",
|
|
344
|
+
lineno=decimal_line,
|
|
345
|
+
)
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
seen_int_params: set[str] = set()
|
|
349
|
+
if not has_value_error_handler and not checks_isdigit:
|
|
350
|
+
for p_name, int_line in int_cast_params:
|
|
351
|
+
if p_name not in seen_int_params:
|
|
352
|
+
seen_int_params.add(p_name)
|
|
353
|
+
expr = f"len({p_name}.strip()) > 0"
|
|
354
|
+
if not any(expr in e for e in explicit):
|
|
355
|
+
result.suggestions.append(
|
|
356
|
+
CandidateSuggestion(
|
|
357
|
+
target_name=qualname,
|
|
358
|
+
kind="function",
|
|
359
|
+
suggested_expression=expr,
|
|
360
|
+
rationale=f"Function calls int() on string parameter '{p_name}' without validating numeric format or handling ValueError.",
|
|
361
|
+
lineno=int_line,
|
|
362
|
+
)
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
seen_uuid_params: set[str] = set()
|
|
366
|
+
if not has_value_error_handler:
|
|
367
|
+
for p_name, uuid_line in uuid_cast_params:
|
|
368
|
+
if p_name not in seen_uuid_params and p_name not in validated_uuid_params:
|
|
369
|
+
seen_uuid_params.add(p_name)
|
|
370
|
+
expr = f"len({p_name}.strip()) in (32, 36)"
|
|
371
|
+
if not any(expr in e for e in explicit):
|
|
372
|
+
result.suggestions.append(
|
|
373
|
+
CandidateSuggestion(
|
|
374
|
+
target_name=qualname,
|
|
375
|
+
kind="function",
|
|
376
|
+
suggested_expression=expr,
|
|
377
|
+
rationale=f"Function calls uuid.UUID() on parameter '{p_name}' without validating UUID format or handling ValueError.",
|
|
378
|
+
lineno=uuid_line,
|
|
379
|
+
)
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
seen_fromhex_params: set[str] = set()
|
|
383
|
+
if not has_value_error_handler:
|
|
384
|
+
for p_name, fromhex_line in fromhex_cast_params:
|
|
385
|
+
if p_name not in seen_fromhex_params and p_name not in validated_fromhex_params:
|
|
386
|
+
seen_fromhex_params.add(p_name)
|
|
387
|
+
expr = f"len({p_name}.strip()) % 2 == 0"
|
|
388
|
+
if not any(expr in e for e in explicit):
|
|
389
|
+
result.suggestions.append(
|
|
390
|
+
CandidateSuggestion(
|
|
391
|
+
target_name=qualname,
|
|
392
|
+
kind="function",
|
|
393
|
+
suggested_expression=expr,
|
|
394
|
+
rationale=f"Function calls bytes.fromhex() on parameter '{p_name}' without validating hex format or handling ValueError.",
|
|
395
|
+
lineno=fromhex_line,
|
|
396
|
+
)
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
for div_expr, div_line in divisors:
|
|
400
|
+
result.suggestions.append(
|
|
401
|
+
CandidateSuggestion(
|
|
402
|
+
target_name=qualname,
|
|
403
|
+
kind="function",
|
|
404
|
+
suggested_expression=f"{div_expr} != 0",
|
|
405
|
+
rationale=f"Division by '{div_expr}' at line {div_line} without explicit non-zero precondition.",
|
|
406
|
+
lineno=div_line,
|
|
407
|
+
)
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
for d_name, k_expr, sub_line in subscripts:
|
|
411
|
+
if (d_name, k_expr) not in checked_pairs and d_name not in get_vars:
|
|
412
|
+
expr = f"{k_expr} in {d_name}"
|
|
413
|
+
if not any(expr in e for e in explicit):
|
|
414
|
+
result.suggestions.append(
|
|
415
|
+
CandidateSuggestion(
|
|
416
|
+
target_name=qualname,
|
|
417
|
+
kind="function",
|
|
418
|
+
suggested_expression=expr,
|
|
419
|
+
rationale=f"Unvalidated subscript '{d_name}[{k_expr}]' at line {sub_line} without membership check or .get().",
|
|
420
|
+
lineno=sub_line,
|
|
421
|
+
)
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
seen_none_params: set[str] = set()
|
|
425
|
+
for p_name, attr_name, attr_line in attr_accesses:
|
|
426
|
+
if p_name in optional_params and p_name not in none_checked_params and p_name not in seen_none_params:
|
|
427
|
+
seen_none_params.add(p_name)
|
|
428
|
+
expr = f"{p_name} is not None"
|
|
429
|
+
if not any(expr in e for e in explicit):
|
|
430
|
+
result.suggestions.append(
|
|
431
|
+
CandidateSuggestion(
|
|
432
|
+
target_name=qualname,
|
|
433
|
+
kind="function",
|
|
434
|
+
suggested_expression=expr,
|
|
435
|
+
rationale=f"Unvalidated attribute access '{p_name}.{attr_name}' on optional parameter at line {attr_line} without None check.",
|
|
436
|
+
lineno=attr_line,
|
|
437
|
+
)
|
|
438
|
+
)
|
|
439
|
+
|
|
440
|
+
for kind_lbl, m_line in mass_assignment_nodes:
|
|
441
|
+
result.suggestions.append(
|
|
442
|
+
CandidateSuggestion(
|
|
443
|
+
target_name=qualname,
|
|
444
|
+
kind="function",
|
|
445
|
+
suggested_expression="payload = {k: v for k, v in payload.items() if k in allowed_keys}",
|
|
446
|
+
rationale=f"Unfiltered mass assignment via {kind_lbl} at line {m_line} on compound payload. Enforce explicit field allowlist.",
|
|
447
|
+
lineno=m_line,
|
|
448
|
+
)
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
for attr_name, m_line, r_line in split_commit_nodes:
|
|
452
|
+
result.suggestions.append(
|
|
453
|
+
CandidateSuggestion(
|
|
454
|
+
target_name=qualname,
|
|
455
|
+
kind="function",
|
|
456
|
+
suggested_expression=f"@transition_invariant(..., policy=TransactionPolicy.STRICT_ATOMIC) on {qualname}",
|
|
457
|
+
rationale=f"State mutation on 'self.{attr_name}' at line {m_line} precedes validation exception at line {r_line} without atomic rollback (split-commit leak).",
|
|
458
|
+
lineno=m_line,
|
|
459
|
+
)
|
|
460
|
+
)
|