@glassbox-ai/engine 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +33 -2
- package/dist/action/acc.d.ts +48 -0
- package/dist/action/acc.js +117 -0
- package/dist/action/builtin-tools.d.ts +5 -0
- package/dist/action/builtin-tools.js +74 -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 +2057 -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 +305 -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 +662 -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 +26 -0
- package/dist/index.js +152 -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 +32 -0
- package/dist/policy/decide.js +105 -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 +13 -0
- package/dist/ruleset/load.js +187 -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 +455 -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 +57 -4
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// C1–C2: 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: 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,32 @@
|
|
|
1
|
+
import type { ActionRisk, ActionTag, Finding, LoadedRuleset, RememberedRule, SessionGrant, 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
|
+
/** The current project; a remembered rule applies only when its projectIdHash equals this. */
|
|
16
|
+
projectIdHash?: string | undefined;
|
|
17
|
+
sessionId: string;
|
|
18
|
+
sessionGrants: readonly SessionGrant[];
|
|
19
|
+
/** checkServerRequest: verdict from the kind's policy (R13) and whether it's locked (R14). */
|
|
20
|
+
requestVerdict?: {
|
|
21
|
+
verdict: Verdict;
|
|
22
|
+
locked: boolean;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface DecideOutput {
|
|
26
|
+
verdict: Verdict;
|
|
27
|
+
verdictBeforeMode?: Verdict;
|
|
28
|
+
}
|
|
29
|
+
/** Fixed in the engine, not in the ruleset: no ruleset can remove a tag from the hard floor. */
|
|
30
|
+
export declare const HARD_FLOOR_TAGS: readonly ActionTag[];
|
|
31
|
+
export declare function decide(d: DecideInput): DecideOutput;
|
|
32
|
+
//# sourceMappingURL=decide.d.ts.map
|
|
@@ -0,0 +1,105 @@
|
|
|
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.
|
|
6
|
+
* - Hard floor (HARD_FLOOR_TAGS, whatever the ruleset says): force push,
|
|
7
|
+
* history rewrite, wipes, payments, production deploys and publishing ask in every mode, and
|
|
8
|
+
* autopilot turns that into deny (R15). Nothing the user set up earlier lowers them: a
|
|
9
|
+
* tagOverride can only raise them to deny; remembered rules and session grants never lift them.
|
|
10
|
+
* - Other irreversible-or-public tags ask in every mode by default, but they are ordinary
|
|
11
|
+
* mode asks: a tagOverride, remembered rule or session grant can lift them.
|
|
12
|
+
* - Other high-risk tags: strict asks, balanced and autopilot warn.
|
|
13
|
+
*/
|
|
14
|
+
function modeVerdict(tag, mode, irreversible) {
|
|
15
|
+
if (tag !== null && irreversible.has(tag))
|
|
16
|
+
return "ask";
|
|
17
|
+
return mode === "strict" ? "ask" : "warn";
|
|
18
|
+
}
|
|
19
|
+
/** Fixed in the engine, not in the ruleset: no ruleset can remove a tag from the hard floor. */
|
|
20
|
+
export const HARD_FLOOR_TAGS = Object.freeze(["vcs.force-push", "vcs.history-rewrite", "fs.destroy", "payment", "deploy.prod", "publish.public"]);
|
|
21
|
+
// Private copy used for decisions: a Set can't be frozen, so the exported list is a frozen array
|
|
22
|
+
// and changing anything a caller can reach doesn't change the floor.
|
|
23
|
+
const HARD_FLOOR = new Set(HARD_FLOOR_TAGS);
|
|
24
|
+
const str = (x) => typeof x === "string" && x.length > 0;
|
|
25
|
+
/** A remembered rule (this project) or session grant (this session) covering (server, tool, tag). */
|
|
26
|
+
function lifted(d, tag) {
|
|
27
|
+
// Own string properties only: a field inherited through a prototype doesn't count.
|
|
28
|
+
const own = (m, k, want) => Object.hasOwn(m, k) && m[k] === want;
|
|
29
|
+
const match = (m, scopeKey, scope) => typeof m === "object" && m !== null && own(m, "serverId", d.serverId) && own(m, "toolName", d.toolName) && own(m, "actionTag", tag) && own(m, scopeKey, scope);
|
|
30
|
+
const project = d.projectIdHash;
|
|
31
|
+
if (str(project) && d.remembered.some((m) => match(m, "projectIdHash", project)))
|
|
32
|
+
return true;
|
|
33
|
+
return str(d.sessionId) && d.sessionGrants.some((g) => match(g, "sessionId", d.sessionId));
|
|
34
|
+
}
|
|
35
|
+
export function decide(d) {
|
|
36
|
+
const mode = d.policy.mode;
|
|
37
|
+
let v = "allow";
|
|
38
|
+
const irreversible = new Set(d.ruleset.irreversibleTags);
|
|
39
|
+
// R3 (calls only): high-risk action, by mode, then tagOverrides, then remembered rules and
|
|
40
|
+
// session grants (mode-driven asks only, never the hard floor).
|
|
41
|
+
if (d.kind === "call" && d.risk.level === "high-risk") {
|
|
42
|
+
const tags = d.risk.tags.filter((t) => t !== "self-protection");
|
|
43
|
+
let r3 = "allow";
|
|
44
|
+
let floorAsk = false;
|
|
45
|
+
const liftable = [];
|
|
46
|
+
const list = tags.length > 0 ? tags : [null];
|
|
47
|
+
for (const t of list) {
|
|
48
|
+
const o = t !== null ? d.policy.tagOverrides?.[t] : undefined;
|
|
49
|
+
let tv;
|
|
50
|
+
if (t !== null && HARD_FLOOR.has(t)) {
|
|
51
|
+
tv = o === "deny" ? "deny" : "ask";
|
|
52
|
+
if (tv === "ask")
|
|
53
|
+
floorAsk = true;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
tv = modeVerdict(t, mode, irreversible);
|
|
57
|
+
if (o === "allow" || o === "ask" || o === "deny")
|
|
58
|
+
tv = o;
|
|
59
|
+
if (tv === "ask" && t !== null)
|
|
60
|
+
liftable.push(t);
|
|
61
|
+
}
|
|
62
|
+
r3 = vmax(r3, tv);
|
|
63
|
+
}
|
|
64
|
+
if (r3 === "ask" && !floorAsk && liftable.length > 0 && liftable.every((t) => lifted(d, t)))
|
|
65
|
+
r3 = "allow";
|
|
66
|
+
v = vmax(v, r3);
|
|
67
|
+
}
|
|
68
|
+
// R4–R7: findings (confidence ≥ threshold).
|
|
69
|
+
let top = -1;
|
|
70
|
+
for (const f of d.findings)
|
|
71
|
+
if (f.confidence >= d.minConfidence)
|
|
72
|
+
top = Math.max(top, SEV.indexOf(f.severity));
|
|
73
|
+
if (top === 2)
|
|
74
|
+
v = vmax(v, mode === "strict" ? "ask" : "warn");
|
|
75
|
+
if (top === 3)
|
|
76
|
+
v = vmax(v, "ask");
|
|
77
|
+
if (top === 4)
|
|
78
|
+
v = vmax(v, "deny"); // R7 🔒
|
|
79
|
+
// R13/R14: server requests.
|
|
80
|
+
if (d.requestVerdict)
|
|
81
|
+
v = vmax(v, d.requestVerdict.verdict);
|
|
82
|
+
if (d.kind === "call") {
|
|
83
|
+
if (d.risk.selfProtection)
|
|
84
|
+
v = vmax(v, "deny"); // R8 🔒
|
|
85
|
+
if (d.risk.unanalyzable)
|
|
86
|
+
v = vmax(v, "ask"); // R9 🔒
|
|
87
|
+
if (d.degraded && d.risk.level === "high-risk")
|
|
88
|
+
v = vmax(v, "deny"); // R10 🔒
|
|
89
|
+
if (d.degraded && d.risk.level !== "high-risk")
|
|
90
|
+
v = vmax(v, "warn"); // R11 🔒
|
|
91
|
+
if (d.protectionDegraded && d.risk.level === "high-risk")
|
|
92
|
+
v = vmax(v, "ask"); // R16 🔒
|
|
93
|
+
}
|
|
94
|
+
else if (d.degraded) {
|
|
95
|
+
v = vmax(v, "warn"); // R11 (results and requests: R10 → R11)
|
|
96
|
+
}
|
|
97
|
+
// R17 🔒: no usable rules at all.
|
|
98
|
+
if (d.ruleset.status === "unavailable")
|
|
99
|
+
v = vmax(v, d.risk.level === "read" ? "warn" : "deny");
|
|
100
|
+
// R15 🔒: nobody is there to answer in autopilot.
|
|
101
|
+
if (mode === "autopilot" && v === "ask")
|
|
102
|
+
return { verdict: "deny", verdictBeforeMode: "ask" };
|
|
103
|
+
return { verdict: v };
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=decide.js.map
|