oculon 0.1.6 → 0.2.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.
Files changed (106) hide show
  1. package/README.md +93 -17
  2. package/dist/cli/main.js +397 -0
  3. package/dist/core/calls.d.ts +63 -0
  4. package/dist/core/calls.js +120 -0
  5. package/dist/core/der.d.ts +2 -0
  6. package/dist/core/der.js +17 -0
  7. package/dist/core/http.d.ts +30 -1
  8. package/dist/core/http.js +133 -29
  9. package/dist/core/pdf.d.ts +51 -0
  10. package/dist/core/pdf.js +211 -0
  11. package/dist/core/qr.d.ts +23 -0
  12. package/dist/core/qr.js +577 -0
  13. package/dist/core/quota.js +5 -0
  14. package/dist/core/raster.d.ts +6 -0
  15. package/dist/core/raster.js +298 -0
  16. package/dist/core/safeurl.d.ts +5 -0
  17. package/dist/core/safeurl.js +26 -0
  18. package/dist/core/verdict.d.ts +30 -0
  19. package/dist/core/verdict.js +164 -0
  20. package/dist/core/zip.d.ts +9 -0
  21. package/dist/core/zip.js +46 -0
  22. package/dist/index.d.ts +83 -0
  23. package/dist/index.js +128 -0
  24. package/dist/mcp.d.ts +1 -0
  25. package/dist/mcp.js +256 -6
  26. package/dist/modules/address.d.ts +6 -2
  27. package/dist/modules/address.js +32 -5
  28. package/dist/modules/apk.d.ts +37 -0
  29. package/dist/modules/apk.js +191 -0
  30. package/dist/modules/approvals.d.ts +40 -0
  31. package/dist/modules/approvals.js +155 -0
  32. package/dist/modules/archive.d.ts +21 -0
  33. package/dist/modules/archive.js +94 -0
  34. package/dist/modules/checks.d.ts +59 -0
  35. package/dist/modules/checks.js +167 -0
  36. package/dist/modules/contract.d.ts +56 -0
  37. package/dist/modules/contract.js +167 -0
  38. package/dist/modules/deployer.d.ts +42 -0
  39. package/dist/modules/deployer.js +90 -0
  40. package/dist/modules/doc.d.ts +83 -0
  41. package/dist/modules/doc.js +841 -0
  42. package/dist/modules/eml.d.ts +46 -0
  43. package/dist/modules/eml.js +175 -0
  44. package/dist/modules/hashcheck.d.ts +19 -3
  45. package/dist/modules/hashcheck.js +28 -4
  46. package/dist/modules/image.d.ts +32 -0
  47. package/dist/modules/image.js +276 -0
  48. package/dist/modules/invite.d.ts +27 -0
  49. package/dist/modules/invite.js +69 -0
  50. package/dist/modules/job.d.ts +33 -0
  51. package/dist/modules/job.js +64 -0
  52. package/dist/modules/meerkat.d.ts +1 -1
  53. package/dist/modules/meerkat.js +35 -2
  54. package/dist/modules/monitor.js +14 -0
  55. package/dist/modules/poisoning.d.ts +43 -0
  56. package/dist/modules/poisoning.js +114 -0
  57. package/dist/modules/qrcheck.d.ts +24 -0
  58. package/dist/modules/qrcheck.js +136 -0
  59. package/dist/modules/repo.d.ts +61 -0
  60. package/dist/modules/repo.js +162 -0
  61. package/dist/modules/report.d.ts +32 -0
  62. package/dist/modules/report.js +108 -0
  63. package/dist/modules/signature.d.ts +35 -0
  64. package/dist/modules/signature.js +283 -0
  65. package/dist/modules/site.d.ts +62 -0
  66. package/dist/modules/site.js +191 -0
  67. package/dist/modules/stocktoken.d.ts +48 -0
  68. package/dist/modules/stocktoken.js +99 -0
  69. package/dist/modules/trace.d.ts +37 -0
  70. package/dist/modules/trace.js +150 -0
  71. package/dist/modules/txexplain.d.ts +40 -0
  72. package/dist/modules/txexplain.js +135 -0
  73. package/dist/modules/urlcheck.js +15 -1
  74. package/dist/modules/utility.d.ts +27 -1
  75. package/dist/modules/utility.js +108 -11
  76. package/dist/modules/where.d.ts +63 -0
  77. package/dist/modules/where.js +165 -0
  78. package/dist/report-pdf.d.ts +7 -0
  79. package/dist/report-pdf.js +84 -0
  80. package/dist/sources/cfurlscan.d.ts +24 -0
  81. package/dist/sources/cfurlscan.js +46 -0
  82. package/dist/sources/chainabuse.d.ts +17 -0
  83. package/dist/sources/chainabuse.js +19 -0
  84. package/dist/sources/evm.d.ts +135 -0
  85. package/dist/sources/evm.js +260 -0
  86. package/dist/sources/phishlists.d.ts +20 -0
  87. package/dist/sources/phishlists.js +90 -0
  88. package/dist/telegram.d.ts +17 -0
  89. package/dist/telegram.js +151 -4
  90. package/dist/web/card.d.ts +28 -0
  91. package/dist/web/card.js +75 -0
  92. package/dist/web/docs.html +1 -1
  93. package/dist/web/index.html +213 -5
  94. package/dist/web/landing.html +1 -1
  95. package/dist/web/legal.html +3 -1
  96. package/dist/web/mcp.html +1 -1
  97. package/dist/web/openapi.js +15 -0
  98. package/dist/web/pages.d.ts +24 -0
  99. package/dist/web/pages.js +139 -0
  100. package/dist/web/public.js +16 -1
  101. package/dist/web/server.d.ts +13 -0
  102. package/dist/web/server.js +152 -13
  103. package/dist/web/token.html +25 -4
  104. package/dist/web/token.json +12 -1
  105. package/dist/web/utility.html +40 -14
  106. package/package.json +2 -2
