@glassbox-ai/engine 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +31 -2
- package/dist/action/acc.d.ts +48 -0
- package/dist/action/acc.js +117 -0
- package/dist/action/inline.d.ts +14 -0
- package/dist/action/inline.js +54 -0
- package/dist/action/paths.d.ts +71 -0
- package/dist/action/paths.js +398 -0
- package/dist/action/powershell.d.ts +9 -0
- package/dist/action/powershell.js +848 -0
- package/dist/action/protected-write.d.ts +11 -0
- package/dist/action/protected-write.js +113 -0
- package/dist/action/schedule.d.ts +43 -0
- package/dist/action/schedule.js +213 -0
- package/dist/action/shell/analyze.d.ts +28 -0
- package/dist/action/shell/analyze.js +2030 -0
- package/dist/action/shell/parse.d.ts +74 -0
- package/dist/action/shell/parse.js +1037 -0
- package/dist/action/shell/view.d.ts +6 -0
- package/dist/action/shell/view.js +89 -0
- package/dist/action/sql.d.ts +3 -0
- package/dist/action/sql.js +259 -0
- package/dist/action/tools.d.ts +20 -0
- package/dist/action/tools.js +240 -0
- package/dist/action/upload.d.ts +17 -0
- package/dist/action/upload.js +72 -0
- package/dist/analysis/fields.d.ts +14 -0
- package/dist/analysis/fields.js +136 -0
- package/dist/analysis/findings.d.ts +25 -0
- package/dist/analysis/findings.js +82 -0
- package/dist/analysis/gate.d.ts +16 -0
- package/dist/analysis/gate.js +660 -0
- package/dist/analysis/manifest.d.ts +3 -0
- package/dist/analysis/manifest.js +369 -0
- package/dist/analysis/match.d.ts +54 -0
- package/dist/analysis/match.js +352 -0
- package/dist/analysis/scan.d.ts +17 -0
- package/dist/analysis/scan.js +97 -0
- package/dist/canon/confusables.generated.d.ts +3 -0
- package/dist/canon/confusables.generated.js +6 -0
- package/dist/canon/decode.d.ts +11 -0
- package/dist/canon/decode.js +147 -0
- package/dist/canon/views.d.ts +36 -0
- package/dist/canon/views.js +283 -0
- package/dist/evidence/evidence.d.ts +12 -0
- package/dist/evidence/evidence.js +149 -0
- package/dist/fingerprint/diff.d.ts +3 -0
- package/dist/fingerprint/diff.js +273 -0
- package/dist/fingerprint/fingerprint.d.ts +4 -0
- package/dist/fingerprint/fingerprint.js +77 -0
- package/dist/grade/score.d.ts +19 -0
- package/dist/grade/score.js +89 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +148 -0
- package/dist/intake/json.d.ts +17 -0
- package/dist/intake/json.js +284 -0
- package/dist/intake/snapshot.d.ts +14 -0
- package/dist/intake/snapshot.js +130 -0
- package/dist/limits.d.ts +4 -0
- package/dist/limits.js +30 -0
- package/dist/policy/decide.d.ts +26 -0
- package/dist/policy/decide.js +74 -0
- package/dist/rules/ids.d.ts +2 -0
- package/dist/rules/ids.js +99 -0
- package/dist/ruleset/baseline-tests.generated.d.ts +6 -0
- package/dist/ruleset/baseline-tests.generated.js +2935 -0
- package/dist/ruleset/baseline.d.ts +3 -0
- package/dist/ruleset/baseline.generated.d.ts +3 -0
- package/dist/ruleset/baseline.generated.js +5 -0
- package/dist/ruleset/baseline.js +44 -0
- package/dist/ruleset/fakes.d.ts +3 -0
- package/dist/ruleset/fakes.js +26 -0
- package/dist/ruleset/load.d.ts +14 -0
- package/dist/ruleset/load.js +173 -0
- package/dist/ruleset/schema.d.ts +120 -0
- package/dist/ruleset/schema.js +54 -0
- package/dist/ruleset/selftest.d.ts +18 -0
- package/dist/ruleset/selftest.js +42 -0
- package/dist/ruleset-entry.d.ts +5 -0
- package/dist/ruleset-entry.js +7 -0
- package/dist/secrets/detect.d.ts +17 -0
- package/dist/secrets/detect.js +236 -0
- package/dist/secrets/redact.d.ts +19 -0
- package/dist/secrets/redact.js +116 -0
- package/dist/types.d.ts +434 -0
- package/dist/types.js +10 -0
- package/dist/util/hash.d.ts +4 -0
- package/dist/util/hash.js +16 -0
- package/dist/util/initclock.d.ts +3 -0
- package/dist/util/initclock.js +17 -0
- package/dist/util/jcs.d.ts +4 -0
- package/dist/util/jcs.js +91 -0
- package/dist/util/misc.d.ts +17 -0
- package/dist/util/misc.js +77 -0
- package/dist/util/re.d.ts +14 -0
- package/dist/util/re.js +47 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +55 -3
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// C1–C2 (engine-spec §5.2): strict, bounded, iterative JSON parser.
|
|
2
|
+
// - builds null-prototype objects; __proto__/constructor/prototype are plain data (CP-PARSE-003)
|
|
3
|
+
// - duplicate keys are kept as "key~dupN" (CP-PARSE-001)
|
|
4
|
+
// - lone surrogates (escaped or raw) are flagged (CP-PARSE-005)
|
|
5
|
+
// - invalid UTF-8 bytes are flagged (CP-PARSE-002); a BOM is stripped
|
|
6
|
+
// - depth and node limits stop the parse (CP-LIMIT-002 / CP-LIMIT-003)
|
|
7
|
+
// Never recurses on input structure and never throws for bad input.
|
|
8
|
+
const PROTO_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
9
|
+
function decode(input, issues) {
|
|
10
|
+
if (typeof input === "string")
|
|
11
|
+
return input;
|
|
12
|
+
try {
|
|
13
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(input);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
issues.push({ code: "CP-PARSE-002" });
|
|
17
|
+
return new TextDecoder("utf-8", { fatal: false }).decode(input);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
class Fail extends Error {
|
|
21
|
+
code;
|
|
22
|
+
constructor(code) {
|
|
23
|
+
super(code);
|
|
24
|
+
this.code = code;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function parseJsonStrict(input, opts = {}) {
|
|
28
|
+
const issues = [];
|
|
29
|
+
const maxDepth = opts.maxDepth ?? 32;
|
|
30
|
+
const maxNodes = opts.maxNodes ?? 100_000;
|
|
31
|
+
let text = decode(input, issues);
|
|
32
|
+
if (text.charCodeAt(0) === 0xfeff)
|
|
33
|
+
text = text.slice(1);
|
|
34
|
+
let i = 0;
|
|
35
|
+
let nodes = 0;
|
|
36
|
+
const stack = [];
|
|
37
|
+
let root = undefined;
|
|
38
|
+
let haveRoot = false;
|
|
39
|
+
const ws = () => {
|
|
40
|
+
while (i < text.length) {
|
|
41
|
+
const c = text.charCodeAt(i);
|
|
42
|
+
if (c === 0x20 || c === 0x09 || c === 0x0a || c === 0x0d)
|
|
43
|
+
i++;
|
|
44
|
+
else
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const readString = (pointer) => {
|
|
49
|
+
// text[i] === '"'
|
|
50
|
+
i++;
|
|
51
|
+
let out = "";
|
|
52
|
+
let start = i;
|
|
53
|
+
while (true) {
|
|
54
|
+
if (i >= text.length)
|
|
55
|
+
throw new Fail("CP-PARSE-008");
|
|
56
|
+
const c = text.charCodeAt(i);
|
|
57
|
+
if (c === 0x22) {
|
|
58
|
+
out += text.slice(start, i);
|
|
59
|
+
i++;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
if (c < 0x20)
|
|
63
|
+
throw new Fail("CP-PARSE-008");
|
|
64
|
+
if (c >= 0xd800 && c <= 0xdfff) {
|
|
65
|
+
const isHigh = c <= 0xdbff;
|
|
66
|
+
const next = text.charCodeAt(i + 1);
|
|
67
|
+
if (!(isHigh && next >= 0xdc00 && next <= 0xdfff))
|
|
68
|
+
issues.push({ code: "CP-PARSE-005", pointer });
|
|
69
|
+
else {
|
|
70
|
+
i += 2;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (c === 0x5c) {
|
|
75
|
+
out += text.slice(start, i);
|
|
76
|
+
const e = text[i + 1];
|
|
77
|
+
i += 2;
|
|
78
|
+
switch (e) {
|
|
79
|
+
case '"':
|
|
80
|
+
out += '"';
|
|
81
|
+
break;
|
|
82
|
+
case "\\":
|
|
83
|
+
out += "\\";
|
|
84
|
+
break;
|
|
85
|
+
case "/":
|
|
86
|
+
out += "/";
|
|
87
|
+
break;
|
|
88
|
+
case "b":
|
|
89
|
+
out += "\b";
|
|
90
|
+
break;
|
|
91
|
+
case "f":
|
|
92
|
+
out += "\f";
|
|
93
|
+
break;
|
|
94
|
+
case "n":
|
|
95
|
+
out += "\n";
|
|
96
|
+
break;
|
|
97
|
+
case "r":
|
|
98
|
+
out += "\r";
|
|
99
|
+
break;
|
|
100
|
+
case "t":
|
|
101
|
+
out += "\t";
|
|
102
|
+
break;
|
|
103
|
+
case "u": {
|
|
104
|
+
const hex = text.slice(i, i + 4);
|
|
105
|
+
if (!/^[0-9a-fA-F]{4}$/.test(hex))
|
|
106
|
+
throw new Fail("CP-PARSE-008");
|
|
107
|
+
const cu = parseInt(hex, 16);
|
|
108
|
+
i += 4;
|
|
109
|
+
if (cu >= 0xd800 && cu <= 0xdbff) {
|
|
110
|
+
const lo = text.slice(i, i + 6);
|
|
111
|
+
if (/^\\u[dD][c-fC-F][0-9a-fA-F]{2}$/.test(lo)) {
|
|
112
|
+
out += String.fromCharCode(cu, parseInt(lo.slice(2), 16));
|
|
113
|
+
i += 6;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
issues.push({ code: "CP-PARSE-005", pointer });
|
|
117
|
+
}
|
|
118
|
+
else if (cu >= 0xdc00 && cu <= 0xdfff) {
|
|
119
|
+
issues.push({ code: "CP-PARSE-005", pointer });
|
|
120
|
+
}
|
|
121
|
+
out += String.fromCharCode(cu);
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
default: throw new Fail("CP-PARSE-008");
|
|
125
|
+
}
|
|
126
|
+
start = i;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
i++;
|
|
130
|
+
}
|
|
131
|
+
return out;
|
|
132
|
+
};
|
|
133
|
+
const readNumber = () => {
|
|
134
|
+
const m = /^-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/.exec(text.slice(i, i + 400));
|
|
135
|
+
if (!m)
|
|
136
|
+
throw new Fail("CP-PARSE-008");
|
|
137
|
+
i += m[0].length;
|
|
138
|
+
const n = Number(m[0]);
|
|
139
|
+
return Number.isFinite(n) ? n : 0;
|
|
140
|
+
};
|
|
141
|
+
const ptrSeg = (k) => String(k).replace(/~/g, "~0").replace(/\//g, "~1");
|
|
142
|
+
const attach = (v) => {
|
|
143
|
+
const top = stack[stack.length - 1];
|
|
144
|
+
if (!top) {
|
|
145
|
+
root = v;
|
|
146
|
+
haveRoot = true;
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (top.kind === "array")
|
|
150
|
+
top.value.push(v);
|
|
151
|
+
else {
|
|
152
|
+
const key = top.key;
|
|
153
|
+
let k = key;
|
|
154
|
+
const n = top.seen.get(key) ?? 0;
|
|
155
|
+
if (n > 0) {
|
|
156
|
+
k = `${key}~dup${n}`;
|
|
157
|
+
issues.push({ code: "CP-PARSE-001", pointer: `${top.pointer}/${ptrSeg(key)}` });
|
|
158
|
+
}
|
|
159
|
+
top.seen.set(key, n + 1);
|
|
160
|
+
Object.defineProperty(top.value, k, { value: v, enumerable: true, writable: true, configurable: true });
|
|
161
|
+
top.key = null;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const childPointer = () => {
|
|
165
|
+
const top = stack[stack.length - 1];
|
|
166
|
+
if (!top)
|
|
167
|
+
return "";
|
|
168
|
+
if (top.kind === "array")
|
|
169
|
+
return `${top.pointer}/${top.value.length}`;
|
|
170
|
+
return `${top.pointer}/${ptrSeg(top.key ?? "")}`;
|
|
171
|
+
};
|
|
172
|
+
try {
|
|
173
|
+
ws();
|
|
174
|
+
while (!haveRoot) {
|
|
175
|
+
ws();
|
|
176
|
+
const top = stack[stack.length - 1];
|
|
177
|
+
// Expecting a key?
|
|
178
|
+
if (top && top.kind === "object" && top.key === null) {
|
|
179
|
+
if (text[i] === "}") {
|
|
180
|
+
i++;
|
|
181
|
+
stack.pop();
|
|
182
|
+
attach(top.value);
|
|
183
|
+
if (stack.length === 0)
|
|
184
|
+
break;
|
|
185
|
+
afterValue();
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (text[i] !== '"')
|
|
189
|
+
throw new Fail("CP-PARSE-008");
|
|
190
|
+
const key = readString(top.pointer);
|
|
191
|
+
if (PROTO_KEYS.has(key))
|
|
192
|
+
issues.push({ code: "CP-PARSE-003", pointer: `${top.pointer}/${ptrSeg(key)}` });
|
|
193
|
+
ws();
|
|
194
|
+
if (text[i] !== ":")
|
|
195
|
+
throw new Fail("CP-PARSE-008");
|
|
196
|
+
i++;
|
|
197
|
+
top.key = key;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (top && top.kind === "array" && text[i] === "]" && top.value.length === 0) {
|
|
201
|
+
i++;
|
|
202
|
+
stack.pop();
|
|
203
|
+
attach(top.value);
|
|
204
|
+
if (stack.length === 0)
|
|
205
|
+
break;
|
|
206
|
+
afterValue();
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
// A value.
|
|
210
|
+
if (++nodes > maxNodes)
|
|
211
|
+
throw new Fail("CP-LIMIT-003");
|
|
212
|
+
const c = text[i];
|
|
213
|
+
const pointer = childPointer();
|
|
214
|
+
if (c === "{" || c === "[") {
|
|
215
|
+
if (stack.length >= maxDepth)
|
|
216
|
+
throw new Fail("CP-LIMIT-002");
|
|
217
|
+
i++;
|
|
218
|
+
if (c === "{")
|
|
219
|
+
stack.push({ kind: "object", value: Object.create(null), pointer, key: null, seen: new Map() });
|
|
220
|
+
else
|
|
221
|
+
stack.push({ kind: "array", value: [], pointer });
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
let v;
|
|
225
|
+
if (c === '"')
|
|
226
|
+
v = readString(pointer);
|
|
227
|
+
else if (c === "t" && text.startsWith("true", i)) {
|
|
228
|
+
v = true;
|
|
229
|
+
i += 4;
|
|
230
|
+
}
|
|
231
|
+
else if (c === "f" && text.startsWith("false", i)) {
|
|
232
|
+
v = false;
|
|
233
|
+
i += 5;
|
|
234
|
+
}
|
|
235
|
+
else if (c === "n" && text.startsWith("null", i)) {
|
|
236
|
+
v = null;
|
|
237
|
+
i += 4;
|
|
238
|
+
}
|
|
239
|
+
else if (c === "-" || (c !== undefined && c >= "0" && c <= "9"))
|
|
240
|
+
v = readNumber();
|
|
241
|
+
else
|
|
242
|
+
throw new Fail("CP-PARSE-008");
|
|
243
|
+
attach(v);
|
|
244
|
+
if (stack.length === 0)
|
|
245
|
+
break;
|
|
246
|
+
afterValue();
|
|
247
|
+
}
|
|
248
|
+
ws();
|
|
249
|
+
if (i !== text.length || !haveRoot)
|
|
250
|
+
throw new Fail("CP-PARSE-008");
|
|
251
|
+
return { ok: true, value: root, issues, degraded: false };
|
|
252
|
+
}
|
|
253
|
+
catch (e) {
|
|
254
|
+
const code = e instanceof Fail ? e.code : "CP-PARSE-008";
|
|
255
|
+
issues.push({ code });
|
|
256
|
+
return { ok: false, value: undefined, issues, degraded: true };
|
|
257
|
+
}
|
|
258
|
+
function afterValue() {
|
|
259
|
+
ws();
|
|
260
|
+
const top = stack[stack.length - 1];
|
|
261
|
+
if (!top)
|
|
262
|
+
return;
|
|
263
|
+
const c = text[i];
|
|
264
|
+
if (c === ",") {
|
|
265
|
+
i++;
|
|
266
|
+
ws();
|
|
267
|
+
if (top.kind === "array" && text[i] === "]")
|
|
268
|
+
throw new Fail("CP-PARSE-008");
|
|
269
|
+
if (top.kind === "object" && text[i] === "}")
|
|
270
|
+
throw new Fail("CP-PARSE-008");
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if ((top.kind === "array" && c === "]") || (top.kind === "object" && c === "}")) {
|
|
274
|
+
i++;
|
|
275
|
+
stack.pop();
|
|
276
|
+
attach(top.value);
|
|
277
|
+
if (stack.length > 0)
|
|
278
|
+
afterValue();
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
throw new Fail("CP-PARSE-008");
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
//# sourceMappingURL=json.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type IntakeIssue } from "./json.js";
|
|
2
|
+
export interface SnapshotResult {
|
|
3
|
+
value: unknown;
|
|
4
|
+
issues: IntakeIssue[];
|
|
5
|
+
degraded: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface SnapshotOptions {
|
|
8
|
+
maxDepth?: number;
|
|
9
|
+
maxNodes?: number;
|
|
10
|
+
maxStringBytes?: number;
|
|
11
|
+
maxBytes?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function snapshot(input: unknown, opts?: SnapshotOptions): SnapshotResult;
|
|
14
|
+
//# sourceMappingURL=snapshot.d.ts.map
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// C2.4 (engine-spec §5.2): snapshot an object input into frozen null-prototype data.
|
|
2
|
+
// Reads only own enumerable data properties via descriptors (getters are never invoked),
|
|
3
|
+
// detects cycles, drops non-JSON values, survives hostile Proxies, bounded and iterative.
|
|
4
|
+
import { parseJsonStrict } from "./json.js";
|
|
5
|
+
import { deepFreeze } from "../util/misc.js";
|
|
6
|
+
const ptrSeg = (k) => String(k).replace(/~/g, "~0").replace(/\//g, "~1");
|
|
7
|
+
export function snapshot(input, opts = {}) {
|
|
8
|
+
const issues = [];
|
|
9
|
+
const maxDepth = opts.maxDepth ?? 32;
|
|
10
|
+
const maxNodes = opts.maxNodes ?? 100_000;
|
|
11
|
+
let degraded = false;
|
|
12
|
+
let nodes = 0;
|
|
13
|
+
let bytes = 0;
|
|
14
|
+
const maxBytes = opts.maxBytes ?? Number.POSITIVE_INFINITY;
|
|
15
|
+
const onPath = new Set();
|
|
16
|
+
let root = null;
|
|
17
|
+
const jobs = [{ src: input, set: (v) => { root = v; }, depth: 0, pointer: "" }];
|
|
18
|
+
while (jobs.length > 0) {
|
|
19
|
+
const job = jobs.pop();
|
|
20
|
+
if (job.exit) {
|
|
21
|
+
onPath.delete(job.exit);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const src = job.src;
|
|
25
|
+
if (++nodes > maxNodes) {
|
|
26
|
+
issues.push({ code: "CP-LIMIT-003", pointer: job.pointer });
|
|
27
|
+
degraded = true;
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
if (typeof src === "string") {
|
|
32
|
+
bytes += src.length;
|
|
33
|
+
if (bytes > maxBytes) {
|
|
34
|
+
issues.push({ code: "CP-LIMIT-001", pointer: job.pointer });
|
|
35
|
+
degraded = true;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
job.set(src);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (src === null || typeof src === "boolean") {
|
|
42
|
+
job.set(src);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (typeof src === "number") {
|
|
46
|
+
job.set(Number.isFinite(src) ? src : null);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (typeof src !== "object") {
|
|
50
|
+
issues.push({ code: "CP-PARSE-009", pointer: job.pointer });
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (src instanceof Uint8Array) {
|
|
54
|
+
const r = parseJsonStrict(src, opts);
|
|
55
|
+
issues.push(...r.issues);
|
|
56
|
+
job.set(r.ok ? r.value : null);
|
|
57
|
+
if (!r.ok)
|
|
58
|
+
degraded = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (onPath.has(src)) {
|
|
62
|
+
issues.push({ code: "CP-PARSE-007", pointer: job.pointer });
|
|
63
|
+
job.set(null);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (job.depth >= maxDepth) {
|
|
67
|
+
issues.push({ code: "CP-LIMIT-002", pointer: job.pointer });
|
|
68
|
+
degraded = true;
|
|
69
|
+
job.set(null);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
onPath.add(src);
|
|
73
|
+
jobs.push({ src: null, set: () => undefined, depth: 0, pointer: "", exit: src });
|
|
74
|
+
const isArray = Array.isArray(src);
|
|
75
|
+
const out = isArray ? [] : Object.create(null);
|
|
76
|
+
job.set(out);
|
|
77
|
+
const keys = Object.keys(src);
|
|
78
|
+
if (isArray) {
|
|
79
|
+
const len = Object.getOwnPropertyDescriptor(src, "length")?.value ?? 0;
|
|
80
|
+
for (let i = 0; i < Math.min(len, maxNodes); i++) {
|
|
81
|
+
const d = Object.getOwnPropertyDescriptor(src, String(i));
|
|
82
|
+
if (!d) {
|
|
83
|
+
out.push(null);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (!("value" in d)) {
|
|
87
|
+
issues.push({ code: "CP-PARSE-006", pointer: `${job.pointer}/${i}` });
|
|
88
|
+
out.push(null);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const idx = out.length;
|
|
92
|
+
out.push(null);
|
|
93
|
+
jobs.push({ src: d.value, set: (v) => { out[idx] = v; }, depth: job.depth + 1, pointer: `${job.pointer}/${i}` });
|
|
94
|
+
}
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
// Push in reverse so keys are processed (and assigned) in order.
|
|
98
|
+
const pending = [];
|
|
99
|
+
for (const k of keys) {
|
|
100
|
+
const d = Object.getOwnPropertyDescriptor(src, k);
|
|
101
|
+
if (!d || !d.enumerable)
|
|
102
|
+
continue;
|
|
103
|
+
const pointer = `${job.pointer}/${ptrSeg(k)}`;
|
|
104
|
+
if (!("value" in d)) {
|
|
105
|
+
issues.push({ code: "CP-PARSE-006", pointer });
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const v = d.value;
|
|
109
|
+
if (v === undefined || typeof v === "function" || typeof v === "symbol" || typeof v === "bigint") {
|
|
110
|
+
issues.push({ code: "CP-PARSE-009", pointer });
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (k === "__proto__" || k === "constructor" || k === "prototype")
|
|
114
|
+
issues.push({ code: "CP-PARSE-003", pointer });
|
|
115
|
+
Object.defineProperty(out, k, { value: null, enumerable: true, writable: true, configurable: true });
|
|
116
|
+
pending.push({ src: v, set: (x) => { out[k] = x; }, depth: job.depth + 1, pointer });
|
|
117
|
+
}
|
|
118
|
+
for (let i = pending.length - 1; i >= 0; i--)
|
|
119
|
+
jobs.push(pending[i]);
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
// A Proxy trap or exotic object threw. Record and continue with what we have.
|
|
123
|
+
degraded = true;
|
|
124
|
+
issues.push({ code: "CP-PARSE-006", pointer: job.pointer });
|
|
125
|
+
job.set(null);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return { value: deepFreeze(root), issues, degraded };
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=snapshot.js.map
|
package/dist/limits.d.ts
ADDED
package/dist/limits.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const DEFAULT_LIMITS = Object.freeze({
|
|
2
|
+
maxInputBytes: 1024 * 1024,
|
|
3
|
+
maxDepth: 32,
|
|
4
|
+
maxNodes: 100_000,
|
|
5
|
+
maxTools: 500,
|
|
6
|
+
maxDescriptionBytes: 16 * 1024,
|
|
7
|
+
maxFieldScanBytes: 64 * 1024,
|
|
8
|
+
maxCommandBytes: 16 * 1024,
|
|
9
|
+
maxSqlBytes: 64 * 1024,
|
|
10
|
+
maxDecodeDepth: 2,
|
|
11
|
+
maxDecodedBytes: 256 * 1024,
|
|
12
|
+
maxFindings: 200,
|
|
13
|
+
maxStringBytes: 1024 * 1024,
|
|
14
|
+
timeBudgetMs: 20,
|
|
15
|
+
hardStopMs: 100,
|
|
16
|
+
signalTtlCalls: 50,
|
|
17
|
+
minConfidenceForVerdict: 0.3,
|
|
18
|
+
});
|
|
19
|
+
export function resolveLimits(over) {
|
|
20
|
+
const out = { ...DEFAULT_LIMITS };
|
|
21
|
+
if (over === null || typeof over !== "object")
|
|
22
|
+
return out;
|
|
23
|
+
for (const k of Object.keys(DEFAULT_LIMITS)) {
|
|
24
|
+
const v = over[k];
|
|
25
|
+
if (typeof v === "number" && Number.isFinite(v) && v >= 0)
|
|
26
|
+
out[k] = v;
|
|
27
|
+
}
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=limits.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ActionRisk, Finding, LoadedRuleset, RememberedRule, UserPolicy, Verdict } from "../types.js";
|
|
2
|
+
export declare const vmax: (a: Verdict, b: Verdict) => Verdict;
|
|
3
|
+
export interface DecideInput {
|
|
4
|
+
kind: "call" | "result" | "server-request";
|
|
5
|
+
risk: ActionRisk;
|
|
6
|
+
findings: readonly Finding[];
|
|
7
|
+
degraded: boolean;
|
|
8
|
+
protectionDegraded: boolean;
|
|
9
|
+
ruleset: LoadedRuleset;
|
|
10
|
+
policy: UserPolicy;
|
|
11
|
+
minConfidence: number;
|
|
12
|
+
serverId: string;
|
|
13
|
+
toolName: string;
|
|
14
|
+
remembered: readonly RememberedRule[];
|
|
15
|
+
/** checkServerRequest: verdict from the kind's policy (R13) and whether it's locked (R14). */
|
|
16
|
+
requestVerdict?: {
|
|
17
|
+
verdict: Verdict;
|
|
18
|
+
locked: boolean;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export interface DecideOutput {
|
|
22
|
+
verdict: Verdict;
|
|
23
|
+
verdictBeforeMode?: Verdict;
|
|
24
|
+
}
|
|
25
|
+
export declare function decide(d: DecideInput): DecideOutput;
|
|
26
|
+
//# sourceMappingURL=decide.d.ts.map
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
const ORDER = ["allow", "warn", "ask", "deny"];
|
|
2
|
+
export const vmax = (a, b) => (ORDER.indexOf(a) >= ORDER.indexOf(b) ? a : b);
|
|
3
|
+
const SEV = ["info", "low", "medium", "high", "critical"];
|
|
4
|
+
/**
|
|
5
|
+
* R3 by mode (spec §6.2, rev 5). Irreversible-or-public tags ask in every mode, so in
|
|
6
|
+
* autopilot they become deny through R15 unless a tag override or a remembered rule lifts
|
|
7
|
+
* the ask first. Other high-risk tags: strict asks, balanced and autopilot warn.
|
|
8
|
+
*/
|
|
9
|
+
function modeVerdict(tag, mode, irreversible) {
|
|
10
|
+
if (tag !== null && irreversible.has(tag))
|
|
11
|
+
return "ask";
|
|
12
|
+
return mode === "strict" ? "ask" : "warn";
|
|
13
|
+
}
|
|
14
|
+
export function decide(d) {
|
|
15
|
+
const mode = d.policy.mode;
|
|
16
|
+
let v = "allow";
|
|
17
|
+
const irreversible = new Set(d.ruleset.irreversibleTags);
|
|
18
|
+
// R3 (calls only): high-risk action, by mode, then tagOverrides, then remembered rules.
|
|
19
|
+
if (d.kind === "call" && d.risk.level === "high-risk") {
|
|
20
|
+
const tags = d.risk.tags.filter((t) => t !== "self-protection");
|
|
21
|
+
let r3 = "allow";
|
|
22
|
+
const askTags = [];
|
|
23
|
+
const list = tags.length > 0 ? tags : [null];
|
|
24
|
+
for (const t of list) {
|
|
25
|
+
let tv = modeVerdict(t, mode, irreversible);
|
|
26
|
+
const o = t !== null ? d.policy.tagOverrides?.[t] : undefined;
|
|
27
|
+
if (o === "allow" || o === "ask" || o === "deny")
|
|
28
|
+
tv = o;
|
|
29
|
+
if (tv === "ask" && t !== null)
|
|
30
|
+
askTags.push(t);
|
|
31
|
+
r3 = vmax(r3, tv);
|
|
32
|
+
}
|
|
33
|
+
if (r3 === "ask" && askTags.length > 0 && askTags.every((t) => d.remembered.some((m) => m.serverId === d.serverId && m.toolName === d.toolName && m.actionTag === t)))
|
|
34
|
+
r3 = "allow";
|
|
35
|
+
v = vmax(v, r3);
|
|
36
|
+
}
|
|
37
|
+
// R4–R7: findings (confidence ≥ threshold).
|
|
38
|
+
let top = -1;
|
|
39
|
+
for (const f of d.findings)
|
|
40
|
+
if (f.confidence >= d.minConfidence)
|
|
41
|
+
top = Math.max(top, SEV.indexOf(f.severity));
|
|
42
|
+
if (top === 2)
|
|
43
|
+
v = vmax(v, mode === "strict" ? "ask" : "warn");
|
|
44
|
+
if (top === 3)
|
|
45
|
+
v = vmax(v, "ask");
|
|
46
|
+
if (top === 4)
|
|
47
|
+
v = vmax(v, "deny"); // R7 🔒
|
|
48
|
+
// R13/R14: server requests.
|
|
49
|
+
if (d.requestVerdict)
|
|
50
|
+
v = vmax(v, d.requestVerdict.verdict);
|
|
51
|
+
if (d.kind === "call") {
|
|
52
|
+
if (d.risk.selfProtection)
|
|
53
|
+
v = vmax(v, "deny"); // R8 🔒
|
|
54
|
+
if (d.risk.unanalyzable)
|
|
55
|
+
v = vmax(v, "ask"); // R9 🔒
|
|
56
|
+
if (d.degraded && d.risk.level === "high-risk")
|
|
57
|
+
v = vmax(v, "deny"); // R10 🔒
|
|
58
|
+
if (d.degraded && d.risk.level !== "high-risk")
|
|
59
|
+
v = vmax(v, "warn"); // R11 🔒
|
|
60
|
+
if (d.protectionDegraded && d.risk.level === "high-risk")
|
|
61
|
+
v = vmax(v, "ask"); // R16 🔒
|
|
62
|
+
}
|
|
63
|
+
else if (d.degraded) {
|
|
64
|
+
v = vmax(v, "warn"); // R11 (results and requests: R10 → R11)
|
|
65
|
+
}
|
|
66
|
+
// R17 🔒: no usable rules at all.
|
|
67
|
+
if (d.ruleset.status === "unavailable")
|
|
68
|
+
v = vmax(v, d.risk.level === "read" ? "warn" : "deny");
|
|
69
|
+
// R15 🔒: nobody is there to answer in autopilot.
|
|
70
|
+
if (mode === "autopilot" && v === "ask")
|
|
71
|
+
return { verdict: "deny", verdictBeforeMode: "ask" };
|
|
72
|
+
return { verdict: v };
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=decide.js.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Rule ids the engine emits from code (structural checks). Every one must exist in the
|
|
2
|
+
// baseline ruleset (tested). Generated from packages/rules-baseline/rules/structural.json.
|
|
3
|
+
export const ENGINE_RULE_IDS = [
|
|
4
|
+
"CP-POISON-020",
|
|
5
|
+
"CP-POISON-021",
|
|
6
|
+
"CP-POISON-030",
|
|
7
|
+
"CP-HIDDEN-001",
|
|
8
|
+
"CP-HIDDEN-002",
|
|
9
|
+
"CP-HIDDEN-003",
|
|
10
|
+
"CP-HIDDEN-004",
|
|
11
|
+
"CP-HIDDEN-005",
|
|
12
|
+
"CP-HIDDEN-006",
|
|
13
|
+
"CP-HIDDEN-007",
|
|
14
|
+
"CP-HIDDEN-008",
|
|
15
|
+
"CP-HIDDEN-009",
|
|
16
|
+
"CP-HIDDEN-010",
|
|
17
|
+
"CP-SHADOW-001",
|
|
18
|
+
"CP-SHADOW-002",
|
|
19
|
+
"CP-SHADOW-010",
|
|
20
|
+
"CP-SHADOW-011",
|
|
21
|
+
"CP-CAP-001",
|
|
22
|
+
"CP-CAP-002",
|
|
23
|
+
"CP-HYGIENE-001",
|
|
24
|
+
"CP-HYGIENE-002",
|
|
25
|
+
"CP-HYGIENE-003",
|
|
26
|
+
"CP-HYGIENE-004",
|
|
27
|
+
"CP-PARSE-001",
|
|
28
|
+
"CP-PARSE-002",
|
|
29
|
+
"CP-PARSE-003",
|
|
30
|
+
"CP-PARSE-004",
|
|
31
|
+
"CP-PARSE-005",
|
|
32
|
+
"CP-PARSE-006",
|
|
33
|
+
"CP-PARSE-007",
|
|
34
|
+
"CP-PARSE-008",
|
|
35
|
+
"CP-PARSE-009",
|
|
36
|
+
"CP-PARSE-010",
|
|
37
|
+
"CP-LIMIT-001",
|
|
38
|
+
"CP-LIMIT-002",
|
|
39
|
+
"CP-LIMIT-003",
|
|
40
|
+
"CP-LIMIT-004",
|
|
41
|
+
"CP-LIMIT-005",
|
|
42
|
+
"CP-LIMIT-006",
|
|
43
|
+
"CP-LIMIT-007",
|
|
44
|
+
"CP-LIMIT-008",
|
|
45
|
+
"CP-LIMIT-010",
|
|
46
|
+
"CP-SHELL-001",
|
|
47
|
+
"CP-SHELL-010",
|
|
48
|
+
"CP-SHELL-011",
|
|
49
|
+
"CP-ACTION-001",
|
|
50
|
+
"CP-ACTION-030",
|
|
51
|
+
"CP-ACTION-020",
|
|
52
|
+
"CP-ACTION-021",
|
|
53
|
+
"CP-SELF-001",
|
|
54
|
+
"CP-SELF-002",
|
|
55
|
+
"CP-SELF-003",
|
|
56
|
+
"CP-SELF-004",
|
|
57
|
+
"CP-SELF-005",
|
|
58
|
+
"CP-SELF-006",
|
|
59
|
+
"CP-SELF-007",
|
|
60
|
+
"CP-EXFIL-030",
|
|
61
|
+
"CP-EXFIL-031",
|
|
62
|
+
"CP-EXFIL-040",
|
|
63
|
+
"CP-EXFIL-032",
|
|
64
|
+
"CP-EXFIL-033",
|
|
65
|
+
"CP-EXFIL-034",
|
|
66
|
+
"CP-EXFIL-035",
|
|
67
|
+
"CP-EXFIL-036",
|
|
68
|
+
"CP-EXFIL-050",
|
|
69
|
+
"CP-INJECT-020",
|
|
70
|
+
"CP-INJECT-021",
|
|
71
|
+
"CP-FLOW-001",
|
|
72
|
+
"CP-FLOW-002",
|
|
73
|
+
"CP-FLOW-003",
|
|
74
|
+
"CP-SRVREQ-001",
|
|
75
|
+
"CP-SRVREQ-020",
|
|
76
|
+
"CP-SRVREQ-030",
|
|
77
|
+
"CP-SRVREQ-040",
|
|
78
|
+
"CP-RUGPULL-001",
|
|
79
|
+
"CP-RUGPULL-002",
|
|
80
|
+
"CP-RUGPULL-003",
|
|
81
|
+
"CP-RUGPULL-004",
|
|
82
|
+
"CP-RUGPULL-005",
|
|
83
|
+
"CP-RUGPULL-006",
|
|
84
|
+
"CP-RUGPULL-007",
|
|
85
|
+
"CP-RUGPULL-008",
|
|
86
|
+
"CP-RUGPULL-009",
|
|
87
|
+
"CP-RUGPULL-010",
|
|
88
|
+
"CP-RUGPULL-011",
|
|
89
|
+
"CP-RUGPULL-012",
|
|
90
|
+
"CP-RUGPULL-013",
|
|
91
|
+
"CP-RUGPULL-014",
|
|
92
|
+
"CP-CLOAK-001",
|
|
93
|
+
"CP-INTEGRITY-001",
|
|
94
|
+
"CP-INTEGRITY-002",
|
|
95
|
+
"CP-INTEGRITY-003",
|
|
96
|
+
"CP-INTEGRITY-004",
|
|
97
|
+
"CP-AUTH-001"
|
|
98
|
+
];
|
|
99
|
+
//# sourceMappingURL=ids.js.map
|