@acepaste/ingress 0.9.6 → 0.9.22

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 (74) hide show
  1. package/dist/src/adapters.d.ts +9 -0
  2. package/dist/src/adapters.js +76 -6
  3. package/dist/src/agent-hooks.d.ts +112 -0
  4. package/dist/src/agent-hooks.js +533 -0
  5. package/dist/src/approvers.d.ts +16 -0
  6. package/dist/src/approvers.js +50 -0
  7. package/dist/src/audio.d.ts +70 -0
  8. package/dist/src/audio.js +224 -0
  9. package/dist/src/audit-changes.d.ts +46 -0
  10. package/dist/src/audit-changes.js +110 -0
  11. package/dist/src/audit-local.d.ts +57 -0
  12. package/dist/src/audit-local.js +231 -0
  13. package/dist/src/audit-parsers.d.ts +1 -1
  14. package/dist/src/audit.js +48 -0
  15. package/dist/src/broker.d.ts +92 -1
  16. package/dist/src/broker.js +222 -3
  17. package/dist/src/calendar.d.ts +36 -0
  18. package/dist/src/calendar.js +227 -0
  19. package/dist/src/captions.d.ts +44 -0
  20. package/dist/src/captions.js +209 -0
  21. package/dist/src/changes.d.ts +105 -0
  22. package/dist/src/changes.js +174 -0
  23. package/dist/src/character.js +15 -1
  24. package/dist/src/classifier-model.d.ts +53 -0
  25. package/dist/src/classifier-model.js +3 -0
  26. package/dist/src/classifier.d.ts +41 -0
  27. package/dist/src/classifier.js +213 -0
  28. package/dist/src/cli.js +501 -1
  29. package/dist/src/egress.d.ts +71 -0
  30. package/dist/src/egress.js +242 -0
  31. package/dist/src/email.d.ts +42 -0
  32. package/dist/src/email.js +424 -0
  33. package/dist/src/engine.d.ts +36 -12
  34. package/dist/src/engine.js +88 -37
  35. package/dist/src/image-meta.d.ts +33 -0
  36. package/dist/src/image-meta.js +433 -0
  37. package/dist/src/image.d.ts +110 -0
  38. package/dist/src/image.js +353 -0
  39. package/dist/src/index.d.ts +50 -5
  40. package/dist/src/index.js +28 -3
  41. package/dist/src/localguard.d.ts +33 -0
  42. package/dist/src/localguard.js +61 -0
  43. package/dist/src/meaning.d.ts +2 -0
  44. package/dist/src/meaning.js +77 -22
  45. package/dist/src/messages.d.ts +79 -0
  46. package/dist/src/messages.js +431 -0
  47. package/dist/src/nearby.d.ts +52 -0
  48. package/dist/src/nearby.js +186 -0
  49. package/dist/src/netpolicy.js +50 -19
  50. package/dist/src/provenance.d.ts +56 -0
  51. package/dist/src/provenance.js +114 -0
  52. package/dist/src/qr.d.ts +25 -0
  53. package/dist/src/qr.js +113 -0
  54. package/dist/src/safeguards.d.ts +61 -0
  55. package/dist/src/safeguards.js +199 -0
  56. package/dist/src/served.d.ts +20 -0
  57. package/dist/src/served.js +53 -0
  58. package/dist/src/shell-commands.d.ts +39 -0
  59. package/dist/src/shell-commands.js +291 -0
  60. package/dist/src/shortcuts.d.ts +45 -0
  61. package/dist/src/shortcuts.js +220 -0
  62. package/dist/src/stream.d.ts +88 -0
  63. package/dist/src/stream.js +119 -0
  64. package/dist/src/terminal.d.ts +29 -0
  65. package/dist/src/terminal.js +313 -0
  66. package/dist/src/tooldefs.d.ts +84 -0
  67. package/dist/src/tooldefs.js +122 -0
  68. package/dist/src/types.d.ts +50 -6
  69. package/dist/src/types.js +15 -2
  70. package/dist/src/visibility.d.ts +23 -3
  71. package/dist/src/visibility.js +285 -78
  72. package/native/ingress-presence.swift +48 -0
  73. package/native/ingress-vision.swift +417 -0
  74. package/package.json +3 -1