@@ -0,0 +1,298 @@
1
+ /**
2
+ * Image decoding to a grayscale raster, without dependencies: PNG (all color types, bit depths 1-16,
3
+ * no interlacing) and baseline JPEG (Huffman, any sampling; only the luminance channel is reconstructed,
4
+ * which is all a QR reader needs). Progressive JPEG and interlaced PNG are rejected with a plain message.
5
+ */
6
+ import { inflateSync } from 'node:zlib';
7
+ const MAX_PIXELS = 25_000_000;
8
+ export function decodeGray(b) {
9
+ if (b.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])))
10
+ return png(b);
11
+ if (b[0] === 0xff && b[1] === 0xd8)
12
+ return jpeg(b);
13
+ throw new Error('not a PNG or JPEG image');
14
+ }
15
+ /* ---------- PNG ---------- */
16
+ function png(b) {
17
+ let i = 8, w = 0, h = 0, depth = 8, ct = 0, interlace = 0;
18
+ let palette = null, trns = null;
19
+ const idat = [];
20
+ while (i + 12 <= b.length) {
21
+ const len = b.readUInt32BE(i), t = b.toString('latin1', i + 4, i + 8), d = b.subarray(i + 8, i + 8 + len);
22
+ if (t === 'IHDR') {
23
+ w = d.readUInt32BE(0);
24
+ h = d.readUInt32BE(4);
25
+ depth = d[8];
26
+ ct = d[9];
27
+ interlace = d[12];
28
+ }
29
+ else if (t === 'PLTE')
30
+ palette = d;
31
+ else if (t === 'tRNS')
32
+ trns = d;
33
+ else if (t === 'IDAT')
34
+ idat.push(d);
35
+ else if (t === 'IEND')
36
+ break;
37
+ i += 12 + len;
38
+ }
39
+ if (!w || !h || w * h > MAX_PIXELS)
40
+ throw new Error('the image is empty or too large');
41
+ if (interlace)
42
+ throw new Error('interlaced PNG is not supported; save it again without interlacing or take a screenshot');
43
+ const ch = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[ct];
44
+ if (!ch)
45
+ throw new Error('unsupported PNG color type');
46
+ const raw = inflateSync(Buffer.concat(idat));
47
+ const bpp = Math.max(1, (ch * depth) >> 3);
48
+ const stride = Math.ceil((w * ch * depth) / 8);
49
+ const cur = Buffer.alloc(stride), prev = Buffer.alloc(stride);
50
+ const out = new Uint8Array(w * h);
51
+ let p = 0;
52
+ for (let y = 0; y < h; y++) {
53
+ const f = raw[p++];
54
+ raw.copy(cur, 0, p, p + stride);
55
+ p += stride;
56
+ for (let x = 0; x < stride; x++) {
57
+ const a = x >= bpp ? cur[x - bpp] : 0, up = prev[x], c = x >= bpp ? prev[x - bpp] : 0;
58
+ let v = cur[x];
59
+ if (f === 1)
60
+ v += a;
61
+ else if (f === 2)
62
+ v += up;
63
+ else if (f === 3)
64
+ v += (a + up) >> 1;
65
+ else if (f === 4) {
66
+ const pa = Math.abs(up - c), pb = Math.abs(a - c), pc = Math.abs(a + up - 2 * c);
67
+ v += pa <= pb && pa <= pc ? a : pb <= pc ? up : c;
68
+ }
69
+ cur[x] = v & 255;
70
+ }
71
+ const sample = (k) => {
72
+ if (depth === 8)
73
+ return cur[k];
74
+ if (depth === 16)
75
+ return cur[k * 2];
76
+ const per = 8 / depth, byte = cur[Math.floor(k / per)], shift = 8 - depth * ((k % per) + 1);
77
+ return (byte >> shift) & ((1 << depth) - 1);
78
+ };
79
+ const scale = depth < 8 && ct !== 3 ? 255 / ((1 << depth) - 1) : 1;
80
+ for (let x = 0; x < w; x++) {
81
+ let g, alpha = 255;
82
+ if (ct === 0)
83
+ g = sample(x) * scale;
84
+ else if (ct === 2) {
85
+ const r = sample(x * 3), gg = sample(x * 3 + 1), bl = sample(x * 3 + 2);
86
+ g = 0.299 * r + 0.587 * gg + 0.114 * bl;
87
+ }
88
+ else if (ct === 3) {
89
+ const k = sample(x) * 3;
90
+ g = palette ? 0.299 * palette[k] + 0.587 * palette[k + 1] + 0.114 * palette[k + 2] : 0;
91
+ if (trns && sample(x) < trns.length)
92
+ alpha = trns[sample(x)];
93
+ }
94
+ else if (ct === 4) {
95
+ g = sample(x * 2);
96
+ alpha = sample(x * 2 + 1);
97
+ }
98
+ else {
99
+ const r = sample(x * 4), gg = sample(x * 4 + 1), bl = sample(x * 4 + 2);
100
+ g = 0.299 * r + 0.587 * gg + 0.114 * bl;
101
+ alpha = sample(x * 4 + 3);
102
+ }
103
+ // Transparent parts sit on white, as a viewer shows them.
104
+ out[y * w + x] = Math.round((g * alpha + 255 * (255 - alpha)) / 255);
105
+ }
106
+ cur.copy(prev);
107
+ }
108
+ return { width: w, height: h, data: out };
109
+ }
110
+ /* ---------- baseline JPEG ---------- */
111
+ const ZZ = [0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63];
112
+ const COS = Array.from({ length: 8 }, (_, x) => Array.from({ length: 8 }, (_, u) => (u === 0 ? Math.SQRT1_2 : 1) * Math.cos(((2 * x + 1) * u * Math.PI) / 16)));
113
+ function buildHuff(counts, syms) {
114
+ const lookup = Array.from({ length: 17 }, () => new Map());
115
+ let code = 0, k = 0;
116
+ for (let len = 1; len <= 16; len++) {
117
+ for (let i = 0; i < counts[len - 1]; i++)
118
+ lookup[len].set(code++, syms[k++]);
119
+ code <<= 1;
120
+ }
121
+ return { lookup };
122
+ }
123
+ function idct(block, out, ow, ox, oy, oh) {
124
+ const tmp = new Float32Array(64);
125
+ for (let y = 0; y < 8; y++)
126
+ for (let u = 0; u < 8; u++) {
127
+ let s = 0;
128
+ for (let v = 0; v < 8; v++)
129
+ s += COS[y][v] * block[v * 8 + u];
130
+ tmp[y * 8 + u] = s;
131
+ }
132
+ for (let y = 0; y < 8; y++)
133
+ for (let x = 0; x < 8; x++) {
134
+ let s = 0;
135
+ for (let u = 0; u < 8; u++)
136
+ s += COS[x][u] * tmp[y * 8 + u];
137
+ const px = ox + x, py = oy + y;
138
+ if (px < ow && py < oh)
139
+ out[py * ow + px] = Math.max(0, Math.min(255, Math.round(s / 4 + 128)));
140
+ }
141
+ }
142
+ function jpeg(b) {
143
+ const qt = [];
144
+ const dcT = [], acT = [];
145
+ let w = 0, h = 0, restart = 0;
146
+ let comps = [];
147
+ let i = 2;
148
+ while (i < b.length) {
149
+ if (b[i] !== 0xff) {
150
+ i++;
151
+ continue;
152
+ }
153
+ const m = b[i + 1];
154
+ if (m === 0xd8 || m === 0x01 || (m >= 0xd0 && m <= 0xd7) || m === 0xff) {
155
+ i += m === 0xff ? 1 : 2;
156
+ continue;
157
+ }
158
+ if (m === 0xd9)
159
+ break;
160
+ const len = b.readUInt16BE(i + 2), seg = b.subarray(i + 4, i + 2 + len);
161
+ if (m === 0xdb) {
162
+ let p = 0;
163
+ while (p < seg.length) {
164
+ const pq = seg[p] >> 4, tq = seg[p] & 15;
165
+ p++;
166
+ const t = new Int32Array(64);
167
+ for (let k = 0; k < 64; k++) {
168
+ t[ZZ[k]] = pq ? seg.readUInt16BE(p + k * 2) : seg[p + k];
169
+ }
170
+ p += pq ? 128 : 64;
171
+ qt[tq] = t;
172
+ }
173
+ }
174
+ else if (m === 0xc4) {
175
+ let p = 0;
176
+ while (p < seg.length) {
177
+ const tc = seg[p] >> 4, th = seg[p] & 15;
178
+ const counts = [...seg.subarray(p + 1, p + 17)];
179
+ const n = counts.reduce((s, x) => s + x, 0);
180
+ const t = buildHuff(counts, [...seg.subarray(p + 17, p + 17 + n)]);
181
+ if (tc === 0)
182
+ dcT[th] = t;
183
+ else
184
+ acT[th] = t;
185
+ p += 17 + n;
186
+ }
187
+ }
188
+ else if (m === 0xc0 || m === 0xc1) {
189
+ h = seg.readUInt16BE(1);
190
+ w = seg.readUInt16BE(3);
191
+ const n = seg[5];
192
+ comps = Array.from({ length: n }, (_, k) => ({ id: seg[6 + k * 3], h: seg[7 + k * 3] >> 4, v: seg[7 + k * 3] & 15, tq: seg[8 + k * 3], td: 0, ta: 0, pred: 0 }));
193
+ }
194
+ else if (m === 0xc2 || m === 0xc3 || (m >= 0xc5 && m <= 0xcf && m !== 0xc8 && m !== 0xcc)) {
195
+ throw new Error('progressive or lossless JPEG is not supported; take a screenshot (PNG) of the QR code instead');
196
+ }
197
+ else if (m === 0xdd)
198
+ restart = seg.readUInt16BE(0);
199
+ else if (m === 0xda) {
200
+ if (!w || !h || w * h > MAX_PIXELS)
201
+ throw new Error('the image is empty or too large');
202
+ const ns = seg[0];
203
+ const scan = Array.from({ length: ns }, (_, k) => { const c = comps.find((x) => x.id === seg[1 + k * 2]); c.td = seg[2 + k * 2] >> 4; c.ta = seg[2 + k * 2] & 15; return c; });
204
+ return scanData(b, i + 2 + len, w, h, comps, scan, qt, dcT, acT, restart);
205
+ }
206
+ i += 2 + len;
207
+ }
208
+ throw new Error('no image data in the JPEG');
209
+ }
210
+ function scanData(b, start, w, h, comps, scan, qt, dcT, acT, restart) {
211
+ const hmax = Math.max(...comps.map((c) => c.h)), vmax = Math.max(...comps.map((c) => c.v));
212
+ const y0 = comps[0];
213
+ // Luminance plane at its own resolution; scaled to full size at the end when subsampled.
214
+ const lw = Math.ceil((w * y0.h) / hmax), lh = Math.ceil((h * y0.v) / vmax);
215
+ const plane = new Uint8Array(lw * lh);
216
+ let pos = start, bitBuf = 0, bitCnt = 0;
217
+ const readBit = () => {
218
+ if (bitCnt === 0) {
219
+ let byte = b[pos++] ?? 0;
220
+ if (byte === 0xff) {
221
+ const nx = b[pos];
222
+ if (nx === 0)
223
+ pos++;
224
+ else if (nx !== undefined && nx >= 0xd0 && nx <= 0xd7) { /* marker: handled by restart */ }
225
+ }
226
+ bitBuf = byte;
227
+ bitCnt = 8;
228
+ if (byte === undefined)
229
+ byte = 0;
230
+ }
231
+ bitCnt--;
232
+ return (bitBuf >> bitCnt) & 1;
233
+ };
234
+ const receive = (n) => { let v = 0; for (let k = 0; k < n; k++)
235
+ v = (v << 1) | readBit(); return v; };
236
+ const extend = (v, n) => (n && v < 1 << (n - 1) ? v - (1 << n) + 1 : v);
237
+ const decode = (t) => { let code = 0; for (let len = 1; len <= 16; len++) {
238
+ code = (code << 1) | readBit();
239
+ const s = t.lookup[len].get(code);
240
+ if (s !== undefined)
241
+ return s;
242
+ } throw new Error('bad Huffman code in JPEG'); };
243
+ const block = new Float32Array(64);
244
+ const mcuX = Math.ceil(w / (8 * hmax)), mcuY = Math.ceil(h / (8 * vmax));
245
+ const single = scan.length === 1;
246
+ const blocksX = single ? Math.ceil(lw / 8) : mcuX, blocksY = single ? Math.ceil(lh / 8) : mcuY;
247
+ let mcus = 0;
248
+ const doBlock = (c, bx, by) => {
249
+ block.fill(0);
250
+ const s = decode(dcT[c.td]);
251
+ c.pred += s ? extend(receive(s), s) : 0;
252
+ const q = qt[c.tq];
253
+ block[0] = c.pred * q[0];
254
+ for (let k = 1; k < 64;) {
255
+ const rs = decode(acT[c.ta]), r = rs >> 4, sz = rs & 15;
256
+ if (!sz) {
257
+ if (r === 15) {
258
+ k += 16;
259
+ continue;
260
+ }
261
+ break;
262
+ }
263
+ k += r;
264
+ if (k > 63)
265
+ break;
266
+ block[ZZ[k]] = extend(receive(sz), sz) * q[ZZ[k]];
267
+ k++;
268
+ }
269
+ if (c === y0)
270
+ idct(block, plane, lw, bx * 8, by * 8, lh);
271
+ };
272
+ for (let my = 0; my < blocksY; my++)
273
+ for (let mx = 0; mx < blocksX; mx++) {
274
+ if (restart && mcus && mcus % restart === 0) {
275
+ bitCnt = 0;
276
+ while (pos < b.length && !(b[pos] === 0xff && b[pos + 1] >= 0xd0 && b[pos + 1] <= 0xd7))
277
+ pos++;
278
+ pos += 2;
279
+ for (const c of comps)
280
+ c.pred = 0;
281
+ }
282
+ if (single)
283
+ doBlock(scan[0], mx, my);
284
+ else
285
+ for (const c of scan)
286
+ for (let v = 0; v < c.v; v++)
287
+ for (let u = 0; u < c.h; u++)
288
+ doBlock(c, mx * c.h + u, my * c.v + v);
289
+ mcus++;
290
+ }
291
+ if (lw === w && lh === h)
292
+ return { width: w, height: h, data: plane };
293
+ const out = new Uint8Array(w * h);
294
+ for (let y = 0; y < h; y++)
295
+ for (let x = 0; x < w; x++)
296
+ out[y * w + x] = plane[Math.min(lh - 1, Math.floor((y * lh) / h)) * lw + Math.min(lw - 1, Math.floor((x * lw) / w))];
297
+ return { width: w, height: h, data: out };
298
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Links Oculon is asked to open itself (page checks, archiving) must point at the public internet:
3
+ * http or https, a real host name, no IP literals, no internal names, no odd ports.
4
+ */
5
+ export declare function publicUrl(input: string): URL;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Links Oculon is asked to open itself (page checks, archiving) must point at the public internet:
3
+ * http or https, a real host name, no IP literals, no internal names, no odd ports.
4
+ */
5
+ export function publicUrl(input) {
6
+ let s = input.trim();
7
+ if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(s))
8
+ s = `https://${s}`;
9
+ let u;
10
+ try {
11
+ u = new URL(s);
12
+ }
13
+ catch {
14
+ throw new Error('not a valid link');
15
+ }
16
+ if (u.protocol !== 'https:' && u.protocol !== 'http:')
17
+ throw new Error('only http and https links are opened');
18
+ const h = u.hostname.toLowerCase();
19
+ if (!h.includes('.') || /^(\d+\.){3}\d+$|^\[|^0x|^localhost$|\.(local|internal|localhost|lan|home|corp|arpa)$/.test(h))
20
+ throw new Error('only links to public hosts are opened');
21
+ if (u.port && !['80', '443', '8080', '8443'].includes(u.port))
22
+ throw new Error('only standard web ports are opened');
23
+ u.username = '';
24
+ u.password = '';
25
+ return u;
26
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * One plain verdict from any check result: what it says, how bad, and why. Used by the verdict cards in AI chat
3
+ * apps, PDF reports, "seen before" changes and brief mode, so every check gets them without its own code.
4
+ * Results differ per module; this reads the fields they share (verdict, risk, level, reasons, flags, warnings).
5
+ */
6
+ export type Tone = 'bad' | 'warn' | 'ok' | 'info';
7
+ export interface Verdict {
8
+ /** What was checked, as shown to people. */
9
+ title: string;
10
+ verdict: string | null;
11
+ tone: Tone;
12
+ risk: number | null;
13
+ points: string[];
14
+ sources: {
15
+ live: number;
16
+ cached: number;
17
+ failed: number;
18
+ names: string[];
19
+ };
20
+ }
21
+ export declare function toneOf(verdict: string | null, risk: number | null): Tone;
22
+ /** The verdict of a module result (or the compact form the API returns). */
23
+ export declare function verdictOf(out: unknown, label?: string): Verdict;
24
+ /** What changed between two verdicts of the same target, in plain words. Empty when nothing did. */
25
+ export declare function verdictChanges(before: Verdict, now: Verdict): string[];
26
+ /**
27
+ * Brief mode: a short answer for agents. Verdict, risk, top points, which sources answered, and the check id
28
+ * for the full result. Results without a verdict are trimmed instead (first items of long lists, no long text).
29
+ */
30
+ export declare function briefOf(out: unknown, extra?: Record<string, unknown>): Record<string, unknown>;
@@ -0,0 +1,164 @@
1
+ /**
2
+ * One plain verdict from any check result: what it says, how bad, and why. Used by the verdict cards in AI chat
3
+ * apps, PDF reports, "seen before" changes and brief mode, so every check gets them without its own code.
4
+ * Results differ per module; this reads the fields they share (verdict, risk, level, reasons, flags, warnings).
5
+ */
6
+ const BAD = /\b(likely scam|scam|malicious|danger|dangerous|spoof|spoofed|high|critical|not valid|invalid|leaked|likely edited|edited|sanction|sanctioned|honeypot|poisoned|cannot sell|vulnerable|exploited|unprotected|not protected|fake|fraud)\b/i;
7
+ const WARN = /\b(suspicious|caution|medium|moderate|partly|warn|warning|signs of|unknown|unverified|elevated|risky|check)\b/i;
8
+ const OK = /\b(ok|safe|valid|protected|clean|low|no known|not found|none found|no signs|legit|genuine|active|operational|no recalls)\b/i;
9
+ export function toneOf(verdict, risk) {
10
+ if (verdict) {
11
+ // "no signs of editing", "no known threats": a negated finding is good news, whatever words follow.
12
+ if (/^(no|nothing|none)\b|\b(not found|none found)\b/i.test(verdict.trim()))
13
+ return 'ok';
14
+ if (BAD.test(verdict) && !/\bno\b|not (a )?scam/i.test(verdict))
15
+ return 'bad';
16
+ if (WARN.test(verdict))
17
+ return 'warn';
18
+ if (OK.test(verdict))
19
+ return 'ok';
20
+ }
21
+ if (risk !== null)
22
+ return risk >= 70 ? 'bad' : risk >= 35 ? 'warn' : 'ok';
23
+ return 'info';
24
+ }
25
+ const text = (v) => {
26
+ if (typeof v === 'string')
27
+ return v.trim() || null;
28
+ if (v && typeof v === 'object') {
29
+ const o = v;
30
+ for (const k of ['text', 'title', 'reason', 'message', 'detail', 'name'])
31
+ if (typeof o[k] === 'string' && o[k].trim())
32
+ return o[k].trim();
33
+ }
34
+ return null;
35
+ };
36
+ function pickVerdict(x) {
37
+ for (const k of ['verdict', 'status', 'level', 'grade', 'assessment']) {
38
+ const v = x[k];
39
+ if (typeof v === 'string' && v.trim() && v.length < 60)
40
+ return k === 'level' && !/risk/i.test(v) ? `${v} risk` : v.trim();
41
+ }
42
+ if (typeof x.risk === 'string' && x.risk.length < 30)
43
+ return `${x.risk} risk`;
44
+ if (typeof x.valid === 'boolean')
45
+ return x.valid ? 'valid' : 'not valid';
46
+ return null;
47
+ }
48
+ function pickRisk(x) {
49
+ for (const k of ['risk', 'riskScore', 'score']) {
50
+ const v = x[k];
51
+ if (typeof v === 'number' && Number.isFinite(v) && v >= 0 && v <= 100)
52
+ return Math.round(v);
53
+ }
54
+ return null;
55
+ }
56
+ function pickPoints(x) {
57
+ for (const k of ['reasons', 'flags', 'warnings', 'findings', 'points', 'issues']) {
58
+ const arr = x[k];
59
+ if (Array.isArray(arr) && arr.length) {
60
+ const pts = arr.map(text).filter((t) => Boolean(t));
61
+ if (pts.length)
62
+ return pts.slice(0, 12);
63
+ }
64
+ }
65
+ return [];
66
+ }
67
+ function sourcesOf(out) {
68
+ const s = { live: 0, cached: 0, failed: 0, names: [] };
69
+ const prov = out?.provenance;
70
+ if (Array.isArray(prov)) {
71
+ for (const p of prov) {
72
+ if (p.status === 'LIVE')
73
+ s.live++;
74
+ else if (p.status === 'CACHED')
75
+ s.cached++;
76
+ else
77
+ s.failed++;
78
+ if (!s.names.includes(p.source))
79
+ s.names.push(p.source);
80
+ }
81
+ }
82
+ else if (prov && typeof prov === 'object' && prov.sources) {
83
+ // The compact { sources: { name: { LIVE: n } } } summary used by the web API and MCP.
84
+ for (const [name, counts] of Object.entries(prov.sources)) {
85
+ s.names.push(name);
86
+ s.live += counts.LIVE ?? 0;
87
+ s.cached += counts.CACHED ?? 0;
88
+ s.failed += (counts.ERROR ?? 0) + (counts.UNAVAILABLE ?? 0);
89
+ }
90
+ }
91
+ return s;
92
+ }
93
+ /** The verdict of a module result (or the compact form the API returns). */
94
+ export function verdictOf(out, label) {
95
+ const o = (out ?? {});
96
+ const x = (o.result && typeof o.result === 'object' && !Array.isArray(o.result) ? o.result : o);
97
+ const verdict = pickVerdict(x);
98
+ const risk = pickRisk(x);
99
+ let points = pickPoints(x);
100
+ if (!points.length && Array.isArray(o.signals))
101
+ points = o.signals.map((s) => text(s)).filter((t) => Boolean(t)).slice(0, 8);
102
+ const title = label ?? (typeof o.input === 'string' && o.input ? o.input : typeof x.input === 'string' ? x.input : '');
103
+ return { title: title.slice(0, 160), verdict, tone: toneOf(verdict, risk), risk, points, sources: sourcesOf(o) };
104
+ }
105
+ /** What changed between two verdicts of the same target, in plain words. Empty when nothing did. */
106
+ export function verdictChanges(before, now) {
107
+ const out = [];
108
+ if ((before.verdict ?? '') !== (now.verdict ?? ''))
109
+ out.push(`verdict: ${before.verdict ?? 'none'} -> ${now.verdict ?? 'none'}`);
110
+ if (before.risk !== now.risk && before.risk !== null && now.risk !== null)
111
+ out.push(`risk ${before.risk} -> ${now.risk}`);
112
+ const was = new Set(before.points), is = new Set(now.points);
113
+ for (const p of now.points)
114
+ if (!was.has(p))
115
+ out.push(`new: ${p}`);
116
+ for (const p of before.points)
117
+ if (!is.has(p))
118
+ out.push(`gone: ${p}`);
119
+ return out.slice(0, 12);
120
+ }
121
+ /**
122
+ * Brief mode: a short answer for agents. Verdict, risk, top points, which sources answered, and the check id
123
+ * for the full result. Results without a verdict are trimmed instead (first items of long lists, no long text).
124
+ */
125
+ export function briefOf(out, extra = {}) {
126
+ const v = verdictOf(out);
127
+ if (v.verdict || v.risk !== null) {
128
+ return {
129
+ ...(v.title ? { target: v.title } : {}),
130
+ verdict: v.verdict,
131
+ ...(v.risk !== null ? { risk: v.risk } : {}),
132
+ points: v.points.slice(0, 5),
133
+ sources: `${v.sources.live} live, ${v.sources.cached} cached, ${v.sources.failed} failed${v.sources.names.length ? ` (${v.sources.names.slice(0, 8).join(', ')})` : ''}`,
134
+ ...extra,
135
+ };
136
+ }
137
+ return { ...trim(out, 0), ...extra };
138
+ }
139
+ function trim(v, depth) {
140
+ if (typeof v === 'string')
141
+ return v.length > 280 ? `${v.slice(0, 277)}...` : v;
142
+ if (Array.isArray(v)) {
143
+ const head = v.slice(0, 5).map((x) => trim(x, depth + 1));
144
+ return v.length > 5 ? [...head, `(${v.length - 5} more)`] : head;
145
+ }
146
+ if (v && typeof v === 'object') {
147
+ if (depth > 3)
148
+ return '(...)';
149
+ const o = {};
150
+ for (const [k, x] of Object.entries(v)) {
151
+ if (k === 'provenance' && x && typeof x === 'object' && !Array.isArray(x)) {
152
+ o.sources = Object.keys(x.sources ?? {}).join(', ');
153
+ continue;
154
+ }
155
+ if (k === 'provenance' && Array.isArray(x)) {
156
+ o.sources = [...new Set(x.map((p) => p.source))].join(', ');
157
+ continue;
158
+ }
159
+ o[k] = trim(x, depth + 1);
160
+ }
161
+ return o;
162
+ }
163
+ return v;
164
+ }
@@ -0,0 +1,9 @@
1
+ export interface ZipEntry {
2
+ name: string;
3
+ method: number;
4
+ size: number;
5
+ csize: number;
6
+ offset: number;
7
+ }
8
+ export declare function zipEntries(b: Buffer): ZipEntry[];
9
+ export declare function zipRead(b: Buffer, e: ZipEntry): Buffer | null;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Minimal ZIP reader (central directory, stored and deflated entries) for Office files, which are ZIP packages.
3
+ * Entries are read lazily; sizes are capped so a zip bomb cannot exhaust memory.
4
+ */
5
+ import { inflateRawSync } from 'node:zlib';
6
+ const MAX_ENTRY = 40 * 1024 * 1024;
7
+ export function zipEntries(b) {
8
+ let eocd = -1;
9
+ for (let i = b.length - 22; i >= Math.max(0, b.length - 66_000); i--)
10
+ if (b.readUInt32LE(i) === 0x06054b50) {
11
+ eocd = i;
12
+ break;
13
+ }
14
+ if (eocd < 0)
15
+ throw new Error('not a valid ZIP package');
16
+ const n = b.readUInt16LE(eocd + 10);
17
+ let p = b.readUInt32LE(eocd + 16);
18
+ const out = [];
19
+ for (let k = 0; k < n && p + 46 <= b.length; k++) {
20
+ if (b.readUInt32LE(p) !== 0x02014b50)
21
+ break;
22
+ const method = b.readUInt16LE(p + 10), csize = b.readUInt32LE(p + 20), size = b.readUInt32LE(p + 24);
23
+ const nl = b.readUInt16LE(p + 28), el = b.readUInt16LE(p + 30), cl = b.readUInt16LE(p + 32);
24
+ const offset = b.readUInt32LE(p + 42);
25
+ out.push({ name: b.toString('utf8', p + 46, p + 46 + nl), method, size, csize, offset });
26
+ p += 46 + nl + el + cl;
27
+ }
28
+ return out;
29
+ }
30
+ export function zipRead(b, e) {
31
+ if (e.size > MAX_ENTRY || e.offset + 30 > b.length)
32
+ return null;
33
+ const nl = b.readUInt16LE(e.offset + 26), el = b.readUInt16LE(e.offset + 28);
34
+ const start = e.offset + 30 + nl + el;
35
+ const data = b.subarray(start, start + e.csize);
36
+ try {
37
+ if (e.method === 0)
38
+ return data;
39
+ if (e.method === 8)
40
+ return inflateRawSync(data, { maxOutputLength: MAX_ENTRY });
41
+ }
42
+ catch {
43
+ return null;
44
+ }
45
+ return null;
46
+ }