@crediolabs/policy-synth 0.3.0 → 0.3.1
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/dist/record/freshness.d.ts +17 -11
- package/dist/record/freshness.js +28 -18
- package/dist-cjs/record/freshness.d.ts +17 -11
- package/dist-cjs/record/freshness.js +28 -18
- package/package.json +2 -2
- package/src/record/freshness.ts +29 -18
- package/dist/adapters/interpreter/index.d.ts +0 -1
- package/dist/adapters/interpreter/index.js +0 -2
- package/dist/adapters/oz/adapter.d.ts +0 -20
- package/dist/adapters/oz/adapter.js +0 -281
- package/dist/adapters/oz/index.d.ts +0 -1
- package/dist/adapters/oz/index.js +0 -2
- package/dist/codegen/compile-gate.d.ts +0 -33
- package/dist/codegen/compile-gate.js +0 -124
- package/dist/codegen/index.d.ts +0 -2
- package/dist/codegen/index.js +0 -8
- package/dist/codegen/template.d.ts +0 -18
- package/dist/codegen/template.js +0 -148
- package/dist/ir/index.d.ts +0 -1
- package/dist/ir/index.js +0 -2
- package/dist/ir/types.d.ts +0 -71
- package/dist/ir/types.js +0 -9
- package/dist/mandate/index.d.ts +0 -2
- package/dist/mandate/index.js +0 -2
- package/dist/mandate/to-ir.d.ts +0 -3
- package/dist/mandate/to-ir.js +0 -60
- package/dist/mandate/types.d.ts +0 -20
- package/dist/mandate/types.js +0 -8
- package/dist/seams/index.d.ts +0 -1
- package/dist/seams/index.js +0 -2
- package/dist/seams/types.d.ts +0 -64
- package/dist/seams/types.js +0 -11
- package/dist/synth/deny-cases.d.ts +0 -28
- package/dist/synth/deny-cases.js +0 -212
- package/dist/synth/evaluate.d.ts +0 -18
- package/dist/synth/evaluate.js +0 -271
- package/dist/synth/harness.d.ts +0 -28
- package/dist/synth/harness.js +0 -47
- package/dist/synth/minimize.d.ts +0 -4
- package/dist/synth/minimize.js +0 -38
- package/dist/synth/permit-context.d.ts +0 -8
- package/dist/synth/permit-context.js +0 -35
- package/dist/synth/predicate-literals.d.ts +0 -5
- package/dist/synth/predicate-literals.js +0 -25
- package/dist/synth/synthesize-from-mandate.d.ts +0 -20
- package/dist/synth/synthesize-from-mandate.js +0 -59
- package/dist/verify/envelope.d.ts +0 -15
- package/dist/verify/envelope.js +0 -22
- package/dist/verify/index.d.ts +0 -3
- package/dist/verify/index.js +0 -3
- package/dist/verify/simulate.d.ts +0 -18
- package/dist/verify/simulate.js +0 -172
- package/dist/verify/verify.d.ts +0 -15
- package/dist/verify/verify.js +0 -181
- package/dist-cjs/adapters/interpreter/index.d.ts +0 -1
- package/dist-cjs/adapters/interpreter/index.js +0 -7
- package/dist-cjs/adapters/oz/adapter.d.ts +0 -20
- package/dist-cjs/adapters/oz/adapter.js +0 -286
- package/dist-cjs/adapters/oz/index.d.ts +0 -1
- package/dist-cjs/adapters/oz/index.js +0 -8
- package/dist-cjs/codegen/compile-gate.d.ts +0 -33
- package/dist-cjs/codegen/compile-gate.js +0 -128
- package/dist-cjs/codegen/index.d.ts +0 -2
- package/dist-cjs/codegen/index.js +0 -14
- package/dist-cjs/codegen/template.d.ts +0 -18
- package/dist-cjs/codegen/template.js +0 -151
- package/dist-cjs/ir/index.d.ts +0 -1
- package/dist-cjs/ir/index.js +0 -3
- package/dist-cjs/ir/types.d.ts +0 -71
- package/dist-cjs/ir/types.js +0 -10
- package/dist-cjs/mandate/index.d.ts +0 -2
- package/dist-cjs/mandate/index.js +0 -6
- package/dist-cjs/mandate/to-ir.d.ts +0 -3
- package/dist-cjs/mandate/to-ir.js +0 -63
- package/dist-cjs/mandate/types.d.ts +0 -20
- package/dist-cjs/mandate/types.js +0 -9
- package/dist-cjs/seams/index.d.ts +0 -1
- package/dist-cjs/seams/index.js +0 -3
- package/dist-cjs/seams/types.d.ts +0 -64
- package/dist-cjs/seams/types.js +0 -12
- package/dist-cjs/synth/deny-cases.d.ts +0 -28
- package/dist-cjs/synth/deny-cases.js +0 -217
- package/dist-cjs/synth/evaluate.d.ts +0 -18
- package/dist-cjs/synth/evaluate.js +0 -274
- package/dist-cjs/synth/harness.d.ts +0 -28
- package/dist-cjs/synth/harness.js +0 -50
- package/dist-cjs/synth/minimize.d.ts +0 -4
- package/dist-cjs/synth/minimize.js +0 -41
- package/dist-cjs/synth/permit-context.d.ts +0 -8
- package/dist-cjs/synth/permit-context.js +0 -38
- package/dist-cjs/synth/predicate-literals.d.ts +0 -5
- package/dist-cjs/synth/predicate-literals.js +0 -28
- package/dist-cjs/synth/synthesize-from-mandate.d.ts +0 -20
- package/dist-cjs/synth/synthesize-from-mandate.js +0 -62
- package/dist-cjs/verify/envelope.d.ts +0 -15
- package/dist-cjs/verify/envelope.js +0 -23
- package/dist-cjs/verify/index.d.ts +0 -3
- package/dist-cjs/verify/index.js +0 -8
- package/dist-cjs/verify/simulate.d.ts +0 -18
- package/dist-cjs/verify/simulate.js +0 -175
- package/dist-cjs/verify/verify.d.ts +0 -15
- package/dist-cjs/verify/verify.js +0 -184
package/dist/synth/deny-cases.js
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import { MAX_SCVAL_CLONE_DEPTH } from "../types.js";
|
|
2
|
-
// Deterministic XLM/USDC adjacency fixture; the shared registry can replace this boundary later.
|
|
3
|
-
const OVERPERMISSIVE_DIMENSIONS = ['argument_reorder'];
|
|
4
|
-
export { OVERPERMISSIVE_DIMENSIONS };
|
|
5
|
-
/** Build deterministic model-evaluated alternatives without mutating the intended call.
|
|
6
|
-
*
|
|
7
|
-
* @param predicate - the synthesized predicate
|
|
8
|
-
* @param permitCtx - EvalContext for the intended (permitted) call
|
|
9
|
-
* @param dimensions - optional whitelist of dimension names to emit; when omitted
|
|
10
|
-
* all known dimensions (including over-permissiveness mutations)
|
|
11
|
-
* are emitted. */
|
|
12
|
-
export function generateCases(predicate, permitCtx, dimensions) {
|
|
13
|
-
const facts = inspectPredicate(predicate);
|
|
14
|
-
const denies = [];
|
|
15
|
-
// arg_bound: set a constrained arg to an opaque ScVal.
|
|
16
|
-
// The reason depends on the predicate shape:
|
|
17
|
-
// - an `eq(call_arg[i], literal)` denies with ARG_MISMATCH
|
|
18
|
-
// - an `in(call_arg[i], [literals])` denies with NOT_IN_ALLOWLIST
|
|
19
|
-
// Both are correct in the TS evaluator; the reason is predicate-dependent
|
|
20
|
-
// so we cannot pin a single canonical reason here.
|
|
21
|
-
const argumentConstraints = [];
|
|
22
|
-
for (const comparison of facts.comparisons) {
|
|
23
|
-
if (comparison.op === 'eq' &&
|
|
24
|
-
comparison.left.kind === 'call_arg' &&
|
|
25
|
-
comparison.right.kind !== 'literal_vec') {
|
|
26
|
-
argumentConstraints.push({ index: comparison.left.index });
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
for (const membership of facts.memberships) {
|
|
30
|
-
if (membership.needle.kind === 'call_arg') {
|
|
31
|
-
argumentConstraints.push({ index: membership.needle.index });
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
for (const constraint of argumentConstraints) {
|
|
35
|
-
const ctx = cloneContext(permitCtx);
|
|
36
|
-
ctx.args[constraint.index] = { type: 'other', value: 'deny-case-opaque-argument' };
|
|
37
|
-
denies.push({ dimension: 'arg_bound', ctx });
|
|
38
|
-
}
|
|
39
|
-
// soroswap_allowed_path: replace a bound vec arg with a different vector.
|
|
40
|
-
// Still valid for eq comparisons on literal_vec leaves.
|
|
41
|
-
for (const comparison of facts.comparisons) {
|
|
42
|
-
if (comparison.op !== 'eq' ||
|
|
43
|
-
comparison.left.kind !== 'call_arg' ||
|
|
44
|
-
comparison.right.kind !== 'literal_vec') {
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
const ctx = cloneContext(permitCtx);
|
|
48
|
-
ctx.args[comparison.left.index] = differentVector(ctx.args[comparison.left.index]);
|
|
49
|
-
denies.push({ dimension: 'soroswap_allowed_path', ctx });
|
|
50
|
-
}
|
|
51
|
-
// argument_reorder: swap first two address args.
|
|
52
|
-
// Skipped when dimensions filter is active so the synth pipeline can emit a policy;
|
|
53
|
-
// the over-permissiveness harness then tests this mutation as a FINDING.
|
|
54
|
-
const constrainedArgIndices = new Set();
|
|
55
|
-
for (const comparison of facts.comparisons) {
|
|
56
|
-
if (comparison.left.kind === 'call_arg')
|
|
57
|
-
constrainedArgIndices.add(comparison.left.index);
|
|
58
|
-
}
|
|
59
|
-
for (const membership of facts.memberships) {
|
|
60
|
-
if (membership.needle.kind === 'call_arg')
|
|
61
|
-
constrainedArgIndices.add(membership.needle.index);
|
|
62
|
-
}
|
|
63
|
-
const hasConstrainedArg = constrainedArgIndices.size > 0;
|
|
64
|
-
if ((!dimensions || dimensions.includes('argument_reorder')) &&
|
|
65
|
-
hasConstrainedArg &&
|
|
66
|
-
permitCtx.args.length >= 2 &&
|
|
67
|
-
permitCtx.args[0]?.type === 'address' &&
|
|
68
|
-
permitCtx.args[1]?.type === 'address' &&
|
|
69
|
-
permitCtx.args[0].value !==
|
|
70
|
-
permitCtx.args[1].value) {
|
|
71
|
-
const ctx = cloneContext(permitCtx);
|
|
72
|
-
// Guard above guarantees args[0] and args[1] exist and are address-typed.
|
|
73
|
-
const a0 = ctx.args[0];
|
|
74
|
-
const a1 = ctx.args[1];
|
|
75
|
-
ctx.args[0] = a1;
|
|
76
|
-
ctx.args[1] = a0;
|
|
77
|
-
denies.push({ dimension: 'argument_reorder', ctx });
|
|
78
|
-
}
|
|
79
|
-
// map_field_flip: flip a bound map field to a different valid value of
|
|
80
|
-
// the same type. OPT-IN only, never ORIGINAL.
|
|
81
|
-
// Targets each `call_arg_field` leaf and produces a ctx whose vec element
|
|
82
|
-
// has a different value of the recorded field. ---
|
|
83
|
-
if (!dimensions || dimensions.includes('map_field_flip')) {
|
|
84
|
-
for (const comparison of facts.comparisons) {
|
|
85
|
-
const sel = comparison.left;
|
|
86
|
-
if (sel.kind !== 'call_arg_field')
|
|
87
|
-
continue;
|
|
88
|
-
const arg = permitCtx.args[sel.index];
|
|
89
|
-
if (arg?.type !== 'vec')
|
|
90
|
-
continue;
|
|
91
|
-
const element = arg.value[sel.element];
|
|
92
|
-
if (element?.type !== 'map' || !Array.isArray(element.value))
|
|
93
|
-
continue;
|
|
94
|
-
const entry = element.value.find((e) => e.key === sel.field);
|
|
95
|
-
if (!entry)
|
|
96
|
-
continue;
|
|
97
|
-
const flipped = flipFieldValue(entry.val);
|
|
98
|
-
if (flipped === null)
|
|
99
|
-
continue;
|
|
100
|
-
const ctx = cloneContext(permitCtx);
|
|
101
|
-
const clonedVec = arg.value.map(cloneScVal);
|
|
102
|
-
const clonedElement = clonedVec[sel.element];
|
|
103
|
-
if (clonedElement?.type !== 'map' || !Array.isArray(clonedElement.value))
|
|
104
|
-
continue;
|
|
105
|
-
clonedElement.value = clonedElement.value.map((e) => e.key === sel.field ? { key: e.key, val: flipped } : e);
|
|
106
|
-
clonedVec[sel.element] = clonedElement;
|
|
107
|
-
ctx.args[sel.index] = { type: 'vec', value: clonedVec };
|
|
108
|
-
denies.push({ dimension: 'map_field_flip', ctx });
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
// vec_append: append a new element to a bound vec. OPT-IN only, never ORIGINAL.
|
|
112
|
-
// Targets every `call_arg_len` leaf; without the length pin a caller can
|
|
113
|
-
// append an extra element to defeat per-element binds. ---
|
|
114
|
-
if (!dimensions || dimensions.includes('vec_append')) {
|
|
115
|
-
for (const comparison of facts.comparisons) {
|
|
116
|
-
const sel = comparison.left;
|
|
117
|
-
if (sel.kind !== 'call_arg_len')
|
|
118
|
-
continue;
|
|
119
|
-
const arg = permitCtx.args[sel.index];
|
|
120
|
-
if (arg?.type !== 'vec')
|
|
121
|
-
continue;
|
|
122
|
-
const ctx = cloneContext(permitCtx);
|
|
123
|
-
ctx.args[sel.index] = {
|
|
124
|
-
type: 'vec',
|
|
125
|
-
value: [...arg.value, { type: 'other', value: 'deny-case-vec-append' }],
|
|
126
|
-
};
|
|
127
|
-
denies.push({ dimension: 'vec_append', ctx });
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
return { permit: cloneContext(permitCtx), denies };
|
|
131
|
-
}
|
|
132
|
-
function inspectPredicate(predicate) {
|
|
133
|
-
const facts = { comparisons: [], memberships: [] };
|
|
134
|
-
visit(predicate, facts);
|
|
135
|
-
return facts;
|
|
136
|
-
}
|
|
137
|
-
function visit(node, facts) {
|
|
138
|
-
switch (node.op) {
|
|
139
|
-
case 'and':
|
|
140
|
-
for (const child of node.children)
|
|
141
|
-
visit(child, facts);
|
|
142
|
-
return;
|
|
143
|
-
case 'in':
|
|
144
|
-
facts.memberships.push(node);
|
|
145
|
-
return;
|
|
146
|
-
case 'eq':
|
|
147
|
-
case 'lte':
|
|
148
|
-
facts.comparisons.push(node);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
function differentVector(actual) {
|
|
152
|
-
if (actual?.type !== 'vec')
|
|
153
|
-
return { type: 'vec', value: [] };
|
|
154
|
-
if (actual.value.length === 0) {
|
|
155
|
-
return { type: 'vec', value: [{ type: 'other', value: 'deny-case-extra-hop' }] };
|
|
156
|
-
}
|
|
157
|
-
if (actual.value.length > 1) {
|
|
158
|
-
const reversed = actual.value.map(cloneScVal).reverse();
|
|
159
|
-
if (JSON.stringify(reversed) !== JSON.stringify(actual.value)) {
|
|
160
|
-
return { type: 'vec', value: reversed };
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
const value = actual.value.map(cloneScVal);
|
|
164
|
-
value[0] = { type: 'other', value: 'deny-case-different-hop' };
|
|
165
|
-
return { type: 'vec', value };
|
|
166
|
-
}
|
|
167
|
-
function cloneContext(ctx) {
|
|
168
|
-
return {
|
|
169
|
-
contract: ctx.contract,
|
|
170
|
-
fn: ctx.fn,
|
|
171
|
-
args: ctx.args.map(cloneScVal),
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
/** Deep-copy an ScVal so a mutation cannot alias the recorded call.
|
|
175
|
-
* Exported so the permit-context builder shares this one implementation. */
|
|
176
|
-
export function cloneScVal(value, depth = 0) {
|
|
177
|
-
if (value.type === 'vec') {
|
|
178
|
-
if (depth >= MAX_SCVAL_CLONE_DEPTH) {
|
|
179
|
-
throw cloneDepthError(value);
|
|
180
|
-
}
|
|
181
|
-
return { type: 'vec', value: value.value.map((v) => cloneScVal(v, depth + 1)) };
|
|
182
|
-
}
|
|
183
|
-
return { ...value };
|
|
184
|
-
}
|
|
185
|
-
function cloneDepthError(value) {
|
|
186
|
-
const err = new Error(`ScVal clone depth exceeds MAX_SCVAL_CLONE_DEPTH (${MAX_SCVAL_CLONE_DEPTH})`);
|
|
187
|
-
err.code = 'SYNTHESIS_ERROR';
|
|
188
|
-
err.severity = 'error';
|
|
189
|
-
err.retryable = false;
|
|
190
|
-
err.depthContext = value.type;
|
|
191
|
-
throw err;
|
|
192
|
-
}
|
|
193
|
-
/** Build a value of the SAME ScVal type that differs from the recorded one,
|
|
194
|
-
* used by the `map_field_flip` mutation to defeat a per-element pin without
|
|
195
|
-
* changing the wire type. Returns null when the ScVal type has no obvious
|
|
196
|
-
* different value (e.g. opaque/other). */
|
|
197
|
-
function flipFieldValue(val) {
|
|
198
|
-
switch (val.type) {
|
|
199
|
-
case 'address': {
|
|
200
|
-
const a = 'GBFKRGJYZXLTDEI36ZCQEIM225NMOCR2VDBOIHJTXJ54FEFFVL2FKALE';
|
|
201
|
-
const b = 'GD6XSMQJ47EHHJOWXQOND5YDVZC37JWZJHYHBKE6QJFSLLJ5KQXM5QS5';
|
|
202
|
-
return { type: 'address', value: val.value === a ? b : a };
|
|
203
|
-
}
|
|
204
|
-
case 'i128':
|
|
205
|
-
case 'u32':
|
|
206
|
-
return { type: val.type, value: String(BigInt(val.value) + 1n) };
|
|
207
|
-
case 'symbol':
|
|
208
|
-
return { type: 'symbol', value: `${val.value}x` };
|
|
209
|
-
default:
|
|
210
|
-
return null;
|
|
211
|
-
}
|
|
212
|
-
}
|
package/dist/synth/evaluate.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { PredicateNode, ScVal } from '../types.ts';
|
|
2
|
-
export interface EvalContext {
|
|
3
|
-
/** Contract the interpreter is asked to enforce against. */
|
|
4
|
-
contract: string;
|
|
5
|
-
/** Function name on that contract. */
|
|
6
|
-
fn: string;
|
|
7
|
-
/** Decoded `ScVal[]` of the top-level authorized call. */
|
|
8
|
-
args: ScVal[];
|
|
9
|
-
}
|
|
10
|
-
export type EvalResult = {
|
|
11
|
-
permit: true;
|
|
12
|
-
} | {
|
|
13
|
-
permit: false;
|
|
14
|
-
reason: string;
|
|
15
|
-
};
|
|
16
|
-
/** Evaluate a `PredicateNode` against the candidate call described by `ctx`.
|
|
17
|
-
* Pure function. Returns `{ permit: true }` or `{ permit: false; reason }`. */
|
|
18
|
-
export declare function evaluate(predicate: PredicateNode, ctx: EvalContext): EvalResult;
|
package/dist/synth/evaluate.js
DELETED
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
// src/synth/evaluate.ts - TypeScript reference evaluator for grammar version 3.
|
|
2
|
-
//
|
|
3
|
-
// Pure function. Determinism: same `(predicate, ctx)` -> byte-identical result,
|
|
4
|
-
// no clock, no randomness. Deny order (deny on FIRST violation, stable reason):
|
|
5
|
-
// 1. contract mismatch -> 'CONTRACT_SCOPE'
|
|
6
|
-
// 2. per-ScVal equality on fn/args; EXACT ordered vector
|
|
7
|
-
// equality; fail-closed on opaque args -> 'ARG_MISMATCH'
|
|
8
|
-
// 3. `in` membership; empty haystack ALWAYS denies -> 'NOT_IN_ALLOWLIST'
|
|
9
|
-
// 4. otherwise permit.
|
|
10
|
-
//
|
|
11
|
-
// Grammar version 3 nodes: and, eq, lte
|
|
12
|
-
// Grammar version 3 leaves: call_contract, call_fn, call_arg(i),
|
|
13
|
-
// call_arg_len(i), call_arg_field(i, element, field),
|
|
14
|
-
// literal_address, literal_i128, literal_symbol, literal_u32, literal_vec
|
|
15
|
-
// Grammar version 3 deny reasons: ARG_MISMATCH, CONTRACT_SCOPE,
|
|
16
|
-
// UNSUPPORTED_NODE, NOT_IN_ALLOWLIST
|
|
17
|
-
/** Evaluate a `PredicateNode` against the candidate call described by `ctx`.
|
|
18
|
-
* Pure function. Returns `{ permit: true }` or `{ permit: false; reason }`. */
|
|
19
|
-
export function evaluate(predicate, ctx) {
|
|
20
|
-
return walk(predicate, ctx);
|
|
21
|
-
}
|
|
22
|
-
/** Walk the predicate tree. Returns the FIRST deny reason encountered on
|
|
23
|
-
* the active branch (so `and` fails-fast). */
|
|
24
|
-
function walk(node, ctx) {
|
|
25
|
-
switch (node.op) {
|
|
26
|
-
case 'and': {
|
|
27
|
-
let lastDeny = null;
|
|
28
|
-
for (const child of node.children) {
|
|
29
|
-
const r = walk(child, ctx);
|
|
30
|
-
if (!r.permit)
|
|
31
|
-
return r;
|
|
32
|
-
lastDeny = r;
|
|
33
|
-
}
|
|
34
|
-
return lastDeny ?? { permit: true };
|
|
35
|
-
}
|
|
36
|
-
case 'eq':
|
|
37
|
-
case 'lte':
|
|
38
|
-
return evalCompare(node.op, node.left, node.right, ctx);
|
|
39
|
-
case 'in':
|
|
40
|
-
return evalIn(node.needle, node.haystack, ctx);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
/** Comparison leaf evaluation. */
|
|
44
|
-
function evalCompare(op, left, right, ctx) {
|
|
45
|
-
// CONTRACT_SCOPE on call_contract eq
|
|
46
|
-
if (left.kind === 'call_contract' && op === 'eq') {
|
|
47
|
-
if (right.kind !== 'literal_address')
|
|
48
|
-
return { permit: false, reason: 'CONTRACT_SCOPE' };
|
|
49
|
-
return right.value === ctx.contract
|
|
50
|
-
? { permit: true }
|
|
51
|
-
: { permit: false, reason: 'CONTRACT_SCOPE' };
|
|
52
|
-
}
|
|
53
|
-
// call_fn equality
|
|
54
|
-
if (left.kind === 'call_fn' && op === 'eq') {
|
|
55
|
-
if (right.kind !== 'literal_symbol')
|
|
56
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
57
|
-
return right.value === ctx.fn ? { permit: true } : { permit: false, reason: 'ARG_MISMATCH' };
|
|
58
|
-
}
|
|
59
|
-
// call_arg comparison (eq / exact-vec, or lte ordered numeric bound)
|
|
60
|
-
if (left.kind === 'call_arg') {
|
|
61
|
-
const actual = ctx.args[left.index];
|
|
62
|
-
if (op !== 'eq')
|
|
63
|
-
return evalArgOrderedCompare(op, actual, right);
|
|
64
|
-
return evalArgEq(op, actual, right, ctx);
|
|
65
|
-
}
|
|
66
|
-
// call_arg_len: length of a vec-typed argument as u32.
|
|
67
|
-
// Fails closed on a non-vec / absent arg or a non-u32 literal.
|
|
68
|
-
if (left.kind === 'call_arg_len') {
|
|
69
|
-
const actual = ctx.args[left.index];
|
|
70
|
-
if (actual?.type !== 'vec')
|
|
71
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
72
|
-
if (right.kind !== 'literal_u32')
|
|
73
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
74
|
-
return actual.value.length === right.value
|
|
75
|
-
? { permit: true }
|
|
76
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
77
|
-
}
|
|
78
|
-
// call_arg_field: value of a field in the map at element i of
|
|
79
|
-
// the vec at argument index. Fails closed on shape / type / range issues.
|
|
80
|
-
if (left.kind === 'call_arg_field') {
|
|
81
|
-
const actual = ctx.args[left.index];
|
|
82
|
-
if (actual?.type !== 'vec')
|
|
83
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
84
|
-
const element = actual.value[left.element];
|
|
85
|
-
if (element?.type !== 'map')
|
|
86
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
87
|
-
if (!Array.isArray(element.value))
|
|
88
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
89
|
-
const entry = element.value.find((e) => e.key === left.field);
|
|
90
|
-
if (!entry)
|
|
91
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
92
|
-
if (op === 'eq')
|
|
93
|
-
return evalArgEq(op, entry.val, right, ctx);
|
|
94
|
-
return evalArgOrderedCompare(op, entry.val, right);
|
|
95
|
-
}
|
|
96
|
-
// Unknown leaf/op combination - structural fail-closed.
|
|
97
|
-
return { permit: false, reason: 'UNSUPPORTED_NODE' };
|
|
98
|
-
}
|
|
99
|
-
/** Per-ScVal equality. Handles literal_vec as an EXACT ordered sequence:
|
|
100
|
-
* compare element-by-element in order; deny if length or any element differs.
|
|
101
|
-
* Opaque args (`type: 'other'`) fail closed. */
|
|
102
|
-
function evalArgEq(op, actual, right, _ctx) {
|
|
103
|
-
// eq(call_arg[i], literal_vec) -> EXACT ordered vector equality.
|
|
104
|
-
if (op === 'eq' && right.kind === 'literal_vec') {
|
|
105
|
-
if (actual?.type !== 'vec')
|
|
106
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
107
|
-
return compareVecExact(actual.value, right.elements)
|
|
108
|
-
? { permit: true }
|
|
109
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
110
|
-
}
|
|
111
|
-
if (op === 'eq' && right.kind === 'literal_address') {
|
|
112
|
-
if (!actual)
|
|
113
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
114
|
-
if (actual.type === 'other')
|
|
115
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
116
|
-
return actual.type === 'address' && actual.value === right.value
|
|
117
|
-
? { permit: true }
|
|
118
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
119
|
-
}
|
|
120
|
-
if (op === 'eq' && right.kind === 'literal_i128') {
|
|
121
|
-
if (actual?.type !== 'i128')
|
|
122
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
123
|
-
return BigInt(actual.value) === BigInt(right.value)
|
|
124
|
-
? { permit: true }
|
|
125
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
126
|
-
}
|
|
127
|
-
if (op === 'eq' && right.kind === 'literal_symbol') {
|
|
128
|
-
if (!actual)
|
|
129
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
130
|
-
return actual.type === 'symbol' && actual.value === right.value
|
|
131
|
-
? { permit: true }
|
|
132
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
133
|
-
}
|
|
134
|
-
if (op === 'eq' && right.kind === 'literal_u32') {
|
|
135
|
-
if (!actual)
|
|
136
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
137
|
-
return actual.type === 'u32' && actual.value === String(right.value)
|
|
138
|
-
? { permit: true }
|
|
139
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
140
|
-
}
|
|
141
|
-
// Anything else: fail closed on opacity (cannot decode the arg reliably).
|
|
142
|
-
if (!actual || actual.type === 'other')
|
|
143
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
144
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
145
|
-
}
|
|
146
|
-
/** Ordered numeric comparison (lte) on a `call_arg`. The interpreter reads the
|
|
147
|
-
* arg as an integer (i128 on the recorder's ScVal surface) and compares it
|
|
148
|
-
* to a numeric literal via BigInt. A non-numeric arg or a non-numeric literal
|
|
149
|
-
* fails closed (ARG_MISMATCH) rather than permitting an undecidable bound. */
|
|
150
|
-
function evalArgOrderedCompare(op, actual, right) {
|
|
151
|
-
const actualInt = argNumericBigInt(actual);
|
|
152
|
-
const literalInt = literalNumericBigInt(right);
|
|
153
|
-
if (actualInt === null || literalInt === null) {
|
|
154
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
155
|
-
}
|
|
156
|
-
return bigintCmp(op, actualInt.toString(), literalInt.toString())
|
|
157
|
-
? { permit: true }
|
|
158
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
159
|
-
}
|
|
160
|
-
/** BigInt value of a numeric-integer ScVal arg (i128 / u32), or null when
|
|
161
|
-
* the arg is absent, opaque, or a non-numeric type. */
|
|
162
|
-
function argNumericBigInt(actual) {
|
|
163
|
-
if (!actual)
|
|
164
|
-
return null;
|
|
165
|
-
if (actual.type === 'i128' || actual.type === 'u32') {
|
|
166
|
-
try {
|
|
167
|
-
return BigInt(actual.value);
|
|
168
|
-
}
|
|
169
|
-
catch {
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
return null;
|
|
174
|
-
}
|
|
175
|
-
/** BigInt value of a numeric-integer literal leaf (`literal_i128` / `literal_u32`),
|
|
176
|
-
* or null for a non-numeric literal. Callers fail closed on null. */
|
|
177
|
-
function literalNumericBigInt(leaf) {
|
|
178
|
-
switch (leaf.kind) {
|
|
179
|
-
case 'literal_i128':
|
|
180
|
-
try {
|
|
181
|
-
return BigInt(leaf.value);
|
|
182
|
-
}
|
|
183
|
-
catch {
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
case 'literal_u32':
|
|
187
|
-
return BigInt(leaf.value);
|
|
188
|
-
default:
|
|
189
|
-
return null;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
/** Element-by-element ordered comparison of an `ScVal[]` against a
|
|
193
|
-
* `PredicateLeaf[]`. Equal-length and equal at every index => permit.
|
|
194
|
-
* Length mismatch OR any element mismatch => deny ARG_MISMATCH. */
|
|
195
|
-
function compareVecExact(actual, expected) {
|
|
196
|
-
if (actual.length !== expected.length)
|
|
197
|
-
return false;
|
|
198
|
-
for (let i = 0; i < expected.length; i++) {
|
|
199
|
-
const e = expected[i];
|
|
200
|
-
const a = actual[i];
|
|
201
|
-
if (!e || !a)
|
|
202
|
-
return false;
|
|
203
|
-
const r = evalArgEq('eq', a, e, {});
|
|
204
|
-
if (!r.permit)
|
|
205
|
-
return false;
|
|
206
|
-
}
|
|
207
|
-
return true;
|
|
208
|
-
}
|
|
209
|
-
/** `in` membership. Empty haystack ALWAYS denies. Opaque needle fails closed. */
|
|
210
|
-
function evalIn(needle, haystack, ctx) {
|
|
211
|
-
if (haystack.length === 0)
|
|
212
|
-
return { permit: false, reason: 'NOT_IN_ALLOWLIST' };
|
|
213
|
-
const actual = resolveLeaf(needle, ctx);
|
|
214
|
-
if (!actual || actual.type === 'other')
|
|
215
|
-
return { permit: false, reason: 'NOT_IN_ALLOWLIST' };
|
|
216
|
-
for (const h of haystack) {
|
|
217
|
-
const r = evalArgEq('eq', actual, h, ctx);
|
|
218
|
-
if (r.permit)
|
|
219
|
-
return { permit: true };
|
|
220
|
-
}
|
|
221
|
-
return { permit: false, reason: 'NOT_IN_ALLOWLIST' };
|
|
222
|
-
}
|
|
223
|
-
/** Resolve a selector leaf to its current ScVal against the candidate call. */
|
|
224
|
-
function resolveLeaf(leaf, ctx) {
|
|
225
|
-
switch (leaf.kind) {
|
|
226
|
-
case 'call_contract':
|
|
227
|
-
return { type: 'address', value: ctx.contract };
|
|
228
|
-
case 'call_fn':
|
|
229
|
-
return { type: 'symbol', value: ctx.fn };
|
|
230
|
-
case 'call_arg':
|
|
231
|
-
return ctx.args[leaf.index];
|
|
232
|
-
case 'call_arg_len':
|
|
233
|
-
// No direct ScVal projection: the length is an integer the comparator
|
|
234
|
-
// resolves against the right-hand literal. Returning undefined keeps
|
|
235
|
-
// the `in` membership path structurally informed (no haystack match).
|
|
236
|
-
return undefined;
|
|
237
|
-
case 'call_arg_field': {
|
|
238
|
-
const actual = ctx.args[leaf.index];
|
|
239
|
-
if (actual?.type !== 'vec')
|
|
240
|
-
return undefined;
|
|
241
|
-
const element = actual.value[leaf.element];
|
|
242
|
-
if (element?.type !== 'map')
|
|
243
|
-
return undefined;
|
|
244
|
-
if (!Array.isArray(element.value))
|
|
245
|
-
return undefined;
|
|
246
|
-
const entry = element.value.find((e) => e.key === leaf.field);
|
|
247
|
-
return entry ? entry.val : undefined;
|
|
248
|
-
}
|
|
249
|
-
case 'literal_address':
|
|
250
|
-
return { type: 'address', value: leaf.value };
|
|
251
|
-
case 'literal_i128':
|
|
252
|
-
return { type: 'i128', value: leaf.value };
|
|
253
|
-
case 'literal_symbol':
|
|
254
|
-
return { type: 'symbol', value: leaf.value };
|
|
255
|
-
case 'literal_u32':
|
|
256
|
-
return { type: 'u32', value: String(leaf.value) };
|
|
257
|
-
case 'literal_vec':
|
|
258
|
-
return undefined;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
/** BigInt compare helper. */
|
|
262
|
-
function bigintCmp(op, aStr, bStr) {
|
|
263
|
-
const a = BigInt(aStr);
|
|
264
|
-
const b = BigInt(bStr);
|
|
265
|
-
switch (op) {
|
|
266
|
-
case 'eq':
|
|
267
|
-
return a === b;
|
|
268
|
-
case 'lte':
|
|
269
|
-
return a <= b;
|
|
270
|
-
}
|
|
271
|
-
}
|
package/dist/synth/harness.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { PredicateNode } from '../types.ts';
|
|
2
|
-
import type { GeneratedCases } from './deny-cases.ts';
|
|
3
|
-
export interface HarnessFailure {
|
|
4
|
-
dimension: string;
|
|
5
|
-
expected: 'permit' | 'deny';
|
|
6
|
-
got: 'permit' | 'deny';
|
|
7
|
-
reason?: string;
|
|
8
|
-
/** When `expectedReason` is set on the deny case and the case actually
|
|
9
|
-
* denies, this carries the canonical reason the TS evaluator MUST emit
|
|
10
|
-
* to stay in lockstep with the Rust interpreter (the cross-layer
|
|
11
|
-
* reason-code contract). A future TS/Rust reason divergence surfaces
|
|
12
|
-
* here in CI. */
|
|
13
|
-
expectedReason?: string;
|
|
14
|
-
actualReason?: string;
|
|
15
|
-
}
|
|
16
|
-
export type HarnessResult = {
|
|
17
|
-
ok: true;
|
|
18
|
-
} | {
|
|
19
|
-
ok: false;
|
|
20
|
-
failures: HarnessFailure[];
|
|
21
|
-
};
|
|
22
|
-
/** Evaluate the intended call and every generated denial without side effects.
|
|
23
|
-
* Asserts both the boolean decision AND, for deny cases that carry an
|
|
24
|
-
* `expectedReason`, the concrete reason the TS evaluator emits. The reason
|
|
25
|
-
* is what the Rust interpreter returns; the cross-layer conformance test in
|
|
26
|
-
* `contracts/policy-interpreter/tests/conformance` mirrors the same contract
|
|
27
|
-
* for the verifyLive path. */
|
|
28
|
-
export declare function runHarness(predicate: PredicateNode, cases: GeneratedCases): HarnessResult;
|
package/dist/synth/harness.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { evaluate } from "./evaluate.js";
|
|
2
|
-
/** Evaluate the intended call and every generated denial without side effects.
|
|
3
|
-
* Asserts both the boolean decision AND, for deny cases that carry an
|
|
4
|
-
* `expectedReason`, the concrete reason the TS evaluator emits. The reason
|
|
5
|
-
* is what the Rust interpreter returns; the cross-layer conformance test in
|
|
6
|
-
* `contracts/policy-interpreter/tests/conformance` mirrors the same contract
|
|
7
|
-
* for the verifyLive path. */
|
|
8
|
-
export function runHarness(predicate, cases) {
|
|
9
|
-
const failures = [];
|
|
10
|
-
const permitResult = evaluate(predicate, cases.permit);
|
|
11
|
-
if (!permitResult.permit) {
|
|
12
|
-
failures.push({
|
|
13
|
-
dimension: 'PERMIT_CASE_FAILED',
|
|
14
|
-
expected: 'permit',
|
|
15
|
-
got: 'deny',
|
|
16
|
-
reason: permitResult.reason,
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
for (const deny of cases.denies) {
|
|
20
|
-
const result = evaluate(predicate, deny.ctx);
|
|
21
|
-
if (result.permit) {
|
|
22
|
-
failures.push({
|
|
23
|
-
dimension: deny.dimension,
|
|
24
|
-
expected: 'deny',
|
|
25
|
-
got: 'permit',
|
|
26
|
-
reason: 'DENY_CASE_FAILURE',
|
|
27
|
-
});
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
// Reason-code assertion (cross-layer contract). When a deny case carries
|
|
31
|
-
// an expected reason, the TS evaluator MUST emit that exact string; a
|
|
32
|
-
// divergence means the TS model and the Rust interpreter disagree on
|
|
33
|
-
// what the user sees in CI - the harness fails so the diff is visible
|
|
34
|
-
// here, not at runtime in production.
|
|
35
|
-
if (deny.expectedReason !== undefined && result.reason !== deny.expectedReason) {
|
|
36
|
-
failures.push({
|
|
37
|
-
dimension: deny.dimension,
|
|
38
|
-
expected: 'deny',
|
|
39
|
-
got: 'deny',
|
|
40
|
-
reason: 'REASON_MISMATCH',
|
|
41
|
-
expectedReason: deny.expectedReason,
|
|
42
|
-
actualReason: result.reason,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return failures.length === 0 ? { ok: true } : { ok: false, failures };
|
|
47
|
-
}
|
package/dist/synth/minimize.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { PredicateNode } from '../types.ts';
|
|
2
|
-
import type { EvalContext } from './evaluate.ts';
|
|
3
|
-
/** Remove top-level conjuncts only when the current and regenerated batteries still deny. */
|
|
4
|
-
export declare function minimize(predicate: PredicateNode, permitCtx: EvalContext, dimensions?: string[]): PredicateNode;
|
package/dist/synth/minimize.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { generateCases } from "./deny-cases.js";
|
|
2
|
-
import { runHarness } from "./harness.js";
|
|
3
|
-
/** Remove top-level conjuncts only when the current and regenerated batteries still deny. */
|
|
4
|
-
export function minimize(predicate, permitCtx, dimensions) {
|
|
5
|
-
if (predicate.op !== 'and')
|
|
6
|
-
return predicate;
|
|
7
|
-
let children = [...predicate.children];
|
|
8
|
-
let index = 0;
|
|
9
|
-
while (index < children.length) {
|
|
10
|
-
const current = { op: 'and', children };
|
|
11
|
-
const currentCases = generateCases(current, permitCtx, dimensions);
|
|
12
|
-
const candidateChildren = children.filter((_, childIndex) => childIndex !== index);
|
|
13
|
-
const candidate = { op: 'and', children: candidateChildren };
|
|
14
|
-
const candidateCases = generateCases(candidate, permitCtx, dimensions);
|
|
15
|
-
const verificationCases = {
|
|
16
|
-
permit: candidateCases.permit,
|
|
17
|
-
denies: mergeDenyCases(currentCases.denies, candidateCases.denies),
|
|
18
|
-
};
|
|
19
|
-
if (runHarness(candidate, verificationCases).ok) {
|
|
20
|
-
children = candidateChildren;
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
index += 1;
|
|
24
|
-
}
|
|
25
|
-
return { op: 'and', children };
|
|
26
|
-
}
|
|
27
|
-
function mergeDenyCases(current, candidate) {
|
|
28
|
-
const merged = [];
|
|
29
|
-
const seen = new Set();
|
|
30
|
-
for (const deny of [...current, ...candidate]) {
|
|
31
|
-
const key = `${deny.dimension}:${JSON.stringify(deny.ctx)}`;
|
|
32
|
-
if (seen.has(key))
|
|
33
|
-
continue;
|
|
34
|
-
seen.add(key);
|
|
35
|
-
merged.push(deny);
|
|
36
|
-
}
|
|
37
|
-
return merged;
|
|
38
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { RecordedTransaction } from '../types.ts';
|
|
2
|
-
import type { EvalContext } from './evaluate.ts';
|
|
3
|
-
export interface PermitContextResponses {
|
|
4
|
-
windowSeconds: number;
|
|
5
|
-
limitAmount?: string;
|
|
6
|
-
validUntilLedger: number;
|
|
7
|
-
}
|
|
8
|
-
export declare function buildPermitContext(tx: RecordedTransaction, responses: PermitContextResponses): EvalContext;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
// src/synth/permit-context.ts - build the EvalContext that represents the
|
|
2
|
-
// recorded call being replayed.
|
|
3
|
-
//
|
|
4
|
-
// Extracted from the over-permissiveness harness so the same construction
|
|
5
|
-
// feeds both the in-process battery and the on-chain replay
|
|
6
|
-
// (scripts/verify-mutations-testnet.ts). Two callers building this
|
|
7
|
-
// separately would be two chances to disagree about what "the recorded
|
|
8
|
-
// call" means, which is the one thing both must share.
|
|
9
|
-
import { cloneScVal } from "./deny-cases.js";
|
|
10
|
-
export function buildPermitContext(tx, responses) {
|
|
11
|
-
const amountByToken = {};
|
|
12
|
-
const totals = new Map();
|
|
13
|
-
for (const m of tx.tokenMovements) {
|
|
14
|
-
const current = totals.get(m.token) ?? 0n;
|
|
15
|
-
totals.set(m.token, current + BigInt(m.amount));
|
|
16
|
-
}
|
|
17
|
-
for (const [token, total] of totals) {
|
|
18
|
-
amountByToken[token] = total.toString();
|
|
19
|
-
}
|
|
20
|
-
const topLevel = tx.invocations[0];
|
|
21
|
-
const scopeContract = topLevel?.contract ?? '';
|
|
22
|
-
const ctx = {
|
|
23
|
-
contract: scopeContract,
|
|
24
|
-
fn: topLevel?.fn ?? '',
|
|
25
|
-
args: (topLevel?.args ?? []).map(cloneScVal),
|
|
26
|
-
atLedger: tx.ledgerSequence,
|
|
27
|
-
nowSeconds: tx.fetchedAt,
|
|
28
|
-
amountByToken,
|
|
29
|
-
windowSpentByToken: {},
|
|
30
|
-
};
|
|
31
|
-
if (responses.validUntilLedger !== undefined) {
|
|
32
|
-
ctx.validUntilLedger = responses.validUntilLedger;
|
|
33
|
-
}
|
|
34
|
-
return ctx;
|
|
35
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { PredicateLeaf } from '../types.ts';
|
|
2
|
-
/** BigInt value of a numeric-integer literal leaf (`literal_i128` / `literal_u64`
|
|
3
|
-
* / `literal_u32`), or null for a non-numeric literal (address / symbol / bytes
|
|
4
|
-
* / vec). Callers fail closed on null. */
|
|
5
|
-
export declare function literalNumericBigInt(leaf: PredicateLeaf): bigint | null;
|