@acepaste/ingress 0.9.1 → 0.9.21

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 (81) hide show
  1. package/LICENSE +1 -1
  2. package/dist/src/adapters.d.ts +9 -0
  3. package/dist/src/adapters.js +76 -6
  4. package/dist/src/agent-hooks.d.ts +112 -0
  5. package/dist/src/agent-hooks.js +533 -0
  6. package/dist/src/approvers.d.ts +16 -0
  7. package/dist/src/approvers.js +50 -0
  8. package/dist/src/audio.d.ts +70 -0
  9. package/dist/src/audio.js +224 -0
  10. package/dist/src/audit-changes.d.ts +46 -0
  11. package/dist/src/audit-changes.js +110 -0
  12. package/dist/src/audit-local.d.ts +57 -0
  13. package/dist/src/audit-local.js +231 -0
  14. package/dist/src/audit-parsers.d.ts +1 -1
  15. package/dist/src/audit.js +48 -0
  16. package/dist/src/broker.d.ts +92 -1
  17. package/dist/src/broker.js +222 -3
  18. package/dist/src/calendar.d.ts +36 -0
  19. package/dist/src/calendar.js +227 -0
  20. package/dist/src/captions.d.ts +44 -0
  21. package/dist/src/captions.js +209 -0
  22. package/dist/src/changes.d.ts +105 -0
  23. package/dist/src/changes.js +174 -0
  24. package/dist/src/character.js +19 -3
  25. package/dist/src/classifier-model.d.ts +53 -0
  26. package/dist/src/classifier-model.js +3 -0
  27. package/dist/src/classifier.d.ts +41 -0
  28. package/dist/src/classifier.js +213 -0
  29. package/dist/src/cli.js +501 -1
  30. package/dist/src/codepass.d.ts +8 -0
  31. package/dist/src/codepass.js +39 -1
  32. package/dist/src/egress.d.ts +71 -0
  33. package/dist/src/egress.js +242 -0
  34. package/dist/src/email.d.ts +42 -0
  35. package/dist/src/email.js +424 -0
  36. package/dist/src/engine.d.ts +35 -12
  37. package/dist/src/engine.js +172 -45
  38. package/dist/src/image-meta.d.ts +33 -0
  39. package/dist/src/image-meta.js +433 -0
  40. package/dist/src/image.d.ts +104 -0
  41. package/dist/src/image.js +347 -0
  42. package/dist/src/index.d.ts +50 -5
  43. package/dist/src/index.js +28 -3
  44. package/dist/src/localguard.d.ts +33 -0
  45. package/dist/src/localguard.js +61 -0
  46. package/dist/src/meaning.d.ts +2 -0
  47. package/dist/src/meaning.js +348 -25
  48. package/dist/src/messages.d.ts +79 -0
  49. package/dist/src/messages.js +431 -0
  50. package/dist/src/multilingual.d.ts +3 -0
  51. package/dist/src/multilingual.js +117 -5
  52. package/dist/src/nearby.d.ts +52 -0
  53. package/dist/src/nearby.js +186 -0
  54. package/dist/src/netpolicy.js +50 -19
  55. package/dist/src/normalize.d.ts +36 -1
  56. package/dist/src/normalize.js +186 -4
  57. package/dist/src/provenance.d.ts +56 -0
  58. package/dist/src/provenance.js +114 -0
  59. package/dist/src/qr.d.ts +25 -0
  60. package/dist/src/qr.js +113 -0
  61. package/dist/src/safeguards.d.ts +61 -0
  62. package/dist/src/safeguards.js +199 -0
  63. package/dist/src/served.d.ts +20 -0
  64. package/dist/src/served.js +53 -0
  65. package/dist/src/shell-commands.d.ts +39 -0
  66. package/dist/src/shell-commands.js +291 -0
  67. package/dist/src/shortcuts.d.ts +45 -0
  68. package/dist/src/shortcuts.js +220 -0
  69. package/dist/src/stream.d.ts +88 -0
  70. package/dist/src/stream.js +119 -0
  71. package/dist/src/terminal.d.ts +29 -0
  72. package/dist/src/terminal.js +313 -0
  73. package/dist/src/tooldefs.d.ts +84 -0
  74. package/dist/src/tooldefs.js +122 -0
  75. package/dist/src/types.d.ts +50 -6
  76. package/dist/src/types.js +15 -2
  77. package/dist/src/visibility.d.ts +23 -3
  78. package/dist/src/visibility.js +285 -78
  79. package/native/ingress-presence.swift +48 -0
  80. package/native/ingress-vision.swift +417 -0
  81. package/package.json +4 -2
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Nearby: what devices around you say about themselves.
3
+ *
4
+ * A Bonjour service name, a Bluetooth device name, a printer's description, a beacon's link: each is a string
5
+ * chosen by whoever owns the device, broadcast to everyone in range, and shown in pickers and lists that an
6
+ * agent may read ("which printer should I use?", "what's on this network?"). Nobody vouches for any of it.
7
+ * It is the lowest-trust content there is (trust level 6), and it is read here as content, never as fact.
8
+ *
9
+ * Checks:
10
+ * instructions in a name or a record the ordinary rules and the classifier's short-text mode
11
+ * authority names "IT Managed Printer", "Apple Setup", "System Update Service"
12
+ * lookalikes a name one or two characters from a device you know, or the same
13
+ * name from a different address
14
+ * links a beacon's or a service's URL is listed, never opened; ones that hide
15
+ * their destination are flagged
16
+ * oversized fields a TXT record or a name far longer than a device needs
17
+ *
18
+ * Ingress cannot stop a broadcast. It gates what an agent reads of it.
19
+ */
20
+ import { type AdapterResult } from "./adapters.js";
21
+ export interface NearbyRecord {
22
+ type: "bonjour" | "bluetooth" | "printer" | "beacon" | "wifi" | "other";
23
+ name: string;
24
+ /** the service type or device class: "_ipp._tcp", "Keyboard" */
25
+ service?: string;
26
+ /** an address that identifies the device: host, MAC, UUID */
27
+ address?: string;
28
+ /** key and value pairs the device publishes (Bonjour TXT, printer make and location) */
29
+ fields?: Record<string, string>;
30
+ url?: string;
31
+ /** paired, connected or added by the person: still its own words, but a device they chose */
32
+ known?: boolean;
33
+ }
34
+ export interface NearbyHints {
35
+ knownDevices?: Array<{
36
+ name: string;
37
+ address?: string;
38
+ }>;
39
+ }
40
+ export declare function looksLikeNearbyJson(doc: unknown): boolean;
41
+ export declare function parseNearby(doc: unknown): NearbyRecord[];
42
+ /** `dns-sd -B <type>` lines: "Timestamp A/R Flags if Domain Service-Type Instance-Name" */
43
+ export declare function parseDnsSdBrowse(text: string): NearbyRecord[];
44
+ /** `system_profiler SPBluetoothDataType -json` */
45
+ export declare function parseBluetoothProfile(json: string): NearbyRecord[];
46
+ /** `lpstat -l -p` blocks: "printer NAME ..." followed by indented "Description:", "Location:" lines */
47
+ export declare function parseLpstatPrinters(text: string): NearbyRecord[];
48
+ /** What this Mac currently lists: Bonjour services, Bluetooth devices, printers. Read-only; nothing is joined, paired or contacted. */
49
+ export declare function collectNearby(opts?: {
50
+ seconds?: number;
51
+ }): Promise<NearbyRecord[]>;
52
+ export declare function adaptNearby(raw: string, hints?: NearbyHints): AdapterResult;
@@ -0,0 +1,186 @@
1
+ /**
2
+ * Nearby: what devices around you say about themselves.
3
+ *
4
+ * A Bonjour service name, a Bluetooth device name, a printer's description, a beacon's link: each is a string
5
+ * chosen by whoever owns the device, broadcast to everyone in range, and shown in pickers and lists that an
6
+ * agent may read ("which printer should I use?", "what's on this network?"). Nobody vouches for any of it.
7
+ * It is the lowest-trust content there is (trust level 6), and it is read here as content, never as fact.
8
+ *
9
+ * Checks:
10
+ * instructions in a name or a record the ordinary rules and the classifier's short-text mode
11
+ * authority names "IT Managed Printer", "Apple Setup", "System Update Service"
12
+ * lookalikes a name one or two characters from a device you know, or the same
13
+ * name from a different address
14
+ * links a beacon's or a service's URL is listed, never opened; ones that hide
15
+ * their destination are flagged
16
+ * oversized fields a TXT record or a name far longer than a device needs
17
+ *
18
+ * Ingress cannot stop a broadcast. It gates what an agent reads of it.
19
+ */
20
+ import { execFile } from "node:child_process";
21
+ import { decodeEntities } from "./adapters.js";
22
+ import { linkTrick } from "./messages.js";
23
+ const str = (v) => (typeof v === "string" ? v : typeof v === "number" ? String(v) : "");
24
+ const TYPES = new Set(["bonjour", "bluetooth", "printer", "beacon", "wifi", "other"]);
25
+ export function looksLikeNearbyJson(doc) {
26
+ const list = Array.isArray(doc) ? doc : doc && typeof doc === "object" && Array.isArray(doc.devices) ? doc.devices : undefined;
27
+ return !!list?.length && list.slice(0, 30).every((r) => !!r && typeof r === "object" && typeof r.name === "string" && typeof r.type === "string" && TYPES.has(String(r.type)));
28
+ }
29
+ export function parseNearby(doc) {
30
+ const list = Array.isArray(doc) ? doc : doc && typeof doc === "object" ? (doc.devices ?? doc.records ?? doc.services) : undefined;
31
+ const out = [];
32
+ for (const r of (Array.isArray(list) ? list : []).slice(0, 2000)) {
33
+ if (!r || typeof r !== "object")
34
+ continue;
35
+ const o = r;
36
+ const name = str(o.name) || str(o.device_name) || str(o.ssid);
37
+ if (!name)
38
+ continue;
39
+ const fields = {};
40
+ for (const src of [o.txt, o.fields, o.attributes])
41
+ if (src && typeof src === "object")
42
+ for (const [k, v] of Object.entries(src).slice(0, 60))
43
+ if (str(v))
44
+ fields[k] = str(v);
45
+ for (const k of ["location", "description", "info", "make_and_model", "manufacturer", "model", "note"])
46
+ if (str(o[k]))
47
+ fields[k] = str(o[k]);
48
+ out.push({ type: TYPES.has(str(o.type)) ? str(o.type) : "other", name, service: str(o.service) || undefined, address: str(o.address) || str(o.host) || str(o.mac) || undefined, fields: Object.keys(fields).length ? fields : undefined, url: str(o.url) || undefined, known: o.known === true || o.paired === true || o.connected === true });
49
+ }
50
+ return out;
51
+ }
52
+ // ---------- reading what the system lists (macOS; read-only) ----------
53
+ /** `dns-sd -B <type>` lines: "Timestamp A/R Flags if Domain Service-Type Instance-Name" */
54
+ export function parseDnsSdBrowse(text) {
55
+ const out = [];
56
+ for (const line of text.split("\n")) {
57
+ const m = line.match(/^\s*\d{1,2}:\d{2}:\d{2}\.\d+\s+Add\s+\d+\s+\d+\s+(\S+)\s+(\S+)\s+(.+?)\s*$/);
58
+ // (the domain column, "local.", is not an address: a browse does not say which host a service is on)
59
+ if (m)
60
+ out.push({ type: "bonjour", name: m[3], service: m[2].replace(/\.$/, "") });
61
+ }
62
+ return out;
63
+ }
64
+ /** `system_profiler SPBluetoothDataType -json` */
65
+ export function parseBluetoothProfile(json) {
66
+ const out = [];
67
+ let doc;
68
+ try {
69
+ doc = JSON.parse(json);
70
+ }
71
+ catch {
72
+ return out;
73
+ }
74
+ for (const ctl of doc?.SPBluetoothDataType ?? []) {
75
+ for (const [key, known] of [["device_connected", true], ["device_not_connected", true], ["devices_list", false]]) {
76
+ for (const entry of ctl[key] ?? []) {
77
+ for (const [name, info] of Object.entries(entry)) {
78
+ const i = (info ?? {});
79
+ out.push({ type: "bluetooth", name, service: str(i.device_minorType) || undefined, address: str(i.device_address) || undefined, known, fields: str(i.device_vendorID) ? { vendor: str(i.device_vendorID) } : undefined });
80
+ }
81
+ }
82
+ }
83
+ }
84
+ return out;
85
+ }
86
+ /** `lpstat -l -p` blocks: "printer NAME ..." followed by indented "Description:", "Location:" lines */
87
+ export function parseLpstatPrinters(text) {
88
+ const out = [];
89
+ let cur;
90
+ for (const line of text.split("\n")) {
91
+ const p = line.match(/^printer\s+(\S+)/);
92
+ if (p) {
93
+ cur = { type: "printer", name: p[1], known: true, fields: {} };
94
+ out.push(cur);
95
+ continue;
96
+ }
97
+ const f = line.match(/^\s+(Description|Location|Interface|Alerts|Connection):\s*(.+)$/);
98
+ if (cur && f && f[2].trim())
99
+ cur.fields[f[1].toLowerCase()] = f[2].trim();
100
+ }
101
+ return out;
102
+ }
103
+ const run = (cmd, args, timeoutMs) => new Promise((resolve) => {
104
+ // dns-sd never exits on its own: it is stopped when the time is up, and what it printed is kept
105
+ execFile(cmd, args, { timeout: timeoutMs, encoding: "utf8", maxBuffer: 16 * 1024 * 1024, killSignal: "SIGTERM" }, (_err, stdout) => resolve(String(stdout ?? "")));
106
+ });
107
+ /** What this Mac currently lists: Bonjour services, Bluetooth devices, printers. Read-only; nothing is joined, paired or contacted. */
108
+ export async function collectNearby(opts = {}) {
109
+ if (process.platform !== "darwin")
110
+ return [];
111
+ const ms = Math.max(1, opts.seconds ?? 3) * 1000;
112
+ const types = parseDnsSdBrowse(await run("/usr/bin/dns-sd", ["-B", "_services._dns-sd._udp", "local."], ms)).map((r) => `${r.name}.${(r.service ?? "").replace(/\.local$/, "")}`).filter((t) => /^_[\w-]+\._(tcp|udp)$/.test(t)).slice(0, 40);
113
+ const browsed = await Promise.all([...new Set(types)].map((t) => run("/usr/bin/dns-sd", ["-B", t, "local."], ms)));
114
+ const [bt, lp] = await Promise.all([run("/usr/sbin/system_profiler", ["SPBluetoothDataType", "-json"], 20_000), run("/usr/bin/lpstat", ["-l", "-p"], 10_000)]);
115
+ return [...browsed.flatMap(parseDnsSdBrowse), ...parseBluetoothProfile(bt), ...parseLpstatPrinters(lp)];
116
+ }
117
+ // ---------- checks ----------
118
+ const AUTHORITY = /\b(?:it[- ]?(?:managed|admin|support|department)|admin(?:istrator)?|system (?:update|service|setup|recovery)|security (?:update|scanner|service)|(?:apple|microsoft|google|windows|macos|ios) (?:setup|support|update|security|recovery|configurator)|device (?:enrollment|management)|mdm|corporate (?:vpn|wifi|network)|free (?:public )?wi-?fi|guest wi-?fi login|software update|firmware update|setup assistant|recovery mode|password (?:reset|recovery))\b/i;
119
+ function distance(a, b) {
120
+ if (Math.abs(a.length - b.length) > 2)
121
+ return 3;
122
+ const prev = Array.from({ length: b.length + 1 }, (_, i) => i);
123
+ for (let i = 1; i <= a.length; i++) {
124
+ let last = prev[0];
125
+ prev[0] = i;
126
+ for (let j = 1; j <= b.length; j++) {
127
+ const t = prev[j];
128
+ prev[j] = Math.min(prev[j] + 1, prev[j - 1] + 1, last + (a[i - 1] === b[j - 1] ? 0 : 1));
129
+ last = t;
130
+ }
131
+ }
132
+ return prev[b.length];
133
+ }
134
+ /** the name with lookalike characters folded, so "Offíce-Printer" and "Office-Printer" compare equal */
135
+ const fold = (s) => s.normalize("NFKD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[0oо]/g, "o").replace(/[1lIіı|]/g, "l").replace(/[аa]/g, "a").replace(/[еe]/g, "e").replace(/[\s_\-.’']+/g, "");
136
+ export function adaptNearby(raw, hints = {}) {
137
+ const segments = [];
138
+ const findings = [];
139
+ const add = (f) => findings.push({ pass: "channel", action: "none", ...f });
140
+ let doc;
141
+ try {
142
+ doc = JSON.parse(raw);
143
+ }
144
+ catch {
145
+ doc = undefined;
146
+ }
147
+ if (doc === undefined || typeof doc !== "object") {
148
+ // a plain list of names, one per line
149
+ segments.push({ text: decodeEntities(typeof doc === "string" ? doc : raw).text, visibility: "visible", path: "nearby/text" });
150
+ return { segments, findings };
151
+ }
152
+ const records = parseNearby(doc);
153
+ const known = [...(hints.knownDevices ?? []), ...records.filter((r) => r.known).map((r) => ({ name: r.name, address: r.address, type: r.type }))];
154
+ // One device shows up in several lists (an iPad over Bluetooth and again over Bonjour), and stock names repeat
155
+ // in every office ("iPad", "HP LaserJet"). The same name at another address means something only within one
156
+ // kind of listing, and only for a name someone chose. (Found on the first live run: this Mac's own iPad.)
157
+ const stock = /^(?:i(?:Pad|Phone|Mac)|Mac(?:Book(?: Pro| Air)?| mini| Studio| Pro)?|AirPods(?: Pro| Max)?|Apple (?:Watch|TV)|HomePod(?: mini)?|Android|Galaxy\b.*|Pixel\b.*|HP\b.*|Brother\b.*|Canon\b.*|EPSON\b.*|Chromecast.*|Living Room|Bedroom|Kitchen|Office)$/i;
158
+ let authority = 0, lookalike = 0, oversized = 0, links = 0;
159
+ records.forEach((r, i) => {
160
+ const path = `nearby[${i}]:${r.type}`;
161
+ segments.push({ text: r.name, visibility: "visible", path: `${path}/name` });
162
+ for (const [k, v] of Object.entries(r.fields ?? {})) {
163
+ // a device's extra records are shown by few pickers: read, and judged as text nobody chose to look at
164
+ segments.push({ text: v, visibility: "attribute", path: `${path}/${k}` });
165
+ if (v.length > 400 && oversized++ < 3)
166
+ add({ kind: "nearby.oversized_field", confidence: 0.3, severity: 0.5, message: `a ${r.type} device publishes ${v.length} characters in its "${k.slice(0, 30)}" record: far more than a device needs to describe itself`, location: `${path}/${k}`, inHidden: true });
167
+ }
168
+ if (r.name.length > 120 && oversized++ < 3)
169
+ add({ kind: "nearby.oversized_field", confidence: 0.3, severity: 0.5, message: `a ${r.type} device has a ${r.name.length}-character name`, location: `${path}/name` });
170
+ if (!r.known && AUTHORITY.test(`${r.name} ${Object.values(r.fields ?? {}).join(" ")}`) && authority++ < 5)
171
+ add({ kind: "nearby.authority_name", confidence: 0.35, severity: 0.6, message: `a ${r.type} device nobody here set up calls itself "${r.name.slice(0, 60)}": anyone in range can broadcast that name`, location: `${path}/name` });
172
+ if (!r.known) {
173
+ const twin = known.find((k) => k.name !== r.name ? fold(k.name) === fold(r.name) || (Math.min(k.name.length, r.name.length) >= 6 && distance(k.name.toLowerCase(), r.name.toLowerCase()) <= 1) : !!k.address && !!r.address && k.address.toLowerCase() !== r.address.toLowerCase() && (!k.type || k.type === r.type) && !stock.test(r.name.trim()));
174
+ if (twin && lookalike++ < 5)
175
+ add({ kind: "nearby.lookalike", confidence: 0.45, severity: 0.65, message: twin.name === r.name ? `a second device is using the name of one you know ("${r.name.slice(0, 60)}") from a different address` : `"${r.name.slice(0, 60)}" is nearly the name of a device you know ("${twin.name.slice(0, 60)}")`, location: `${path}/name` });
176
+ }
177
+ const url = r.url ?? Object.values(r.fields ?? {}).find((v) => /^https?:\/\//i.test(v));
178
+ if (url) {
179
+ const trick = linkTrick(url);
180
+ segments.push({ text: `[${r.type} link: ${url.slice(0, 200)}]`, visibility: "attribute", path: `${path}/url` });
181
+ if (trick && links++ < 5)
182
+ add({ kind: "nearby.link", confidence: 0.4, severity: 0.6, message: `a ${r.type} device broadcasts a link that is ${trick}. Links from devices are listed, never opened`, location: `${path}/url` });
183
+ }
184
+ });
185
+ return { segments, findings };
186
+ }
@@ -37,30 +37,60 @@ const BLOCKED_V4 = [
37
37
  export function isPrivateIPv4(ip) {
38
38
  return BLOCKED_V4.some((c) => inCidr4(ip, c));
39
39
  }
40
+ /** Expand an IPv6 literal to its 8 groups (lower-case hex, no leading zeros), so every spelling compares alike. */
41
+ function expandIPv6(ip) {
42
+ let s = ip.toLowerCase().replace(/%.*$/, ""); // drop a zone id (fe80::1%en0)
43
+ // a dotted IPv4 tail (::ffff:127.0.0.1) becomes two groups
44
+ const v4 = s.match(/^(.*:)(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
45
+ if (v4) {
46
+ const [a, b, c, d] = v4.slice(2).map(Number);
47
+ if ([a, b, c, d].some((n) => n > 255))
48
+ return null;
49
+ s = `${v4[1]}${((a << 8) | b).toString(16)}:${((c << 8) | d).toString(16)}`;
50
+ }
51
+ const halves = s.split("::");
52
+ if (halves.length > 2)
53
+ return null;
54
+ const head = halves[0] ? halves[0].split(":") : [];
55
+ const tail = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
56
+ const fill = halves.length === 2 ? 8 - head.length - tail.length : 0;
57
+ if (fill < 0 || (halves.length === 1 && head.length !== 8))
58
+ return null;
59
+ const groups = [...head, ...Array(fill).fill("0"), ...tail];
60
+ if (groups.length !== 8 || groups.some((g) => !/^[0-9a-f]{1,4}$/.test(g)))
61
+ return null;
62
+ return groups.map((g) => parseInt(g, 16));
63
+ }
64
+ const v4From = (hi, lo) => `${hi >> 8}.${hi & 255}.${lo >> 8}.${lo & 255}`;
40
65
  export function isPrivateIPv6(ip) {
41
- const l = ip.toLowerCase();
42
- if (l === "::" || l === "::1")
66
+ // Every spelling of an address must give the same answer: 0:0:0:0:0:0:0:1, ::ffff:127.0.0.1 and
67
+ // ::ffff:7f00:1 are all loopback. An address that cannot be parsed is treated as blocked.
68
+ const g = expandIPv6(ip);
69
+ if (!g)
43
70
  return true;
44
- // IPv4-mapped ::ffff:a.b.c.d
45
- const mapped = l.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/);
46
- if (mapped)
47
- return isPrivateIPv4(mapped[1]);
48
- // IPv4-mapped in hex form ::ffff:7f00:1
49
- const mappedHex = l.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/);
50
- if (mappedHex) {
51
- const a = parseInt(mappedHex[1], 16), b = parseInt(mappedHex[2], 16);
52
- return isPrivateIPv4(`${a >> 8}.${a & 255}.${b >> 8}.${b & 255}`);
53
- }
54
- if (l.startsWith("fe8") || l.startsWith("fe9") || l.startsWith("fea") || l.startsWith("feb"))
71
+ const zeros = (n) => g.slice(0, n).every((x) => x === 0);
72
+ if (zeros(8))
73
+ return true; // ::
74
+ if (zeros(7) && g[7] === 1)
75
+ return true; // ::1
76
+ if (zeros(5) && g[5] === 0xffff)
77
+ return isPrivateIPv4(v4From(g[6], g[7])); // IPv4-mapped ::ffff:a.b.c.d
78
+ if (zeros(6))
79
+ return isPrivateIPv4(v4From(g[6], g[7])); // IPv4-compatible ::a.b.c.d (deprecated, still parsed)
80
+ if (g[0] === 0x2002)
81
+ return isPrivateIPv4(v4From(g[1], g[2])); // 6to4 carries an IPv4 address
82
+ if (g[0] === 0x64 && g[1] === 0xff9b)
83
+ return true; // NAT64 well-known prefix can reach v4 space
84
+ if ((g[0] & 0xffc0) === 0xfe80)
55
85
  return true; // link-local fe80::/10
56
- if (l.startsWith("fc") || l.startsWith("fd"))
86
+ if ((g[0] & 0xffc0) === 0xfec0)
87
+ return true; // site-local fec0::/10 (deprecated, still private by intent)
88
+ if ((g[0] & 0xfe00) === 0xfc00)
57
89
  return true; // unique local fc00::/7
58
- if (l.startsWith("ff"))
90
+ if ((g[0] & 0xff00) === 0xff00)
59
91
  return true; // multicast
60
- if (l.startsWith("2001:db8"))
92
+ if (g[0] === 0x2001 && g[1] === 0xdb8)
61
93
  return true; // documentation
62
- if (l.startsWith("64:ff9b"))
63
- return true; // NAT64 well-known prefix can reach v4 space
64
94
  return false;
65
95
  }
66
96
  export function isPrivateIP(ip) {
@@ -83,7 +113,8 @@ function hostMatches(host, pattern) {
83
113
  * them (DNS rebinding defense: the fetch must connect to what we checked).
84
114
  */
85
115
  export async function checkTarget(url, policy, resolver = defaultResolve) {
86
- const host = url.hostname.replace(/^\[|\]$/g, "");
116
+ // one spelling per host: no brackets, no trailing dot ("localhost." is localhost), lower case
117
+ const host = url.hostname.replace(/^\[|\]$/g, "").replace(/\.$/, "").toLowerCase();
87
118
  const port = url.port ? Number(url.port) : url.protocol === "https:" ? 443 : 80;
88
119
  if (port !== 80 && port !== 443 && !policy.extraPorts.includes(port)) {
89
120
  throw new NetPolicyError(`port ${port} not allowed`, "bad_port");
@@ -22,8 +22,43 @@ export declare function deLeet(text: string): string;
22
22
  export declare function deSpace(text: string): string;
23
23
  export declare function reverse(text: string): string;
24
24
  export declare function rot13(text: string): string;
25
- /** NFKC fold + script skeleton + strip combining marks: the shape a human reads. */
25
+ /**
26
+ * NFKC fold + script skeleton + strip combining marks: the shape a human reads.
27
+ * A word is folded only when it is a disguise: it mixes Latin letters with look-alikes (іgnore with a Cyrillic і),
28
+ * or it is made wholly of look-alikes and sits in Latin text (an English word typed in Cyrillic inside English
29
+ * prose). Real Cyrillic, Greek or Armenian words in their own text stay as they are, so the view never turns
30
+ * Russian or Ukrainian into Latin junk (0.9.5).
31
+ */
26
32
  export declare function skeleton(text: string): string;
33
+ export declare function unflip(text: string): string;
34
+ export declare function persianKeysToLatin(text: string): string;
35
+ export declare function latinKeysToPersian(text: string): string;
36
+ /**
37
+ * Indic fold (Devanagari): the same letter is stored precomposed or as base + nukta, and casual writing drops the
38
+ * nukta altogether (नज़र / नजर); ZWJ and ZWNJ shape conjuncts; Devanagari digits and the danda punctuate. NFKC
39
+ * decomposes the nukta letters, then the nukta and joiners drop out, digits become ASCII, dandas become stops.
40
+ */
41
+ export declare function foldIndic(text: string): string;
42
+ /**
43
+ * Accents off Latin letters only (bỏ qua -> bo qua, đ -> d, ş/ș -> s): how Vietnamese, Romanian or Turkish is typed
44
+ * on a phone without the right keyboard. Other scripts' marks are untouched, and "70kg" is not mangled.
45
+ */
46
+ export declare function stripLatinAccents(text: string): string;
47
+ export declare function foldArabicScript(text: string): string;
48
+ /**
49
+ * Compose loose Hangul letters into syllables: ㅁㅜㅅㅣ -> 무시, ㅇㅣㄴ -> 인, also one letter per space
50
+ * (ㅁ ㅜ ㅅ ㅣ). NFKC composes initial+vowel pairs but turns every consonant into an initial, so a final
51
+ * consonant (ㄴ in 인) never joins; this does what a Korean reader does.
52
+ */
53
+ export declare function composeHangul(text: string): string;
54
+ /** Each Hangul syllable reduced to its initial consonant: the shape of 초성 shorthand (이전 지시 무시 -> ㅇㅈ ㅈㅅ ㅁㅅ). */
55
+ export declare function hangulInitials(text: string): string;
56
+ /**
57
+ * Rejoin CJK spelled out with spaces (以 下 は 無 視) and runs of single Hangul syllables (무 시 하 세 요).
58
+ * Chinese and Japanese do not put spaces between characters, so a run of them is spacing used to break a match;
59
+ * Korean spaces words, so only runs of one-syllable "words" are joined.
60
+ */
61
+ export declare function despaceCJK(text: string): string;
27
62
  /** Variants of the text the meaning pass should score. The original is always first. */
28
63
  export declare function variants(text: string): Variant[];
29
64
  export interface Decoded {
@@ -56,13 +56,188 @@ const SKELETON = {
56
56
  "ı": "i", "ȷ": "j", "ɪ": "i", "ɴ": "n", "ʀ": "r", "ᴀ": "a", "ᴄ": "c", "ᴇ": "e", "ᴏ": "o", "ᴘ": "p", "ᴛ": "t", "ᴜ": "u", "ᴠ": "v", "ᴡ": "w", "ᴢ": "z",
57
57
  "ß": "ss", "ł": "l", "đ": "d", "ħ": "h", "ŧ": "t", "ɍ": "r", "ℓ": "l", "K": "K", "Ω": "O", "Å": "A",
58
58
  };
59
- /** NFKC fold + script skeleton + strip combining marks: the shape a human reads. */
59
+ const LATIN_LETTER = /\p{Script=Latin}/u;
60
+ const ANY_LETTER = /\p{L}/u;
61
+ /** share of Latin among the letters within `span` characters of an offset */
62
+ function latinShareAround(text, at, span = 160) {
63
+ let latin = 0, letters = 0;
64
+ for (const ch of text.slice(Math.max(0, at - span), at + span)) {
65
+ if (!ANY_LETTER.test(ch))
66
+ continue;
67
+ letters++;
68
+ if (LATIN_LETTER.test(ch) && !SKELETON[ch])
69
+ latin++;
70
+ }
71
+ return letters ? latin / letters : 0;
72
+ }
73
+ /**
74
+ * NFKC fold + script skeleton + strip combining marks: the shape a human reads.
75
+ * A word is folded only when it is a disguise: it mixes Latin letters with look-alikes (іgnore with a Cyrillic і),
76
+ * or it is made wholly of look-alikes and sits in Latin text (an English word typed in Cyrillic inside English
77
+ * prose). Real Cyrillic, Greek or Armenian words in their own text stay as they are, so the view never turns
78
+ * Russian or Ukrainian into Latin junk (0.9.5).
79
+ */
60
80
  export function skeleton(text) {
61
81
  const nfkc = text.normalize("NFKC");
82
+ const out = nfkc.replace(/[\p{L}\p{M}]+/gu, (w, off) => {
83
+ let mapped = false, latin = false, allMappable = true;
84
+ for (const ch of w) {
85
+ if (SKELETON[ch])
86
+ mapped = true;
87
+ else if (LATIN_LETTER.test(ch))
88
+ latin = true;
89
+ else if (ANY_LETTER.test(ch))
90
+ allMappable = false;
91
+ }
92
+ if (!mapped)
93
+ return w;
94
+ if (latin || (allMappable && latinShareAround(nfkc, off) >= 0.6)) {
95
+ let f = "";
96
+ for (const ch of w)
97
+ f += SKELETON[ch] ?? ch;
98
+ return f;
99
+ }
100
+ return w;
101
+ });
102
+ // marks come off Latin letters only (ïgnörë, the dot of İ); й, ё, Devanagari vowel signs and Arabic vowels are letters' own
103
+ return out.normalize("NFD").replace(/(\p{Script=Latin})\p{M}+/gu, "$1").normalize("NFC");
104
+ }
105
+ /** Upside-down text (ɐuɐ ǝɹouƃᴉ): read it the right way up. Only when the text carries the tell-tale turned letters. */
106
+ const FLIP = {
107
+ "ɐ": "a", "q": "b", "ɔ": "c", "p": "d", "ǝ": "e", "ɟ": "f", "ƃ": "g", "ɥ": "h", "ᴉ": "i", "ı": "i", "ɾ": "j", "ʞ": "k", "ɯ": "m", "u": "n", "d": "p", "b": "q", "ɹ": "r", "ʇ": "t", "n": "u", "ʌ": "v", "ʍ": "w", "ʎ": "y",
108
+ "∀": "A", "Ɔ": "C", "Ǝ": "E", "Ⅎ": "F", "⅁": "G", "ſ": "J", "Ʞ": "K", "˥": "L", "Ԁ": "P", "ᴚ": "R", "┴": "T", "ꓕ": "T", "∩": "U", "Λ": "V", "⅄": "Y", "W": "M", "M": "W",
109
+ "˙": ".", "'": ",", ",": "'", "¿": "?", "¡": "!", "(": ")", ")": "(", "[": "]", "]": "[",
110
+ };
111
+ const FLIP_TELLS = /[ɐɔǝɟƃɥᴉɾʞɯɹʇʌʍʎ∀ƆƎℲ⅁ſꞰ˥Ԁᴚꓕ┴∩Λ⅄]/g;
112
+ export function unflip(text) {
113
+ if ((text.match(FLIP_TELLS) ?? []).length < 3)
114
+ return text;
115
+ return [...text].reverse().map((c) => FLIP[c] ?? c).join("");
116
+ }
117
+ /**
118
+ * Keyboard-layout cipher: English typed with a Persian layout active (هلدخقث = "ignore") and Persian typed with a
119
+ * Latin layout active. Red teams at an Iranian carrier's hackathon used both to get past a guard.
120
+ * Standard Persian layout (ISIRI 9147) on a US keyboard.
121
+ */
122
+ const FA_KEYS = {
123
+ "ض": "q", "ص": "w", "ث": "e", "ق": "r", "ف": "t", "غ": "y", "ع": "u", "ه": "i", "خ": "o", "ح": "p", "ج": "[", "چ": "]",
124
+ "ش": "a", "س": "s", "ی": "d", "ي": "d", "ب": "f", "ل": "g", "ا": "h", "ت": "j", "ن": "k", "م": "l", "ک": ";", "ك": ";", "گ": "'",
125
+ "ظ": "z", "ط": "x", "ز": "c", "ر": "v", "ذ": "b", "د": "n", "پ": "m", "ئ": "m", "و": ",",
126
+ };
127
+ const LATIN_TO_FA = Object.fromEntries(Object.entries(FA_KEYS).filter(([fa]) => !"يكئ".includes(fa)).map(([fa, en]) => [en, fa]));
128
+ export function persianKeysToLatin(text) {
129
+ if ((text.match(/[؀-ۿ]/g) ?? []).length < 6)
130
+ return text;
131
+ return text.replace(/[؀-ۿ]/g, (c) => FA_KEYS[c] ?? c);
132
+ }
133
+ export function latinKeysToPersian(text) {
134
+ if ((text.match(/[a-z]/gi) ?? []).length < 6)
135
+ return text;
136
+ return text.toLowerCase().replace(/[a-z;',[\]]/g, (c) => LATIN_TO_FA[c] ?? c);
137
+ }
138
+ /**
139
+ * Indic fold (Devanagari): the same letter is stored precomposed or as base + nukta, and casual writing drops the
140
+ * nukta altogether (नज़र / नजर); ZWJ and ZWNJ shape conjuncts; Devanagari digits and the danda punctuate. NFKC
141
+ * decomposes the nukta letters, then the nukta and joiners drop out, digits become ASCII, dandas become stops.
142
+ */
143
+ export function foldIndic(text) {
144
+ if (!/[ऀ-ॿ]/.test(text))
145
+ return text;
146
+ return text.normalize("NFKC")
147
+ .replace(/[़‌‍]/g, "")
148
+ .replace(/[०-९]/g, (d) => String(d.charCodeAt(0) - 0x0966))
149
+ .replace(/[।॥]/g, ".");
150
+ }
151
+ /**
152
+ * Accents off Latin letters only (bỏ qua -> bo qua, đ -> d, ş/ș -> s): how Vietnamese, Romanian or Turkish is typed
153
+ * on a phone without the right keyboard. Other scripts' marks are untouched, and "70kg" is not mangled.
154
+ */
155
+ export function stripLatinAccents(text) {
156
+ return text.normalize("NFD").replace(/(\p{Script=Latin})\p{M}+/gu, "$1").replace(/[đĐ]/g, (c) => (c === "đ" ? "d" : "D")).normalize("NFC");
157
+ }
158
+ // ---------- non-Latin matching views (0.9.3) ----------
159
+ // These views exist only for matching. Nothing here is ever delivered: the text the model receives is the original.
160
+ /**
161
+ * Arabic-script fold. Persian ی/ک and Arabic ي/ك look identical but are different code points, so a rule written
162
+ * with one misses text typed with the other; hamza and tā marbūṭa forms, tatweel and short-vowel marks vary the
163
+ * same way. The zero-width non-joiner is a real Persian character (it is not stripped from delivered text, and the
164
+ * character pass does not flag it between Persian letters), but it sits inside words a rule spells without it, so
165
+ * the matching view drops it. Rules are compiled through the same fold.
166
+ */
167
+ const ARABIC_FOLD = {
168
+ "ي": "ی", "ى": "ی", "ئ": "ی", "ے": "ی", // ي ى ئ ے -> ی
169
+ "ك": "ک", // ك -> ک
170
+ "ة": "ه", "ۀ": "ه", // ة ۀ -> ه
171
+ "أ": "ا", "إ": "ا", "آ": "ا", "ٱ": "ا", // أ إ آ ٱ -> ا
172
+ "ؤ": "و", // ؤ -> و
173
+ };
174
+ export function foldArabicScript(text) {
175
+ if (!/[؀-ۿ]/.test(text))
176
+ return text;
177
+ return text
178
+ .replace(/[ـً-ٰٟۖ-ۭ‌‍]/g, "")
179
+ .replace(/[يىئےكةۀأإآٱؤ]/g, (c) => ARABIC_FOLD[c] ?? c);
180
+ }
181
+ const CHO = "ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ";
182
+ const JUNG = "ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ";
183
+ const JONG = ["", "ㄱ", "ㄲ", "ㄳ", "ㄴ", "ㄵ", "ㄶ", "ㄷ", "ㄹ", "ㄺ", "ㄻ", "ㄼ", "ㄽ", "ㄾ", "ㄿ", "ㅀ", "ㅁ", "ㅂ", "ㅄ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"];
184
+ const VOWEL_PAIR = { "ㅗㅏ": "ㅘ", "ㅗㅐ": "ㅙ", "ㅗㅣ": "ㅚ", "ㅜㅓ": "ㅝ", "ㅜㅔ": "ㅞ", "ㅜㅣ": "ㅟ", "ㅡㅣ": "ㅢ" };
185
+ const FINAL_PAIR = { "ㄱㅅ": "ㄳ", "ㄴㅈ": "ㄵ", "ㄴㅎ": "ㄶ", "ㄹㄱ": "ㄺ", "ㄹㅁ": "ㄻ", "ㄹㅂ": "ㄼ", "ㄹㅅ": "ㄽ", "ㄹㅌ": "ㄾ", "ㄹㅍ": "ㄿ", "ㄹㅎ": "ㅀ", "ㅂㅅ": "ㅄ" };
186
+ const isCons = (c) => !!c && c >= "ㄱ" && c <= "ㅎ";
187
+ const isVow = (c) => !!c && c >= "ㅏ" && c <= "ㅣ";
188
+ /**
189
+ * Compose loose Hangul letters into syllables: ㅁㅜㅅㅣ -> 무시, ㅇㅣㄴ -> 인, also one letter per space
190
+ * (ㅁ ㅜ ㅅ ㅣ). NFKC composes initial+vowel pairs but turns every consonant into an initial, so a final
191
+ * consonant (ㄴ in 인) never joins; this does what a Korean reader does.
192
+ */
193
+ export function composeHangul(text) {
194
+ if (!/[ㄱ-ㅣ]/.test(text))
195
+ return text;
196
+ const s = [...text.replace(/(?<=[ㄱ-ㅣ])[ \t ]+(?=[ㄱ-ㅣ])/g, "")];
62
197
  let out = "";
63
- for (const ch of nfkc)
64
- out += SKELETON[ch] ?? ch;
65
- return out.normalize("NFD").replace(/\p{M}+/gu, "").normalize("NFC");
198
+ for (let i = 0; i < s.length;) {
199
+ const c = s[i];
200
+ if (isCons(c) && CHO.includes(c) && isVow(s[i + 1])) {
201
+ let v = s[i + 1], j = i + 2;
202
+ if (isVow(s[j]) && VOWEL_PAIR[v + s[j]]) {
203
+ v = VOWEL_PAIR[v + s[j]];
204
+ j++;
205
+ }
206
+ let t = "";
207
+ // a consonant is a final unless a vowel follows it (then it starts the next syllable)
208
+ if (isCons(s[j]) && !isVow(s[j + 1])) {
209
+ if (isCons(s[j + 1]) && FINAL_PAIR[s[j] + s[j + 1]] && !isVow(s[j + 2])) {
210
+ t = FINAL_PAIR[s[j] + s[j + 1]];
211
+ j += 2;
212
+ }
213
+ else if (JONG.includes(s[j])) {
214
+ t = s[j];
215
+ j++;
216
+ }
217
+ }
218
+ out += String.fromCharCode(0xac00 + (CHO.indexOf(c) * 21 + JUNG.indexOf(v)) * 28 + JONG.indexOf(t));
219
+ i = j;
220
+ continue;
221
+ }
222
+ out += c;
223
+ i++;
224
+ }
225
+ return out;
226
+ }
227
+ /** Each Hangul syllable reduced to its initial consonant: the shape of 초성 shorthand (이전 지시 무시 -> ㅇㅈ ㅈㅅ ㅁㅅ). */
228
+ export function hangulInitials(text) {
229
+ return text.replace(/[가-힣]/g, (c) => CHO[Math.floor((c.charCodeAt(0) - 0xac00) / 588)]);
230
+ }
231
+ /**
232
+ * Rejoin CJK spelled out with spaces (以 下 は 無 視) and runs of single Hangul syllables (무 시 하 세 요).
233
+ * Chinese and Japanese do not put spaces between characters, so a run of them is spacing used to break a match;
234
+ * Korean spaces words, so only runs of one-syllable "words" are joined.
235
+ */
236
+ export function despaceCJK(text) {
237
+ const cjk = String.raw `\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}ー`;
238
+ return text
239
+ .replace(new RegExp(`(?<=[${cjk}])[ \\t\\u3000]+(?=[${cjk}])`, "gu"), "")
240
+ .replace(/(?<![가-힣])[가-힣](?:[  ][가-힣]){2,}(?![가-힣])/g, (run) => run.replace(/[  ]/g, ""));
66
241
  }
67
242
  /** Variants of the text the meaning pass should score. The original is always first. */
68
243
  export function variants(text) {
@@ -81,6 +256,13 @@ export function variants(text) {
81
256
  out.push({ name: "reversed", text: reverse(sk), discount: 0.85 });
82
257
  out.push({ name: "rot13", text: rot13(sk), discount: 0.85 });
83
258
  }
259
+ // 0.9.5: upside-down text, and English typed on a Persian keyboard layout
260
+ const uf = unflip(text);
261
+ if (uf !== text)
262
+ out.push({ name: "unflipped", text: skeleton(uf), discount: 0.9 });
263
+ const fk = persianKeysToLatin(text);
264
+ if (fk !== text)
265
+ out.push({ name: "persian-keyboard", text: fk, discount: 0.85 });
84
266
  return out;
85
267
  }
86
268
  // ---------- decoders ----------
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Provenance marks: what an agent wrote after reading outside content.
3
+ *
4
+ * A run that has read a web page, an email or a tool result may have been steered by it. What that run
5
+ * writes to disk is the way one steered moment reaches a later, clean session: a note, a script, a config.
6
+ * So each file written in such a run is marked: which session, when, and what it had read. When a later
7
+ * session reads the file, the mark says its content is at best "written by an agent earlier" (trust level 3
8
+ * only if the run that wrote it was clean; this one was not).
9
+ *
10
+ * A mark labels; it does not block. The files are the person's own project, and withholding them would stop
11
+ * work. The reader is told, the file is scanned as outside content, and the reading session counts as having
12
+ * read outside content.
13
+ *
14
+ * Marks are kept by path with the file's hash, so a mark also says whether the file changed since.
15
+ * A person clears a mark (`ingress provenance clear <path>`); an agent cannot.
16
+ */
17
+ export interface ProvenanceMark {
18
+ path: string;
19
+ /** hash of the file when it was marked; undefined if it could not be read */
20
+ sha256?: string;
21
+ at: string;
22
+ session: string;
23
+ /** the last few things the writing session had read */
24
+ sources: string[];
25
+ }
26
+ export declare class ProvenanceLedger {
27
+ private file;
28
+ constructor(dir?: string);
29
+ private read;
30
+ private write;
31
+ /** Mark a file as written by a session that had read outside content. */
32
+ mark(path: string, by: {
33
+ session: string;
34
+ sources: readonly string[];
35
+ at?: string;
36
+ }): ProvenanceMark;
37
+ /** The mark on a file, and whether the file is still what was written then. */
38
+ lookup(path: string): {
39
+ mark: ProvenanceMark;
40
+ unchanged: boolean;
41
+ } | undefined;
42
+ /** Remove a mark: a person has looked at the file. Returns whether there was one. */
43
+ clear(path: string): boolean;
44
+ list(): ProvenanceMark[];
45
+ }
46
+ /** One sentence for whoever reads a marked file. */
47
+ export declare function describeMark(m: ProvenanceMark, unchanged: boolean): string;
48
+ /**
49
+ * Where the operating system says a file came from: macOS marks downloads (and what is unpacked from them)
50
+ * with a quarantine attribute naming the app that fetched them, and often the address. Undefined when the
51
+ * file has no such mark, or on a system without it.
52
+ */
53
+ export declare function downloadOrigin(path: string): {
54
+ agent?: string;
55
+ from?: string;
56
+ } | undefined;