@@ -0,0 +1,424 @@
1
+ /**
2
+ * Email (RFC 5322 / MIME text, and the JSON that Gmail, Microsoft Graph and most mail tools return).
3
+ *
4
+ * An email is the easiest way to put text in front of someone else's assistant: no click is needed, only
5
+ * that the assistant reads the inbox. What makes the channel different from a web page:
6
+ *
7
+ * - One message is several documents. A person's mail client shows the HTML part; a tool often reads the
8
+ * plain part, the snippet or the raw headers. Text that only the plain part has is text the person never
9
+ * saw: the email version of serving an AI fetcher a different page.
10
+ * - The sender describes themselves. A display name is free text ("IT Security Team"), Reply-To can point
11
+ * anywhere, and whether the message really came from its From domain is in the authentication results.
12
+ * - It fetches. Remote images and stylesheets load when a message is rendered, which is how a message
13
+ * carries data out (the image URL is the request). Ingress never loads them: they are listed.
14
+ * - Marketing mail hides text on purpose (a "preheader" shown only in the inbox list) and is full of
15
+ * tracking pixels. That is furniture: recorded, and never a refusal unless an instruction is in it.
16
+ *
17
+ * The adapter turns a message into segments with the visibility a person had of each piece, plus findings
18
+ * about the message's own structure. Attachments it can read as text are read; the rest are listed as not
19
+ * inspected, so a clean verdict never means "not looked at".
20
+ */
21
+ import { analyzeHtml } from "./visibility.js";
22
+ import { passagesOnlyIn } from "./served.js";
23
+ import { adaptCalendar } from "./calendar.js";
24
+ const MAX_DEPTH = 8, MAX_PARTS = 300;
25
+ // ---------- MIME ----------
26
+ function decodeWords(v) {
27
+ // RFC 2047: =?charset?B|Q?text?=, adjacent words joined without the space between them
28
+ return v.replace(/(=\?[^?\s]+\?[bBqQ]\?[^?\s]*\?=)(\s+(?==\?))?/g, (_, w) => {
29
+ const m = w.match(/^=\?([^?]+)\?([bBqQ])\?([^?]*)\?=$/);
30
+ if (!m)
31
+ return w;
32
+ try {
33
+ const bytes = m[2].toUpperCase() === "B" ? Buffer.from(m[3], "base64") : Buffer.from(m[3].replace(/_/g, " ").replace(/=([0-9A-Fa-f]{2})/g, (_x, h) => String.fromCharCode(parseInt(h, 16))), "latin1");
34
+ return new TextDecoder(m[1].replace(/\*.*$/, "").toLowerCase(), { fatal: false }).decode(bytes);
35
+ }
36
+ catch {
37
+ return w;
38
+ }
39
+ });
40
+ }
41
+ function parseHeaders(block) {
42
+ const out = new Map();
43
+ for (const line of block.replace(/\n[ \t]+/g, " ").split("\n")) {
44
+ const i = line.indexOf(":");
45
+ if (i <= 0)
46
+ continue;
47
+ const name = line.slice(0, i).trim().toLowerCase();
48
+ const value = decodeWords(line.slice(i + 1).trim());
49
+ // repeated headers (Received, Authentication-Results) are kept together
50
+ out.set(name, out.has(name) ? `${out.get(name)}\n${value}` : value);
51
+ }
52
+ return out;
53
+ }
54
+ function parseParams(v) {
55
+ const [type, ...rest] = v.split(/;(?=(?:[^"]*"[^"]*")*[^"]*$)/);
56
+ const params = {};
57
+ for (const p of rest) {
58
+ const eq = p.indexOf("=");
59
+ if (eq <= 0)
60
+ continue;
61
+ let k = p.slice(0, eq).trim().toLowerCase(), val = p.slice(eq + 1).trim().replace(/^"|"$/g, "");
62
+ if (k.endsWith("*")) { // RFC 2231: name*=utf-8''percent-encoded
63
+ k = k.slice(0, -1);
64
+ const m = val.match(/^([^']*)'[^']*'(.*)$/);
65
+ if (m) {
66
+ try {
67
+ val = decodeURIComponent(m[2]);
68
+ }
69
+ catch {
70
+ val = m[2];
71
+ }
72
+ }
73
+ }
74
+ params[k.replace(/\*\d+$/, "")] = (params[k.replace(/\*\d+$/, "")] ?? "") + decodeWords(val);
75
+ }
76
+ return { type: (type ?? "").trim().toLowerCase(), params };
77
+ }
78
+ function decodeBody(body, encoding, charset) {
79
+ let buf;
80
+ const enc = encoding.trim().toLowerCase();
81
+ if (enc === "base64")
82
+ buf = Buffer.from(body.replace(/[^A-Za-z0-9+/=]/g, ""), "base64");
83
+ else if (enc === "quoted-printable") {
84
+ // =XX is one byte; anything else is itself. A body that carries raw non-ASCII characters (it should not,
85
+ // but mail in the wild does) keeps them: they are written out as the UTF-8 they arrived as.
86
+ const chunks = [];
87
+ const src = body.replace(/=\r?\n/g, "");
88
+ let last = 0;
89
+ for (const m of src.matchAll(/=([0-9A-Fa-f]{2})/g)) {
90
+ if (m.index > last)
91
+ chunks.push(Buffer.from(src.slice(last, m.index), "utf8"));
92
+ chunks.push(Buffer.from([parseInt(m[1], 16)]));
93
+ last = m.index + 3;
94
+ }
95
+ if (last < src.length)
96
+ chunks.push(Buffer.from(src.slice(last), "utf8"));
97
+ buf = Buffer.concat(chunks);
98
+ }
99
+ else
100
+ return { text: body, bytes: Buffer.byteLength(body) }; // 7bit, 8bit, binary: already text as received
101
+ let text;
102
+ try {
103
+ text = new TextDecoder(charset || "utf-8", { fatal: false }).decode(buf);
104
+ }
105
+ catch {
106
+ text = buf.toString("utf8");
107
+ }
108
+ return { text, bytes: buf.length };
109
+ }
110
+ const TEXTLIKE = /^(text\/|application\/(json|xml|xhtml\+xml|ics|x-ics|ld\+json|javascript|x-sh|x-yaml|yaml)|message\/rfc822)/;
111
+ function parsePart(raw, depth, budget) {
112
+ const split = raw.search(/\n\n/);
113
+ const headBlock = split < 0 ? raw : raw.slice(0, split);
114
+ const body = split < 0 ? "" : raw.slice(split + 2);
115
+ const headers = parseHeaders(headBlock);
116
+ const ct = parseParams(headers.get("content-type") ?? "text/plain");
117
+ const disp = parseParams(headers.get("content-disposition") ?? "");
118
+ const filename = disp.params.filename ?? ct.params.name;
119
+ const part = { contentType: ct.type || "text/plain", params: ct.params, headers, filename, bytes: Buffer.byteLength(body), parts: [] };
120
+ if (part.contentType.startsWith("multipart/") && ct.params.boundary && depth < MAX_DEPTH) {
121
+ const b = ct.params.boundary;
122
+ const pieces = body.split(new RegExp(`(?:^|\\n)--${b.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:--)?[ \\t]*(?:\\n|$)`));
123
+ for (const piece of pieces.slice(1)) {
124
+ if (!piece.trim() || budget.n++ > MAX_PARTS)
125
+ continue;
126
+ part.parts.push(parsePart(piece.replace(/^\n+/, ""), depth + 1, budget));
127
+ }
128
+ return part;
129
+ }
130
+ if (TEXTLIKE.test(part.contentType)) {
131
+ const d = decodeBody(body, headers.get("content-transfer-encoding") ?? "", (ct.params.charset ?? "").toLowerCase());
132
+ part.text = d.text;
133
+ part.bytes = d.bytes;
134
+ }
135
+ return part;
136
+ }
137
+ /** Parse a raw message (headers, blank line, body). Tolerant of bare LF and of missing MIME headers. */
138
+ export function parseEmail(raw) {
139
+ return parsePart(raw.replace(/\r\n?/g, "\n").replace(/^\s*\n/, ""), 0, { n: 0 });
140
+ }
141
+ const leaves = (p) => (p.parts.length ? p.parts.flatMap(leaves) : [p]);
142
+ function parseAddress(v) {
143
+ if (!v)
144
+ return undefined;
145
+ const m = v.match(/^\s*(?:"?([^"<]*?)"?\s*)?<([^<>\s]+@[^<>\s]+)>\s*$/) ?? v.match(/^\s*()([^\s<>@]+@[^\s<>]+)\s*$/);
146
+ if (!m)
147
+ return undefined;
148
+ const address = m[2].toLowerCase();
149
+ return { name: (m[1] ?? "").trim(), address, domain: address.split("@")[1] ?? "" };
150
+ }
151
+ /** the part of a host a person recognises: example.co.uk, example.com */
152
+ const site = (domain) => { const p = domain.split("."); const tld2 = /^(co|com|org|net|gov|ac|edu)$/.test(p[p.length - 2] ?? "") && (p[p.length - 1] ?? "").length === 2; return p.slice(tld2 ? -3 : -2).join("."); };
153
+ const FREEMAIL = /^(gmail|googlemail|outlook|hotmail|live|yahoo|ymail|aol|icloud|me|proton|protonmail|pm|gmx|mail|yandex|zoho|qq|163|126)\.[a-z.]+$/;
154
+ const AUTHORITY_NAME = /\b(security|support|help ?desk|it (team|department|admin)|administrator|admin|billing|accounts? (team|payable)|payroll|human resources|hr (team|department)|compliance|legal|ceo|cfo|system|no-?reply|postmaster|microsoft|google|apple|amazon|paypal|anthropic|openai|github|bank)\b/i;
155
+ function senderFindings(h) {
156
+ const out = [];
157
+ const from = parseAddress(h.get("from"));
158
+ if (!from)
159
+ return out;
160
+ const replyTo = parseAddress((h.get("reply-to") ?? "").split(",")[0]);
161
+ // A mailing list or a newsletter service sets Reply-To on purpose and says what it is (List-Id,
162
+ // List-Unsubscribe, Precedence: bulk). There the mismatch is how the list works, and it is not reported.
163
+ const declaredList = h.has("list-id") || h.has("list-unsubscribe") || h.has("list-post") || /\b(bulk|list)\b/i.test(h.get("precedence") ?? "") || h.has("mailing-list") || h.has("x-mailing-list");
164
+ if (replyTo && site(replyTo.domain) !== site(from.domain) && !declaredList) {
165
+ // on its own this is weak: plenty of people answer from one address and read another
166
+ out.push({ pass: "channel", kind: "email.sender_mismatch", confidence: 0.2, severity: 0.4, message: `replies go to ${replyTo.domain}, not to the sender's ${from.domain}`, excerpt: `From ${from.address}, Reply-To ${replyTo.address}`, location: "email/header:reply-to", action: "none" });
167
+ }
168
+ // a display name that is itself an address, at a different site than the real one
169
+ const inName = from.name.match(/[^\s<>@"]+@([a-z0-9.-]+\.[a-z]{2,})/i);
170
+ if (inName && site(inName[1].toLowerCase()) !== site(from.domain)) {
171
+ out.push({ pass: "channel", kind: "email.sender_mismatch", confidence: 0.6, severity: 0.7, message: `the sender's display name shows an address at ${inName[1].toLowerCase()}, but the message is from ${from.domain}`, excerpt: `${from.name} <${from.address}>`, location: "email/header:from", action: "none" });
172
+ }
173
+ else if (from.name && AUTHORITY_NAME.test(from.name) && FREEMAIL.test(from.domain)) {
174
+ out.push({ pass: "channel", kind: "email.sender_mismatch", confidence: 0.35, severity: 0.6, message: `the sender calls itself "${from.name.slice(0, 60)}" and writes from a free mail address (${from.domain})`, excerpt: `${from.name} <${from.address}>`, location: "email/header:from", action: "none" });
175
+ }
176
+ // what the receiving server concluded about where it really came from
177
+ const auth = (h.get("authentication-results") ?? "").toLowerCase();
178
+ const failed = ["dmarc", "spf", "dkim"].filter((k) => new RegExp(`\\b${k}=(fail|softfail|permerror)\\b`).test(auth));
179
+ if (failed.includes("dmarc") || failed.length >= 2) {
180
+ out.push({ pass: "channel", kind: "email.authentication_failed", confidence: 0.45, severity: 0.6, message: `the receiving server could not confirm the sender (${failed.join(", ")} failed)`, location: "email/header:authentication-results", action: "none" });
181
+ }
182
+ return out;
183
+ }
184
+ // ---------- adapter ----------
185
+ /** headers a mail client shows a person */
186
+ const SHOWN_HEADERS = ["subject", "from", "to", "cc", "reply-to", "date"];
187
+ /** headers that are routing and bookkeeping, not prose */
188
+ const BOOKKEEPING = /^(received|authentication-results|arc-|dkim-signature|domainkey-|x-google-|x-gm-|x-ms-|x-microsoft-|x-forefront-|x-originating-|x-received|message-id|in-reply-to|references|mime-version|content-|return-path|delivered-to|list-(unsubscribe|id|post|help|archive|subscribe|owner)|precedence|feedback-id|x-mailer|x-spam|x-virus|x-sg-|x-ses-|x-mc-|x-mailgun-|x-pm-|received-spf|errors-to|sender|thread-|importance|x-priority|priority|user-agent|x-report-abuse|x-campaign|x-csa-|bimi-|x-entity-ref-id|auto-submitted)/;
189
+ function adaptParsed(root, path, segments, findings, depth) {
190
+ const h = root.headers;
191
+ for (const name of SHOWN_HEADERS) {
192
+ const v = h.get(name);
193
+ if (v)
194
+ segments.push({ text: v, visibility: "visible", path: `${path}/header:${name}` });
195
+ }
196
+ // any other header with prose in it: a tool reading raw headers sees it, a person never does
197
+ for (const [name, v] of h) {
198
+ if (SHOWN_HEADERS.includes(name) || BOOKKEEPING.test(name))
199
+ continue;
200
+ if (v.trim().split(/\s+/).length >= 4)
201
+ segments.push({ text: v, visibility: "attribute", path: `${path}/header:${name}` });
202
+ }
203
+ findings.push(...senderFindings(h));
204
+ const all = leaves(root);
205
+ const isAttachment = (p) => !!p.filename || /attachment/i.test(p.headers.get("content-disposition") ?? "");
206
+ const htmlParts = all.filter((p) => p.contentType === "text/html" && !isAttachment(p) && p.text);
207
+ const plainParts = all.filter((p) => p.contentType === "text/plain" && !isAttachment(p) && p.text);
208
+ const html = htmlParts.map((p) => p.text).join("\n");
209
+ const plain = plainParts.map((p) => p.text).join("\n");
210
+ let remote = [];
211
+ if (html.trim()) {
212
+ const v = analyzeHtml(html);
213
+ for (const s of v.segments)
214
+ segments.push({ ...s, path: `${path}/html${s.path}` });
215
+ findings.push(...v.findings.map((f) => ({ ...f, location: `${path}/html${f.location ?? ""}` })));
216
+ remote = [...html.matchAll(/<(?:img|link|script|iframe|source|video|audio|embed|object|input)\b[^>]*?\b(?:src|href|data|background|poster)\s*=\s*["']?(https?:\/\/[^"'\s>]+)/gi), ...html.matchAll(/url\(\s*["']?(https?:\/\/[^"')\s]+)/gi)].map((m) => m[1]);
217
+ if (plain.trim()) {
218
+ // what the plain part says that the HTML part, hidden text included, does not say anywhere
219
+ const only = passagesOnlyIn(plain, v.segments.map((s) => s.text).join(" "));
220
+ if (only.length) {
221
+ findings.push({ pass: "channel", kind: "email.part_divergence", confidence: 0.3, severity: 0.6, message: `the plain-text part says ${only.length} thing(s) the part a mail client shows does not`, excerpt: only[0].slice(0, 160), location: `${path}/text`, action: "none" });
222
+ for (const [i, t] of only.entries())
223
+ segments.push({ text: t, visibility: "concealed", path: `${path}/text~plain-only[${i}]` });
224
+ }
225
+ }
226
+ }
227
+ else if (plain.trim()) {
228
+ segments.push({ text: plain, visibility: "visible", path: `${path}/text` });
229
+ }
230
+ if (remote.length) {
231
+ const hosts = [...new Set(remote.map((u) => { try {
232
+ return new URL(u).hostname;
233
+ }
234
+ catch {
235
+ return "";
236
+ } }).filter(Boolean))];
237
+ // informational: Ingress did not load them, and says so
238
+ findings.push({ pass: "channel", kind: "email.remote_content", confidence: 0, severity: 0, message: `${remote.length} remote resource(s) from ${hosts.length} host(s) were not loaded: ${hosts.slice(0, 6).join(", ")}`, location: `${path}/html`, action: "none" });
239
+ // A remote address with a blank to fill in ("?d={{conversation}}") is a request waiting for data: whoever
240
+ // renders the message, or an assistant asked to "show the image", completes it. An opaque token in a
241
+ // tracking pixel is not that, and every newsletter has one.
242
+ const templated = remote.find((u) => /[?&][^=&#]*=[^&#]*(?:\{\{|\$\{|%7B%7B|\{[a-z_ ]{3,}\}|\[(?:secret|key|token|context|conversation|summary|data|history|prompt|messages?)\])/i.test(u));
243
+ if (templated)
244
+ findings.push({ pass: "channel", kind: "meaning.exfil_link", confidence: 0.7, severity: 0.9, message: "a remote resource address in the message has a placeholder to be filled with data", excerpt: templated.slice(0, 120), location: `${path}/html`, action: "neutralized" });
245
+ }
246
+ const skipped = [];
247
+ let n = 0;
248
+ for (const p of all) {
249
+ if (htmlParts.includes(p) || plainParts.includes(p))
250
+ continue;
251
+ const label = p.filename ?? p.contentType;
252
+ if (p.filename)
253
+ segments.push({ text: p.filename, visibility: "visible", path: `${path}/attachment[${n}]@name` });
254
+ if (p.text === undefined) {
255
+ skipped.push(`${label} (${p.contentType}, ${p.bytes} bytes)`);
256
+ n++;
257
+ continue;
258
+ }
259
+ const at = `${path}/attachment[${n++}]`;
260
+ if (/calendar|\bics\b/.test(p.contentType) || /\.ics$/i.test(p.filename ?? "")) {
261
+ const c = adaptCalendar(p.text);
262
+ for (const s of c.segments)
263
+ segments.push({ ...s, path: `${at}/${s.path}` });
264
+ findings.push(...c.findings);
265
+ }
266
+ else if (p.contentType === "message/rfc822" && depth < 4) {
267
+ adaptParsed(parseEmail(p.text), `${at}/message`, segments, findings, depth + 1);
268
+ }
269
+ else if (p.contentType === "text/html" || /\.html?$/i.test(p.filename ?? "")) {
270
+ const v = analyzeHtml(p.text);
271
+ for (const s of v.segments)
272
+ segments.push({ ...s, path: `${at}${s.path}` });
273
+ findings.push(...v.findings);
274
+ }
275
+ else {
276
+ // a text attachment is read when someone opens it: the reader sees all of it
277
+ segments.push({ text: p.text, visibility: "visible", path: at });
278
+ }
279
+ }
280
+ if (skipped.length)
281
+ findings.push({ pass: "channel", kind: "email.uninspected_part", confidence: 0, severity: 0, message: `${skipped.length} attachment(s) could not be read as text and were not inspected: ${skipped.slice(0, 5).join("; ")}`, location: `${path}/attachments`, action: "none" });
282
+ }
283
+ const str = (v) => (typeof v === "string" ? v : "");
284
+ const b64url = (s) => Buffer.from(s.replace(/-/g, "+").replace(/_/g, "/"), "base64").toString("utf8");
285
+ /** Gmail API (users.messages.get, format=full) payload -> the same tree a raw message parses to. */
286
+ function fromGmailPayload(p, depth = 0) {
287
+ const headers = new Map();
288
+ for (const h of Array.isArray(p.headers) ? p.headers : []) {
289
+ if (h && typeof h === "object") {
290
+ const n = str(h.name).toLowerCase();
291
+ if (n)
292
+ headers.set(n, headers.has(n) ? `${headers.get(n)}\n${str(h.value)}` : str(h.value));
293
+ }
294
+ }
295
+ const ct = parseParams(headers.get("content-type") ?? str(p.mimeType));
296
+ const type = str(p.mimeType).toLowerCase() || ct.type || "text/plain";
297
+ const body = (p.body ?? {});
298
+ const part = { contentType: type, params: ct.params, headers, filename: str(p.filename) || undefined, bytes: Number(body.size) || 0, parts: [] };
299
+ if (Array.isArray(p.parts) && depth < MAX_DEPTH)
300
+ part.parts = p.parts.filter((x) => !!x && typeof x === "object").slice(0, MAX_PARTS).map((x) => fromGmailPayload(x, depth + 1));
301
+ else if (typeof body.data === "string" && TEXTLIKE.test(type))
302
+ part.text = b64url(body.data);
303
+ return part;
304
+ }
305
+ /** A message as Microsoft Graph, or a simple mail tool, returns it -> the same tree. */
306
+ function fromFlatJson(m) {
307
+ const headers = new Map();
308
+ const addr = (v) => {
309
+ if (typeof v === "string")
310
+ return v;
311
+ if (Array.isArray(v))
312
+ return v.map(addr).filter(Boolean).join(", ");
313
+ if (v && typeof v === "object") {
314
+ const e = (v.emailAddress ?? v);
315
+ const a = str(e.address) || str(e.email);
316
+ const n = str(e.name);
317
+ return a ? (n ? `${n} <${a}>` : a) : n;
318
+ }
319
+ return "";
320
+ };
321
+ const set = (k, v) => { if (v)
322
+ headers.set(k, v); };
323
+ set("subject", str(m.subject));
324
+ set("from", addr(m.from ?? m.sender));
325
+ set("to", addr(m.toRecipients ?? m.to));
326
+ set("cc", addr(m.ccRecipients ?? m.cc));
327
+ set("reply-to", addr(m.replyTo ?? m.reply_to ?? m["reply-to"]));
328
+ set("date", str(m.receivedDateTime) || str(m.date));
329
+ for (const h of Array.isArray(m.internetMessageHeaders) ? m.internetMessageHeaders : []) {
330
+ if (h && typeof h === "object") {
331
+ const n = str(h.name).toLowerCase();
332
+ if (n && !headers.has(n))
333
+ headers.set(n, str(h.value));
334
+ }
335
+ }
336
+ const body = m.body && typeof m.body === "object" ? m.body : undefined;
337
+ const htmlText = str(m.html) || str(m.bodyHtml) || str(m.htmlBody) || str(m.body_html) || (body && /html/i.test(str(body.contentType)) ? str(body.content) : "");
338
+ const plainText = str(m.text) || str(m.plain) || str(m.bodyText) || str(m.textBody) || str(m.body_text) || (typeof m.body === "string" ? m.body : "") || (body && !/html/i.test(str(body.contentType)) ? str(body.content) : "") || str(m.bodyPreview) || str(m.snippet);
339
+ const parts = [];
340
+ const leaf = (contentType, text) => ({ contentType, params: {}, headers: new Map(), text, bytes: Buffer.byteLength(text), parts: [] });
341
+ if (plainText)
342
+ parts.push(leaf("text/plain", plainText));
343
+ if (htmlText)
344
+ parts.push(leaf("text/html", htmlText));
345
+ for (const a of Array.isArray(m.attachments) ? m.attachments : []) {
346
+ if (!a || typeof a !== "object")
347
+ continue;
348
+ const A = a;
349
+ const type = (str(A.contentType) || str(A.mimeType) || "application/octet-stream").toLowerCase();
350
+ const p = { contentType: type, params: {}, headers: new Map(), filename: str(A.name) || str(A.filename) || undefined, bytes: Number(A.size) || 0, parts: [] };
351
+ const data = str(A.contentBytes) || str(A.data) || str(A.content);
352
+ if (data && TEXTLIKE.test(type)) {
353
+ try {
354
+ p.text = str(A.content) ? str(A.content) : Buffer.from(data, "base64").toString("utf8");
355
+ }
356
+ catch { /* not text after all */ }
357
+ }
358
+ parts.push(p);
359
+ }
360
+ return { contentType: "multipart/alternative", params: {}, headers, bytes: 0, parts };
361
+ }
362
+ /** Is this parsed JSON one email message, or a list of them? */
363
+ export function looksLikeEmailJson(doc) {
364
+ const one = (m) => {
365
+ if (!m || typeof m !== "object")
366
+ return false;
367
+ const o = m;
368
+ if (o.payload && typeof o.payload === "object" && Array.isArray(o.payload.headers))
369
+ return true; // Gmail
370
+ const hasSender = "from" in o || "sender" in o;
371
+ const hasBody = "body" in o || "bodyPreview" in o || "snippet" in o || "text" in o || "html" in o || "textBody" in o || "htmlBody" in o;
372
+ return "subject" in o && hasSender && hasBody;
373
+ };
374
+ if (Array.isArray(doc))
375
+ return doc.length > 0 && doc.every(one);
376
+ if (doc && typeof doc === "object") {
377
+ const d = doc;
378
+ for (const k of ["messages", "value", "items", "emails", "results"])
379
+ if (Array.isArray(d[k]) && d[k].length && d[k].every(one))
380
+ return true;
381
+ }
382
+ return one(doc);
383
+ }
384
+ export function adaptEmail(raw) {
385
+ const segments = [];
386
+ const findings = [];
387
+ const head = raw.slice(0, 2000).trimStart();
388
+ if (head.startsWith("{") || head.startsWith("[")) {
389
+ let doc;
390
+ try {
391
+ doc = JSON.parse(raw);
392
+ }
393
+ catch {
394
+ doc = undefined;
395
+ }
396
+ if (doc !== undefined) {
397
+ const d = doc;
398
+ const list = Array.isArray(doc) ? doc : ["messages", "value", "items", "emails", "results"].map((k) => d?.[k]).find(Array.isArray) ?? [doc];
399
+ list.slice(0, 500).forEach((m, i) => {
400
+ if (!m || typeof m !== "object")
401
+ return;
402
+ const o = m;
403
+ const root = o.payload && typeof o.payload === "object" ? fromGmailPayload(o.payload) : fromFlatJson(o);
404
+ adaptParsed(root, list.length > 1 ? `email[${i}]` : "email", segments, findings, 0);
405
+ // a snippet is what an inbox list (and many tools) show before the message is opened
406
+ if (o.payload && str(o.snippet))
407
+ segments.push({ text: str(o.snippet), visibility: "visible", path: `${list.length > 1 ? `email[${i}]` : "email"}/snippet` });
408
+ });
409
+ return { segments, findings };
410
+ }
411
+ }
412
+ adaptParsed(parseEmail(raw), "email", segments, findings, 0);
413
+ return { segments, findings };
414
+ }
415
+ /** Does this text look like a raw message: header lines first, with the headers every message has? */
416
+ export function looksLikeRawEmail(text) {
417
+ const head = text.slice(0, 8000).replace(/\r\n?/g, "\n");
418
+ const block = head.split(/\n\n/)[0];
419
+ if (!/^[A-Za-z][A-Za-z0-9-]*:[ \t]/.test(block))
420
+ return false;
421
+ const names = new Set([...block.matchAll(/^([A-Za-z][A-Za-z0-9-]*):/gm)].map((m) => m[1].toLowerCase()));
422
+ const core = ["from", "to", "subject", "date", "message-id", "received", "mime-version", "return-path", "delivered-to"].filter((n) => names.has(n)).length;
423
+ return names.has("from") && core >= 3;
424
+ }
@@ -12,8 +12,11 @@
12
12
  */
13
13
  import { type KeyObject } from "node:crypto";
14
14
  import { acquireWithBrowser, type FetchLike } from "./acquire.js";
15
+ import { type VisionRunner } from "./image.js";
16
+ import { type AudioRunner } from "./audio.js";
17
+ import { servedWords, compareServed } from "./served.js";
15
18
  import { runSandbox } from "./sandbox.js";
16
- import { type Finding, type IngressConfig, type IngressResult, type TaintReceipt, type Tier } from "./types.js";
19
+ import { type ContentKind, type Finding, type IngressConfig, type IngressResult, type SourceInfo, type TaintReceipt, type Tier } from "./types.js";
17
20
  export interface IngressOptions {
18
21
  /** human explicitly asked for a suspect page anyway; delivers neutralized text. Never applies to hostile. */
19
22
  override?: boolean;
@@ -71,7 +74,37 @@ export declare class IngressEngine {
71
74
  scan(content: string, opts?: IngressOptions & {
72
75
  url?: string;
73
76
  isHtml?: boolean;
74
- contentType?: string;
77
+ contentType?: string; /** the channel, when the caller knows it; otherwise it is detected */
78
+ kind?: ContentKind; /** where it came from and how far that source is trusted */
79
+ source?: SourceInfo; /** for captions: what was actually said (a transcript of the audio), and speakers from outside the organisation */
80
+ spoken?: string;
81
+ outsideSpeakers?: string[]; /** for nearby devices: the ones the person knows, for lookalike checks */
82
+ knownDevices?: Array<{
83
+ name: string;
84
+ address?: string;
85
+ }>;
86
+ }): Promise<IngressResult>;
87
+ /**
88
+ * Gate an image: what it says to a person, and what it says only to a machine (see image.ts).
89
+ * The delivered text is the text a person can read in the picture, with a line for each code in it.
90
+ * Whether the picture itself goes on to the model is the caller's decision, made on the verdict.
91
+ */
92
+ scanImage(bytes: Uint8Array, opts?: IngressOptions & {
93
+ url?: string;
94
+ source?: SourceInfo; /** words shown beside the image: a caption, alt text, a file name */
95
+ caption?: string; /** for a screenshot: the text the page's own source says is on it (see image.ts) */
96
+ sourceText?: string; /** how to read the pixels (default: the vision helper when built); false reads the container only */
97
+ vision?: VisionRunner | false;
98
+ }): Promise<IngressResult>;
99
+ /**
100
+ * Gate a sound recording: what is said in it, and what is there only for a machine (see audio.ts).
101
+ * The delivered text is the transcript of what a listener hears. `captions`, when given, are compared with
102
+ * what was said.
103
+ */
104
+ scanAudio(bytes: Uint8Array, opts?: IngressOptions & {
105
+ url?: string;
106
+ source?: SourceInfo; /** how to transcribe (default: the command in INGRESS_AUDIO_TRANSCRIBER); false reads the container only */
107
+ transcriber?: AudioRunner | false;
75
108
  }): Promise<IngressResult>;
76
109
  /**
77
110
  * Gate what the model will actually receive when several sources are combined (search
@@ -145,16 +178,7 @@ export declare const VISIBILITY_ONLY_CAP = 0.2;
145
178
  export declare function aggregate(findings: Finding[], ctx?: {
146
179
  cloakingCompared?: boolean;
147
180
  }): number;
148
- /** words of served text with markup, link targets and bare URLs removed, so Markdown and HTML of the same page compare equal */
149
- export declare function servedWords(text: string): string[];
150
- /** how much of what the agent received is absent from what the browser received (word occurrences, not distinct words) */
151
- export declare function compareServed(agentText: string, humanText: string): {
152
- agentWords: number;
153
- agentOnlyWords: number;
154
- agentOnlyShare: number;
155
- longestAgentOnlyRun: number;
156
- sample: string;
157
- };
181
+ export { servedWords, compareServed };
158
182
  /**
159
183
  * Spotlighting-style envelope. Random boundary so content cannot forge the
160
184
  * closing marker. The model is told, in one line, how to treat the content.