apodictic-sdk 0.0.0-stage → 3.9.0
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.
- package/README.md +58 -2
- package/package.json +29 -4
- package/src/enclave_worker.py +717 -0
- package/src/index.d.ts +157 -0
- package/src/index.mjs +622 -0
- package/src/odoo20_guard.mjs +321 -0
- package/src/pglite_shadow.d.mts +36 -0
- package/src/pglite_shadow.mjs +286 -0
- package/src/standalone_z3_enclave.py +2094 -0
- package/src/z3_wasm_verifier.mjs +1314 -0
|
@@ -0,0 +1,2094 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Apodictic Standalone Z3 5.4.1 Formal Verification Kernel & SQLite WAL Enclave.
|
|
4
|
+
Self-contained engine shipped directly inside @apodictic/sdk so npm/npx installs
|
|
5
|
+
run full Z3 theorem proving, SQL AST inspection, PII scrubbing, persistent rollback,
|
|
6
|
+
3-way match, indirect tax verification, bi-temporal queries, and WORM SHA-256 sealing
|
|
7
|
+
without requiring the monorepo.
|
|
8
|
+
"""
|
|
9
|
+
import base64
|
|
10
|
+
from collections import deque
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
import hashlib
|
|
13
|
+
import json
|
|
14
|
+
import math
|
|
15
|
+
import os
|
|
16
|
+
import re
|
|
17
|
+
import sqlite3
|
|
18
|
+
import string
|
|
19
|
+
import sys
|
|
20
|
+
import time
|
|
21
|
+
import uuid
|
|
22
|
+
from typing import Any, Dict, List, Optional, Set, Tuple, Union
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
import z3
|
|
26
|
+
except ImportError:
|
|
27
|
+
msg = (
|
|
28
|
+
"Python z3-solver not found. Install with: pip install z3-solver, "
|
|
29
|
+
"or pass --endpoint https://www.apodictic.dev/api/v1/enclave"
|
|
30
|
+
)
|
|
31
|
+
sys.stderr.write(json.dumps({"error": msg}) + "\n")
|
|
32
|
+
sys.stderr.flush()
|
|
33
|
+
sys.exit(2)
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
import sqlglot
|
|
37
|
+
from sqlglot import exp as sg_exp
|
|
38
|
+
HAS_SQLGLOT = True
|
|
39
|
+
except Exception:
|
|
40
|
+
HAS_SQLGLOT = False
|
|
41
|
+
|
|
42
|
+
ZERO_WIDTH_RE = re.compile(r"[\u200B-\u200D\uFEFF\u00AD]")
|
|
43
|
+
SQL_COMMENT_RE = re.compile(r"--[^\n]*|/\*[\s\S]*?\*/")
|
|
44
|
+
STRING_LIT_RE = re.compile(r"'(?:''|[^'])*'")
|
|
45
|
+
|
|
46
|
+
_IBAN_ALPHA_MAP: Dict[int, str] = {
|
|
47
|
+
ord(ch): str(idx) for idx, ch in enumerate(string.digits + string.ascii_uppercase)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def validate_iban_mod97(candidate: str) -> bool:
|
|
52
|
+
"""ISO-7064 Mod-97-10 IBAN mathematical checksum verification."""
|
|
53
|
+
cleaned = re.sub(r"[\s-]+", "", candidate or "").upper()
|
|
54
|
+
if len(cleaned) < 15 or len(cleaned) > 34:
|
|
55
|
+
return False
|
|
56
|
+
if not (cleaned[:2].isalpha() and cleaned[2:4].isdigit() and cleaned[4:].isalnum()):
|
|
57
|
+
return False
|
|
58
|
+
try:
|
|
59
|
+
rearranged = cleaned[4:] + cleaned[:2] + "00"
|
|
60
|
+
numeric = int(rearranged.translate(_IBAN_ALPHA_MAP))
|
|
61
|
+
return f"{98 - (numeric % 97):0>2}" == cleaned[2:4]
|
|
62
|
+
except Exception:
|
|
63
|
+
return False
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def validate_luhn_pan(candidate: str) -> bool:
|
|
67
|
+
"""Luhn Mod-10 payment card PAN checksum verification."""
|
|
68
|
+
digits_str = re.sub(r"[\s-]+", "", candidate or "")
|
|
69
|
+
if not digits_str.isdigit() or len(digits_str) < 13 or len(digits_str) > 19:
|
|
70
|
+
return False
|
|
71
|
+
digits = [int(d) for d in digits_str]
|
|
72
|
+
checksum = sum(digits[-1::-2])
|
|
73
|
+
for d in digits[-2::-2]:
|
|
74
|
+
doubled = d * 2
|
|
75
|
+
checksum += doubled - 9 if doubled > 9 else doubled
|
|
76
|
+
return checksum > 0 and (checksum % 10 == 0)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
PII_RULES = [
|
|
80
|
+
("SECRET", re.compile(r"-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----")),
|
|
81
|
+
("SECRET", re.compile(r"\b(?:sk_live|sk_test|pk_live|pk_test|rk_live|rk_test|ghp|gho|glpat|xoxb|xoxp|odoo_rpc_live)_[A-Za-z0-9_-]{6,}\b")),
|
|
82
|
+
("SECRET", re.compile(r"\bAIzaSy[A-Za-z0-9_-]{20,40}\b")),
|
|
83
|
+
("SECRET", re.compile(r"\bAKIA[0-9A-Z]{16}\b")),
|
|
84
|
+
("SECRET", re.compile(r"\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b")),
|
|
85
|
+
("DB_URL", re.compile(r"\bpostgres(?:ql)?://[^\s'\"]+@[^\s'\"]+\b", re.I)),
|
|
86
|
+
("IBAN", re.compile(r"\b[A-Za-z]{2}\d{2}(?:[A-Za-z0-9]{10,30}|(?:[\s-][A-Za-z0-9]{4}){2,7}(?:[\s-][A-Za-z0-9]{1,4})?)\b")),
|
|
87
|
+
("CARD", re.compile(r"\b(?:\d{4}[-\s]?\d{6}[-\s]?\d{5}|\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{1,7}|\d{13,19})\b")),
|
|
88
|
+
("EMAIL", re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b")),
|
|
89
|
+
("TAX", re.compile(r"\b(?:ETA-\d{6,12}|TIN\s*\d{2}-\d{7}|\d{3}-\d{3}-\d{3})\b")),
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
SEVERITY_ORDER = {
|
|
93
|
+
"DROP": 7,
|
|
94
|
+
"TRUNCATE": 6,
|
|
95
|
+
"DELETE": 5,
|
|
96
|
+
"ALTER": 4,
|
|
97
|
+
"UPDATE": 3,
|
|
98
|
+
"INSERT": 2,
|
|
99
|
+
"QUERY": 0,
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
GENESIS_HASH = "0" * 64
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def rfc6962_leaf_hash(data: bytes) -> bytes:
|
|
106
|
+
return hashlib.sha256(b"\x00" + data).digest()
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def rfc6962_node_hash(left: bytes, right: bytes) -> bytes:
|
|
110
|
+
return hashlib.sha256(b"\x01" + left + right).digest()
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _largest_power_of_two_less_than(n: int) -> int:
|
|
114
|
+
if n <= 1:
|
|
115
|
+
return 0
|
|
116
|
+
k = 1
|
|
117
|
+
while (k << 1) < n:
|
|
118
|
+
k <<= 1
|
|
119
|
+
return k
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def rfc6962_tree_hash(leaves: List[bytes]) -> bytes:
|
|
123
|
+
n = len(leaves)
|
|
124
|
+
if n == 0:
|
|
125
|
+
return hashlib.sha256(b"").digest()
|
|
126
|
+
if n == 1:
|
|
127
|
+
return leaves[0]
|
|
128
|
+
k = _largest_power_of_two_less_than(n)
|
|
129
|
+
return rfc6962_node_hash(rfc6962_tree_hash(leaves[:k]), rfc6962_tree_hash(leaves[k:]))
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def rfc6962_audit_path(m: int, leaves: List[bytes]) -> List[bytes]:
|
|
133
|
+
n = len(leaves)
|
|
134
|
+
if n <= 1 or m >= n:
|
|
135
|
+
return []
|
|
136
|
+
k = _largest_power_of_two_less_than(n)
|
|
137
|
+
if m < k:
|
|
138
|
+
return rfc6962_audit_path(m, leaves[:k]) + [rfc6962_tree_hash(leaves[k:])]
|
|
139
|
+
return rfc6962_audit_path(m - k, leaves[k:]) + [rfc6962_tree_hash(leaves[:k])]
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def rfc6962_verify_inclusion(
|
|
143
|
+
leaf_hash: bytes,
|
|
144
|
+
leaf_index: int,
|
|
145
|
+
tree_size: int,
|
|
146
|
+
audit_path: List[bytes],
|
|
147
|
+
expected_root: bytes,
|
|
148
|
+
) -> bool:
|
|
149
|
+
if leaf_index >= tree_size or tree_size <= 0:
|
|
150
|
+
return False
|
|
151
|
+
if tree_size == 1:
|
|
152
|
+
return len(audit_path) == 0 and leaf_hash == expected_root
|
|
153
|
+
|
|
154
|
+
def _reconstruct(m: int, n: int, leaf: bytes, path: List[bytes]) -> Optional[bytes]:
|
|
155
|
+
if n == 1:
|
|
156
|
+
return leaf if len(path) == 0 else None
|
|
157
|
+
if not path:
|
|
158
|
+
return None
|
|
159
|
+
k = _largest_power_of_two_less_than(n)
|
|
160
|
+
sibling = path[-1]
|
|
161
|
+
sub = path[:-1]
|
|
162
|
+
if m < k:
|
|
163
|
+
left = _reconstruct(m, k, leaf, sub)
|
|
164
|
+
return rfc6962_node_hash(left, sibling) if left else None
|
|
165
|
+
right = _reconstruct(m - k, n - k, leaf, sub)
|
|
166
|
+
return rfc6962_node_hash(sibling, right) if right else None
|
|
167
|
+
|
|
168
|
+
computed = _reconstruct(leaf_index, tree_size, leaf_hash, audit_path)
|
|
169
|
+
return computed == expected_root
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
POLICY_STMT_RE = re.compile(
|
|
173
|
+
r"\b(?P<effect>permit|forbid)\s*\((?P<head>[^)]*)\)\s*"
|
|
174
|
+
r"(?:(?P<guard_kind>when|unless)\s*\{\s*(?P<guard_expr>[^}]*)\s*\})?\s*;",
|
|
175
|
+
re.I | re.S,
|
|
176
|
+
)
|
|
177
|
+
POLICY_ACTION_RE = re.compile(r'Action::"([^"]+)"', re.I)
|
|
178
|
+
POLICY_PRINCIPAL_RE = re.compile(r'principal\s*(?P<op>in|==)\s*(?P<target>[A-Za-z0-9_]+::"[^"]+")', re.I)
|
|
179
|
+
POLICY_RESOURCE_RE = re.compile(r'resource\s*(?P<op>in|==)\s*(?P<target>[A-Za-z0-9_]+::"[^"]+")', re.I)
|
|
180
|
+
|
|
181
|
+
DEFAULT_ENTERPRISE_POLICY = """
|
|
182
|
+
permit(
|
|
183
|
+
principal,
|
|
184
|
+
action,
|
|
185
|
+
resource
|
|
186
|
+
) when {
|
|
187
|
+
context.amount >= 0 &&
|
|
188
|
+
context.post_balance >= context.min_reserve &&
|
|
189
|
+
(context.amount <= context.spending_limit || context.cfo_signed == true)
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
forbid(
|
|
193
|
+
principal,
|
|
194
|
+
action in [Action::"DROP", Action::"TRUNCATE", Action::"DELETE", Action::"ALTER"],
|
|
195
|
+
resource
|
|
196
|
+
) unless {
|
|
197
|
+
context.reversal_armed == true
|
|
198
|
+
};
|
|
199
|
+
""".strip()
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
class EntityHierarchyGraph:
|
|
203
|
+
def __init__(self, parent_edges: Optional[Dict[str, List[str]]] = None):
|
|
204
|
+
self.parents: Dict[str, List[str]] = {}
|
|
205
|
+
if parent_edges:
|
|
206
|
+
for child, plist in parent_edges.items():
|
|
207
|
+
self.parents[str(child)] = [str(p) for p in plist]
|
|
208
|
+
|
|
209
|
+
def ancestors(self, entity: str) -> Set[str]:
|
|
210
|
+
visited: Set[str] = set()
|
|
211
|
+
queue = deque([entity])
|
|
212
|
+
while queue:
|
|
213
|
+
curr = queue.popleft()
|
|
214
|
+
if curr in visited:
|
|
215
|
+
continue
|
|
216
|
+
visited.add(curr)
|
|
217
|
+
for p in self.parents.get(curr, []):
|
|
218
|
+
if p not in visited:
|
|
219
|
+
queue.append(p)
|
|
220
|
+
return visited
|
|
221
|
+
|
|
222
|
+
def is_member_of(self, entity: Optional[str], target: Optional[str], op: Optional[str]) -> bool:
|
|
223
|
+
if not op or not target:
|
|
224
|
+
return True
|
|
225
|
+
if not entity:
|
|
226
|
+
return False
|
|
227
|
+
if op == "==":
|
|
228
|
+
return entity == target
|
|
229
|
+
if op == "in":
|
|
230
|
+
return target in self.ancestors(entity)
|
|
231
|
+
return False
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
class GuardCompileError(ValueError):
|
|
235
|
+
"""Raised when a policy guard expression contains invalid syntax or unknown fields."""
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@dataclass
|
|
239
|
+
class _LiteralNode:
|
|
240
|
+
value: Union[bool, float, str]
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
@dataclass
|
|
244
|
+
class _VarNode:
|
|
245
|
+
name: str
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
@dataclass
|
|
249
|
+
class _UnaryNode:
|
|
250
|
+
op: str
|
|
251
|
+
operand: Any
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
@dataclass
|
|
255
|
+
class _BinaryNode:
|
|
256
|
+
op: str
|
|
257
|
+
left: Any
|
|
258
|
+
right: Any
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
_GUARD_TOKEN_SPEC = [
|
|
262
|
+
("SKIP", r"\s+"),
|
|
263
|
+
("OR", r"\|\|"),
|
|
264
|
+
("AND", r"&&"),
|
|
265
|
+
("EQ", r"=="),
|
|
266
|
+
("NE", r"!="),
|
|
267
|
+
("LE", r"<="),
|
|
268
|
+
("GE", r">="),
|
|
269
|
+
("LT", r"<"),
|
|
270
|
+
("GT", r">"),
|
|
271
|
+
("NOT", r"!"),
|
|
272
|
+
("PLUS", r"\+"),
|
|
273
|
+
("MINUS", r"-"),
|
|
274
|
+
("STAR", r"\*"),
|
|
275
|
+
("SLASH", r"/"),
|
|
276
|
+
("LPAREN", r"\("),
|
|
277
|
+
("RPAREN", r"\)"),
|
|
278
|
+
("NUMBER", r"\d+(?:\.\d+)?"),
|
|
279
|
+
("STRING", r'"(?:\\.|[^"\\])*"|\'(?:\\.|[^\'\\])*\''),
|
|
280
|
+
("IDENT", r"[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*"),
|
|
281
|
+
]
|
|
282
|
+
_GUARD_TOKEN_RE = re.compile("|".join(f"(?P<{name}>{pat})" for name, pat in _GUARD_TOKEN_SPEC))
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
class PolicyGuardZ3Compiler:
|
|
286
|
+
"""Zero-eval recursive-descent parser and symbolic Z3 5.4.1 compiler for policy guards."""
|
|
287
|
+
|
|
288
|
+
def __init__(self, expr: str):
|
|
289
|
+
self.tokens = self._tokenize(expr)
|
|
290
|
+
self.pos = 0
|
|
291
|
+
|
|
292
|
+
@staticmethod
|
|
293
|
+
def _tokenize(expr: str) -> List[Tuple[str, str]]:
|
|
294
|
+
tokens: List[Tuple[str, str]] = []
|
|
295
|
+
idx = 0
|
|
296
|
+
n = len(expr)
|
|
297
|
+
while idx < n:
|
|
298
|
+
m = _GUARD_TOKEN_RE.match(expr, idx)
|
|
299
|
+
if not m:
|
|
300
|
+
raise GuardCompileError(f"Unexpected token at position {idx}")
|
|
301
|
+
kind = m.lastgroup
|
|
302
|
+
val = m.group()
|
|
303
|
+
idx = m.end()
|
|
304
|
+
if kind != "SKIP":
|
|
305
|
+
tokens.append((kind or "", val))
|
|
306
|
+
return tokens
|
|
307
|
+
|
|
308
|
+
def _peek(self) -> Optional[Tuple[str, str]]:
|
|
309
|
+
return self.tokens[self.pos] if self.pos < len(self.tokens) else None
|
|
310
|
+
|
|
311
|
+
def _consume(self, expected: Optional[str] = None) -> Tuple[str, str]:
|
|
312
|
+
tok = self._peek()
|
|
313
|
+
if tok is None:
|
|
314
|
+
raise GuardCompileError("Unexpected end of guard expression")
|
|
315
|
+
if expected and tok[0] != expected:
|
|
316
|
+
raise GuardCompileError(f"Expected {expected}, got {tok[0]}")
|
|
317
|
+
self.pos += 1
|
|
318
|
+
return tok
|
|
319
|
+
|
|
320
|
+
@classmethod
|
|
321
|
+
def parse(cls, expr: str) -> Any:
|
|
322
|
+
stripped = (expr or "").strip()
|
|
323
|
+
if not stripped:
|
|
324
|
+
return _LiteralNode(True)
|
|
325
|
+
parser = cls(stripped)
|
|
326
|
+
ast = parser._parse_or()
|
|
327
|
+
if parser._peek() is not None:
|
|
328
|
+
raise GuardCompileError(f"Trailing tokens: {parser._peek()}")
|
|
329
|
+
return ast
|
|
330
|
+
|
|
331
|
+
def _parse_or(self) -> Any:
|
|
332
|
+
node = self._parse_and()
|
|
333
|
+
while (tok := self._peek()) is not None and tok[0] == "OR":
|
|
334
|
+
self._consume()
|
|
335
|
+
node = _BinaryNode("||", node, self._parse_and())
|
|
336
|
+
return node
|
|
337
|
+
|
|
338
|
+
def _parse_and(self) -> Any:
|
|
339
|
+
node = self._parse_cmp()
|
|
340
|
+
while (tok := self._peek()) is not None and tok[0] == "AND":
|
|
341
|
+
self._consume()
|
|
342
|
+
node = _BinaryNode("&&", node, self._parse_cmp())
|
|
343
|
+
return node
|
|
344
|
+
|
|
345
|
+
def _parse_cmp(self) -> Any:
|
|
346
|
+
node = self._parse_add()
|
|
347
|
+
while (tok := self._peek()) is not None and tok[0] in ("EQ", "NE", "LT", "LE", "GT", "GE"):
|
|
348
|
+
_, op = self._consume()
|
|
349
|
+
node = _BinaryNode(op, node, self._parse_add())
|
|
350
|
+
return node
|
|
351
|
+
|
|
352
|
+
def _parse_add(self) -> Any:
|
|
353
|
+
node = self._parse_mul()
|
|
354
|
+
while (tok := self._peek()) is not None and tok[0] in ("PLUS", "MINUS"):
|
|
355
|
+
_, op = self._consume()
|
|
356
|
+
node = _BinaryNode(op, node, self._parse_mul())
|
|
357
|
+
return node
|
|
358
|
+
|
|
359
|
+
def _parse_mul(self) -> Any:
|
|
360
|
+
node = self._parse_unary()
|
|
361
|
+
while (tok := self._peek()) is not None and tok[0] in ("STAR", "SLASH"):
|
|
362
|
+
_, op = self._consume()
|
|
363
|
+
node = _BinaryNode(op, node, self._parse_unary())
|
|
364
|
+
return node
|
|
365
|
+
|
|
366
|
+
def _parse_unary(self) -> Any:
|
|
367
|
+
tok = self._peek()
|
|
368
|
+
if tok is not None and tok[0] in ("NOT", "MINUS"):
|
|
369
|
+
_, op = self._consume()
|
|
370
|
+
return _UnaryNode(op, self._parse_unary())
|
|
371
|
+
return self._parse_primary()
|
|
372
|
+
|
|
373
|
+
def _parse_primary(self) -> Any:
|
|
374
|
+
tok = self._peek()
|
|
375
|
+
if tok is None:
|
|
376
|
+
raise GuardCompileError("Expected operand")
|
|
377
|
+
kind, val = tok
|
|
378
|
+
if kind == "LPAREN":
|
|
379
|
+
self._consume("LPAREN")
|
|
380
|
+
node = self._parse_or()
|
|
381
|
+
self._consume("RPAREN")
|
|
382
|
+
return node
|
|
383
|
+
if kind == "NUMBER":
|
|
384
|
+
self._consume()
|
|
385
|
+
return _LiteralNode(float(val))
|
|
386
|
+
if kind == "STRING":
|
|
387
|
+
self._consume()
|
|
388
|
+
return _LiteralNode(val[1:-1].replace('\\"', '"').replace("\\'", "'"))
|
|
389
|
+
if kind == "IDENT":
|
|
390
|
+
self._consume()
|
|
391
|
+
low = val.lower()
|
|
392
|
+
if low == "true":
|
|
393
|
+
return _LiteralNode(True)
|
|
394
|
+
if low == "false":
|
|
395
|
+
return _LiteralNode(False)
|
|
396
|
+
if low in ("and", "or", "not"):
|
|
397
|
+
raise GuardCompileError(f"Unsupported keyword operator {val!r}")
|
|
398
|
+
field_name = val[len("context."):] if val.startswith("context.") else val
|
|
399
|
+
return _VarNode(field_name)
|
|
400
|
+
raise GuardCompileError(f"Unexpected token {tok!r}")
|
|
401
|
+
|
|
402
|
+
@classmethod
|
|
403
|
+
def eval_ast(cls, node: Any, ctx_map: Dict[str, Any]) -> Any:
|
|
404
|
+
if isinstance(node, _LiteralNode):
|
|
405
|
+
return node.value
|
|
406
|
+
if isinstance(node, _VarNode):
|
|
407
|
+
if node.name not in ctx_map:
|
|
408
|
+
raise GuardCompileError(f"Unbound context field: {node.name}")
|
|
409
|
+
return ctx_map[node.name]
|
|
410
|
+
if isinstance(node, _UnaryNode):
|
|
411
|
+
val = cls.eval_ast(node.operand, ctx_map)
|
|
412
|
+
if node.op == "!":
|
|
413
|
+
if not isinstance(val, bool):
|
|
414
|
+
raise GuardCompileError("Boolean NOT requires boolean operand")
|
|
415
|
+
return not val
|
|
416
|
+
if node.op == "-":
|
|
417
|
+
if isinstance(val, bool) or not isinstance(val, (int, float)):
|
|
418
|
+
raise GuardCompileError("Unary minus requires numeric operand")
|
|
419
|
+
return -float(val)
|
|
420
|
+
if isinstance(node, _BinaryNode):
|
|
421
|
+
if node.op == "&&":
|
|
422
|
+
return bool(cls.eval_ast(node.left, ctx_map)) and bool(cls.eval_ast(node.right, ctx_map))
|
|
423
|
+
if node.op == "||":
|
|
424
|
+
return bool(cls.eval_ast(node.left, ctx_map)) or bool(cls.eval_ast(node.right, ctx_map))
|
|
425
|
+
left = cls.eval_ast(node.left, ctx_map)
|
|
426
|
+
right = cls.eval_ast(node.right, ctx_map)
|
|
427
|
+
if node.op == "+":
|
|
428
|
+
return float(left) + float(right)
|
|
429
|
+
if node.op == "-":
|
|
430
|
+
return float(left) - float(right)
|
|
431
|
+
if node.op == "*":
|
|
432
|
+
return float(left) * float(right)
|
|
433
|
+
if node.op == "/":
|
|
434
|
+
r_num = float(right)
|
|
435
|
+
if r_num == 0.0:
|
|
436
|
+
raise GuardCompileError("Division by zero")
|
|
437
|
+
return float(left) / r_num
|
|
438
|
+
if node.op == "==":
|
|
439
|
+
return left == right
|
|
440
|
+
if node.op == "!=":
|
|
441
|
+
return left != right
|
|
442
|
+
if isinstance(left, bool) or isinstance(right, bool):
|
|
443
|
+
raise GuardCompileError("Ordering comparison cannot accept boolean")
|
|
444
|
+
if node.op == "<":
|
|
445
|
+
return float(left) < float(right)
|
|
446
|
+
if node.op == "<=":
|
|
447
|
+
return float(left) <= float(right)
|
|
448
|
+
if node.op == ">":
|
|
449
|
+
return float(left) > float(right)
|
|
450
|
+
if node.op == ">=":
|
|
451
|
+
return float(left) >= float(right)
|
|
452
|
+
raise GuardCompileError(f"Unsupported AST node: {node!r}")
|
|
453
|
+
|
|
454
|
+
@classmethod
|
|
455
|
+
def compile_z3(cls, node: Any, z3_ctx: z3.Context, z3_vars: Dict[str, Any]) -> Any:
|
|
456
|
+
if isinstance(node, _LiteralNode):
|
|
457
|
+
if isinstance(node.value, bool):
|
|
458
|
+
return z3.BoolVal(node.value, ctx=z3_ctx)
|
|
459
|
+
if isinstance(node.value, (int, float)):
|
|
460
|
+
return z3.RealVal(str(node.value), ctx=z3_ctx)
|
|
461
|
+
return z3.StringVal(str(node.value), ctx=z3_ctx)
|
|
462
|
+
if isinstance(node, _VarNode):
|
|
463
|
+
if node.name not in z3_vars:
|
|
464
|
+
raise GuardCompileError(f"Unbound Z3 context symbol: {node.name}")
|
|
465
|
+
return z3_vars[node.name]
|
|
466
|
+
if isinstance(node, _UnaryNode):
|
|
467
|
+
sub = cls.compile_z3(node.operand, z3_ctx, z3_vars)
|
|
468
|
+
if node.op == "!":
|
|
469
|
+
if not z3.is_bool(sub):
|
|
470
|
+
raise GuardCompileError("Z3 NOT requires BoolRef")
|
|
471
|
+
return z3.Not(sub)
|
|
472
|
+
if node.op == "-":
|
|
473
|
+
if not z3.is_arith(sub):
|
|
474
|
+
raise GuardCompileError("Z3 unary minus requires ArithRef")
|
|
475
|
+
return -sub
|
|
476
|
+
if isinstance(node, _BinaryNode):
|
|
477
|
+
lhs = cls.compile_z3(node.left, z3_ctx, z3_vars)
|
|
478
|
+
rhs = cls.compile_z3(node.right, z3_ctx, z3_vars)
|
|
479
|
+
if node.op == "&&":
|
|
480
|
+
if not (z3.is_bool(lhs) and z3.is_bool(rhs)):
|
|
481
|
+
raise GuardCompileError("Z3 && requires BoolRef")
|
|
482
|
+
return z3.And(lhs, rhs)
|
|
483
|
+
if node.op == "||":
|
|
484
|
+
if not (z3.is_bool(lhs) and z3.is_bool(rhs)):
|
|
485
|
+
raise GuardCompileError("Z3 || requires BoolRef")
|
|
486
|
+
return z3.Or(lhs, rhs)
|
|
487
|
+
if node.op in ("+", "-", "*", "/"):
|
|
488
|
+
if not (z3.is_arith(lhs) and z3.is_arith(rhs)):
|
|
489
|
+
raise GuardCompileError("Z3 arithmetic requires ArithRef")
|
|
490
|
+
if node.op == "+":
|
|
491
|
+
return lhs + rhs
|
|
492
|
+
if node.op == "-":
|
|
493
|
+
return lhs - rhs
|
|
494
|
+
if node.op == "*":
|
|
495
|
+
return lhs * rhs
|
|
496
|
+
return lhs / rhs
|
|
497
|
+
if node.op == "==":
|
|
498
|
+
if lhs.sort() != rhs.sort():
|
|
499
|
+
raise GuardCompileError("Z3 == sort mismatch")
|
|
500
|
+
return lhs == rhs
|
|
501
|
+
if node.op == "!=":
|
|
502
|
+
if lhs.sort() != rhs.sort():
|
|
503
|
+
raise GuardCompileError("Z3 != sort mismatch")
|
|
504
|
+
return lhs != rhs
|
|
505
|
+
if not (z3.is_arith(lhs) and z3.is_arith(rhs)):
|
|
506
|
+
raise GuardCompileError("Z3 ordering requires ArithRef")
|
|
507
|
+
if node.op == "<":
|
|
508
|
+
return lhs < rhs
|
|
509
|
+
if node.op == "<=":
|
|
510
|
+
return lhs <= rhs
|
|
511
|
+
if node.op == ">":
|
|
512
|
+
return lhs > rhs
|
|
513
|
+
if node.op == ">=":
|
|
514
|
+
return lhs >= rhs
|
|
515
|
+
raise GuardCompileError(f"Unsupported Z3 AST node: {node!r}")
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
def evaluate_policy_z3(
|
|
519
|
+
policy_text: str,
|
|
520
|
+
action: str,
|
|
521
|
+
context: Dict[str, Any],
|
|
522
|
+
principal: Optional[str] = None,
|
|
523
|
+
resource: Optional[str] = None,
|
|
524
|
+
entity_hierarchy: Optional[Dict[str, List[str]]] = None,
|
|
525
|
+
) -> Dict[str, Any]:
|
|
526
|
+
src = (policy_text or DEFAULT_ENTERPRISE_POLICY).strip()
|
|
527
|
+
norm_action = (action or "UPDATE").upper()
|
|
528
|
+
dag = EntityHierarchyGraph(entity_hierarchy or context.get("entity_hierarchy"))
|
|
529
|
+
eff_principal = principal or context.get("principal")
|
|
530
|
+
eff_resource = resource or context.get("resource")
|
|
531
|
+
|
|
532
|
+
amount = float(context.get("amount", 0.0))
|
|
533
|
+
current_balance = float(context.get("current_balance", 250000.0))
|
|
534
|
+
min_reserve = float(context.get("min_reserve", 50000.0))
|
|
535
|
+
spending_limit = float(context.get("spending_limit", 50000.0))
|
|
536
|
+
cfo_signed = bool(context.get("cfo_signed", False))
|
|
537
|
+
reversal_armed = bool(context.get("reversal_armed", True))
|
|
538
|
+
post_balance = current_balance - amount
|
|
539
|
+
|
|
540
|
+
eval_ctx: Dict[str, Any] = {
|
|
541
|
+
"amount": amount,
|
|
542
|
+
"current_balance": current_balance,
|
|
543
|
+
"post_balance": post_balance,
|
|
544
|
+
"min_reserve": min_reserve,
|
|
545
|
+
"spending_limit": spending_limit,
|
|
546
|
+
"cfo_signed": cfo_signed,
|
|
547
|
+
"reversal_armed": reversal_armed,
|
|
548
|
+
}
|
|
549
|
+
for k, v in (context or {}).items():
|
|
550
|
+
if k in eval_ctx or k in ("entity_hierarchy", "principal", "resource"):
|
|
551
|
+
continue
|
|
552
|
+
if isinstance(v, bool):
|
|
553
|
+
eval_ctx[k] = v
|
|
554
|
+
elif isinstance(v, (int, float)):
|
|
555
|
+
eval_ctx[k] = float(v)
|
|
556
|
+
elif isinstance(v, str):
|
|
557
|
+
eval_ctx[k] = v
|
|
558
|
+
|
|
559
|
+
z3_ctx = z3.Context()
|
|
560
|
+
s = z3.Solver(ctx=z3_ctx)
|
|
561
|
+
z3_vars: Dict[str, Any] = {}
|
|
562
|
+
for k, v in eval_ctx.items():
|
|
563
|
+
if isinstance(v, bool):
|
|
564
|
+
z_var = z3.Bool(f"ctx_{k}", ctx=z3_ctx)
|
|
565
|
+
s.add(z_var == z3.BoolVal(v, ctx=z3_ctx))
|
|
566
|
+
elif isinstance(v, (int, float)):
|
|
567
|
+
z_var = z3.Real(f"ctx_{k}", ctx=z3_ctx)
|
|
568
|
+
if k != "post_balance":
|
|
569
|
+
s.add(z_var == z3.RealVal(str(v), ctx=z3_ctx))
|
|
570
|
+
else:
|
|
571
|
+
z_var = z3.String(f"ctx_{k}", ctx=z3_ctx)
|
|
572
|
+
s.add(z_var == z3.StringVal(str(v), ctx=z3_ctx))
|
|
573
|
+
z3_vars[k] = z_var
|
|
574
|
+
if "post_balance" in z3_vars and "current_balance" in z3_vars and "amount" in z3_vars:
|
|
575
|
+
s.add(z3_vars["post_balance"] == z3_vars["current_balance"] - z3_vars["amount"])
|
|
576
|
+
|
|
577
|
+
permit_matched: List[str] = []
|
|
578
|
+
forbid_triggered: List[str] = []
|
|
579
|
+
matching_permit_z3: List[z3.BoolRef] = []
|
|
580
|
+
matching_forbid_z3: List[z3.BoolRef] = []
|
|
581
|
+
hierarchy_scope_checked = False
|
|
582
|
+
any_permit_scope_matched = False
|
|
583
|
+
any_permit_scope_failed = False
|
|
584
|
+
rules = list(POLICY_STMT_RE.finditer(src))
|
|
585
|
+
|
|
586
|
+
for idx, m in enumerate(rules):
|
|
587
|
+
effect = m.group("effect").lower()
|
|
588
|
+
head = m.group("head") or ""
|
|
589
|
+
actions = [a.upper() for a in POLICY_ACTION_RE.findall(head)]
|
|
590
|
+
if actions and norm_action not in actions:
|
|
591
|
+
continue
|
|
592
|
+
p_m = POLICY_PRINCIPAL_RE.search(head)
|
|
593
|
+
r_m = POLICY_RESOURCE_RE.search(head)
|
|
594
|
+
if p_m or r_m:
|
|
595
|
+
hierarchy_scope_checked = True
|
|
596
|
+
p_ok = dag.is_member_of(
|
|
597
|
+
eff_principal,
|
|
598
|
+
p_m.group("target") if p_m else None,
|
|
599
|
+
p_m.group("op").lower() if p_m else None,
|
|
600
|
+
)
|
|
601
|
+
res_ok = dag.is_member_of(
|
|
602
|
+
eff_resource,
|
|
603
|
+
r_m.group("target") if r_m else None,
|
|
604
|
+
r_m.group("op").lower() if r_m else None,
|
|
605
|
+
)
|
|
606
|
+
if not (p_ok and res_ok):
|
|
607
|
+
if effect == "permit":
|
|
608
|
+
any_permit_scope_failed = True
|
|
609
|
+
continue
|
|
610
|
+
if effect == "permit":
|
|
611
|
+
any_permit_scope_matched = True
|
|
612
|
+
|
|
613
|
+
guard_kind = m.group("guard_kind").lower() if m.group("guard_kind") else None
|
|
614
|
+
guard_expr = (m.group("guard_expr") or "").strip()
|
|
615
|
+
try:
|
|
616
|
+
guard_ast = PolicyGuardZ3Compiler.parse(guard_expr)
|
|
617
|
+
guard_ok = bool(PolicyGuardZ3Compiler.eval_ast(guard_ast, eval_ctx))
|
|
618
|
+
z_guard = PolicyGuardZ3Compiler.compile_z3(guard_ast, z3_ctx, z3_vars)
|
|
619
|
+
if not z3.is_bool(z_guard):
|
|
620
|
+
raise GuardCompileError("Guard must compile to BoolRef")
|
|
621
|
+
except GuardCompileError:
|
|
622
|
+
guard_ok = False
|
|
623
|
+
z_guard = z3.BoolVal(False, ctx=z3_ctx)
|
|
624
|
+
|
|
625
|
+
if guard_kind == "unless":
|
|
626
|
+
applies = not guard_ok
|
|
627
|
+
z_applies = z3.Not(z_guard)
|
|
628
|
+
else:
|
|
629
|
+
applies = guard_ok
|
|
630
|
+
z_applies = z_guard
|
|
631
|
+
|
|
632
|
+
rule_id = f"apodictic_policy_{idx + 1}_{effect}"
|
|
633
|
+
if effect == "permit":
|
|
634
|
+
matching_permit_z3.append(z_applies)
|
|
635
|
+
if applies:
|
|
636
|
+
permit_matched.append(rule_id)
|
|
637
|
+
elif effect == "forbid":
|
|
638
|
+
matching_forbid_z3.append(z_applies)
|
|
639
|
+
if applies:
|
|
640
|
+
forbid_triggered.append(rule_id)
|
|
641
|
+
|
|
642
|
+
hierarchy_matched = any_permit_scope_matched or (not any_permit_scope_failed)
|
|
643
|
+
z_forbid_none = (
|
|
644
|
+
z3.And(*[z3.Not(zf) for zf in matching_forbid_z3])
|
|
645
|
+
if matching_forbid_z3
|
|
646
|
+
else z3.BoolVal(True, ctx=z3_ctx)
|
|
647
|
+
)
|
|
648
|
+
z_hier = z3.BoolVal((not hierarchy_scope_checked) or hierarchy_matched, ctx=z3_ctx)
|
|
649
|
+
z_any_permit = (
|
|
650
|
+
z3.Or(*matching_permit_z3)
|
|
651
|
+
if matching_permit_z3
|
|
652
|
+
else z3.BoolVal(False, ctx=z3_ctx)
|
|
653
|
+
)
|
|
654
|
+
|
|
655
|
+
s.assert_and_track(z_forbid_none, "POLICY_FORBID_OVERRIDE")
|
|
656
|
+
s.assert_and_track(z_hier, "POLICY_ENTITY_HIERARCHY_MATCH")
|
|
657
|
+
s.assert_and_track(z_any_permit, "POLICY_EXPLICIT_PERMIT_REQUIRED")
|
|
658
|
+
|
|
659
|
+
chk = s.check()
|
|
660
|
+
allowed = chk == z3.sat
|
|
661
|
+
return {
|
|
662
|
+
"engine": "apodictic-policy-z3-compiler-v3-symbolic-ast",
|
|
663
|
+
"decision": "ALLOW" if allowed else "DENY",
|
|
664
|
+
"verdict": "SAT" if allowed else "UNSAT",
|
|
665
|
+
"allowed": allowed,
|
|
666
|
+
"action": norm_action,
|
|
667
|
+
"principal": eff_principal,
|
|
668
|
+
"resource": eff_resource,
|
|
669
|
+
"principal_ancestors": sorted(dag.ancestors(eff_principal)) if eff_principal else [],
|
|
670
|
+
"resource_ancestors": sorted(dag.ancestors(eff_resource)) if eff_resource else [],
|
|
671
|
+
"rules_evaluated": len(rules),
|
|
672
|
+
"permit_matched": permit_matched,
|
|
673
|
+
"forbid_triggered": forbid_triggered,
|
|
674
|
+
"unsat_core": [] if allowed else [str(c) for c in s.unsat_core()],
|
|
675
|
+
"smt_formula": s.to_smt2(),
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
def strip_zero_width(text: str) -> str:
|
|
680
|
+
return ZERO_WIDTH_RE.sub("", text or "")
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
def strip_sql_comments(sql: str) -> str:
|
|
684
|
+
# Replace comments with space while respecting quoted strings cleanly
|
|
685
|
+
parts: List[str] = []
|
|
686
|
+
i = 0
|
|
687
|
+
n = len(sql)
|
|
688
|
+
while i < n:
|
|
689
|
+
ch = sql[i]
|
|
690
|
+
if ch == "'":
|
|
691
|
+
j = i + 1
|
|
692
|
+
while j < n:
|
|
693
|
+
if sql[j] == "'" and j + 1 < n and sql[j + 1] == "'":
|
|
694
|
+
j += 2
|
|
695
|
+
elif sql[j] == "'":
|
|
696
|
+
j += 1
|
|
697
|
+
break
|
|
698
|
+
else:
|
|
699
|
+
j += 1
|
|
700
|
+
parts.append(sql[i:j])
|
|
701
|
+
i = j
|
|
702
|
+
elif sql[i : i + 2] == "--":
|
|
703
|
+
j = sql.find("\n", i)
|
|
704
|
+
i = n if j == -1 else j + 1
|
|
705
|
+
parts.append(" ")
|
|
706
|
+
elif sql[i : i + 2] == "/*":
|
|
707
|
+
j = sql.find("*/", i + 2)
|
|
708
|
+
i = n if j == -1 else j + 2
|
|
709
|
+
parts.append(" ")
|
|
710
|
+
else:
|
|
711
|
+
parts.append(ch)
|
|
712
|
+
i += 1
|
|
713
|
+
return "".join(parts)
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
def split_sql_statements(sql: str) -> List[str]:
|
|
717
|
+
clean = strip_sql_comments(strip_zero_width(sql))
|
|
718
|
+
stmts: List[str] = []
|
|
719
|
+
buf: List[str] = []
|
|
720
|
+
in_single = False
|
|
721
|
+
i = 0
|
|
722
|
+
while i < len(clean):
|
|
723
|
+
ch = clean[i]
|
|
724
|
+
if ch == "'":
|
|
725
|
+
in_single = not in_single
|
|
726
|
+
buf.append(ch)
|
|
727
|
+
if in_single and i + 1 < len(clean) and clean[i + 1] == "'":
|
|
728
|
+
buf.append("'")
|
|
729
|
+
i += 2
|
|
730
|
+
in_single = not in_single
|
|
731
|
+
continue
|
|
732
|
+
elif ch == ";" and not in_single:
|
|
733
|
+
piece = "".join(buf).strip()
|
|
734
|
+
if piece:
|
|
735
|
+
stmts.append(piece)
|
|
736
|
+
buf = []
|
|
737
|
+
i += 1
|
|
738
|
+
continue
|
|
739
|
+
else:
|
|
740
|
+
buf.append(ch)
|
|
741
|
+
i += 1
|
|
742
|
+
tail = "".join(buf).strip()
|
|
743
|
+
if tail:
|
|
744
|
+
stmts.append(tail)
|
|
745
|
+
return stmts
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
DOLLAR_BLOCK_RE = re.compile(r"\$([A-Za-z0-9_]*)\$([\s\S]*?)\$\1\$")
|
|
749
|
+
EXECUTE_STRING_RE = re.compile(r"\bEXECUTE\s+'((?:''|[^'])*)'", re.I)
|
|
750
|
+
CHR_CALL_RE = re.compile(r"\bCHR\s*\(\s*([0-9]{1,3})\s*\)", re.I)
|
|
751
|
+
HEX_DECODE_RE = re.compile(r"\bDECODE\s*\(\s*'([0-9A-Fa-f]+)'\s*,\s*'hex'\s*\)", re.I)
|
|
752
|
+
B64_DECODE_RE = re.compile(r"\bDECODE\s*\(\s*'([A-Za-z0-9+/=]+)'\s*,\s*'base64'\s*\)", re.I)
|
|
753
|
+
PARENTHESIZED_DML_DDL_RE = re.compile(
|
|
754
|
+
r"\(\s*((?:SELECT|INSERT|UPDATE|DELETE|DROP|TRUNCATE|ALTER)\b[^()]+)\)",
|
|
755
|
+
re.I,
|
|
756
|
+
)
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
def deobfuscate_dynamic_sql(sql_text: str) -> str:
|
|
760
|
+
out = sql_text
|
|
761
|
+
if CHR_CALL_RE.search(out):
|
|
762
|
+
out = CHR_CALL_RE.sub(lambda m: f"'{chr(int(m.group(1)))}'" if 32 <= int(m.group(1)) <= 126 else " ", out)
|
|
763
|
+
if HEX_DECODE_RE.search(out):
|
|
764
|
+
def _hex(m: re.Match) -> str:
|
|
765
|
+
try:
|
|
766
|
+
return f"'{bytes.fromhex(m.group(1)).decode('utf-8', errors='ignore')}'"
|
|
767
|
+
except Exception:
|
|
768
|
+
return m.group(0)
|
|
769
|
+
out = HEX_DECODE_RE.sub(_hex, out)
|
|
770
|
+
if B64_DECODE_RE.search(out):
|
|
771
|
+
def _b64(m: re.Match) -> str:
|
|
772
|
+
try:
|
|
773
|
+
return f"'{base64.b64decode(m.group(1)).decode('utf-8', errors='ignore')}'"
|
|
774
|
+
except Exception:
|
|
775
|
+
return m.group(0)
|
|
776
|
+
out = B64_DECODE_RE.sub(_b64, out)
|
|
777
|
+
prev = None
|
|
778
|
+
while prev != out and "||" in out:
|
|
779
|
+
prev = out
|
|
780
|
+
out = re.sub(r"'([^']*)'\s*\|\|\s*'([^']*)'", r"'\1\2'", out)
|
|
781
|
+
out = re.sub(r"\bCONVERT_FROM\s*\(\s*'([^']*)'\s*,\s*'[^']*'\s*\)", r"'\1'", out, flags=re.I)
|
|
782
|
+
return out
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
def extract_procedural_and_cte_clauses(sql_text: str) -> Tuple[List[str], bool]:
|
|
786
|
+
sql_text = deobfuscate_dynamic_sql(sql_text)
|
|
787
|
+
extracted: List[str] = []
|
|
788
|
+
upper = sql_text.upper()
|
|
789
|
+
is_proc_ddl = bool(
|
|
790
|
+
re.search(r"\bCREATE\s+(?:OR\s+REPLACE\s+)?(?:FUNCTION|PROCEDURE|TRIGGER)\b", upper)
|
|
791
|
+
or re.search(r"\bDO\s+(?:LANGUAGE\s+\w+\s+)?\$", upper)
|
|
792
|
+
)
|
|
793
|
+
for exec_m in EXECUTE_STRING_RE.finditer(DOLLAR_BLOCK_RE.sub(" ", sql_text)):
|
|
794
|
+
dyn_sql = deobfuscate_dynamic_sql(exec_m.group(1).replace("''", "'"))
|
|
795
|
+
if dyn_sql.strip():
|
|
796
|
+
extracted.append(dyn_sql.strip())
|
|
797
|
+
for m in DOLLAR_BLOCK_RE.finditer(sql_text):
|
|
798
|
+
body = deobfuscate_dynamic_sql(m.group(2))
|
|
799
|
+
for exec_m in EXECUTE_STRING_RE.finditer(body):
|
|
800
|
+
dyn_sql = deobfuscate_dynamic_sql(exec_m.group(1).replace("''", "'"))
|
|
801
|
+
if dyn_sql.strip():
|
|
802
|
+
extracted.append(dyn_sql.strip())
|
|
803
|
+
cleaned_body = re.sub(r"\b(?:DECLARE|BEGIN|END|EXCEPTION|RETURN\s+\w+)\b", " ; ", body, flags=re.I)
|
|
804
|
+
for part in cleaned_body.split(";"):
|
|
805
|
+
p = part.strip()
|
|
806
|
+
if re.match(r"^(?:SELECT|INSERT|UPDATE|DELETE|DROP|TRUNCATE|ALTER)\b", p, re.I):
|
|
807
|
+
extracted.append(p)
|
|
808
|
+
scan_text = DOLLAR_BLOCK_RE.sub(" ", sql_text)
|
|
809
|
+
for m in PARENTHESIZED_DML_DDL_RE.finditer(scan_text):
|
|
810
|
+
sub = m.group(1).strip()
|
|
811
|
+
if sub:
|
|
812
|
+
extracted.append(sub)
|
|
813
|
+
# Balanced-parenthesis scanner handles nested subqueries inside CTEs e.g. WITH x AS (DELETE ... WHERE id IN (SELECT 1))
|
|
814
|
+
paren_stack: List[int] = []
|
|
815
|
+
in_sq = False
|
|
816
|
+
idx_p = 0
|
|
817
|
+
while idx_p < len(scan_text):
|
|
818
|
+
ch_p = scan_text[idx_p]
|
|
819
|
+
if ch_p == "'":
|
|
820
|
+
if in_sq and idx_p + 1 < len(scan_text) and scan_text[idx_p + 1] == "'":
|
|
821
|
+
idx_p += 2
|
|
822
|
+
continue
|
|
823
|
+
in_sq = not in_sq
|
|
824
|
+
elif not in_sq and ch_p == "(":
|
|
825
|
+
paren_stack.append(idx_p)
|
|
826
|
+
elif not in_sq and ch_p == ")" and paren_stack:
|
|
827
|
+
s_idx = paren_stack.pop()
|
|
828
|
+
inner_p = scan_text[s_idx + 1 : idx_p].strip()
|
|
829
|
+
if re.match(r"^(?:SELECT|INSERT|UPDATE|DELETE|DROP|TRUNCATE|ALTER|COPY|CALL|CREATE|GRANT|REVOKE)\b", inner_p, re.I):
|
|
830
|
+
if inner_p not in extracted:
|
|
831
|
+
extracted.append(inner_p)
|
|
832
|
+
idx_p += 1
|
|
833
|
+
if re.match(r"^\s*WITH\b", scan_text, re.I):
|
|
834
|
+
depth = 0
|
|
835
|
+
last_close = -1
|
|
836
|
+
for idx, ch in enumerate(scan_text):
|
|
837
|
+
if ch == "(":
|
|
838
|
+
depth += 1
|
|
839
|
+
elif ch == ")":
|
|
840
|
+
depth = max(0, depth - 1)
|
|
841
|
+
if depth == 0:
|
|
842
|
+
tail_after = scan_text[idx + 1 :].lstrip()
|
|
843
|
+
if re.match(r"^(?:SELECT|INSERT|UPDATE|DELETE|DROP|TRUNCATE|ALTER)\b", tail_after, re.I):
|
|
844
|
+
last_close = idx
|
|
845
|
+
break
|
|
846
|
+
if last_close != -1:
|
|
847
|
+
main_tail = scan_text[last_close + 1 :].strip().rstrip(";").strip()
|
|
848
|
+
if main_tail:
|
|
849
|
+
extracted.append(main_tail)
|
|
850
|
+
return extracted, is_proc_ddl
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
def _sg_eval_const(expr: Any) -> Optional[float]:
|
|
854
|
+
if not HAS_SQLGLOT or expr is None:
|
|
855
|
+
return None
|
|
856
|
+
if isinstance(expr, sg_exp.Paren):
|
|
857
|
+
return _sg_eval_const(expr.this)
|
|
858
|
+
if isinstance(expr, sg_exp.Neg):
|
|
859
|
+
v = _sg_eval_const(expr.this)
|
|
860
|
+
return (-v) if v is not None else None
|
|
861
|
+
if isinstance(expr, sg_exp.Literal) and expr.is_number:
|
|
862
|
+
try:
|
|
863
|
+
return float(expr.this)
|
|
864
|
+
except Exception:
|
|
865
|
+
return None
|
|
866
|
+
if isinstance(expr, sg_exp.Add):
|
|
867
|
+
l, r = _sg_eval_const(expr.left), _sg_eval_const(expr.right)
|
|
868
|
+
return (l + r) if (l is not None and r is not None) else None
|
|
869
|
+
if isinstance(expr, sg_exp.Sub):
|
|
870
|
+
l, r = _sg_eval_const(expr.left), _sg_eval_const(expr.right)
|
|
871
|
+
return (l - r) if (l is not None and r is not None) else None
|
|
872
|
+
if isinstance(expr, sg_exp.Mul):
|
|
873
|
+
l, r = _sg_eval_const(expr.left), _sg_eval_const(expr.right)
|
|
874
|
+
return (l * r) if (l is not None and r is not None) else None
|
|
875
|
+
if isinstance(expr, sg_exp.Div):
|
|
876
|
+
l, r = _sg_eval_const(expr.left), _sg_eval_const(expr.right)
|
|
877
|
+
return (l / r) if (l is not None and r is not None and r != 0.0) else None
|
|
878
|
+
return None
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
def _sg_is_tautology_expr(expr: Any) -> bool:
|
|
882
|
+
if not HAS_SQLGLOT or expr is None:
|
|
883
|
+
return False
|
|
884
|
+
if isinstance(expr, sg_exp.Paren):
|
|
885
|
+
return _sg_is_tautology_expr(expr.this)
|
|
886
|
+
if isinstance(expr, sg_exp.Boolean):
|
|
887
|
+
return bool(expr.this)
|
|
888
|
+
if isinstance(expr, sg_exp.Not) and isinstance(expr.this, sg_exp.Boolean):
|
|
889
|
+
return not bool(expr.this.this)
|
|
890
|
+
if isinstance(expr, sg_exp.Or):
|
|
891
|
+
return _sg_is_tautology_expr(expr.left) or _sg_is_tautology_expr(expr.right)
|
|
892
|
+
if isinstance(expr, sg_exp.And):
|
|
893
|
+
return _sg_is_tautology_expr(expr.left) and _sg_is_tautology_expr(expr.right)
|
|
894
|
+
if isinstance(expr, sg_exp.EQ):
|
|
895
|
+
l_num, r_num = _sg_eval_const(expr.left), _sg_eval_const(expr.right)
|
|
896
|
+
if l_num is not None and r_num is not None:
|
|
897
|
+
return l_num == r_num
|
|
898
|
+
if isinstance(expr.left, sg_exp.Literal) and isinstance(expr.right, sg_exp.Literal):
|
|
899
|
+
return str(expr.left.this) == str(expr.right.this)
|
|
900
|
+
return False
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
def analyze_sql_ast(sql: str) -> Dict[str, Any]:
|
|
904
|
+
deobf = deobfuscate_dynamic_sql(sql)
|
|
905
|
+
inner_clauses, is_proc_ddl = extract_procedural_and_cte_clauses(deobf)
|
|
906
|
+
top_level_sql = DOLLAR_BLOCK_RE.sub(" ", deobf)
|
|
907
|
+
stmts = split_sql_statements(top_level_sql)
|
|
908
|
+
has_multi = len(stmts) > 1
|
|
909
|
+
# Always preserve top-level statements alongside unwrapped subqueries/CTEs
|
|
910
|
+
all_clauses = list(stmts)
|
|
911
|
+
for c in inner_clauses:
|
|
912
|
+
if c not in all_clauses:
|
|
913
|
+
all_clauses.append(c)
|
|
914
|
+
if not all_clauses:
|
|
915
|
+
return {
|
|
916
|
+
"statement_type": "ALTER" if is_proc_ddl else "QUERY",
|
|
917
|
+
"target_tables": [],
|
|
918
|
+
"has_where": False,
|
|
919
|
+
"is_unbounded": is_proc_ddl,
|
|
920
|
+
"multi_statement": has_multi,
|
|
921
|
+
"statement_count": 1 if is_proc_ddl else 0,
|
|
922
|
+
"ast_engine": "sqlglot-postgres" if HAS_SQLGLOT else "recursive-descent",
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
highest_op = "QUERY"
|
|
926
|
+
tables: List[str] = []
|
|
927
|
+
mutating_count = 0
|
|
928
|
+
all_mutating_have_where = True
|
|
929
|
+
any_unbounded = has_multi
|
|
930
|
+
unparsed_clauses: List[str] = []
|
|
931
|
+
|
|
932
|
+
if HAS_SQLGLOT:
|
|
933
|
+
for clause in all_clauses:
|
|
934
|
+
try:
|
|
935
|
+
trees = [t for t in sqlglot.parse(clause, read="postgres", error_level=sqlglot.ErrorLevel.IGNORE) if t is not None]
|
|
936
|
+
except Exception:
|
|
937
|
+
trees = []
|
|
938
|
+
if not trees:
|
|
939
|
+
unparsed_clauses.append(clause)
|
|
940
|
+
continue
|
|
941
|
+
for tree in trees:
|
|
942
|
+
for tbl in tree.find_all(sg_exp.Table):
|
|
943
|
+
tname = (tbl.name or "").lower()
|
|
944
|
+
if tname and tname not in tables:
|
|
945
|
+
tables.append(tname)
|
|
946
|
+
for subnode in tree.walk():
|
|
947
|
+
op: Optional[str] = None
|
|
948
|
+
if isinstance(subnode, sg_exp.Drop):
|
|
949
|
+
op = "DROP"
|
|
950
|
+
elif isinstance(subnode, sg_exp.TruncateTable):
|
|
951
|
+
op = "TRUNCATE"
|
|
952
|
+
elif isinstance(subnode, sg_exp.Delete):
|
|
953
|
+
op = "DELETE"
|
|
954
|
+
elif isinstance(subnode, (sg_exp.Alter, sg_exp.Create)):
|
|
955
|
+
op = "ALTER"
|
|
956
|
+
elif isinstance(subnode, (sg_exp.Update, sg_exp.Merge)):
|
|
957
|
+
op = "UPDATE"
|
|
958
|
+
elif isinstance(subnode, sg_exp.Insert):
|
|
959
|
+
op = "INSERT"
|
|
960
|
+
elif isinstance(subnode, sg_exp.Select):
|
|
961
|
+
op = "QUERY"
|
|
962
|
+
elif isinstance(subnode, sg_exp.Command):
|
|
963
|
+
cmd_name = (subnode.this or "").upper().strip()
|
|
964
|
+
if cmd_name in ("DROP", "TRUNCATE", "ALTER"):
|
|
965
|
+
op = cmd_name
|
|
966
|
+
elif cmd_name == "COPY":
|
|
967
|
+
op = "INSERT"
|
|
968
|
+
if op is None:
|
|
969
|
+
continue
|
|
970
|
+
if SEVERITY_ORDER.get(op, 0) > SEVERITY_ORDER.get(highest_op, 0):
|
|
971
|
+
highest_op = op
|
|
972
|
+
if op in ("UPDATE", "DELETE"):
|
|
973
|
+
mutating_count += 1
|
|
974
|
+
wn = subnode.args.get("where")
|
|
975
|
+
if wn is None or _sg_is_tautology_expr(getattr(wn, "this", wn)):
|
|
976
|
+
all_mutating_have_where = False
|
|
977
|
+
any_unbounded = True
|
|
978
|
+
elif op in ("DROP", "TRUNCATE", "ALTER"):
|
|
979
|
+
mutating_count += 1
|
|
980
|
+
all_mutating_have_where = False
|
|
981
|
+
any_unbounded = True
|
|
982
|
+
else:
|
|
983
|
+
unparsed_clauses = list(all_clauses)
|
|
984
|
+
|
|
985
|
+
for st in unparsed_clauses:
|
|
986
|
+
upper = st.upper().strip()
|
|
987
|
+
m_head = re.match(r"^([A-Z_]+)", upper)
|
|
988
|
+
first_kw = m_head.group(1) if m_head else "UNKNOWN"
|
|
989
|
+
if first_kw in ("SELECT", "EXPLAIN", "SHOW"):
|
|
990
|
+
op = "QUERY"
|
|
991
|
+
elif first_kw == "WITH":
|
|
992
|
+
op = "UPDATE" if re.search(r"\b(?:DELETE|DROP|TRUNCATE|ALTER|UPDATE|INSERT|COPY)\b", upper) else "QUERY"
|
|
993
|
+
elif first_kw == "INSERT":
|
|
994
|
+
op = "UPDATE" if re.search(r"\bON\s+CONFLICT\b[\s\S]*\bDO\s+UPDATE\b", upper) else "INSERT"
|
|
995
|
+
elif first_kw in ("UPDATE", "MERGE"):
|
|
996
|
+
op = "UPDATE"
|
|
997
|
+
elif first_kw in ("DELETE", "DROP", "TRUNCATE", "ALTER"):
|
|
998
|
+
op = first_kw
|
|
999
|
+
else:
|
|
1000
|
+
op = "ALTER"
|
|
1001
|
+
any_unbounded = True
|
|
1002
|
+
all_mutating_have_where = False
|
|
1003
|
+
if SEVERITY_ORDER.get(op, 5) >= SEVERITY_ORDER.get(highest_op, 0):
|
|
1004
|
+
highest_op = op
|
|
1005
|
+
|
|
1006
|
+
tbl = ""
|
|
1007
|
+
m = re.search(r"^(?:UPDATE|INSERT\s+INTO|DELETE\s+FROM|DELETE|DROP\s+TABLE|TRUNCATE\s+TABLE|ALTER\s+TABLE|COPY)\s+([A-Za-z0-9_.\"`]+)", upper)
|
|
1008
|
+
if m:
|
|
1009
|
+
tbl = m.group(1).strip('"`').lower()
|
|
1010
|
+
else:
|
|
1011
|
+
m2 = re.search(r"\bFROM\s+([A-Za-z0-9_.\"`]+)", upper)
|
|
1012
|
+
if m2:
|
|
1013
|
+
tbl = m2.group(1).strip('"`').lower()
|
|
1014
|
+
if tbl and tbl not in tables:
|
|
1015
|
+
tables.append(tbl)
|
|
1016
|
+
|
|
1017
|
+
has_where = bool(re.search(r"\bWHERE\b", upper))
|
|
1018
|
+
has_tautology = bool(
|
|
1019
|
+
re.search(
|
|
1020
|
+
r"\bWHERE\s+[\s\(]*(?:TRUE|1\s*=\s*1|2\s*=\s*2|2\s*>\s*1|1\s*<\s*2|0\s*=\s*0|'[^']+'\s*=\s*'[^']+')\b"
|
|
1021
|
+
r"|\bOR\s+[\s\(]*(?:TRUE|1\s*=\s*1|2\s*=\s*2|2\s*>\s*1)\b"
|
|
1022
|
+
r"|\bWHERE\s+([A-Z_][A-Z0-9_]*)\s*=\s*\1\b"
|
|
1023
|
+
r"|\bWHERE\s+[A-Z0-9_.]+\s+IS\s+NOT\s+NULL\s*(?:;|$)",
|
|
1024
|
+
upper,
|
|
1025
|
+
)
|
|
1026
|
+
)
|
|
1027
|
+
if op in ("UPDATE", "DELETE"):
|
|
1028
|
+
mutating_count += 1
|
|
1029
|
+
if not has_where or has_tautology:
|
|
1030
|
+
all_mutating_have_where = False
|
|
1031
|
+
any_unbounded = True
|
|
1032
|
+
elif op in ("DROP", "TRUNCATE", "ALTER"):
|
|
1033
|
+
mutating_count += 1
|
|
1034
|
+
all_mutating_have_where = False
|
|
1035
|
+
any_unbounded = True
|
|
1036
|
+
|
|
1037
|
+
if is_proc_ddl and SEVERITY_ORDER.get(highest_op, 0) < SEVERITY_ORDER.get("UPDATE", 3):
|
|
1038
|
+
highest_op = "ALTER"
|
|
1039
|
+
|
|
1040
|
+
if is_proc_ddl and highest_op in ("DROP", "TRUNCATE", "DELETE", "ALTER"):
|
|
1041
|
+
any_unbounded = True
|
|
1042
|
+
|
|
1043
|
+
return {
|
|
1044
|
+
"statement_type": highest_op,
|
|
1045
|
+
"target_tables": tables,
|
|
1046
|
+
"has_where": all_mutating_have_where if mutating_count > 0 and highest_op in ("UPDATE", "DELETE") else False,
|
|
1047
|
+
"is_unbounded": any_unbounded,
|
|
1048
|
+
"multi_statement": has_multi,
|
|
1049
|
+
"statement_count": len(all_clauses),
|
|
1050
|
+
"ast_engine": "sqlglot-postgres" if HAS_SQLGLOT else "recursive-descent",
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
def extract_sql_amount(sql: str, current_balance: float = 0.0) -> float:
|
|
1055
|
+
deobf = deobfuscate_dynamic_sql(strip_sql_comments(strip_zero_width(sql)))
|
|
1056
|
+
inner_clauses, _ = extract_procedural_and_cte_clauses(deobf)
|
|
1057
|
+
top_stmts = split_sql_statements(DOLLAR_BLOCK_RE.sub(" ", deobf))
|
|
1058
|
+
clauses = list(top_stmts)
|
|
1059
|
+
for cl in inner_clauses:
|
|
1060
|
+
if cl not in clauses:
|
|
1061
|
+
clauses.append(cl)
|
|
1062
|
+
|
|
1063
|
+
ast_total = 0.0
|
|
1064
|
+
zeroed_bal = False
|
|
1065
|
+
debit_cols = {"balance", "cash", "reserve", "available", "funds", "credit", "equity"}
|
|
1066
|
+
amt_cols = {"amount", "amt", "total", "total_amount", "payment_amount", "payout", "disbursement", "transfer_amount", "debit", "wire_amount", "refund_amount"}
|
|
1067
|
+
if HAS_SQLGLOT:
|
|
1068
|
+
def _extract_rhs_debit(expr: Any, col: str) -> Optional[float]:
|
|
1069
|
+
cv = _sg_eval_const(expr)
|
|
1070
|
+
if cv is not None:
|
|
1071
|
+
if cv <= 0:
|
|
1072
|
+
return float(current_balance) if current_balance > 0 else 1e9
|
|
1073
|
+
if 0 < current_balance and cv < current_balance:
|
|
1074
|
+
return float(current_balance) - cv
|
|
1075
|
+
return 0.0
|
|
1076
|
+
# Collect subtracted or negative-added constants on the same column: balance - 45000 - 45000 or balance + (-90000)
|
|
1077
|
+
def _sum_drains(node: Any) -> Tuple[bool, float]:
|
|
1078
|
+
if isinstance(node, sg_exp.Column):
|
|
1079
|
+
return node.name.lower().split(".")[-1] == col, 0.0
|
|
1080
|
+
if isinstance(node, sg_exp.Paren):
|
|
1081
|
+
return _sum_drains(node.this)
|
|
1082
|
+
if isinstance(node, sg_exp.Sub):
|
|
1083
|
+
has_c, l_drain = _sum_drains(node.left)
|
|
1084
|
+
r_val = _sg_eval_const(node.right)
|
|
1085
|
+
if has_c and r_val is not None:
|
|
1086
|
+
return True, l_drain + r_val
|
|
1087
|
+
return has_c, l_drain
|
|
1088
|
+
if isinstance(node, sg_exp.Add):
|
|
1089
|
+
has_c, l_drain = _sum_drains(node.left)
|
|
1090
|
+
r_val = _sg_eval_const(node.right)
|
|
1091
|
+
if has_c and r_val is not None:
|
|
1092
|
+
return True, l_drain - r_val
|
|
1093
|
+
has_r, r_drain = _sum_drains(node.right)
|
|
1094
|
+
l_val = _sg_eval_const(node.left)
|
|
1095
|
+
if has_r and l_val is not None:
|
|
1096
|
+
return True, r_drain - l_val
|
|
1097
|
+
return False, 0.0
|
|
1098
|
+
return False, 0.0
|
|
1099
|
+
has_col, drain = _sum_drains(expr)
|
|
1100
|
+
if has_col and drain > 0:
|
|
1101
|
+
return drain
|
|
1102
|
+
if isinstance(expr, (sg_exp.Case, sg_exp.Subquery)):
|
|
1103
|
+
return float(current_balance) if current_balance > 0 else 1e9
|
|
1104
|
+
return None
|
|
1105
|
+
|
|
1106
|
+
for cl in clauses:
|
|
1107
|
+
try:
|
|
1108
|
+
trees = sqlglot.parse(cl, read="postgres", error_level=sqlglot.ErrorLevel.IGNORE)
|
|
1109
|
+
except Exception:
|
|
1110
|
+
trees = []
|
|
1111
|
+
for tree in trees:
|
|
1112
|
+
if tree is None:
|
|
1113
|
+
continue
|
|
1114
|
+
for upd in tree.find_all(sg_exp.Update):
|
|
1115
|
+
for eq in upd.expressions:
|
|
1116
|
+
if not isinstance(eq, sg_exp.EQ) or eq.left is None or eq.right is None:
|
|
1117
|
+
continue
|
|
1118
|
+
col = (eq.left.name if hasattr(eq.left, "name") else eq.left.sql()).lower().split(".")[-1]
|
|
1119
|
+
if col in debit_cols:
|
|
1120
|
+
dr = _extract_rhs_debit(eq.right, col)
|
|
1121
|
+
if dr is not None and dr > 0:
|
|
1122
|
+
ast_total += dr
|
|
1123
|
+
elif dr is None:
|
|
1124
|
+
zeroed_bal = True
|
|
1125
|
+
elif col in amt_cols:
|
|
1126
|
+
v = _sg_eval_const(eq.right)
|
|
1127
|
+
if v is not None and v > 0:
|
|
1128
|
+
ast_total += v
|
|
1129
|
+
for ins in tree.find_all(sg_exp.Insert):
|
|
1130
|
+
col_nodes = ins.this.expressions if isinstance(ins.this, sg_exp.Schema) else []
|
|
1131
|
+
col_names = [c.name.lower().split(".")[-1] for c in col_nodes if hasattr(c, "name")]
|
|
1132
|
+
values_node = ins.args.get("expression")
|
|
1133
|
+
if isinstance(values_node, sg_exp.Values):
|
|
1134
|
+
for tup in values_node.expressions:
|
|
1135
|
+
exprs = list(getattr(tup, "expressions", []) or [])
|
|
1136
|
+
matched = False
|
|
1137
|
+
if col_names and len(col_names) == len(exprs):
|
|
1138
|
+
for cname, val_expr in zip(col_names, exprs):
|
|
1139
|
+
if cname in amt_cols:
|
|
1140
|
+
v = _sg_eval_const(val_expr)
|
|
1141
|
+
if v is not None and v > 0:
|
|
1142
|
+
ast_total += v
|
|
1143
|
+
matched = True
|
|
1144
|
+
if not matched:
|
|
1145
|
+
nums = [_sg_eval_const(e) for e in exprs]
|
|
1146
|
+
pos_nums = [x for x in nums if x is not None and x > 0]
|
|
1147
|
+
tbl_str = str(ins.this).lower()
|
|
1148
|
+
if pos_nums and any(k in tbl_str for k in ("transfer", "payout", "payment", "wire", "disburse", "ledger", "invoice", "journal", "move", "refund", "charge", "account", "vendor")):
|
|
1149
|
+
ast_total += max(pos_nums)
|
|
1150
|
+
|
|
1151
|
+
if zeroed_bal:
|
|
1152
|
+
return max(ast_total, float(current_balance) if current_balance > 0 else 1e9)
|
|
1153
|
+
|
|
1154
|
+
clean = deobf
|
|
1155
|
+
dollar_inner = " ; ".join(m.group(2) for m in DOLLAR_BLOCK_RE.finditer(clean))
|
|
1156
|
+
exec_inner = " ; ".join(m.group(1).replace("''", "'") for m in EXECUTE_STRING_RE.finditer(clean))
|
|
1157
|
+
without_dollars = DOLLAR_BLOCK_RE.sub(" ", clean)
|
|
1158
|
+
scrubbed_main = STRING_LIT_RE.sub(" '' ", without_dollars)
|
|
1159
|
+
scrubbed_dollar = STRING_LIT_RE.sub(" '' ", dollar_inner)
|
|
1160
|
+
scrubbed = f"{scrubbed_main} ; {scrubbed_dollar} ; {exec_inner}"
|
|
1161
|
+
|
|
1162
|
+
cands: List[float] = []
|
|
1163
|
+
for bal_m in re.finditer(
|
|
1164
|
+
r"\b(?:[A-Za-z_][A-Za-z0-9_]*\.)?(?:balance|cash|reserve|available|funds|credit|equity)\s*=\s*([^\n,;]+?)(?=\bWHERE\b|\bRETURNING\b|$)",
|
|
1165
|
+
scrubbed,
|
|
1166
|
+
re.I,
|
|
1167
|
+
):
|
|
1168
|
+
rhs = bal_m.group(1).strip()
|
|
1169
|
+
if re.match(r"^(?:[A-Za-z_][A-Za-z0-9_.]*)\s*[-+]", rhs, re.I):
|
|
1170
|
+
for sub_m in re.finditer(r"(?:-\s*|\+\s*\(\s*-\s*)([0-9]+(?:\.[0-9]+)?)", rhs):
|
|
1171
|
+
cands.append(float(sub_m.group(1)))
|
|
1172
|
+
elif re.match(r"^[0-9]+(?:\.[0-9]+)?$", rhs):
|
|
1173
|
+
lit_target = float(rhs)
|
|
1174
|
+
if lit_target <= 0:
|
|
1175
|
+
cands.append(float(current_balance) if current_balance > 0 else 1e9)
|
|
1176
|
+
elif current_balance > 0 and lit_target < current_balance:
|
|
1177
|
+
cands.append(float(current_balance) - lit_target)
|
|
1178
|
+
for m in re.finditer(
|
|
1179
|
+
r"\b(?:[A-Za-z_][A-Za-z0-9_]*\.)?(?:amount|amt|total|total_amount|payment_amount|payout|disbursement|transfer_amount|debit|wire_amount|refund_amount)\s*=\s*([0-9]+(?:\.[0-9]+)?)",
|
|
1180
|
+
scrubbed,
|
|
1181
|
+
re.I,
|
|
1182
|
+
):
|
|
1183
|
+
cands.append(float(m.group(1)))
|
|
1184
|
+
for ins_m in re.finditer(
|
|
1185
|
+
r"\bINSERT\s+INTO\s+[A-Za-z0-9_.]*(?:transfer|payout|payment|wire|disburse|ledger|invoice|journal|move|refund|charge|account|vendor)[A-Za-z0-9_.]*(?:\s*\(([^)]*)\))?\s*\bVALUES\s*\(([^)]+)\)",
|
|
1186
|
+
scrubbed,
|
|
1187
|
+
re.I,
|
|
1188
|
+
):
|
|
1189
|
+
cols = [c.strip().strip('"`').lower() for c in ins_m.group(1).split(",")] if ins_m.group(1) else []
|
|
1190
|
+
vals = [v.strip() for v in ins_m.group(2).split(",")]
|
|
1191
|
+
matched_named = False
|
|
1192
|
+
if cols and len(cols) == len(vals):
|
|
1193
|
+
for cn, vn in zip(cols, vals):
|
|
1194
|
+
if cn in amt_cols and re.match(r"^[0-9]+(?:\.[0-9]+)?$", vn):
|
|
1195
|
+
cands.append(float(vn))
|
|
1196
|
+
matched_named = True
|
|
1197
|
+
if not matched_named:
|
|
1198
|
+
nums = [float(x) for x in re.findall(r"(?<![A-Za-z_])([0-9]+(?:\.[0-9]+)?)", ins_m.group(2))]
|
|
1199
|
+
if nums:
|
|
1200
|
+
cands.append(max(nums))
|
|
1201
|
+
regex_total = sum(cands) if cands else 0.0
|
|
1202
|
+
return max(ast_total, regex_total)
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
def _format_safe_amount(val: float) -> str:
|
|
1206
|
+
rounded = round(max(0.0, float(val)), 2)
|
|
1207
|
+
if abs(rounded - round(rounded)) < 1e-9:
|
|
1208
|
+
return str(int(round(rounded)))
|
|
1209
|
+
return f"{rounded:.2f}".rstrip("0").rstrip(".")
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
def synthesize_patched_sql(sql: Optional[str], max_safe_amount: float) -> Optional[str]:
|
|
1213
|
+
if not sql or not isinstance(sql, str):
|
|
1214
|
+
return None
|
|
1215
|
+
if re.match(r"^\s*(?:DROP|TRUNCATE|ALTER)\b", sql.upper()):
|
|
1216
|
+
return None
|
|
1217
|
+
safe_str = _format_safe_amount(max_safe_amount)
|
|
1218
|
+
candidates: List[Tuple[float, int, int]] = []
|
|
1219
|
+
for pat in (
|
|
1220
|
+
r"\b(?:[A-Za-z_][A-Za-z0-9_]*\.)?(?:balance|cash|reserve|available|funds|credit|equity)\s*=\s*(?:[A-Za-z_][A-Za-z0-9_.]*\s*)?-\s*([0-9]+(?:\.[0-9]+)?)",
|
|
1221
|
+
r"\b(?:[A-Za-z_][A-Za-z0-9_]*\.)?(?:amount|amt|total|total_amount|payment_amount|payout|disbursement|transfer_amount|debit|wire_amount|refund_amount)\s*=\s*([0-9]+(?:\.[0-9]+)?)",
|
|
1222
|
+
):
|
|
1223
|
+
for m in re.finditer(pat, sql, re.I):
|
|
1224
|
+
candidates.append((float(m.group(1)), m.start(1), m.end(1)))
|
|
1225
|
+
if not candidates:
|
|
1226
|
+
for ins_m in re.finditer(r"\bVALUES\s*\(([^)]+)\)", sql, re.I):
|
|
1227
|
+
base = ins_m.start(1)
|
|
1228
|
+
for num_m in re.finditer(r"(?<![A-Za-z0-9_.])([0-9]+(?:\.[0-9]+)?)", ins_m.group(1)):
|
|
1229
|
+
if ins_m.group(1)[: num_m.start(1)].count("'") % 2 == 1:
|
|
1230
|
+
continue
|
|
1231
|
+
candidates.append((float(num_m.group(1)), base + num_m.start(1), base + num_m.end(1)))
|
|
1232
|
+
if not candidates:
|
|
1233
|
+
for num_m in re.finditer(r"(?<![A-Za-z0-9_.])([0-9]+(?:\.[0-9]+)?)", sql):
|
|
1234
|
+
prefix = sql[: num_m.start(1)]
|
|
1235
|
+
if prefix.count("'") % 2 == 1:
|
|
1236
|
+
continue
|
|
1237
|
+
if re.search(r"\b(?:id|account_id)\s*=\s*$", prefix, re.I):
|
|
1238
|
+
continue
|
|
1239
|
+
candidates.append((float(num_m.group(1)), num_m.start(1), num_m.end(1)))
|
|
1240
|
+
if not candidates:
|
|
1241
|
+
return None
|
|
1242
|
+
_, start, end = max(candidates, key=lambda item: item[0])
|
|
1243
|
+
return sql[:start] + safe_str + sql[end:]
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
def compute_counterfactual_patch(
|
|
1247
|
+
initial_balance: float,
|
|
1248
|
+
minimum_reserve: float,
|
|
1249
|
+
spending_limit: float,
|
|
1250
|
+
cfo_signed: bool,
|
|
1251
|
+
total_spend: float,
|
|
1252
|
+
sql: Optional[str] = None,
|
|
1253
|
+
) -> Dict[str, Any]:
|
|
1254
|
+
opt_ctx = z3.Context()
|
|
1255
|
+
opt = z3.Optimize(ctx=opt_ctx)
|
|
1256
|
+
opt.set("timeout", 5000)
|
|
1257
|
+
safe_amt = z3.Real("safe_amt", ctx=opt_ctx)
|
|
1258
|
+
opt.add(safe_amt >= 0)
|
|
1259
|
+
opt.add(z3.RealVal(str(initial_balance), ctx=opt_ctx) - safe_amt >= z3.RealVal(str(minimum_reserve), ctx=opt_ctx))
|
|
1260
|
+
if not cfo_signed:
|
|
1261
|
+
opt.add(safe_amt <= z3.RealVal(str(spending_limit), ctx=opt_ctx))
|
|
1262
|
+
opt.maximize(safe_amt)
|
|
1263
|
+
|
|
1264
|
+
max_safe_amount = 0.0
|
|
1265
|
+
if opt.check() == z3.sat:
|
|
1266
|
+
val = opt.model().eval(safe_amt, model_completion=True)
|
|
1267
|
+
try:
|
|
1268
|
+
if hasattr(val, "as_fraction"):
|
|
1269
|
+
max_safe_amount = float(val.as_fraction())
|
|
1270
|
+
else:
|
|
1271
|
+
max_safe_amount = float(val.as_decimal(6).rstrip("?"))
|
|
1272
|
+
except Exception:
|
|
1273
|
+
max_safe_amount = 0.0
|
|
1274
|
+
max_safe_amount = round(max(0.0, max_safe_amount), 2)
|
|
1275
|
+
reduction_delta = round(max(0.0, float(total_spend) - max_safe_amount), 2)
|
|
1276
|
+
patched_sql = synthesize_patched_sql(sql, max_safe_amount)
|
|
1277
|
+
return {
|
|
1278
|
+
"max_safe_amount": max_safe_amount,
|
|
1279
|
+
"reduction_delta": reduction_delta,
|
|
1280
|
+
"patched_sql": patched_sql,
|
|
1281
|
+
"solver": "z3.Optimize",
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
|
|
1285
|
+
def scrub_text_detailed(raw: str) -> Tuple[str, int, Dict[str, Any]]:
|
|
1286
|
+
text = strip_zero_width(raw)
|
|
1287
|
+
count = 0
|
|
1288
|
+
seen: Dict[str, str] = {}
|
|
1289
|
+
counters: Dict[str, int] = {}
|
|
1290
|
+
iban_mod97_valid = 0
|
|
1291
|
+
luhn_pan_valid = 0
|
|
1292
|
+
|
|
1293
|
+
def _sub(prefix: str, val: str) -> str:
|
|
1294
|
+
nonlocal count, iban_mod97_valid, luhn_pan_valid
|
|
1295
|
+
if prefix == "CARD" and not validate_luhn_pan(val):
|
|
1296
|
+
return val
|
|
1297
|
+
if prefix == "IBAN" and validate_iban_mod97(val):
|
|
1298
|
+
iban_mod97_valid += 1
|
|
1299
|
+
elif prefix == "CARD":
|
|
1300
|
+
luhn_pan_valid += 1
|
|
1301
|
+
if val in seen:
|
|
1302
|
+
return seen[val]
|
|
1303
|
+
counters[prefix] = counters.get(prefix, 0) + 1
|
|
1304
|
+
tok = f"<SYM_{prefix}_{counters[prefix]:02d}>"
|
|
1305
|
+
seen[val] = tok
|
|
1306
|
+
count += 1
|
|
1307
|
+
return tok
|
|
1308
|
+
|
|
1309
|
+
for prefix, regex in PII_RULES:
|
|
1310
|
+
text = regex.sub(lambda m, p=prefix: _sub(p, m.group(0)), text)
|
|
1311
|
+
return text, count, {
|
|
1312
|
+
"iban_mod97_valid_count": iban_mod97_valid,
|
|
1313
|
+
"luhn_pan_valid_count": luhn_pan_valid,
|
|
1314
|
+
"validators": ["ISO-7064-Mod-97-10", "Luhn-Mod-10"],
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
def scrub_text(raw: str) -> Tuple[str, int]:
|
|
1319
|
+
text, count, _ = scrub_text_detailed(raw)
|
|
1320
|
+
return text, count
|
|
1321
|
+
|
|
1322
|
+
|
|
1323
|
+
class StandaloneEnclaveSession:
|
|
1324
|
+
def __init__(self, state_db_path: str = ""):
|
|
1325
|
+
self.started_at = time.perf_counter()
|
|
1326
|
+
self.state_db_path = state_db_path or os.environ.get("APODICTIC_STATE_DB") or os.path.join(
|
|
1327
|
+
os.path.expanduser("~"), ".apodictic", "enclave_state.db"
|
|
1328
|
+
)
|
|
1329
|
+
os.makedirs(os.path.dirname(self.state_db_path), exist_ok=True)
|
|
1330
|
+
self.conn = sqlite3.connect(self.state_db_path, check_same_thread=False, timeout=5.0)
|
|
1331
|
+
self.conn.row_factory = sqlite3.Row
|
|
1332
|
+
self.conn.execute("PRAGMA journal_mode=WAL;")
|
|
1333
|
+
self.conn.execute("PRAGMA busy_timeout=5000;")
|
|
1334
|
+
self._init_schema()
|
|
1335
|
+
self.conn.execute(
|
|
1336
|
+
"INSERT OR IGNORE INTO ledger_accounts (account_id, balance, min_reserve, debits_pending, debits_posted, credits_posted) VALUES (?, ?, ?, 0.0, 0.0, ?)",
|
|
1337
|
+
("sdk_default", 250000.0, 50000.0, 250000.0),
|
|
1338
|
+
)
|
|
1339
|
+
self.conn.execute(
|
|
1340
|
+
"UPDATE ledger_accounts SET credits_posted = balance WHERE account_id = ? AND credits_posted = 0.0 AND debits_pending = 0.0 AND debits_posted = 0.0",
|
|
1341
|
+
("sdk_default",),
|
|
1342
|
+
)
|
|
1343
|
+
self.conn.commit()
|
|
1344
|
+
|
|
1345
|
+
def _init_schema(self) -> None:
|
|
1346
|
+
self.conn.executescript(
|
|
1347
|
+
"""
|
|
1348
|
+
CREATE TABLE IF NOT EXISTS velocity_events (
|
|
1349
|
+
account_id TEXT NOT NULL,
|
|
1350
|
+
amount REAL NOT NULL,
|
|
1351
|
+
ts REAL NOT NULL
|
|
1352
|
+
);
|
|
1353
|
+
CREATE TABLE IF NOT EXISTS worm_blocks (
|
|
1354
|
+
block_index INTEGER PRIMARY KEY,
|
|
1355
|
+
block_json TEXT NOT NULL
|
|
1356
|
+
);
|
|
1357
|
+
CREATE TABLE IF NOT EXISTS ledger_accounts (
|
|
1358
|
+
account_id TEXT PRIMARY KEY,
|
|
1359
|
+
balance REAL NOT NULL,
|
|
1360
|
+
min_reserve REAL NOT NULL DEFAULT 0.0,
|
|
1361
|
+
debits_pending REAL NOT NULL DEFAULT 0.0,
|
|
1362
|
+
debits_posted REAL NOT NULL DEFAULT 0.0,
|
|
1363
|
+
credits_posted REAL NOT NULL DEFAULT 0.0
|
|
1364
|
+
);
|
|
1365
|
+
CREATE TABLE IF NOT EXISTS wal_tuple_journal (
|
|
1366
|
+
tuple_id TEXT PRIMARY KEY,
|
|
1367
|
+
batch_id TEXT NOT NULL,
|
|
1368
|
+
lsn TEXT NOT NULL,
|
|
1369
|
+
table_name TEXT NOT NULL,
|
|
1370
|
+
before_image_json TEXT NOT NULL,
|
|
1371
|
+
after_image_json TEXT NOT NULL,
|
|
1372
|
+
restored INTEGER NOT NULL DEFAULT 0,
|
|
1373
|
+
created_at TEXT NOT NULL
|
|
1374
|
+
);
|
|
1375
|
+
CREATE TABLE IF NOT EXISTS rollback_batches (
|
|
1376
|
+
batch_id TEXT PRIMARY KEY,
|
|
1377
|
+
account_id TEXT NOT NULL,
|
|
1378
|
+
amount REAL NOT NULL,
|
|
1379
|
+
target_system TEXT NOT NULL,
|
|
1380
|
+
action TEXT NOT NULL,
|
|
1381
|
+
status TEXT NOT NULL,
|
|
1382
|
+
created_at REAL NOT NULL,
|
|
1383
|
+
rolled_back_at REAL
|
|
1384
|
+
);
|
|
1385
|
+
CREATE TABLE IF NOT EXISTS bitemporal_records (
|
|
1386
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1387
|
+
entity_id TEXT NOT NULL,
|
|
1388
|
+
data_json TEXT NOT NULL,
|
|
1389
|
+
valid_start INTEGER NOT NULL,
|
|
1390
|
+
valid_end INTEGER NOT NULL,
|
|
1391
|
+
system_start INTEGER NOT NULL,
|
|
1392
|
+
system_end INTEGER NOT NULL
|
|
1393
|
+
);
|
|
1394
|
+
"""
|
|
1395
|
+
)
|
|
1396
|
+
for col in ("debits_pending", "debits_posted", "credits_posted"):
|
|
1397
|
+
try:
|
|
1398
|
+
self.conn.execute(f"ALTER TABLE ledger_accounts ADD COLUMN {col} REAL NOT NULL DEFAULT 0.0")
|
|
1399
|
+
except sqlite3.OperationalError:
|
|
1400
|
+
pass
|
|
1401
|
+
self.conn.execute(
|
|
1402
|
+
"UPDATE ledger_accounts SET credits_posted = balance WHERE credits_posted = 0.0 AND debits_pending = 0.0 AND debits_posted = 0.0 AND balance > 0.0"
|
|
1403
|
+
)
|
|
1404
|
+
self.conn.commit()
|
|
1405
|
+
|
|
1406
|
+
def _get_double_entry_account(self, account_id: str) -> Dict[str, Any]:
|
|
1407
|
+
row = self.conn.execute("SELECT * FROM ledger_accounts WHERE account_id = ?", (account_id,)).fetchone()
|
|
1408
|
+
if not row:
|
|
1409
|
+
return {
|
|
1410
|
+
"account_id": account_id,
|
|
1411
|
+
"balance": 0.0,
|
|
1412
|
+
"min_reserve": 0.0,
|
|
1413
|
+
"debits_pending": 0.0,
|
|
1414
|
+
"debits_posted": 0.0,
|
|
1415
|
+
"credits_posted": 0.0,
|
|
1416
|
+
"available_balance": 0.0,
|
|
1417
|
+
}
|
|
1418
|
+
credits_posted = float(row["credits_posted"])
|
|
1419
|
+
debits_posted = float(row["debits_posted"])
|
|
1420
|
+
debits_pending = float(row["debits_pending"])
|
|
1421
|
+
return {
|
|
1422
|
+
"account_id": row["account_id"],
|
|
1423
|
+
"balance": round(float(row["balance"]), 4),
|
|
1424
|
+
"min_reserve": round(float(row["min_reserve"]), 4),
|
|
1425
|
+
"debits_pending": round(debits_pending, 4),
|
|
1426
|
+
"debits_posted": round(debits_posted, 4),
|
|
1427
|
+
"credits_posted": round(credits_posted, 4),
|
|
1428
|
+
"available_balance": round(credits_posted - debits_posted - debits_pending, 4),
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
def _all_blocks(self) -> List[Dict[str, Any]]:
|
|
1432
|
+
rows = self.conn.execute("SELECT block_json FROM worm_blocks ORDER BY block_index ASC").fetchall()
|
|
1433
|
+
return [json.loads(r["block_json"]) for r in rows]
|
|
1434
|
+
|
|
1435
|
+
def _append_block(self, proof_id: str, action: str, verdict: str, target_system: str, zk_digest: str, metadata: Dict[str, Any]) -> Dict[str, Any]:
|
|
1436
|
+
last_row = self.conn.execute("SELECT block_index, block_json FROM worm_blocks ORDER BY block_index DESC LIMIT 1").fetchone()
|
|
1437
|
+
prev_blk = json.loads(last_row["block_json"]) if last_row else None
|
|
1438
|
+
prev_hash = prev_blk["block_hash"] if prev_blk else GENESIS_HASH
|
|
1439
|
+
idx = int(prev_blk["block_index"]) + 1 if prev_blk else 1
|
|
1440
|
+
payload: Dict[str, Any] = {
|
|
1441
|
+
"block_index": idx,
|
|
1442
|
+
"previous_hash": prev_hash,
|
|
1443
|
+
"timestamp_ns": time.time_ns(),
|
|
1444
|
+
"proof_id": proof_id,
|
|
1445
|
+
"action": action,
|
|
1446
|
+
"verdict": verdict,
|
|
1447
|
+
"target_system": target_system,
|
|
1448
|
+
"zk_digest": zk_digest,
|
|
1449
|
+
"metadata": metadata,
|
|
1450
|
+
}
|
|
1451
|
+
serialized = json.dumps(payload, sort_keys=True)
|
|
1452
|
+
block_hash = hashlib.sha256(f"{prev_hash}:{serialized}".encode("utf-8")).hexdigest()
|
|
1453
|
+
payload["block_hash"] = block_hash
|
|
1454
|
+
rows = self.conn.execute("SELECT block_json FROM worm_blocks ORDER BY block_index ASC").fetchall()
|
|
1455
|
+
leaves = [rfc6962_leaf_hash(str(json.loads(r["block_json"])["block_hash"]).encode("utf-8")) for r in rows]
|
|
1456
|
+
new_leaf = rfc6962_leaf_hash(block_hash.encode("utf-8"))
|
|
1457
|
+
leaves.append(new_leaf)
|
|
1458
|
+
payload["rfc6962_leaf_hash"] = new_leaf.hex()
|
|
1459
|
+
payload["rfc6962_tree_root"] = rfc6962_tree_hash(leaves).hex()
|
|
1460
|
+
self.conn.execute(
|
|
1461
|
+
"INSERT INTO worm_blocks (block_index, block_json) VALUES (?, ?)",
|
|
1462
|
+
(idx, json.dumps(payload, sort_keys=True)),
|
|
1463
|
+
)
|
|
1464
|
+
self.conn.commit()
|
|
1465
|
+
return payload
|
|
1466
|
+
|
|
1467
|
+
def generate_rfc6962_inclusion_proof(self, block_index: Optional[int] = None) -> Dict[str, Any]:
|
|
1468
|
+
blocks = self._all_blocks()
|
|
1469
|
+
if not blocks:
|
|
1470
|
+
return {
|
|
1471
|
+
"spec": "RFC-6962-Certificate-Transparency-v1",
|
|
1472
|
+
"block_index": 0,
|
|
1473
|
+
"leaf_index": 0,
|
|
1474
|
+
"tree_size": 0,
|
|
1475
|
+
"leaf_hash": None,
|
|
1476
|
+
"root_hash": hashlib.sha256(b"").hexdigest(),
|
|
1477
|
+
"audit_path": [],
|
|
1478
|
+
"inclusion_verified": False,
|
|
1479
|
+
}
|
|
1480
|
+
target_idx = int(block_index) if block_index else len(blocks)
|
|
1481
|
+
idx = target_idx - 1
|
|
1482
|
+
if idx < 0 or idx >= len(blocks):
|
|
1483
|
+
raise ValueError(f"Block index {target_idx} out of bounds (1..{len(blocks)})")
|
|
1484
|
+
leaves = [rfc6962_leaf_hash(str(b["block_hash"]).encode("utf-8")) for b in blocks]
|
|
1485
|
+
leaf = leaves[idx]
|
|
1486
|
+
path = rfc6962_audit_path(idx, leaves)
|
|
1487
|
+
root = rfc6962_tree_hash(leaves)
|
|
1488
|
+
verified = rfc6962_verify_inclusion(leaf, idx, len(leaves), path, root)
|
|
1489
|
+
return {
|
|
1490
|
+
"spec": "RFC-6962-Certificate-Transparency-v1",
|
|
1491
|
+
"block_index": target_idx,
|
|
1492
|
+
"leaf_index": idx,
|
|
1493
|
+
"tree_size": len(leaves),
|
|
1494
|
+
"leaf_hash": leaf.hex(),
|
|
1495
|
+
"root_hash": root.hex(),
|
|
1496
|
+
"audit_path": [p.hex() for p in path],
|
|
1497
|
+
"inclusion_verified": verified,
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
def _verify_chain(self) -> Tuple[bool, int, str]:
|
|
1501
|
+
blocks = self._all_blocks()
|
|
1502
|
+
prev = GENESIS_HASH
|
|
1503
|
+
for blk in blocks:
|
|
1504
|
+
if blk.get("previous_hash") != prev:
|
|
1505
|
+
return False, len(blocks), blocks[-1].get("block_hash", GENESIS_HASH)
|
|
1506
|
+
copy = dict(blk)
|
|
1507
|
+
actual = copy.pop("block_hash", "")
|
|
1508
|
+
copy.pop("rfc6962_leaf_hash", None)
|
|
1509
|
+
copy.pop("rfc6962_tree_root", None)
|
|
1510
|
+
serialized = json.dumps(copy, sort_keys=True)
|
|
1511
|
+
recalc = hashlib.sha256(f"{prev}:{serialized}".encode("utf-8")).hexdigest()
|
|
1512
|
+
if recalc != actual:
|
|
1513
|
+
return False, len(blocks), blocks[-1].get("block_hash", GENESIS_HASH)
|
|
1514
|
+
prev = actual
|
|
1515
|
+
return True, len(blocks), prev
|
|
1516
|
+
|
|
1517
|
+
def handle(self, method: str, params: Dict[str, Any]) -> Dict[str, Any]:
|
|
1518
|
+
if method == "health":
|
|
1519
|
+
valid, cnt, tip = self._verify_chain()
|
|
1520
|
+
accts = self.conn.execute("SELECT COUNT(DISTINCT account_id) FROM velocity_events").fetchone()[0]
|
|
1521
|
+
bt_cnt = self.conn.execute("SELECT COUNT(*) FROM bitemporal_records").fetchone()[0]
|
|
1522
|
+
return {
|
|
1523
|
+
"status": "healthy",
|
|
1524
|
+
"mode": "standalone-enclave",
|
|
1525
|
+
"z3_version": z3.get_version_string(),
|
|
1526
|
+
"uptime_ms": round((time.perf_counter() - self.started_at) * 1000.0, 2),
|
|
1527
|
+
"worm_blocks": cnt,
|
|
1528
|
+
"worm_integrity_valid": valid,
|
|
1529
|
+
"tip_hash": tip,
|
|
1530
|
+
"velocity_accounts": accts,
|
|
1531
|
+
"bitemporal_records": bt_cnt,
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
if method == "verify_mutation":
|
|
1535
|
+
t0 = time.perf_counter()
|
|
1536
|
+
raw_sql = str(params.get("sql", ""))
|
|
1537
|
+
acct = str(params.get("accountId", "sdk_default"))
|
|
1538
|
+
has_explicit_bal = bool(params.get("hasExplicitCurrentBalance", "currentBalance" in params and params.get("currentBalance") is not None))
|
|
1539
|
+
existing_acct_row = self.conn.execute(
|
|
1540
|
+
"SELECT balance, min_reserve FROM ledger_accounts WHERE account_id = ?",
|
|
1541
|
+
(acct,),
|
|
1542
|
+
).fetchone()
|
|
1543
|
+
use_persisted_bal = existing_acct_row is not None and not has_explicit_bal and not bool(params.get("stateless", False))
|
|
1544
|
+
curr_bal = float(existing_acct_row["balance"]) if use_persisted_bal else float(params.get("currentBalance", 250000.0))
|
|
1545
|
+
min_res = float(existing_acct_row["min_reserve"]) if use_persisted_bal else float(params.get("minReserve", 50000.0))
|
|
1546
|
+
spend_cap = float(params.get("maxAutonomousLimit", 50000.0))
|
|
1547
|
+
cfo_signed = bool(params.get("cfoSigned", False))
|
|
1548
|
+
vat_rate = float(params.get("statutoryVat", 0.14))
|
|
1549
|
+
exp_vat = float(params.get("expectedVat", 0.14))
|
|
1550
|
+
extracted_amt = extract_sql_amount(raw_sql, current_balance=curr_bal)
|
|
1551
|
+
amt_raw = params.get("amount")
|
|
1552
|
+
caller_amt = float(amt_raw) if amt_raw is not None else 0.0
|
|
1553
|
+
if caller_amt < 0 or caller_amt != caller_amt or math.isinf(caller_amt):
|
|
1554
|
+
raise ValueError("Invalid negative, NaN, or infinite amount")
|
|
1555
|
+
amt_val = max(caller_amt, extracted_amt)
|
|
1556
|
+
if amt_val < 0 or amt_val != amt_val:
|
|
1557
|
+
raise ValueError("Invalid negative or NaN amount")
|
|
1558
|
+
|
|
1559
|
+
sanitized_sql, redactions, dlp_checksums = scrub_text_detailed(raw_sql)
|
|
1560
|
+
dlp_ms = (time.perf_counter() - t0) * 1000.0
|
|
1561
|
+
|
|
1562
|
+
ast_t0 = time.perf_counter()
|
|
1563
|
+
ast = analyze_sql_ast(raw_sql)
|
|
1564
|
+
ast_ms = (time.perf_counter() - ast_t0) * 1000.0
|
|
1565
|
+
|
|
1566
|
+
has_multi = bool(ast["multi_statement"])
|
|
1567
|
+
is_destructive = has_multi or ast["is_unbounded"] or ast["statement_type"] in ("DELETE", "DROP", "TRUNCATE", "ALTER")
|
|
1568
|
+
rev_armed = False if has_multi else (not is_destructive)
|
|
1569
|
+
|
|
1570
|
+
cutoff = time.time() - 86400.0
|
|
1571
|
+
prior_sum = float(
|
|
1572
|
+
self.conn.execute(
|
|
1573
|
+
"SELECT COALESCE(SUM(amount), 0.0) FROM velocity_events WHERE account_id = ? AND ts >= ?",
|
|
1574
|
+
(acct, cutoff),
|
|
1575
|
+
).fetchone()[0]
|
|
1576
|
+
)
|
|
1577
|
+
effective_amt = amt_val + prior_sum
|
|
1578
|
+
|
|
1579
|
+
z0 = time.perf_counter()
|
|
1580
|
+
ctx = z3.Context()
|
|
1581
|
+
s = z3.Solver(ctx=ctx)
|
|
1582
|
+
s.set("timeout", 5000)
|
|
1583
|
+
|
|
1584
|
+
z_bal = z3.Real("initial_balance", ctx=ctx)
|
|
1585
|
+
z_min = z3.Real("min_reserve", ctx=ctx)
|
|
1586
|
+
z_cap = z3.Real("spending_limit", ctx=ctx)
|
|
1587
|
+
z_cfo = z3.Bool("cfo_signed", ctx=ctx)
|
|
1588
|
+
z_dest = z3.Bool("is_destructive", ctx=ctx)
|
|
1589
|
+
z_arm = z3.Bool("reversal_armed", ctx=ctx)
|
|
1590
|
+
z_vat = z3.Real("vat_rate", ctx=ctx)
|
|
1591
|
+
z_evat = z3.Real("expected_vat", ctx=ctx)
|
|
1592
|
+
z_amt = z3.Real("tx_effective_amt", ctx=ctx)
|
|
1593
|
+
|
|
1594
|
+
s.add(z_bal == z3.RealVal(str(curr_bal), ctx=ctx))
|
|
1595
|
+
s.add(z_min == z3.RealVal(str(min_res), ctx=ctx))
|
|
1596
|
+
s.add(z_cap == z3.RealVal(str(spend_cap), ctx=ctx))
|
|
1597
|
+
s.add(z_cfo == z3.BoolVal(cfo_signed, ctx=ctx))
|
|
1598
|
+
s.add(z_dest == z3.BoolVal(is_destructive, ctx=ctx))
|
|
1599
|
+
s.add(z_arm == z3.BoolVal(rev_armed, ctx=ctx))
|
|
1600
|
+
s.add(z_vat == z3.RealVal(str(vat_rate), ctx=ctx))
|
|
1601
|
+
s.add(z_evat == z3.RealVal(str(exp_vat), ctx=ctx))
|
|
1602
|
+
s.add(z_amt == z3.RealVal(str(effective_amt), ctx=ctx))
|
|
1603
|
+
|
|
1604
|
+
s.assert_and_track(z3.Or(z3.Not(z_dest), z_arm), "INV_REVERSIBILITY_GUARD")
|
|
1605
|
+
s.assert_and_track(z_vat == z_evat, "INV_STATUTORY_TAX_PARITY")
|
|
1606
|
+
s.assert_and_track(z_amt >= 0, "INV_NON_NEGATIVE")
|
|
1607
|
+
s.assert_and_track(z3.Or(z_amt <= z_cap, z_cfo), "INV_SPEND_CAP")
|
|
1608
|
+
s.assert_and_track(z_bal - z_amt >= z_min, "INV_SOLVENCY_FLOOR")
|
|
1609
|
+
|
|
1610
|
+
debit_total_raw = params.get("debitTotal")
|
|
1611
|
+
credit_total_raw = params.get("creditTotal")
|
|
1612
|
+
double_entry_detail: Optional[str] = None
|
|
1613
|
+
if debit_total_raw is not None and credit_total_raw is not None:
|
|
1614
|
+
debit_total = float(debit_total_raw)
|
|
1615
|
+
credit_total = float(credit_total_raw)
|
|
1616
|
+
z_debit = z3.Real("debit_total", ctx=ctx)
|
|
1617
|
+
z_credit = z3.Real("credit_total", ctx=ctx)
|
|
1618
|
+
z_tol = z3.RealVal("0.005", ctx=ctx)
|
|
1619
|
+
s.add(z_debit == z3.RealVal(str(debit_total), ctx=ctx))
|
|
1620
|
+
s.add(z_credit == z3.RealVal(str(credit_total), ctx=ctx))
|
|
1621
|
+
s.assert_and_track(
|
|
1622
|
+
z3.And(z_debit - z_credit <= z_tol, z_credit - z_debit <= z_tol),
|
|
1623
|
+
"INV_DOUBLE_ENTRY_BALANCE",
|
|
1624
|
+
)
|
|
1625
|
+
if abs(debit_total - credit_total) > 0.005:
|
|
1626
|
+
double_entry_detail = (
|
|
1627
|
+
f"INV_DOUBLE_ENTRY_BALANCE: sum(debit)={debit_total:.2f} != "
|
|
1628
|
+
f"sum(credit)={credit_total:.2f} (drift=${abs(debit_total - credit_total):.2f})"
|
|
1629
|
+
)
|
|
1630
|
+
|
|
1631
|
+
chk = s.check()
|
|
1632
|
+
z3_ms = (time.perf_counter() - z0) * 1000.0
|
|
1633
|
+
verified = chk == z3.sat
|
|
1634
|
+
verdict = "SAT" if verified else "UNSAT"
|
|
1635
|
+
core = [] if verified else [str(c) for c in s.unsat_core()]
|
|
1636
|
+
if has_multi and "MULTI_STATEMENT_SQL_BLOCKED" not in core:
|
|
1637
|
+
core.insert(0, "MULTI_STATEMENT_SQL_BLOCKED")
|
|
1638
|
+
violation_reason = (
|
|
1639
|
+
double_entry_detail
|
|
1640
|
+
if (not verified and "INV_DOUBLE_ENTRY_BALANCE" in core and double_entry_detail)
|
|
1641
|
+
else (core[0] if core else None)
|
|
1642
|
+
)
|
|
1643
|
+
counterfactual_patch = (
|
|
1644
|
+
None
|
|
1645
|
+
if verified
|
|
1646
|
+
else compute_counterfactual_patch(
|
|
1647
|
+
initial_balance=curr_bal,
|
|
1648
|
+
minimum_reserve=min_res,
|
|
1649
|
+
spending_limit=spend_cap,
|
|
1650
|
+
cfo_signed=cfo_signed,
|
|
1651
|
+
total_spend=effective_amt,
|
|
1652
|
+
sql=raw_sql,
|
|
1653
|
+
)
|
|
1654
|
+
)
|
|
1655
|
+
|
|
1656
|
+
proof_id = f"prf_{int(time.time()*1000)}_{uuid.uuid4().hex[:6]}"
|
|
1657
|
+
digest = hashlib.sha256(f"{proof_id}:{verdict}:{curr_bal}:{effective_amt}:{sanitized_sql}".encode()).hexdigest()
|
|
1658
|
+
|
|
1659
|
+
if verified and amt_val > 0:
|
|
1660
|
+
self.conn.execute(
|
|
1661
|
+
"INSERT INTO velocity_events (account_id, amount, ts) VALUES (?, ?, ?)",
|
|
1662
|
+
(acct, amt_val, time.time()),
|
|
1663
|
+
)
|
|
1664
|
+
|
|
1665
|
+
blk = self._append_block(
|
|
1666
|
+
proof_id=proof_id,
|
|
1667
|
+
action=ast["statement_type"],
|
|
1668
|
+
verdict=verdict,
|
|
1669
|
+
target_system=str(params.get("targetSystem", "POSTGRESQL")).upper(),
|
|
1670
|
+
zk_digest=digest,
|
|
1671
|
+
metadata={"tables": ast["target_tables"], "unbounded": ast["is_unbounded"], "multi_statement": has_multi},
|
|
1672
|
+
)
|
|
1673
|
+
|
|
1674
|
+
target_sys_raw = str(params.get("targetSystem", "postgresql")).lower()
|
|
1675
|
+
is_ext_escrow = any(k in target_sys_raw for k in ("stripe", "wire", "iso20022", "netsuite")) or any(
|
|
1676
|
+
k in raw_sql.upper() for k in ("WIRE", "PAYOUT", "STRIPE", "ISO20022")
|
|
1677
|
+
)
|
|
1678
|
+
escrow_status = ("ESCROW_HELD" if is_ext_escrow else "SAVEPOINT_STAGED") if verified else "VETOED_PRE_COMMIT"
|
|
1679
|
+
batch_id = None
|
|
1680
|
+
wal_before_image: Optional[Dict[str, Any]] = None
|
|
1681
|
+
if verified:
|
|
1682
|
+
self.conn.execute(
|
|
1683
|
+
"INSERT OR IGNORE INTO ledger_accounts (account_id, balance, min_reserve, debits_pending, debits_posted, credits_posted) VALUES (?, ?, ?, 0.0, 0.0, ?)",
|
|
1684
|
+
(acct, curr_bal, min_res, curr_bal),
|
|
1685
|
+
)
|
|
1686
|
+
wal_before_image = self._get_double_entry_account(acct)
|
|
1687
|
+
if is_ext_escrow:
|
|
1688
|
+
self.conn.execute(
|
|
1689
|
+
"UPDATE ledger_accounts SET balance = balance - ?, debits_pending = debits_pending + ? WHERE account_id = ?",
|
|
1690
|
+
(amt_val, amt_val, acct),
|
|
1691
|
+
)
|
|
1692
|
+
else:
|
|
1693
|
+
self.conn.execute(
|
|
1694
|
+
"UPDATE ledger_accounts SET balance = balance - ?, debits_posted = debits_posted + ? WHERE account_id = ?",
|
|
1695
|
+
(amt_val, amt_val, acct),
|
|
1696
|
+
)
|
|
1697
|
+
wal_after_image = self._get_double_entry_account(acct)
|
|
1698
|
+
batch_id = f"batch_{int(time.time()*1000)}_{blk['block_index']}"
|
|
1699
|
+
lsn = f"0/{int(time.time_ns()) & 0xFFFFFFFF:08X}"
|
|
1700
|
+
self.conn.execute(
|
|
1701
|
+
"INSERT INTO wal_tuple_journal (tuple_id, batch_id, lsn, table_name, before_image_json, after_image_json, restored, created_at) VALUES (?, ?, ?, ?, ?, ?, 0, ?)",
|
|
1702
|
+
(
|
|
1703
|
+
f"wal_{uuid.uuid4().hex[:12]}",
|
|
1704
|
+
batch_id,
|
|
1705
|
+
lsn,
|
|
1706
|
+
(ast["target_tables"][0] if ast["target_tables"] else "ledger_accounts"),
|
|
1707
|
+
json.dumps(wal_before_image),
|
|
1708
|
+
json.dumps(wal_after_image),
|
|
1709
|
+
time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
|
1710
|
+
),
|
|
1711
|
+
)
|
|
1712
|
+
self.conn.execute(
|
|
1713
|
+
"INSERT INTO rollback_batches (batch_id, account_id, amount, target_system, action, status, created_at) VALUES (?, ?, ?, ?, ?, 'STAGED', ?)",
|
|
1714
|
+
(batch_id, acct, amt_val, target_sys_raw, ast["statement_type"], time.time()),
|
|
1715
|
+
)
|
|
1716
|
+
self.conn.commit()
|
|
1717
|
+
double_entry_acct = self._get_double_entry_account(acct) if verified else None
|
|
1718
|
+
|
|
1719
|
+
total_ms = (time.perf_counter() - t0) * 1000.0
|
|
1720
|
+
return {
|
|
1721
|
+
"verdict": verdict,
|
|
1722
|
+
"verified": verified,
|
|
1723
|
+
"proof_id": proof_id,
|
|
1724
|
+
"proof_digest": digest,
|
|
1725
|
+
"unsat_core": core,
|
|
1726
|
+
"violation_reason": violation_reason,
|
|
1727
|
+
"counterfactual_patch": counterfactual_patch,
|
|
1728
|
+
"block_index": blk["block_index"],
|
|
1729
|
+
"block_hash": blk["block_hash"],
|
|
1730
|
+
"rfc6962_leaf_hash": blk.get("rfc6962_leaf_hash"),
|
|
1731
|
+
"rfc6962_tree_root": blk.get("rfc6962_tree_root"),
|
|
1732
|
+
"rollback_batch_id": batch_id,
|
|
1733
|
+
"escrow_status": escrow_status,
|
|
1734
|
+
"double_entry_account": double_entry_acct,
|
|
1735
|
+
"wal_before_image": wal_before_image,
|
|
1736
|
+
"dlp_checksums": dlp_checksums,
|
|
1737
|
+
"timings": {
|
|
1738
|
+
"dlp_latency_ms": round(dlp_ms, 3),
|
|
1739
|
+
"ast_latency_ms": round(ast_ms, 3),
|
|
1740
|
+
"z3_solve_ms": round(z3_ms, 3),
|
|
1741
|
+
"total_proxy_latency_ms": round(total_ms, 3),
|
|
1742
|
+
},
|
|
1743
|
+
"redactions": redactions,
|
|
1744
|
+
"sanitized_sql": sanitized_sql,
|
|
1745
|
+
"ast_analysis": ast,
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
if method == "rollback":
|
|
1749
|
+
t0 = time.perf_counter()
|
|
1750
|
+
batch_id = str(params.get("batchId", ""))
|
|
1751
|
+
if not batch_id:
|
|
1752
|
+
raise ValueError("batchId is required")
|
|
1753
|
+
row = self.conn.execute("SELECT * FROM rollback_batches WHERE batch_id = ?", (batch_id,)).fetchone()
|
|
1754
|
+
compensated = []
|
|
1755
|
+
escrow_res = "NOOP"
|
|
1756
|
+
restored_tuple: Optional[Dict[str, Any]] = None
|
|
1757
|
+
double_entry_acct: Optional[Dict[str, Any]] = None
|
|
1758
|
+
if row and row["status"] == "STAGED":
|
|
1759
|
+
ts_low = str(row["target_system"] or "").lower()
|
|
1760
|
+
is_esc = any(k in ts_low for k in ("stripe", "wire", "iso20022", "netsuite"))
|
|
1761
|
+
if is_esc:
|
|
1762
|
+
self.conn.execute(
|
|
1763
|
+
"UPDATE ledger_accounts SET balance = balance + ?, debits_pending = MAX(0.0, debits_pending - ?) WHERE account_id = ?",
|
|
1764
|
+
(row["amount"], row["amount"], row["account_id"]),
|
|
1765
|
+
)
|
|
1766
|
+
else:
|
|
1767
|
+
self.conn.execute(
|
|
1768
|
+
"UPDATE ledger_accounts SET balance = balance + ?, debits_posted = MAX(0.0, debits_posted - ?) WHERE account_id = ?",
|
|
1769
|
+
(row["amount"], row["amount"], row["account_id"]),
|
|
1770
|
+
)
|
|
1771
|
+
self.conn.execute(
|
|
1772
|
+
"UPDATE rollback_batches SET status = 'ROLLED_BACK', rolled_back_at = ? WHERE batch_id = ?",
|
|
1773
|
+
(time.time(), batch_id),
|
|
1774
|
+
)
|
|
1775
|
+
wal_row = self.conn.execute(
|
|
1776
|
+
"SELECT before_image_json, lsn, table_name FROM wal_tuple_journal WHERE batch_id = ?",
|
|
1777
|
+
(batch_id,),
|
|
1778
|
+
).fetchone()
|
|
1779
|
+
if wal_row:
|
|
1780
|
+
self.conn.execute("UPDATE wal_tuple_journal SET restored = 1 WHERE batch_id = ?", (batch_id,))
|
|
1781
|
+
restored_tuple = {
|
|
1782
|
+
"lsn": wal_row["lsn"],
|
|
1783
|
+
"table_name": wal_row["table_name"],
|
|
1784
|
+
"restored_before_image": json.loads(wal_row["before_image_json"]),
|
|
1785
|
+
}
|
|
1786
|
+
self.conn.commit()
|
|
1787
|
+
double_entry_acct = self._get_double_entry_account(row["account_id"])
|
|
1788
|
+
escrow_res = "ESCROW_CANCELLED_PRE_DISPATCH" if is_esc else "SAVEPOINT_ROLLED_BACK"
|
|
1789
|
+
compensated.append({
|
|
1790
|
+
"batch_id": batch_id,
|
|
1791
|
+
"system": row["target_system"],
|
|
1792
|
+
"amount_restored": row["amount"],
|
|
1793
|
+
"status": "COMPENSATED",
|
|
1794
|
+
"escrow_status": escrow_res,
|
|
1795
|
+
"pgoutput_restored_tuple": restored_tuple,
|
|
1796
|
+
"double_entry_account": double_entry_acct,
|
|
1797
|
+
})
|
|
1798
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000.0, 3)
|
|
1799
|
+
ok = len(compensated) > 0
|
|
1800
|
+
valid, _, tip = self._verify_chain()
|
|
1801
|
+
blk = self._append_block(
|
|
1802
|
+
proof_id=f"rb_{batch_id}",
|
|
1803
|
+
action="ATOMIC_ROLLBACK",
|
|
1804
|
+
verdict="REVERTED" if ok else "NOOP",
|
|
1805
|
+
target_system="POSTGRESQL",
|
|
1806
|
+
zk_digest=tip,
|
|
1807
|
+
metadata={"batch_id": batch_id, "chain_valid": valid, "escrow_status": escrow_res},
|
|
1808
|
+
)
|
|
1809
|
+
return {
|
|
1810
|
+
"batch_id": batch_id,
|
|
1811
|
+
"success": ok,
|
|
1812
|
+
"actions_compensated": len(compensated),
|
|
1813
|
+
"escrow_status": escrow_res,
|
|
1814
|
+
"total_dispatch_ms": elapsed_ms,
|
|
1815
|
+
"sla_met": elapsed_ms < 38.0,
|
|
1816
|
+
"compensated_steps": compensated,
|
|
1817
|
+
"pgoutput_restored_tuple": restored_tuple,
|
|
1818
|
+
"double_entry_account": double_entry_acct,
|
|
1819
|
+
"audit_block_index": blk["block_index"],
|
|
1820
|
+
"audit_block_hash": blk["block_hash"],
|
|
1821
|
+
"rfc6962_leaf_hash": blk.get("rfc6962_leaf_hash"),
|
|
1822
|
+
"rfc6962_tree_root": blk.get("rfc6962_tree_root"),
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
if method == "verify_three_way_match":
|
|
1826
|
+
t0 = time.perf_counter()
|
|
1827
|
+
po = params["po"]
|
|
1828
|
+
receipts = params.get("receipts", [])
|
|
1829
|
+
invoices = params.get("invoices", [])
|
|
1830
|
+
tol = float(params.get("priceTolerancePct", 0.02))
|
|
1831
|
+
rec_qty = sum(float(r["receivedQty"]) for r in receipts if r["sku"] == po["sku"])
|
|
1832
|
+
bill_qty = sum(float(i["billedQty"]) for i in invoices if i["sku"] == po["sku"])
|
|
1833
|
+
avg_price = (
|
|
1834
|
+
sum(float(i["billedQty"]) * float(i["billedUnitPrice"]) for i in invoices if i["sku"] == po["sku"]) / bill_qty
|
|
1835
|
+
if bill_qty > 0
|
|
1836
|
+
else float(po["unitPrice"])
|
|
1837
|
+
)
|
|
1838
|
+
|
|
1839
|
+
ctx = z3.Context()
|
|
1840
|
+
s = z3.Solver(ctx=ctx)
|
|
1841
|
+
z_po_q = z3.RealVal(str(float(po["orderedQty"])), ctx=ctx)
|
|
1842
|
+
z_rec_q = z3.RealVal(str(rec_qty), ctx=ctx)
|
|
1843
|
+
z_bill_q = z3.RealVal(str(bill_qty), ctx=ctx)
|
|
1844
|
+
z_po_p = z3.RealVal(str(float(po["unitPrice"])), ctx=ctx)
|
|
1845
|
+
z_bill_p = z3.RealVal(str(avg_price), ctx=ctx)
|
|
1846
|
+
|
|
1847
|
+
s.assert_and_track(z_bill_q <= z_rec_q, "INV_BILLED_LE_RECEIVED")
|
|
1848
|
+
s.assert_and_track(z_bill_q <= z_po_q, "INV_BILLED_LE_ORDERED")
|
|
1849
|
+
s.assert_and_track(z_bill_p <= z_po_p * z3.RealVal(str(1.0 + tol), ctx=ctx), "INV_UNIT_PRICE_TOLERANCE")
|
|
1850
|
+
|
|
1851
|
+
matched = s.check() == z3.sat
|
|
1852
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000.0, 3)
|
|
1853
|
+
var_pct = round(((avg_price - float(po["unitPrice"])) / max(0.01, float(po["unitPrice"]))) * 100.0, 2)
|
|
1854
|
+
return {
|
|
1855
|
+
"matched": matched,
|
|
1856
|
+
"verdict": "SAT" if matched else "UNSAT",
|
|
1857
|
+
"sku": po["sku"],
|
|
1858
|
+
"po_qty": float(po["orderedQty"]),
|
|
1859
|
+
"received_qty": rec_qty,
|
|
1860
|
+
"billed_qty": bill_qty,
|
|
1861
|
+
"price_variance_pct": var_pct,
|
|
1862
|
+
"violated_rules": [] if matched else [str(c) for c in s.unsat_core()],
|
|
1863
|
+
"z3_solve_ms": elapsed_ms,
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
if method == "verify_tax_compliance":
|
|
1867
|
+
t0 = time.perf_counter()
|
|
1868
|
+
jur = str(params["jurisdiction"])
|
|
1869
|
+
lines = params.get("lines", [])
|
|
1870
|
+
has_stamp = bool(params.get("hasCryptographicStamp", True))
|
|
1871
|
+
tol = float(params.get("tolerance", 0.02))
|
|
1872
|
+
rate = {"EGYPT_ETA": 0.14, "KSA_ZATCA": 0.15, "EU_VAT_DE": 0.19, "EU_REVERSE_CHARGE": 0.0}.get(jur, 0.14)
|
|
1873
|
+
|
|
1874
|
+
ctx = z3.Context()
|
|
1875
|
+
s = z3.Solver(ctx=ctx)
|
|
1876
|
+
exp_total = 0.0
|
|
1877
|
+
claimed_total = 0.0
|
|
1878
|
+
if jur == "KSA_ZATCA":
|
|
1879
|
+
s.assert_and_track(z3.BoolVal(has_stamp, ctx=ctx), "INV_ZATCA_CRYPTO_STAMP_REQUIRED")
|
|
1880
|
+
for idx, ln in enumerate(lines):
|
|
1881
|
+
net = float(ln["netAmount"])
|
|
1882
|
+
c_vat = float(ln["claimedVatAmount"])
|
|
1883
|
+
e_vat = 0.0 if ln.get("isExempt") else round(net * rate, 2)
|
|
1884
|
+
exp_total += e_vat
|
|
1885
|
+
claimed_total += c_vat
|
|
1886
|
+
diff = z3.RealVal(str(abs(c_vat - e_vat)), ctx=ctx)
|
|
1887
|
+
s.assert_and_track(diff <= z3.RealVal(str(tol), ctx=ctx), f"INV_TAX_LINE_{idx}_{jur}")
|
|
1888
|
+
|
|
1889
|
+
ok = s.check() == z3.sat
|
|
1890
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000.0, 3)
|
|
1891
|
+
core = [] if ok else [str(c) for c in s.unsat_core()]
|
|
1892
|
+
return {
|
|
1893
|
+
"jurisdiction": jur,
|
|
1894
|
+
"verdict": "SAT" if ok else "UNSAT",
|
|
1895
|
+
"verified": ok,
|
|
1896
|
+
"expected_total_vat": round(exp_total, 2),
|
|
1897
|
+
"claimed_total_vat": round(claimed_total, 2),
|
|
1898
|
+
"violated_tax_laws": core,
|
|
1899
|
+
"z3_solve_ms": elapsed_ms,
|
|
1900
|
+
"proof_hash": hashlib.sha256(f"{jur}:{exp_total}:{claimed_total}:{ok}".encode()).hexdigest(),
|
|
1901
|
+
}
|
|
1902
|
+
|
|
1903
|
+
if method == "bitemporal_insert":
|
|
1904
|
+
eid = str(params["entityId"])
|
|
1905
|
+
vs = int(params["validStart"])
|
|
1906
|
+
ve = int(params.get("validEnd", 9999999999))
|
|
1907
|
+
if ve < 0:
|
|
1908
|
+
ve = 9999999999
|
|
1909
|
+
ss = int(params["systemTime"]) if params.get("systemTime") is not None else int(time.time())
|
|
1910
|
+
self.conn.execute(
|
|
1911
|
+
"INSERT INTO bitemporal_records (entity_id, data_json, valid_start, valid_end, system_start, system_end) VALUES (?, ?, ?, ?, ?, 9999999999)",
|
|
1912
|
+
(eid, json.dumps(params["data"]), vs, ve, ss),
|
|
1913
|
+
)
|
|
1914
|
+
self.conn.commit()
|
|
1915
|
+
return {"inserted": True, "entity_id": eid, "valid_start": vs, "system_start": ss}
|
|
1916
|
+
|
|
1917
|
+
if method == "bitemporal_restate":
|
|
1918
|
+
eid = str(params["entityId"])
|
|
1919
|
+
vs = int(params["validStart"])
|
|
1920
|
+
ve = int(params.get("validEnd", 9999999999))
|
|
1921
|
+
if ve < 0:
|
|
1922
|
+
ve = 9999999999
|
|
1923
|
+
ss = int(params["systemTime"]) if params.get("systemTime") is not None else int(time.time())
|
|
1924
|
+
self.conn.execute(
|
|
1925
|
+
"UPDATE bitemporal_records SET system_end = ? WHERE entity_id = ? AND system_end = 9999999999",
|
|
1926
|
+
(ss, eid),
|
|
1927
|
+
)
|
|
1928
|
+
self.conn.execute(
|
|
1929
|
+
"INSERT INTO bitemporal_records (entity_id, data_json, valid_start, valid_end, system_start, system_end) VALUES (?, ?, ?, ?, ?, 9999999999)",
|
|
1930
|
+
(eid, json.dumps(params["data"]), vs, ve, ss),
|
|
1931
|
+
)
|
|
1932
|
+
self.conn.commit()
|
|
1933
|
+
return {"restated": True, "entity_id": eid, "valid_start": vs, "system_start": ss}
|
|
1934
|
+
|
|
1935
|
+
if method == "bitemporal_as_of":
|
|
1936
|
+
eid = str(params["entityId"])
|
|
1937
|
+
vt = int(params["validTime"])
|
|
1938
|
+
st = int(params["systemTime"])
|
|
1939
|
+
row = self.conn.execute(
|
|
1940
|
+
"""
|
|
1941
|
+
SELECT data_json FROM bitemporal_records
|
|
1942
|
+
WHERE entity_id = ?
|
|
1943
|
+
AND valid_start <= ? AND ? < valid_end
|
|
1944
|
+
AND system_start <= ? AND ? < system_end
|
|
1945
|
+
ORDER BY id DESC LIMIT 1
|
|
1946
|
+
""",
|
|
1947
|
+
(eid, vt, vt, st, st),
|
|
1948
|
+
).fetchone()
|
|
1949
|
+
return {"found": row is not None, "data": json.loads(row["data_json"]) if row else None}
|
|
1950
|
+
|
|
1951
|
+
if method == "verify_audit_chain":
|
|
1952
|
+
valid, cnt, tip = self._verify_chain()
|
|
1953
|
+
return {"valid": valid, "block_count": cnt, "tip_hash": tip}
|
|
1954
|
+
|
|
1955
|
+
if method == "flush_escrow":
|
|
1956
|
+
t0 = time.perf_counter()
|
|
1957
|
+
cooling_sec = float(params.get("coolingWindowSec", 0.0))
|
|
1958
|
+
batch_id = str(params.get("batchId", "")).strip()
|
|
1959
|
+
cutoff = time.time() - max(0.0, cooling_sec)
|
|
1960
|
+
if batch_id:
|
|
1961
|
+
rows = self.conn.execute(
|
|
1962
|
+
"SELECT * FROM rollback_batches WHERE batch_id = ? AND status = 'STAGED'",
|
|
1963
|
+
(batch_id,),
|
|
1964
|
+
).fetchall()
|
|
1965
|
+
else:
|
|
1966
|
+
rows = self.conn.execute(
|
|
1967
|
+
"SELECT * FROM rollback_batches WHERE status = 'STAGED' AND created_at <= ? ORDER BY created_at ASC",
|
|
1968
|
+
(cutoff,),
|
|
1969
|
+
).fetchall()
|
|
1970
|
+
dispatched = []
|
|
1971
|
+
now = time.time()
|
|
1972
|
+
for row in rows:
|
|
1973
|
+
ts_low = str(row["target_system"] or "").lower()
|
|
1974
|
+
if not any(k in ts_low for k in ("stripe", "wire", "iso20022", "netsuite")) and not batch_id:
|
|
1975
|
+
continue
|
|
1976
|
+
b_id = row["batch_id"]
|
|
1977
|
+
self.conn.execute(
|
|
1978
|
+
"UPDATE ledger_accounts SET debits_pending = MAX(0.0, debits_pending - ?), debits_posted = debits_posted + ? WHERE account_id = ?",
|
|
1979
|
+
(row["amount"], row["amount"], row["account_id"]),
|
|
1980
|
+
)
|
|
1981
|
+
self.conn.execute(
|
|
1982
|
+
"UPDATE rollback_batches SET status = 'DISPATCHED_TO_RAIL', rolled_back_at = ? WHERE batch_id = ?",
|
|
1983
|
+
(now, b_id),
|
|
1984
|
+
)
|
|
1985
|
+
rail_ref = f"iso20022_pain001_{b_id}" if ("wire" in ts_low or "iso20022" in ts_low) else f"po_stripe_{b_id}"
|
|
1986
|
+
dispatched.append({
|
|
1987
|
+
"batch_id": b_id,
|
|
1988
|
+
"account_id": row["account_id"],
|
|
1989
|
+
"amount": row["amount"],
|
|
1990
|
+
"target_system": row["target_system"],
|
|
1991
|
+
"escrow_status": "DISPATCHED_TO_RAIL",
|
|
1992
|
+
"settlement_reference": rail_ref,
|
|
1993
|
+
"double_entry_account": self._get_double_entry_account(row["account_id"]),
|
|
1994
|
+
})
|
|
1995
|
+
self.conn.commit()
|
|
1996
|
+
valid, _, tip = self._verify_chain()
|
|
1997
|
+
blk = self._append_block(
|
|
1998
|
+
proof_id=f"escrow_flush_{int(now*1000)}",
|
|
1999
|
+
action="ESCROW_OUTBOX_SETTLEMENT_FLUSH",
|
|
2000
|
+
verdict="DISPATCHED" if dispatched else "NOOP",
|
|
2001
|
+
target_system="ESCROW_RAIL",
|
|
2002
|
+
zk_digest=tip,
|
|
2003
|
+
metadata={"flushed_count": len(dispatched), "chain_valid": valid},
|
|
2004
|
+
)
|
|
2005
|
+
return {
|
|
2006
|
+
"flushed_count": len(dispatched),
|
|
2007
|
+
"dispatched": dispatched,
|
|
2008
|
+
"escrow_status": "DISPATCHED_TO_RAIL" if dispatched else "NO_ELIGIBLE_ESCROW",
|
|
2009
|
+
"flush_latency_ms": round((time.perf_counter() - t0) * 1000.0, 3),
|
|
2010
|
+
"audit_block_index": blk["block_index"],
|
|
2011
|
+
"audit_block_hash": blk["block_hash"],
|
|
2012
|
+
"rfc6962_leaf_hash": blk.get("rfc6962_leaf_hash"),
|
|
2013
|
+
"rfc6962_tree_root": blk.get("rfc6962_tree_root"),
|
|
2014
|
+
}
|
|
2015
|
+
|
|
2016
|
+
if method == "merkle_inclusion_proof":
|
|
2017
|
+
block_idx_raw = params.get("blockIndex")
|
|
2018
|
+
block_idx = int(block_idx_raw) if block_idx_raw is not None else None
|
|
2019
|
+
return self.generate_rfc6962_inclusion_proof(block_idx)
|
|
2020
|
+
|
|
2021
|
+
if method == "evaluate_policy":
|
|
2022
|
+
t0 = time.perf_counter()
|
|
2023
|
+
policy_text = str(params.get("policyText") or DEFAULT_ENTERPRISE_POLICY)
|
|
2024
|
+
action = str(params.get("action") or "UPDATE").upper()
|
|
2025
|
+
ctx_raw = params.get("context")
|
|
2026
|
+
ctx = dict(ctx_raw) if isinstance(ctx_raw, dict) else {}
|
|
2027
|
+
if "amount" in params and "amount" not in ctx:
|
|
2028
|
+
ctx["amount"] = params["amount"]
|
|
2029
|
+
if "currentBalance" in params and "current_balance" not in ctx:
|
|
2030
|
+
ctx["current_balance"] = params["currentBalance"]
|
|
2031
|
+
if "minReserve" in params and "min_reserve" not in ctx:
|
|
2032
|
+
ctx["min_reserve"] = params["minReserve"]
|
|
2033
|
+
if "spendingLimit" in params and "spending_limit" not in ctx:
|
|
2034
|
+
ctx["spending_limit"] = params["spendingLimit"]
|
|
2035
|
+
if "cfoSigned" in params and "cfo_signed" not in ctx:
|
|
2036
|
+
ctx["cfo_signed"] = params["cfoSigned"]
|
|
2037
|
+
if "reversalArmed" in params and "reversal_armed" not in ctx:
|
|
2038
|
+
ctx["reversal_armed"] = params["reversalArmed"]
|
|
2039
|
+
principal = params.get("principal") or ctx.get("principal")
|
|
2040
|
+
resource = params.get("resource") or ctx.get("resource")
|
|
2041
|
+
hierarchy = params.get("entityHierarchy") or params.get("entity_hierarchy") or ctx.get("entity_hierarchy")
|
|
2042
|
+
res = evaluate_policy_z3(
|
|
2043
|
+
policy_text=policy_text,
|
|
2044
|
+
action=action,
|
|
2045
|
+
context=ctx,
|
|
2046
|
+
principal=principal,
|
|
2047
|
+
resource=resource,
|
|
2048
|
+
entity_hierarchy=hierarchy,
|
|
2049
|
+
)
|
|
2050
|
+
digest = hashlib.sha256(
|
|
2051
|
+
json.dumps({"action": action, "principal": principal, "resource": resource, "verdict": res["verdict"]}, sort_keys=True).encode()
|
|
2052
|
+
).hexdigest()
|
|
2053
|
+
blk = self._append_block(
|
|
2054
|
+
proof_id=f"pol_{int(time.time()*1000)}_{uuid.uuid4().hex[:6]}",
|
|
2055
|
+
action="POLICY_EVAL",
|
|
2056
|
+
verdict=res["verdict"],
|
|
2057
|
+
target_system="POLICY_ENGINE",
|
|
2058
|
+
zk_digest=digest,
|
|
2059
|
+
metadata={
|
|
2060
|
+
"principal": principal,
|
|
2061
|
+
"resource": resource,
|
|
2062
|
+
"unsat_core": res["unsat_core"],
|
|
2063
|
+
},
|
|
2064
|
+
)
|
|
2065
|
+
res["latency_ms"] = round((time.perf_counter() - t0) * 1000.0, 3)
|
|
2066
|
+
res["block_index"] = blk["block_index"]
|
|
2067
|
+
res["block_hash"] = blk["block_hash"]
|
|
2068
|
+
res["rfc6962_leaf_hash"] = blk.get("rfc6962_leaf_hash")
|
|
2069
|
+
res["rfc6962_tree_root"] = blk.get("rfc6962_tree_root")
|
|
2070
|
+
return res
|
|
2071
|
+
|
|
2072
|
+
raise ValueError(f"Unknown enclave method: {method}")
|
|
2073
|
+
|
|
2074
|
+
|
|
2075
|
+
def main() -> None:
|
|
2076
|
+
session = StandaloneEnclaveSession()
|
|
2077
|
+
for raw_line in sys.stdin:
|
|
2078
|
+
line = raw_line.strip()
|
|
2079
|
+
if not line:
|
|
2080
|
+
continue
|
|
2081
|
+
req_id = None
|
|
2082
|
+
try:
|
|
2083
|
+
req = json.loads(line)
|
|
2084
|
+
req_id = req.get("id")
|
|
2085
|
+
result = session.handle(req.get("method", ""), req.get("params") or {})
|
|
2086
|
+
sys.stdout.write(json.dumps({"id": req_id, "result": result}) + "\n")
|
|
2087
|
+
sys.stdout.flush()
|
|
2088
|
+
except Exception as exc:
|
|
2089
|
+
sys.stdout.write(json.dumps({"id": req_id, "error": {"message": str(exc)}}) + "\n")
|
|
2090
|
+
sys.stdout.flush()
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
if __name__ == "__main__":
|
|
2094
|
+
main()
|