mandat 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 +202 -0
- package/README.md +293 -3
- package/dist/approvals/approvals.d.ts +136 -0
- package/dist/approvals/approvals.js +262 -0
- package/dist/bridge.d.ts +38 -0
- package/dist/bridge.js +117 -0
- package/dist/cli/args.d.ts +9 -0
- package/dist/cli/args.js +39 -0
- package/dist/cli/bin.d.ts +2 -0
- package/dist/cli/bin.js +3 -0
- package/dist/cli/main.d.ts +1 -0
- package/dist/cli/main.js +649 -0
- package/dist/cli/templates.d.ts +91 -0
- package/dist/cli/templates.js +57 -0
- package/dist/client.d.ts +54 -0
- package/dist/client.js +88 -0
- package/dist/controlplane/config.d.ts +48 -0
- package/dist/controlplane/config.js +33 -0
- package/dist/controlplane/console/.claude-flow/neural/patterns.json +402 -0
- package/dist/controlplane/console/.claude-flow/neural/stats.json +6 -0
- package/dist/controlplane/console/.claude-flow/policy/ledger-anchors.json +3 -0
- package/dist/controlplane/console/.claude-flow/policy/state.json +54 -0
- package/dist/controlplane/console/app.css +93 -0
- package/dist/controlplane/console/app.js +330 -0
- package/dist/controlplane/console/index.html +70 -0
- package/dist/controlplane/server.d.ts +17 -0
- package/dist/controlplane/server.js +427 -0
- package/dist/guard/guard.d.ts +154 -0
- package/dist/guard/guard.js +290 -0
- package/dist/guard/pinning.d.ts +46 -0
- package/dist/guard/pinning.js +49 -0
- package/dist/guard/remote.d.ts +78 -0
- package/dist/guard/remote.js +185 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +67 -0
- package/dist/ledger/export.d.ts +27 -0
- package/dist/ledger/export.js +169 -0
- package/dist/ledger/ledger.d.ts +117 -0
- package/dist/ledger/ledger.js +139 -0
- package/dist/ledger/redact.d.ts +13 -0
- package/dist/ledger/redact.js +73 -0
- package/dist/ledger/report.d.ts +71 -0
- package/dist/ledger/report.js +128 -0
- package/dist/mcp/protect.d.ts +8 -0
- package/dist/mcp/protect.js +68 -0
- package/dist/mcp/server.d.ts +49 -0
- package/dist/mcp/server.js +211 -0
- package/dist/oidc.d.ts +52 -0
- package/dist/oidc.js +194 -0
- package/dist/policy/engine.d.ts +135 -0
- package/dist/policy/engine.js +243 -0
- package/dist/policy/expr.d.ts +56 -0
- package/dist/policy/expr.js +506 -0
- package/dist/scan/findings.d.ts +32 -0
- package/dist/scan/findings.js +105 -0
- package/dist/scan/remote.d.ts +7 -0
- package/dist/scan/remote.js +225 -0
- package/dist/scan/static.d.ts +15 -0
- package/dist/scan/static.js +245 -0
- package/dist/store.d.ts +14 -0
- package/dist/store.js +40 -0
- package/dist/sts/jwt.d.ts +64 -0
- package/dist/sts/jwt.js +168 -0
- package/dist/sts/revocation.d.ts +38 -0
- package/dist/sts/revocation.js +52 -0
- package/dist/sts/sts.d.ts +83 -0
- package/dist/sts/sts.js +239 -0
- package/dist/util.d.ts +28 -0
- package/dist/util.js +137 -0
- package/package.json +47 -4
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
// A small, safe, CEL-flavoured expression language for policy conditions.
|
|
2
|
+
//
|
|
3
|
+
// args.amount <= 50 && args.currency == 'EUR'
|
|
4
|
+
// session.tainted && !args.to.endsWith('@acme.com')
|
|
5
|
+
// args.recipients.all(r, r.endsWith('@acme.com'))
|
|
6
|
+
// tool.risk in ['destructive', 'financial']
|
|
7
|
+
// has(args.dry_run) && args.dry_run == true
|
|
8
|
+
//
|
|
9
|
+
// Parsed once into an AST; evaluation is a tree walk with no access to the JS runtime.
|
|
10
|
+
// Missing fields evaluate to `null`; ordering comparisons involving null are false.
|
|
11
|
+
export class ExprError extends Error {
|
|
12
|
+
}
|
|
13
|
+
const OPS = ["&&", "||", "==", "!=", "<=", ">=", "<", ">", "!", "+", "-", "*", "/", "%", "(", ")", "[", "]", ".", ",", "?", ":"];
|
|
14
|
+
const MAX_EXPR_LENGTH = 4096;
|
|
15
|
+
const MAX_REGEX_LENGTH = 256;
|
|
16
|
+
function tokenize(src) {
|
|
17
|
+
if (src.length > MAX_EXPR_LENGTH)
|
|
18
|
+
throw new ExprError("Expression too long");
|
|
19
|
+
const toks = [];
|
|
20
|
+
let i = 0;
|
|
21
|
+
while (i < src.length) {
|
|
22
|
+
const c = src[i];
|
|
23
|
+
if (/\s/.test(c)) {
|
|
24
|
+
i++;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (/[0-9]/.test(c)) {
|
|
28
|
+
const m = /^\d+(\.\d+)?/.exec(src.slice(i));
|
|
29
|
+
toks.push({ t: "num", v: m[0], pos: i });
|
|
30
|
+
i += m[0].length;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (c === "'" || c === '"') {
|
|
34
|
+
let j = i + 1;
|
|
35
|
+
let out = "";
|
|
36
|
+
while (j < src.length && src[j] !== c) {
|
|
37
|
+
if (src[j] === "\\" && j + 1 < src.length) {
|
|
38
|
+
const n = src[j + 1];
|
|
39
|
+
out += n === "n" ? "\n" : n === "t" ? "\t" : n;
|
|
40
|
+
j += 2;
|
|
41
|
+
}
|
|
42
|
+
else
|
|
43
|
+
out += src[j++];
|
|
44
|
+
}
|
|
45
|
+
if (j >= src.length)
|
|
46
|
+
throw new ExprError(`Unterminated string at ${i}`);
|
|
47
|
+
toks.push({ t: "str", v: out, pos: i });
|
|
48
|
+
i = j + 1;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (/[A-Za-z_]/.test(c)) {
|
|
52
|
+
const m = /^[A-Za-z_][A-Za-z0-9_]*/.exec(src.slice(i));
|
|
53
|
+
toks.push({ t: "id", v: m[0], pos: i });
|
|
54
|
+
i += m[0].length;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const op = OPS.find((o) => src.startsWith(o, i));
|
|
58
|
+
if (!op)
|
|
59
|
+
throw new ExprError(`Unexpected character '${c}' at ${i}`);
|
|
60
|
+
toks.push({ t: "op", v: op, pos: i });
|
|
61
|
+
i += op.length;
|
|
62
|
+
}
|
|
63
|
+
toks.push({ t: "eof", v: "", pos: src.length });
|
|
64
|
+
return toks;
|
|
65
|
+
}
|
|
66
|
+
class Parser {
|
|
67
|
+
i = 0;
|
|
68
|
+
toks;
|
|
69
|
+
constructor(toks) {
|
|
70
|
+
this.toks = toks;
|
|
71
|
+
}
|
|
72
|
+
peek() { return this.toks[this.i]; }
|
|
73
|
+
next() { return this.toks[this.i++]; }
|
|
74
|
+
isOp(v) { const t = this.peek(); return t.t === "op" && t.v === v; }
|
|
75
|
+
isId(v) { const t = this.peek(); return t.t === "id" && t.v === v; }
|
|
76
|
+
expect(v) {
|
|
77
|
+
const t = this.next();
|
|
78
|
+
if (t.t !== "op" || t.v !== v)
|
|
79
|
+
throw new ExprError(`Expected '${v}' at ${t.pos}, got '${t.v || "end"}'`);
|
|
80
|
+
}
|
|
81
|
+
parse() {
|
|
82
|
+
const n = this.ternary();
|
|
83
|
+
if (this.peek().t !== "eof")
|
|
84
|
+
throw new ExprError(`Unexpected '${this.peek().v}' at ${this.peek().pos}`);
|
|
85
|
+
return n;
|
|
86
|
+
}
|
|
87
|
+
ternary() {
|
|
88
|
+
const c = this.or();
|
|
89
|
+
if (this.isOp("?")) {
|
|
90
|
+
this.next();
|
|
91
|
+
const t = this.ternary();
|
|
92
|
+
this.expect(":");
|
|
93
|
+
const f = this.ternary();
|
|
94
|
+
return { k: "cond", c, t, f };
|
|
95
|
+
}
|
|
96
|
+
return c;
|
|
97
|
+
}
|
|
98
|
+
or() {
|
|
99
|
+
let l = this.and();
|
|
100
|
+
while (this.isOp("||")) {
|
|
101
|
+
this.next();
|
|
102
|
+
l = { k: "bin", op: "||", l, r: this.and() };
|
|
103
|
+
}
|
|
104
|
+
return l;
|
|
105
|
+
}
|
|
106
|
+
and() {
|
|
107
|
+
let l = this.cmp();
|
|
108
|
+
while (this.isOp("&&")) {
|
|
109
|
+
this.next();
|
|
110
|
+
l = { k: "bin", op: "&&", l, r: this.cmp() };
|
|
111
|
+
}
|
|
112
|
+
return l;
|
|
113
|
+
}
|
|
114
|
+
cmp() {
|
|
115
|
+
const l = this.add();
|
|
116
|
+
const t = this.peek();
|
|
117
|
+
if ((t.t === "op" && ["==", "!=", "<", "<=", ">", ">="].includes(t.v)) || (t.t === "id" && t.v === "in")) {
|
|
118
|
+
this.next();
|
|
119
|
+
return { k: "bin", op: t.v, l, r: this.add() };
|
|
120
|
+
}
|
|
121
|
+
return l;
|
|
122
|
+
}
|
|
123
|
+
add() {
|
|
124
|
+
let l = this.mul();
|
|
125
|
+
while (this.isOp("+") || this.isOp("-")) {
|
|
126
|
+
const op = this.next().v;
|
|
127
|
+
l = { k: "bin", op, l, r: this.mul() };
|
|
128
|
+
}
|
|
129
|
+
return l;
|
|
130
|
+
}
|
|
131
|
+
mul() {
|
|
132
|
+
let l = this.unary();
|
|
133
|
+
while (this.isOp("*") || this.isOp("/") || this.isOp("%")) {
|
|
134
|
+
const op = this.next().v;
|
|
135
|
+
l = { k: "bin", op, l, r: this.unary() };
|
|
136
|
+
}
|
|
137
|
+
return l;
|
|
138
|
+
}
|
|
139
|
+
unary() {
|
|
140
|
+
if (this.isOp("!")) {
|
|
141
|
+
this.next();
|
|
142
|
+
return { k: "un", op: "!", x: this.unary() };
|
|
143
|
+
}
|
|
144
|
+
if (this.isOp("-")) {
|
|
145
|
+
this.next();
|
|
146
|
+
return { k: "un", op: "-", x: this.unary() };
|
|
147
|
+
}
|
|
148
|
+
return this.postfix();
|
|
149
|
+
}
|
|
150
|
+
postfix() {
|
|
151
|
+
let n = this.primary();
|
|
152
|
+
for (;;) {
|
|
153
|
+
if (this.isOp(".")) {
|
|
154
|
+
this.next();
|
|
155
|
+
const name = this.next();
|
|
156
|
+
if (name.t !== "id")
|
|
157
|
+
throw new ExprError(`Expected property name at ${name.pos}`);
|
|
158
|
+
if (this.isOp("(")) {
|
|
159
|
+
this.next();
|
|
160
|
+
if (name.v === "all" || name.v === "exists" || name.v === "filter") {
|
|
161
|
+
const v = this.next();
|
|
162
|
+
if (v.t !== "id")
|
|
163
|
+
throw new ExprError(`${name.v}() expects a variable name at ${v.pos}`);
|
|
164
|
+
this.expect(",");
|
|
165
|
+
const body = this.ternary();
|
|
166
|
+
this.expect(")");
|
|
167
|
+
n = { k: "macro", obj: n, fn: name.v, v: v.v, body };
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
n = { k: "method", obj: n, fn: name.v, args: this.args() };
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else
|
|
174
|
+
n = { k: "member", obj: n, prop: name.v };
|
|
175
|
+
}
|
|
176
|
+
else if (this.isOp("[")) {
|
|
177
|
+
this.next();
|
|
178
|
+
const idx = this.ternary();
|
|
179
|
+
this.expect("]");
|
|
180
|
+
n = { k: "index", obj: n, idx };
|
|
181
|
+
}
|
|
182
|
+
else
|
|
183
|
+
return n;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
args() {
|
|
187
|
+
const out = [];
|
|
188
|
+
if (this.isOp(")")) {
|
|
189
|
+
this.next();
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
for (;;) {
|
|
193
|
+
out.push(this.ternary());
|
|
194
|
+
if (this.isOp(",")) {
|
|
195
|
+
this.next();
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
this.expect(")");
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
primary() {
|
|
203
|
+
const t = this.next();
|
|
204
|
+
if (t.t === "num")
|
|
205
|
+
return { k: "lit", v: Number(t.v) };
|
|
206
|
+
if (t.t === "str")
|
|
207
|
+
return { k: "lit", v: t.v };
|
|
208
|
+
if (t.t === "id") {
|
|
209
|
+
if (t.v === "true")
|
|
210
|
+
return { k: "lit", v: true };
|
|
211
|
+
if (t.v === "false")
|
|
212
|
+
return { k: "lit", v: false };
|
|
213
|
+
if (t.v === "null")
|
|
214
|
+
return { k: "lit", v: null };
|
|
215
|
+
if (this.isOp("(")) {
|
|
216
|
+
this.next();
|
|
217
|
+
return { k: "call", fn: t.v, args: this.args() };
|
|
218
|
+
}
|
|
219
|
+
return { k: "id", name: t.v };
|
|
220
|
+
}
|
|
221
|
+
if (t.t === "op" && t.v === "(") {
|
|
222
|
+
const n = this.ternary();
|
|
223
|
+
this.expect(")");
|
|
224
|
+
return n;
|
|
225
|
+
}
|
|
226
|
+
if (t.t === "op" && t.v === "[") {
|
|
227
|
+
const items = [];
|
|
228
|
+
if (!this.isOp("]")) {
|
|
229
|
+
for (;;) {
|
|
230
|
+
items.push(this.ternary());
|
|
231
|
+
if (this.isOp(",")) {
|
|
232
|
+
this.next();
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
this.expect("]");
|
|
239
|
+
return { k: "list", items };
|
|
240
|
+
}
|
|
241
|
+
throw new ExprError(`Unexpected '${t.v || "end of expression"}' at ${t.pos}`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const FUNCTIONS = new Set(["size", "has", "int", "double", "string", "lower", "upper", "abs", "min", "max"]);
|
|
245
|
+
const METHODS = new Set(["startsWith", "endsWith", "contains", "matches", "size", "lower", "upper", "trim", "keys"]);
|
|
246
|
+
/** Parses and statically validates an expression. Throws ExprError on bad syntax. */
|
|
247
|
+
export function compileExpr(source) {
|
|
248
|
+
const ast = new Parser(tokenize(source)).parse();
|
|
249
|
+
validate(ast);
|
|
250
|
+
return { source, ast, eval: (ctx) => evaluate(ast, ctx, new Map()) };
|
|
251
|
+
}
|
|
252
|
+
function validate(n) {
|
|
253
|
+
switch (n.k) {
|
|
254
|
+
case "call":
|
|
255
|
+
if (!FUNCTIONS.has(n.fn))
|
|
256
|
+
throw new ExprError(`Unknown function ${n.fn}()`);
|
|
257
|
+
if (n.fn === "has" && (n.args.length !== 1 || (n.args[0].k !== "member" && n.args[0].k !== "index")))
|
|
258
|
+
throw new ExprError("has() expects a field selection, e.g. has(args.x)");
|
|
259
|
+
n.args.forEach(validate);
|
|
260
|
+
return;
|
|
261
|
+
case "method":
|
|
262
|
+
if (!METHODS.has(n.fn))
|
|
263
|
+
throw new ExprError(`Unknown method .${n.fn}()`);
|
|
264
|
+
if (n.fn === "matches") {
|
|
265
|
+
const p = n.args[0];
|
|
266
|
+
if (!p || p.k !== "lit" || typeof p.v !== "string")
|
|
267
|
+
throw new ExprError(".matches() requires a string literal pattern");
|
|
268
|
+
if (p.v.length > MAX_REGEX_LENGTH)
|
|
269
|
+
throw new ExprError("Regex pattern too long");
|
|
270
|
+
new RegExp(p.v); // syntax check at load time
|
|
271
|
+
}
|
|
272
|
+
validate(n.obj);
|
|
273
|
+
n.args.forEach(validate);
|
|
274
|
+
return;
|
|
275
|
+
case "macro":
|
|
276
|
+
validate(n.obj);
|
|
277
|
+
validate(n.body);
|
|
278
|
+
return;
|
|
279
|
+
case "member":
|
|
280
|
+
validate(n.obj);
|
|
281
|
+
return;
|
|
282
|
+
case "index":
|
|
283
|
+
validate(n.obj);
|
|
284
|
+
validate(n.idx);
|
|
285
|
+
return;
|
|
286
|
+
case "list":
|
|
287
|
+
n.items.forEach(validate);
|
|
288
|
+
return;
|
|
289
|
+
case "un":
|
|
290
|
+
validate(n.x);
|
|
291
|
+
return;
|
|
292
|
+
case "bin":
|
|
293
|
+
validate(n.l);
|
|
294
|
+
validate(n.r);
|
|
295
|
+
return;
|
|
296
|
+
case "cond":
|
|
297
|
+
validate(n.c);
|
|
298
|
+
validate(n.t);
|
|
299
|
+
validate(n.f);
|
|
300
|
+
return;
|
|
301
|
+
default: return;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const regexCache = new Map();
|
|
305
|
+
function prop(obj, key) {
|
|
306
|
+
if (obj === null || obj === undefined)
|
|
307
|
+
return null;
|
|
308
|
+
if (typeof obj === "object" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
309
|
+
const v = obj[key];
|
|
310
|
+
return v === undefined ? null : v;
|
|
311
|
+
}
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
function truthy(v) {
|
|
315
|
+
if (typeof v !== "boolean")
|
|
316
|
+
throw new ExprError(`Expected boolean, got ${typeName(v)}`);
|
|
317
|
+
return v;
|
|
318
|
+
}
|
|
319
|
+
function typeName(v) {
|
|
320
|
+
return v === null ? "null" : Array.isArray(v) ? "list" : typeof v;
|
|
321
|
+
}
|
|
322
|
+
function equals(a, b) {
|
|
323
|
+
if (a === b)
|
|
324
|
+
return true;
|
|
325
|
+
if (Array.isArray(a) && Array.isArray(b))
|
|
326
|
+
return a.length === b.length && a.every((x, i) => equals(x, b[i]));
|
|
327
|
+
if (a && b && typeof a === "object" && typeof b === "object") {
|
|
328
|
+
const ka = Object.keys(a), kb = Object.keys(b);
|
|
329
|
+
return ka.length === kb.length && ka.every((k) => equals(a[k], b[k]));
|
|
330
|
+
}
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
function size(v) {
|
|
334
|
+
if (typeof v === "string" || Array.isArray(v))
|
|
335
|
+
return v.length;
|
|
336
|
+
if (v && typeof v === "object")
|
|
337
|
+
return Object.keys(v).length;
|
|
338
|
+
throw new ExprError(`size() of ${typeName(v)}`);
|
|
339
|
+
}
|
|
340
|
+
function str(v, fn) {
|
|
341
|
+
if (typeof v !== "string")
|
|
342
|
+
throw new ExprError(`.${fn}() expects a string, got ${typeName(v)}`);
|
|
343
|
+
return v;
|
|
344
|
+
}
|
|
345
|
+
function evaluate(n, ctx, vars) {
|
|
346
|
+
switch (n.k) {
|
|
347
|
+
case "lit": return n.v;
|
|
348
|
+
case "id": return vars.has(n.name) ? vars.get(n.name) : prop(ctx, n.name);
|
|
349
|
+
case "list": return n.items.map((i) => evaluate(i, ctx, vars));
|
|
350
|
+
case "member": return prop(evaluate(n.obj, ctx, vars), n.prop);
|
|
351
|
+
case "index": {
|
|
352
|
+
const o = evaluate(n.obj, ctx, vars);
|
|
353
|
+
const i = evaluate(n.idx, ctx, vars);
|
|
354
|
+
if (Array.isArray(o) && typeof i === "number")
|
|
355
|
+
return o[i] ?? null;
|
|
356
|
+
if (typeof i === "string")
|
|
357
|
+
return prop(o, i);
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
case "cond": return truthy(evaluate(n.c, ctx, vars)) ? evaluate(n.t, ctx, vars) : evaluate(n.f, ctx, vars);
|
|
361
|
+
case "un": {
|
|
362
|
+
const x = evaluate(n.x, ctx, vars);
|
|
363
|
+
if (n.op === "!")
|
|
364
|
+
return !truthy(x);
|
|
365
|
+
if (typeof x !== "number")
|
|
366
|
+
throw new ExprError(`Unary minus on ${typeName(x)}`);
|
|
367
|
+
return -x;
|
|
368
|
+
}
|
|
369
|
+
case "bin": {
|
|
370
|
+
if (n.op === "&&")
|
|
371
|
+
return truthy(evaluate(n.l, ctx, vars)) && truthy(evaluate(n.r, ctx, vars));
|
|
372
|
+
if (n.op === "||")
|
|
373
|
+
return truthy(evaluate(n.l, ctx, vars)) || truthy(evaluate(n.r, ctx, vars));
|
|
374
|
+
const l = evaluate(n.l, ctx, vars);
|
|
375
|
+
const r = evaluate(n.r, ctx, vars);
|
|
376
|
+
switch (n.op) {
|
|
377
|
+
case "==": return equals(l, r);
|
|
378
|
+
case "!=": return !equals(l, r);
|
|
379
|
+
case "in":
|
|
380
|
+
if (Array.isArray(r))
|
|
381
|
+
return r.some((x) => equals(x, l));
|
|
382
|
+
if (typeof r === "string" && typeof l === "string")
|
|
383
|
+
return r.includes(l);
|
|
384
|
+
if (r && typeof r === "object" && typeof l === "string")
|
|
385
|
+
return Object.prototype.hasOwnProperty.call(r, l);
|
|
386
|
+
return false;
|
|
387
|
+
case "<":
|
|
388
|
+
case "<=":
|
|
389
|
+
case ">":
|
|
390
|
+
case ">=": {
|
|
391
|
+
if (l === null || r === null)
|
|
392
|
+
return false;
|
|
393
|
+
if (typeof l !== typeof r || (typeof l !== "number" && typeof l !== "string"))
|
|
394
|
+
throw new ExprError(`Cannot compare ${typeName(l)} ${n.op} ${typeName(r)}`);
|
|
395
|
+
const a = l, b = r;
|
|
396
|
+
return n.op === "<" ? a < b : n.op === "<=" ? a <= b : n.op === ">" ? a > b : a >= b;
|
|
397
|
+
}
|
|
398
|
+
case "+":
|
|
399
|
+
if (typeof l === "number" && typeof r === "number")
|
|
400
|
+
return l + r;
|
|
401
|
+
if (typeof l === "string" && typeof r === "string")
|
|
402
|
+
return l + r;
|
|
403
|
+
if (Array.isArray(l) && Array.isArray(r))
|
|
404
|
+
return [...l, ...r];
|
|
405
|
+
throw new ExprError(`Cannot add ${typeName(l)} and ${typeName(r)}`);
|
|
406
|
+
case "-":
|
|
407
|
+
case "*":
|
|
408
|
+
case "/":
|
|
409
|
+
case "%": {
|
|
410
|
+
if (typeof l !== "number" || typeof r !== "number")
|
|
411
|
+
throw new ExprError(`Arithmetic on ${typeName(l)}, ${typeName(r)}`);
|
|
412
|
+
if ((n.op === "/" || n.op === "%") && r === 0)
|
|
413
|
+
throw new ExprError("Division by zero");
|
|
414
|
+
return n.op === "-" ? l - r : n.op === "*" ? l * r : n.op === "/" ? l / r : l % r;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
throw new ExprError(`Unknown operator ${n.op}`);
|
|
418
|
+
}
|
|
419
|
+
case "call": {
|
|
420
|
+
if (n.fn === "has") {
|
|
421
|
+
const sel = n.args[0];
|
|
422
|
+
const base = evaluate(sel.k === "member" ? sel.obj : sel.obj, ctx, vars);
|
|
423
|
+
const key = sel.k === "member" ? sel.prop : evaluate(sel.idx, ctx, vars);
|
|
424
|
+
return base !== null && typeof base === "object" && Object.prototype.hasOwnProperty.call(base, String(key))
|
|
425
|
+
&& base[String(key)] !== undefined;
|
|
426
|
+
}
|
|
427
|
+
const a = n.args.map((x) => evaluate(x, ctx, vars));
|
|
428
|
+
switch (n.fn) {
|
|
429
|
+
case "size": return size(a[0]);
|
|
430
|
+
case "int": {
|
|
431
|
+
const v = Number(a[0]);
|
|
432
|
+
if (!Number.isFinite(v))
|
|
433
|
+
throw new ExprError("int() of non-number");
|
|
434
|
+
return Math.trunc(v);
|
|
435
|
+
}
|
|
436
|
+
case "double": {
|
|
437
|
+
const v = Number(a[0]);
|
|
438
|
+
if (!Number.isFinite(v))
|
|
439
|
+
throw new ExprError("double() of non-number");
|
|
440
|
+
return v;
|
|
441
|
+
}
|
|
442
|
+
case "string": return a[0] === null ? "null" : typeof a[0] === "object" ? JSON.stringify(a[0]) : String(a[0]);
|
|
443
|
+
case "lower": return str(a[0], "lower").toLowerCase();
|
|
444
|
+
case "upper": return str(a[0], "upper").toUpperCase();
|
|
445
|
+
case "abs":
|
|
446
|
+
if (typeof a[0] !== "number")
|
|
447
|
+
throw new ExprError("abs() of non-number");
|
|
448
|
+
return Math.abs(a[0]);
|
|
449
|
+
case "min":
|
|
450
|
+
case "max": {
|
|
451
|
+
const nums = (a.length === 1 && Array.isArray(a[0]) ? a[0] : a);
|
|
452
|
+
if (!nums.length || nums.some((x) => typeof x !== "number"))
|
|
453
|
+
throw new ExprError(`${n.fn}() expects numbers`);
|
|
454
|
+
return n.fn === "min" ? Math.min(...nums) : Math.max(...nums);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
throw new ExprError(`Unknown function ${n.fn}`);
|
|
458
|
+
}
|
|
459
|
+
case "method": {
|
|
460
|
+
const o = evaluate(n.obj, ctx, vars);
|
|
461
|
+
const a = n.args.map((x) => evaluate(x, ctx, vars));
|
|
462
|
+
switch (n.fn) {
|
|
463
|
+
case "startsWith": return str(o, n.fn).startsWith(str(a[0], n.fn));
|
|
464
|
+
case "endsWith": return str(o, n.fn).endsWith(str(a[0], n.fn));
|
|
465
|
+
case "contains":
|
|
466
|
+
if (Array.isArray(o))
|
|
467
|
+
return o.some((x) => equals(x, a[0]));
|
|
468
|
+
return str(o, n.fn).includes(str(a[0], n.fn));
|
|
469
|
+
case "matches": {
|
|
470
|
+
const p = a[0];
|
|
471
|
+
let re = regexCache.get(p);
|
|
472
|
+
if (!re) {
|
|
473
|
+
re = new RegExp(p);
|
|
474
|
+
regexCache.set(p, re);
|
|
475
|
+
}
|
|
476
|
+
return re.test(str(o, n.fn).slice(0, 10_000));
|
|
477
|
+
}
|
|
478
|
+
case "size": return size(o);
|
|
479
|
+
case "lower": return str(o, n.fn).toLowerCase();
|
|
480
|
+
case "upper": return str(o, n.fn).toUpperCase();
|
|
481
|
+
case "trim": return str(o, n.fn).trim();
|
|
482
|
+
case "keys":
|
|
483
|
+
if (o && typeof o === "object" && !Array.isArray(o))
|
|
484
|
+
return Object.keys(o);
|
|
485
|
+
throw new ExprError(".keys() of non-map");
|
|
486
|
+
}
|
|
487
|
+
throw new ExprError(`Unknown method ${n.fn}`);
|
|
488
|
+
}
|
|
489
|
+
case "macro": {
|
|
490
|
+
const o = evaluate(n.obj, ctx, vars);
|
|
491
|
+
const list = o === null ? [] : o;
|
|
492
|
+
if (!Array.isArray(list))
|
|
493
|
+
throw new ExprError(`.${n.fn}() on ${typeName(o)}`);
|
|
494
|
+
const run = (item) => {
|
|
495
|
+
const inner = new Map(vars);
|
|
496
|
+
inner.set(n.v, item);
|
|
497
|
+
return evaluate(n.body, ctx, inner);
|
|
498
|
+
};
|
|
499
|
+
if (n.fn === "all")
|
|
500
|
+
return list.every((x) => truthy(run(x)));
|
|
501
|
+
if (n.fn === "exists")
|
|
502
|
+
return list.some((x) => truthy(run(x)));
|
|
503
|
+
return list.filter((x) => truthy(run(x)));
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export type Severity = "critical" | "high" | "medium" | "low" | "info";
|
|
2
|
+
export declare const SEVERITIES: Severity[];
|
|
3
|
+
export interface Finding {
|
|
4
|
+
rule: string;
|
|
5
|
+
severity: Severity;
|
|
6
|
+
title: string;
|
|
7
|
+
detail: string;
|
|
8
|
+
fix: string;
|
|
9
|
+
fixCode?: string;
|
|
10
|
+
/** OWASP Top 10 for Agentic Applications (ASIxx) / OWASP MCP Top 10 (MCPxx) references. */
|
|
11
|
+
owasp: string[];
|
|
12
|
+
location?: {
|
|
13
|
+
url?: string;
|
|
14
|
+
file?: string;
|
|
15
|
+
line?: number;
|
|
16
|
+
server?: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface ScanReport {
|
|
20
|
+
target: string;
|
|
21
|
+
kind: "remote" | "config" | "source";
|
|
22
|
+
scanned_at: string;
|
|
23
|
+
findings: Finding[];
|
|
24
|
+
passed: string[];
|
|
25
|
+
info?: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
export declare function severityRank(s: Severity): number;
|
|
28
|
+
export declare function sortFindings(f: Finding[]): Finding[];
|
|
29
|
+
export declare function shouldFail(reports: ScanReport[], failOn: Severity | "none"): boolean;
|
|
30
|
+
export declare function formatText(reports: ScanReport[], color?: boolean): string;
|
|
31
|
+
export declare function formatSarif(reports: ScanReport[]): string;
|
|
32
|
+
export declare function formatHtml(reports: ScanReport[]): string;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Finding model + output formats (text, JSON, SARIF 2.1.0, HTML).
|
|
2
|
+
export const SEVERITIES = ["critical", "high", "medium", "low", "info"];
|
|
3
|
+
export function severityRank(s) {
|
|
4
|
+
return SEVERITIES.indexOf(s);
|
|
5
|
+
}
|
|
6
|
+
export function sortFindings(f) {
|
|
7
|
+
return [...f].sort((a, b) => severityRank(a.severity) - severityRank(b.severity));
|
|
8
|
+
}
|
|
9
|
+
export function shouldFail(reports, failOn) {
|
|
10
|
+
if (failOn === "none")
|
|
11
|
+
return false;
|
|
12
|
+
return reports.some((r) => r.findings.some((f) => severityRank(f.severity) <= severityRank(failOn)));
|
|
13
|
+
}
|
|
14
|
+
const COLORS = { critical: "\x1b[41m\x1b[97m", high: "\x1b[31m", medium: "\x1b[33m", low: "\x1b[36m", info: "\x1b[90m" };
|
|
15
|
+
const RESET = "\x1b[0m";
|
|
16
|
+
export function formatText(reports, color = process.stdout.isTTY) {
|
|
17
|
+
const c = (s, t) => (color ? `${COLORS[s]}${t}${RESET}` : t);
|
|
18
|
+
const out = [];
|
|
19
|
+
for (const r of reports) {
|
|
20
|
+
out.push(`\nMandat scan — ${r.kind}: ${r.target}`);
|
|
21
|
+
out.push("─".repeat(72));
|
|
22
|
+
if (!r.findings.length)
|
|
23
|
+
out.push(" No findings.");
|
|
24
|
+
for (const f of sortFindings(r.findings)) {
|
|
25
|
+
const loc = f.location?.file ? ` (${f.location.file}${f.location.line ? `:${f.location.line}` : ""})` : f.location?.server ? ` [${f.location.server}]` : "";
|
|
26
|
+
out.push(` ${c(f.severity, f.severity.toUpperCase().padEnd(8))} ${f.rule} ${f.title}${loc}`);
|
|
27
|
+
out.push(` ${f.detail}`);
|
|
28
|
+
out.push(` Fix: ${f.fix}`);
|
|
29
|
+
if (f.fixCode)
|
|
30
|
+
out.push(f.fixCode.split("\n").map((l) => ` ${l}`).join("\n"));
|
|
31
|
+
if (f.owasp.length)
|
|
32
|
+
out.push(` OWASP: ${f.owasp.join(", ")}`);
|
|
33
|
+
out.push("");
|
|
34
|
+
}
|
|
35
|
+
if (r.passed.length)
|
|
36
|
+
out.push(` Passed: ${r.passed.join(" · ")}`);
|
|
37
|
+
const counts = SEVERITIES.map((s) => [s, r.findings.filter((f) => f.severity === s).length]).filter(([, n]) => n);
|
|
38
|
+
out.push(` Summary: ${counts.length ? counts.map(([s, n]) => `${n} ${s}`).join(", ") : "clean"}`);
|
|
39
|
+
}
|
|
40
|
+
return out.join("\n") + "\n";
|
|
41
|
+
}
|
|
42
|
+
export function formatSarif(reports) {
|
|
43
|
+
const rules = new Map();
|
|
44
|
+
for (const r of reports)
|
|
45
|
+
for (const f of r.findings)
|
|
46
|
+
if (!rules.has(f.rule))
|
|
47
|
+
rules.set(f.rule, f);
|
|
48
|
+
const level = (s) => (s === "critical" || s === "high" ? "error" : s === "medium" ? "warning" : "note");
|
|
49
|
+
const sarif = {
|
|
50
|
+
$schema: "https://json.schemastore.org/sarif-2.1.0.json",
|
|
51
|
+
version: "2.1.0",
|
|
52
|
+
runs: [{
|
|
53
|
+
tool: {
|
|
54
|
+
driver: {
|
|
55
|
+
name: "mandat-scan",
|
|
56
|
+
version: "0.1.0",
|
|
57
|
+
rules: [...rules.values()].map((f) => ({
|
|
58
|
+
id: f.rule,
|
|
59
|
+
name: f.title.replace(/[^A-Za-z0-9]+/g, ""),
|
|
60
|
+
shortDescription: { text: f.title },
|
|
61
|
+
help: { text: f.fix + (f.fixCode ? "\n\n" + f.fixCode : "") },
|
|
62
|
+
properties: { tags: ["security", ...f.owasp], "security-severity": { critical: "9.5", high: "8.0", medium: "5.5", low: "3.0", info: "0.0" }[f.severity] },
|
|
63
|
+
})),
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
results: reports.flatMap((r) => r.findings.map((f) => ({
|
|
67
|
+
ruleId: f.rule,
|
|
68
|
+
level: level(f.severity),
|
|
69
|
+
message: { text: `${f.title}: ${f.detail}` },
|
|
70
|
+
locations: [{
|
|
71
|
+
physicalLocation: f.location?.file
|
|
72
|
+
? { artifactLocation: { uri: f.location.file.replace(/\\/g, "/") }, region: { startLine: f.location.line ?? 1 } }
|
|
73
|
+
: { artifactLocation: { uri: f.location?.url ?? r.target } },
|
|
74
|
+
}],
|
|
75
|
+
}))),
|
|
76
|
+
}],
|
|
77
|
+
};
|
|
78
|
+
return JSON.stringify(sarif, null, 2);
|
|
79
|
+
}
|
|
80
|
+
const esc = (s) => s.replace(/[&<>"']/g, (ch) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[ch]);
|
|
81
|
+
export function formatHtml(reports) {
|
|
82
|
+
const body = reports.map((r) => `
|
|
83
|
+
<section>
|
|
84
|
+
<h2>${esc(r.kind)} · ${esc(r.target)}</h2>
|
|
85
|
+
<p class="meta">Scanned ${esc(r.scanned_at)} · ${r.findings.length} findings · passed: ${esc(r.passed.join(", ") || "—")}</p>
|
|
86
|
+
${sortFindings(r.findings).map((f) => `
|
|
87
|
+
<article class="f ${f.severity}">
|
|
88
|
+
<header><span class="sev">${f.severity}</span> <code>${esc(f.rule)}</code> <strong>${esc(f.title)}</strong></header>
|
|
89
|
+
<p>${esc(f.detail)}</p>
|
|
90
|
+
<p><b>Fix:</b> ${esc(f.fix)}</p>
|
|
91
|
+
${f.fixCode ? `<pre>${esc(f.fixCode)}</pre>` : ""}
|
|
92
|
+
${f.owasp.length ? `<p class="meta">OWASP: ${esc(f.owasp.join(", "))}</p>` : ""}
|
|
93
|
+
</article>`).join("")}
|
|
94
|
+
</section>`).join("");
|
|
95
|
+
return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
96
|
+
<title>Mandat scan report</title>
|
|
97
|
+
<style>
|
|
98
|
+
body{font:15px/1.5 system-ui,sans-serif;max-width:960px;margin:2rem auto;padding:0 1rem;color:#1d1d1f;background:#fafafa}
|
|
99
|
+
h1{font-size:1.6rem}h2{font-size:1.15rem;margin-top:2rem}.meta{color:#666;font-size:.85rem}
|
|
100
|
+
.f{background:#fff;border:1px solid #e3e3e3;border-left:5px solid #999;border-radius:6px;padding:.75rem 1rem;margin:.75rem 0}
|
|
101
|
+
.f.critical{border-left-color:#b00020}.f.high{border-left-color:#e53935}.f.medium{border-left-color:#f9a825}.f.low{border-left-color:#039be5}
|
|
102
|
+
.sev{text-transform:uppercase;font-size:.75rem;font-weight:700;letter-spacing:.04em}
|
|
103
|
+
pre{background:#f3f3f3;padding:.6rem;border-radius:4px;overflow-x:auto;font-size:.82rem}
|
|
104
|
+
</style></head><body><h1>Mandat scan report</h1>${body}</body></html>`;
|
|
105
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ScanReport } from "./findings.ts";
|
|
2
|
+
export interface RemoteScanOptions {
|
|
3
|
+
active?: boolean;
|
|
4
|
+
timeoutMs?: number;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
}
|
|
7
|
+
export declare function scanRemote(target: string, opts?: RemoteScanOptions): Promise<ScanReport>;
|