@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,291 @@
1
+ /**
2
+ * Which shell commands bring outside content into an agent's context?
3
+ *
4
+ * A PostToolUse hook sees a command and its output. Output that came from the network (curl, wget, gh, a
5
+ * script that calls fetch) or from a background task's output file is outside content and must be gated;
6
+ * the output of `ls` or `npm test` is not. This module answers that question from the command text.
7
+ *
8
+ * It errs toward gating: a command it cannot parse is gated if a network program appears in it at all.
9
+ */
10
+ import { existsSync, readFileSync, statSync } from "node:fs";
11
+ import { isAbsolute, join } from "node:path";
12
+ import { isIP } from "node:net";
13
+ const NET_CLIENTS = new Set(["curl", "wget", "http", "https", "xh", "xhs", "aria2c", "lynx", "w3m", "links", "elinks", "gh", "nc", "ncat", "netcat", "telnet", "ftp", "sftp", "scp", "rsync", "dig", "nslookup", "host", "whois", "openssl"]);
14
+ const INTERPRETERS = new Set(["node", "python", "python3", "ruby", "perl", "php", "deno", "bun", "tsx", "ts-node", "npx", "bunx", "uvx", "pipx"]);
15
+ const WRAPPERS = new Set(["sudo", "env", "command", "time", "nohup", "exec", "nice", "caffeinate", "stdbuf", "unbuffer", "xargs", "watch"]);
16
+ const OPS = new Set([";", "&", "|", "(", ")", "`", "\n"]);
17
+ const NETWORK_CODE = /https?:\/\/|\bfetch\s*\(|urllib|\brequests\b|http\.client|aiohttp|httpx|axios|node:https?|require\(\s*['"]https?['"]\s*\)|net\/http|Net::HTTP|LWP::|WebSocket|curl_init|file_get_contents\s*\(\s*['"]https?|socket\.|XMLHttpRequest|undici|got\(|node-fetch/;
18
+ /** Split a command line into words and operators. Throws on an unterminated quote. */
19
+ export function shellTokens(command) {
20
+ const out = [];
21
+ let cur = "", has = false;
22
+ const push = () => { if (has)
23
+ out.push(cur); cur = ""; has = false; };
24
+ for (let i = 0; i < command.length; i++) {
25
+ const c = command[i];
26
+ if (c === "'") {
27
+ const end = command.indexOf("'", i + 1);
28
+ if (end < 0)
29
+ throw new Error("unterminated single quote");
30
+ cur += command.slice(i + 1, end);
31
+ has = true;
32
+ i = end;
33
+ }
34
+ else if (c === '"') {
35
+ let j = i + 1, s = "";
36
+ for (; j < command.length && command[j] !== '"'; j++) {
37
+ if (command[j] === "\\" && j + 1 < command.length)
38
+ j++;
39
+ s += command[j];
40
+ }
41
+ if (j >= command.length)
42
+ throw new Error("unterminated double quote");
43
+ cur += s;
44
+ has = true;
45
+ i = j;
46
+ }
47
+ else if (c === "\\" && i + 1 < command.length) {
48
+ cur += command[++i];
49
+ has = true;
50
+ }
51
+ else if (c === " " || c === "\t" || c === "\r")
52
+ push();
53
+ else if (OPS.has(c)) {
54
+ push();
55
+ let op = c;
56
+ while (i + 1 < command.length && OPS.has(command[i + 1]) && command[i + 1] !== "\n")
57
+ op += command[++i];
58
+ out.push(op);
59
+ }
60
+ else {
61
+ cur += c;
62
+ has = true;
63
+ }
64
+ }
65
+ push();
66
+ return out;
67
+ }
68
+ // (an empty quoted word, '' or "", is an argument, not an operator: before 0.9.17 it ended the command it was in)
69
+ const isOp = (t) => t.length > 0 && [...t].every((c) => OPS.has(c));
70
+ /** Each simple command in command position: its program name (without a path) and its arguments. */
71
+ export function shellInvocations(command) {
72
+ const toks = shellTokens(command);
73
+ const out = [];
74
+ let i = 0, atCmd = true;
75
+ while (i < toks.length) {
76
+ const t = toks[i];
77
+ if (isOp(t)) {
78
+ atCmd = true;
79
+ i++;
80
+ continue;
81
+ }
82
+ if (atCmd) {
83
+ const word = t.replace(/^[$(`]+/, "");
84
+ if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(word)) {
85
+ i++;
86
+ continue;
87
+ }
88
+ const base = word.split("/").pop() ?? word;
89
+ if (WRAPPERS.has(base)) {
90
+ i++;
91
+ continue;
92
+ }
93
+ if (base === "timeout") {
94
+ i += 2;
95
+ continue;
96
+ }
97
+ let j = i + 1;
98
+ while (j < toks.length && !isOp(toks[j]))
99
+ j++;
100
+ out.push({ program: base, args: toks.slice(i + 1, j) });
101
+ i = j;
102
+ atCmd = false;
103
+ continue;
104
+ }
105
+ i++;
106
+ }
107
+ return out;
108
+ }
109
+ /** programs that only read, whatever file they are pointed at (given no output redirect) */
110
+ const READERS = new Set(["cat", "less", "more", "head", "tail", "grep", "egrep", "fgrep", "rg", "ag", "ls", "wc", "stat", "file", "diff", "cmp", "jq", "test", "[", "[[", "md5", "md5sum", "shasum", "sha256sum", "uniq", "cut", "tr", "echo", "printf", "pwd", "which", "type", "basename", "dirname", "realpath", "readlink", "du", "df", "tree", "bat", "cd", "true", "false", "nl", "strings", "xxd", "hexdump", "od", "column", "fold", "rev", "tac", "comm", "date", "id", "whoami", "uname", "hostname", "printenv", "sleep"]);
111
+ const HARMLESS_REDIRECT = /^(?:\d*>&\d+|[\d&]*>>?\/dev\/null|\d*<.*)$/;
112
+ function readsOnly(program, args) {
113
+ for (let i = 0; i < args.length; i++) {
114
+ const a = args[i];
115
+ if (!/^[\d&]*>/.test(a))
116
+ continue;
117
+ if (HARMLESS_REDIRECT.test(a))
118
+ continue;
119
+ if (/^[\d&]*>>?$/.test(a) && args[i + 1] === "/dev/null") {
120
+ i++;
121
+ continue;
122
+ }
123
+ return false; // output goes to a file
124
+ }
125
+ if (READERS.has(program))
126
+ return true;
127
+ switch (program) {
128
+ case "sed": return !args.some((a) => /^-[a-zA-Z]*i|^--in-place/.test(a)) && !args.some((a) => /(?:^|;|\s)w\s+\S/.test(a));
129
+ case "awk":
130
+ case "gawk": return !args.some((a) => />|\bsystem\s*\(|\|/.test(a));
131
+ case "sort": return !args.some((a) => /^-[a-zA-Z]*o|^--output/.test(a));
132
+ case "find": return !args.some((a) => /^-(?:delete|exec|execdir|ok|okdir|fprint|fprintf|fls)$/.test(a));
133
+ case "plutil": return args[0] === "-p" || args[0] === "-lint";
134
+ case "yq": return !args.some((a) => /^-[a-zA-Z]*i|^--inplace/.test(a));
135
+ case "git": return ["status", "log", "diff", "show", "branch", "remote", "rev-parse", "ls-files", "blame", "describe", "tag"].includes(args.find((a) => !a.startsWith("-")) ?? "") && !args.some((a) => /^-(?:d|D|m|M)$|^--(?:delete|move|set-upstream|add)/.test(a));
136
+ default: return false;
137
+ }
138
+ }
139
+ /**
140
+ * The parts of a command line that can change something: every simple command except those that only read.
141
+ * `cat ~/.zshrc | grep PATH` changes nothing; `echo x >> ~/.zshrc` does. Used so that looking at a startup
142
+ * file or an agent's settings is not mistaken for editing it. A command that cannot be parsed is returned whole.
143
+ */
144
+ export function shellWrites(command) {
145
+ let inv;
146
+ try {
147
+ inv = shellInvocations(command);
148
+ }
149
+ catch {
150
+ return [command];
151
+ }
152
+ // a here-document or a loop body is not understood by the splitter: keep the whole command
153
+ if (/<<-?\s*['"]?\w/.test(command))
154
+ return [command];
155
+ return inv.filter((i) => !readsOnly(i.program, i.args)).map((i) => [i.program, ...i.args].join(" "));
156
+ }
157
+ /**
158
+ * Ways a command line sends data to another machine, in plain words; empty when it only asks for something.
159
+ * `curl https://x/page` asks. `curl -d @notes.txt https://x`, `cat key | nc host 9000`, `scp a host:b`,
160
+ * a request built from another command's output: those send.
161
+ */
162
+ export function shellUploads(command) {
163
+ const why = new Set();
164
+ let inv = [];
165
+ try {
166
+ inv = shellInvocations(command);
167
+ }
168
+ catch { /* judged from the raw text below */ }
169
+ const piped = (program) => new RegExp(String.raw `\|\s*(?:\S+/)?${program}\b`).test(command);
170
+ for (const { program, args } of inv) {
171
+ const joined = args.join(" ");
172
+ if (program === "curl") {
173
+ for (let i = 0; i < args.length; i++) {
174
+ const a = args[i], v = args[i + 1] ?? "";
175
+ if (/^(?:-d|--data(?:-binary|-raw|-urlencode|-ascii)?|--json)$/.test(a) && /^@|=@/.test(v))
176
+ why.add(v === "@-" ? "it sends the output of another command" : "it sends the contents of a file");
177
+ if (/^(?:-d|--data(?:-binary|-urlencode)?|--json)@/.test(a) || /^--data(?:-binary|-urlencode)?=@/.test(a))
178
+ why.add("it sends the contents of a file");
179
+ if (/^(?:-F|--form)$/.test(a) && /=[@<]/.test(v))
180
+ why.add("it uploads a file as a form field");
181
+ if (/^(?:-T|--upload-file)$/.test(a) || /^--upload-file=/.test(a))
182
+ why.add("it uploads a file");
183
+ }
184
+ }
185
+ if (program === "wget" && /--(?:post-file|body-file)\b/.test(joined))
186
+ why.add("it sends the contents of a file");
187
+ if (["nc", "ncat", "netcat", "telnet", "socat"].includes(program) && (piped(program) || args.some((a) => /^</.test(a))))
188
+ why.add("it pipes data to a network connection");
189
+ if (program === "scp" || program === "sftp")
190
+ why.add("it copies files to or from another machine");
191
+ if (program === "rsync" && args.some((a) => /^rsync:\/\/|^[^/\s:]+@?[^/\s:]*:[^/]/.test(a) && !/^[A-Za-z]:\\/.test(a)))
192
+ why.add("it copies files to or from another machine");
193
+ if (program === "git" && args[0] === "push" && args.slice(1).some((a) => /:\/\/|^[\w.-]+@[\w.-]+:/.test(a)))
194
+ why.add("it pushes the repository to an address given in the command");
195
+ if (program === "gh" && args[0] === "gist" && args[1] === "create")
196
+ why.add("it publishes files as a gist");
197
+ if (["aws", "gsutil", "s5cmd", "rclone", "az"].includes(program) && /\b(?:cp|sync|mv|copy|upload|put)\b/.test(joined) && /\b(?:s3|gs|az|r2):\/\/|\b\w+:[\w/-]+/.test(joined))
198
+ why.add("it copies files to cloud storage");
199
+ if (["mail", "mailx", "sendmail", "mutt", "msmtp"].includes(program))
200
+ why.add("it sends mail");
201
+ }
202
+ // a request whose address or body is built from another command's output
203
+ if (/(?:^|[;&|(\s])(?:\S+\/)?(?:curl|wget|http|https|xh|nc|ncat)\b[^\n;|&]*(?:\$\(|`)/.test(command))
204
+ why.add("it builds a request from the output of another command");
205
+ return [...why];
206
+ }
207
+ function isLoopbackHost(host) {
208
+ const h = host.replace(/^\[|\]$/g, "").toLowerCase().replace(/\.$/, "");
209
+ if (h === "localhost" || h === "ip6-localhost" || h.endsWith(".localhost"))
210
+ return true;
211
+ if (isIP(h) === 4)
212
+ return h.startsWith("127.") || h === "0.0.0.0";
213
+ if (isIP(h) === 6)
214
+ return h === "::1" || h === "::";
215
+ return false;
216
+ }
217
+ /** (host, port) of each URL-like argument; port is undefined when not given. */
218
+ function argTargets(args) {
219
+ const out = [];
220
+ for (const a of args) {
221
+ if (a.startsWith("-") || a.startsWith("@"))
222
+ continue;
223
+ if (a.includes("://")) {
224
+ try {
225
+ const u = new URL(a);
226
+ out.push({ host: u.hostname, port: u.port ? Number(u.port) : undefined });
227
+ }
228
+ catch {
229
+ out.push({ host: "" });
230
+ }
231
+ continue;
232
+ }
233
+ const m = a.match(/^(\[[0-9a-fA-F:]+\]|[A-Za-z0-9.-]+)(?::(\d+))?(?:[/?#].*)?$/);
234
+ if (m && (m[1].includes(".") || m[1].toLowerCase() === "localhost" || m[1].startsWith("[")))
235
+ out.push({ host: m[1], port: m[2] ? Number(m[2]) : undefined });
236
+ }
237
+ return out;
238
+ }
239
+ function scriptUsesNetwork(args, cwd) {
240
+ const code = [];
241
+ let takeNext = false;
242
+ for (const a of args) {
243
+ if (takeNext) {
244
+ code.push(a);
245
+ takeNext = false;
246
+ }
247
+ else if (["-e", "-c", "--eval", "-p", "--print", "-r"].includes(a))
248
+ takeNext = true;
249
+ else if (!a.startsWith("-") && /\.(m?js|cjs|ts|mts|py|rb|pl|php)$/.test(a)) {
250
+ const path = isAbsolute(a) ? a : join(cwd ?? ".", a);
251
+ try {
252
+ if (existsSync(path) && statSync(path).size <= 2_000_000)
253
+ code.push(readFileSync(path, "utf8"));
254
+ }
255
+ catch { /* unreadable: not evidence either way */ }
256
+ break;
257
+ }
258
+ }
259
+ return code.some((c) => NETWORK_CODE.test(c));
260
+ }
261
+ /**
262
+ * A label for where this command's output came from if it must be gated, else undefined.
263
+ * Loopback is not exempt: a dev server, a local model server and a tunnel all serve content someone else
264
+ * wrote. Only requests addressed solely to `ownPort` are skipped.
265
+ */
266
+ export function shellOutputSource(command, opts = {}) {
267
+ if (/\/tasks\/[^\s'"]+\.output\b/.test(command))
268
+ return "background-task-output";
269
+ let found;
270
+ try {
271
+ found = shellInvocations(command);
272
+ }
273
+ catch {
274
+ // unparseable (a heredoc, an odd quote): gate if a network program or interpreter appears in command position at all
275
+ const names = [...NET_CLIENTS, ...INTERPRETERS].join("|");
276
+ return new RegExp(`(?:^|[;&|(\`\\n])\\s*(?:\\S*/)?(${names})\\b`, "m").test(command) ? "bash:unparsed" : undefined;
277
+ }
278
+ for (const { program, args } of found) {
279
+ if (NET_CLIENTS.has(program)) {
280
+ if (program === "gh")
281
+ return `gh:${args.slice(0, 2).join(" ")}`;
282
+ const targets = argTargets(args);
283
+ if (opts.ownPort !== undefined && targets.length && targets.every((t) => isLoopbackHost(t.host) && t.port === opts.ownPort))
284
+ continue;
285
+ return args.find((a) => a.includes("://")) ?? `${program}:${args.join(" ").slice(0, 80)}`;
286
+ }
287
+ if (INTERPRETERS.has(program) && scriptUsesNetwork(args, opts.cwd))
288
+ return `${program}:network-code`;
289
+ }
290
+ return undefined;
291
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Shortcuts: reading an automation before it is installed.
3
+ *
4
+ * A shared shortcut is a program someone else wrote that runs with your contacts, photos, files, messages and
5
+ * network. Its actions are listed in the file. This scanner reads them and says what the shortcut can do, in
6
+ * three questions that matter together:
7
+ *
8
+ * does it read private data? contacts, photos, files, the clipboard, location, messages, health
9
+ * does it take input from outside? the contents of a web address, a page, a feed, whatever is shared to it
10
+ * can it send things out? a web request with a body, a message, an email, a post, a remote script
11
+ *
12
+ * Any one is ordinary. All three in one shortcut is a program that can be told from outside to send your data
13
+ * somewhere, and it is reported as that. Running code (shell, AppleScript, JavaScript on a page, SSH) and
14
+ * running other shortcuts are reported. Every piece of text in it (comments, message bodies, scripts, URLs)
15
+ * is scanned like any other content.
16
+ *
17
+ * Input: the shortcut as JSON or an XML property list (`plutil -convert json -o - file.shortcut` gives JSON).
18
+ * A signed shortcut exported from the app is encrypted to Apple's format and cannot be read here; one opened
19
+ * from a shared link, or saved unsigned, can.
20
+ */
21
+ import type { AdapterResult } from "./adapters.js";
22
+ type Json = Record<string, unknown>;
23
+ export interface ShortcutAction {
24
+ id: string;
25
+ params: Json;
26
+ }
27
+ export interface ShortcutSummary {
28
+ actions: number;
29
+ readsPrivate: string[];
30
+ outsideInput: string[];
31
+ sendsOut: string[];
32
+ runsCode: string[];
33
+ runsOtherShortcuts: string[];
34
+ destinations: string[];
35
+ acceptsSharedInput: boolean;
36
+ }
37
+ export declare function looksLikeShortcut(doc: unknown): boolean;
38
+ /** A property list in XML, as far as a shortcut needs: dictionaries, arrays, strings, numbers, booleans. */
39
+ export declare function parsePlistXml(xml: string): unknown;
40
+ export declare function summarizeShortcut(doc: unknown): {
41
+ summary: ShortcutSummary;
42
+ actions: ShortcutAction[];
43
+ };
44
+ export declare function adaptShortcut(raw: string): AdapterResult;
45
+ export {};
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Shortcuts: reading an automation before it is installed.
3
+ *
4
+ * A shared shortcut is a program someone else wrote that runs with your contacts, photos, files, messages and
5
+ * network. Its actions are listed in the file. This scanner reads them and says what the shortcut can do, in
6
+ * three questions that matter together:
7
+ *
8
+ * does it read private data? contacts, photos, files, the clipboard, location, messages, health
9
+ * does it take input from outside? the contents of a web address, a page, a feed, whatever is shared to it
10
+ * can it send things out? a web request with a body, a message, an email, a post, a remote script
11
+ *
12
+ * Any one is ordinary. All three in one shortcut is a program that can be told from outside to send your data
13
+ * somewhere, and it is reported as that. Running code (shell, AppleScript, JavaScript on a page, SSH) and
14
+ * running other shortcuts are reported. Every piece of text in it (comments, message bodies, scripts, URLs)
15
+ * is scanned like any other content.
16
+ *
17
+ * Input: the shortcut as JSON or an XML property list (`plutil -convert json -o - file.shortcut` gives JSON).
18
+ * A signed shortcut exported from the app is encrypted to Apple's format and cannot be read here; one opened
19
+ * from a shared link, or saved unsigned, can.
20
+ */
21
+ import { linkTrick } from "./messages.js";
22
+ const short = (id) => id.replace(/^is\.workflow\.actions\./, "").replace(/^com\.apple\./, "");
23
+ const PRIVATE = [
24
+ [/contacts?$|selectcontacts|getcontact|filter\.contacts/i, "contacts"], [/photos?$|selectphoto|getlastphoto|getlatestlivephotos|filter\.photos|takephoto|getlastscreenshot/i, "photos"],
25
+ [/getclipboard/i, "the clipboard"], [/location$|getcurrentlocation|getmylocation/i, "your location"], [/health\.quantity|filter\.health/i, "health data"],
26
+ [/documentpicker\.open|file\.getfoldercontents|file$|getfile|filter\.files|file\.select/i, "files"], [/getmessages|searchmessages|filter\.messages|imessage\.search/i, "messages"],
27
+ [/filter\.calendarevents|getupcomingevents|filter\.reminders|getupcomingreminders/i, "calendar and reminders"], [/filter\.notes|shownote|getnote/i, "notes"],
28
+ [/getwifi|getipaddress|getdevicedetails|getbatterylevel/i, "device details"], [/dictatetext|recordaudio/i, "the microphone"], [/safari\.geturl|getcurrenturl|getarticle.*safari|readinglist/i, "what you are browsing"],
29
+ [/mail\.search|getemails|filter\.mail/i, "email"], [/password|keychain/i, "saved passwords"],
30
+ ];
31
+ const OUTSIDE = [
32
+ [/downloadurl$/i, "the contents of a web address"], [/getwebpagecontents|getarticle|gethtmlfromrichtext/i, "a web page"], [/rss|getitemsfromfeed|rss\.extract/i, "a feed"],
33
+ [/url\.expand|getheadersofurl/i, "a web address's reply"], [/detect\.link|geturlcomponent/i, "links found in its input"], [/scanbarcode/i, "a scanned code"],
34
+ ];
35
+ const SENDS = [
36
+ [/sendmessage|sendimessage/i, "sends a message"], [/sendemail|mail\.send/i, "sends an email"], [/airdropdocument/i, "sends over AirDrop"],
37
+ [/postonfacebook|tweet|postto|instagram|tumblr|wordpress|slack|discord|telegram|whatsapp\.send/i, "posts to a service"], [/dropbox\.savefile|box\.savefile|evernote|uploadto|imgur|cloudapp|pastebin/i, "uploads to a service"],
38
+ [/runsshscript/i, "runs a script on another machine"], [/share$|showsharesheet/i, "opens the share sheet"], [/openurl$|openxcallbackurl|openin$/i, "opens a web address or another app"],
39
+ ];
40
+ const CODE = [
41
+ [/runshellscript/i, "a shell script"], [/runapplescript|runjavascriptforautomation/i, "AppleScript or JavaScript for Automation"], [/runjavascriptonwebpage/i, "JavaScript on a web page"], [/runsshscript/i, "a script over SSH"],
42
+ ];
43
+ export function looksLikeShortcut(doc) {
44
+ return !!doc && typeof doc === "object" && !Array.isArray(doc) && Array.isArray(doc.WFWorkflowActions);
45
+ }
46
+ /** A property list in XML, as far as a shortcut needs: dictionaries, arrays, strings, numbers, booleans. */
47
+ export function parsePlistXml(xml) {
48
+ const tokens = [...xml.matchAll(/<(\/?)(plist|dict|array|key|string|integer|real|true|false|data|date)\b[^>]*?(\/?)>|([^<]+)/g)];
49
+ let i = 0;
50
+ const dec = (s) => s.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Number(n))).replace(/&#x([0-9a-f]+);/gi, (_, n) => String.fromCodePoint(parseInt(n, 16))).replace(/&amp;/g, "&");
51
+ const textUntilClose = (tag) => { let s = ""; while (i < tokens.length && !(tokens[i][1] === "/" && tokens[i][2] === tag)) {
52
+ s += tokens[i][4] ?? "";
53
+ i++;
54
+ } i++; return dec(s); };
55
+ const value = (depth) => {
56
+ while (i < tokens.length && tokens[i][4] !== undefined)
57
+ i++; // whitespace between elements
58
+ const t = tokens[i++];
59
+ if (!t || depth > 64)
60
+ return undefined;
61
+ const tag = t[2], selfClosing = t[3] === "/";
62
+ if (tag === "true")
63
+ return true;
64
+ if (tag === "false")
65
+ return false;
66
+ if (selfClosing)
67
+ return tag === "dict" ? {} : tag === "array" ? [] : "";
68
+ if (tag === "string" || tag === "data" || tag === "date")
69
+ return textUntilClose(tag);
70
+ if (tag === "integer" || tag === "real")
71
+ return Number(textUntilClose(tag));
72
+ if (tag === "array") {
73
+ const a = [];
74
+ for (;;) {
75
+ while (i < tokens.length && tokens[i][4] !== undefined)
76
+ i++;
77
+ if (i >= tokens.length || (tokens[i][1] === "/" && tokens[i][2] === "array")) {
78
+ i++;
79
+ break;
80
+ }
81
+ a.push(value(depth + 1));
82
+ if (a.length > 20_000)
83
+ break;
84
+ }
85
+ return a;
86
+ }
87
+ if (tag === "dict") {
88
+ const d = {};
89
+ for (;;) {
90
+ while (i < tokens.length && tokens[i][4] !== undefined)
91
+ i++;
92
+ if (i >= tokens.length || (tokens[i][1] === "/" && tokens[i][2] === "dict")) {
93
+ i++;
94
+ break;
95
+ }
96
+ if (tokens[i][2] !== "key") {
97
+ i++;
98
+ continue;
99
+ }
100
+ i++;
101
+ const k = textUntilClose("key");
102
+ d[k] = value(depth + 1);
103
+ }
104
+ return d;
105
+ }
106
+ if (tag === "plist")
107
+ return value(depth + 1);
108
+ return undefined;
109
+ };
110
+ return value(0);
111
+ }
112
+ /** every string inside an action's parameters, with the path to it */
113
+ function strings(v, path, out, depth = 0) {
114
+ if (depth > 12 || out.length > 4000)
115
+ return;
116
+ if (typeof v === "string") {
117
+ if (v.trim().length >= 2 && !/^[0-9A-F]{8}-[0-9A-F]{4}-/i.test(v))
118
+ out.push({ path, text: v });
119
+ }
120
+ else if (Array.isArray(v))
121
+ v.slice(0, 500).forEach((x, i) => strings(x, `${path}[${i}]`, out, depth + 1));
122
+ else if (v && typeof v === "object")
123
+ for (const [k, x] of Object.entries(v)) {
124
+ if (!/^(UUID|GroupingIdentifier|WFSerializationType|OutputUUID|CustomOutputName|Type|WFControlFlowMode)$/.test(k))
125
+ strings(x, path ? `${path}.${k}` : k, out, depth + 1);
126
+ }
127
+ }
128
+ export function summarizeShortcut(doc) {
129
+ const d = (doc ?? {});
130
+ const actions = (Array.isArray(d.WFWorkflowActions) ? d.WFWorkflowActions : []).slice(0, 5000).map((a) => ({ id: String(a?.WFWorkflowActionIdentifier ?? ""), params: (a?.WFWorkflowActionParameters ?? {}) }));
131
+ const s = { actions: actions.length, readsPrivate: [], outsideInput: [], sendsOut: [], runsCode: [], runsOtherShortcuts: [], destinations: [], acceptsSharedInput: Array.isArray(d.WFWorkflowInputContentItemClasses) && d.WFWorkflowInputContentItemClasses.length > 0 && Array.isArray(d.WFWorkflowTypes) && d.WFWorkflowTypes.some((t) => /ActionExtension|Share|QuickActions|ReceivesOnScreenContent/i.test(String(t))) };
132
+ const push = (list, v) => { if (!list.includes(v))
133
+ list.push(v); };
134
+ for (const a of actions) {
135
+ const id = short(a.id);
136
+ for (const [re, what] of PRIVATE)
137
+ if (re.test(id))
138
+ push(s.readsPrivate, what);
139
+ for (const [re, what] of OUTSIDE)
140
+ if (re.test(id))
141
+ push(s.outsideInput, what);
142
+ for (const [re, what] of SENDS)
143
+ if (re.test(id))
144
+ push(s.sendsOut, what);
145
+ for (const [re, what] of CODE)
146
+ if (re.test(id))
147
+ push(s.runsCode, what);
148
+ if (/runworkflow$/i.test(id))
149
+ push(s.runsOtherShortcuts, String(a.params.WFWorkflowName ?? a.params.WFWorkflow?.workflowName ?? "another shortcut").slice(0, 60));
150
+ // a web request that carries a body is a way out, whatever the action is called
151
+ if (/downloadurl$/i.test(id)) {
152
+ const method = String(a.params.WFHTTPMethod ?? "GET").toUpperCase();
153
+ if (method !== "GET" || a.params.WFHTTPBodyType || a.params.WFFormValues || a.params.WFJSONValues || a.params.WFRequestVariable)
154
+ push(s.sendsOut, `sends a web request with a body (${method})`);
155
+ }
156
+ const found = [];
157
+ strings(a.params, "", found);
158
+ for (const f of found)
159
+ for (const m of f.text.matchAll(/\bhttps?:\/\/[^\s"'<>]+|\b[\w.+-]+@[\w-]+(?:\.[\w-]+)+\b|(?<!\d)\+\d[\d\s().-]{7,}\d/g))
160
+ push(s.destinations, m[0].slice(0, 120));
161
+ }
162
+ if (s.acceptsSharedInput)
163
+ push(s.outsideInput, "whatever is shared to it from other apps");
164
+ return { summary: s, actions };
165
+ }
166
+ export function adaptShortcut(raw) {
167
+ const segments = [];
168
+ const findings = [];
169
+ const add = (f) => findings.push({ pass: "channel", action: "none", ...f });
170
+ let doc;
171
+ const head = raw.trimStart();
172
+ if (head.startsWith("{")) {
173
+ try {
174
+ doc = JSON.parse(raw);
175
+ }
176
+ catch {
177
+ doc = undefined;
178
+ }
179
+ }
180
+ else if (/^(?:<\?xml|<!DOCTYPE plist|<plist)/i.test(head)) {
181
+ try {
182
+ doc = parsePlistXml(raw);
183
+ }
184
+ catch {
185
+ doc = undefined;
186
+ }
187
+ }
188
+ if (!looksLikeShortcut(doc)) {
189
+ // not a shortcut this reader understands: scanned as text, and said
190
+ segments.push({ text: raw, visibility: "visible", path: "automation/text" });
191
+ if (/^AEA1|^bplist00/.test(head))
192
+ add({ kind: "automation.unreadable", confidence: 0.1, severity: 0.2, message: head.startsWith("AEA1") ? "a signed shortcut is encrypted to Apple's format and its actions cannot be read here: nothing in it was checked" : "a binary property list: convert it with `plutil -convert json -o - <file>` and scan that" });
193
+ return { segments, findings };
194
+ }
195
+ const { summary: s, actions } = summarizeShortcut(doc);
196
+ const name = String(doc.WFWorkflowName ?? "");
197
+ // what it says about itself, and every piece of text inside it
198
+ segments.push({ text: [`Shortcut${name ? ` "${name}"` : ""}: ${s.actions} actions.`, s.readsPrivate.length ? `Reads: ${s.readsPrivate.join(", ")}.` : "", s.outsideInput.length ? `Takes in: ${s.outsideInput.join(", ")}.` : "", s.sendsOut.length ? `Can send out: ${s.sendsOut.join("; ")}.` : "", s.runsCode.length ? `Runs: ${s.runsCode.join(", ")}.` : "", s.destinations.length ? `Addresses written into it: ${s.destinations.slice(0, 12).join(", ")}.` : ""].filter(Boolean).join("\n"), visibility: "visible", path: "automation/summary" });
199
+ actions.forEach((a, i) => {
200
+ const found = [];
201
+ strings(a.params, "", found);
202
+ // a comment is written for the person; everything else is a parameter nobody reads in a long shortcut
203
+ for (const f of found)
204
+ segments.push({ text: f.text.replace(//g, " "), visibility: /comment$/i.test(a.id) ? "visible" : "attribute", path: `automation/action[${i}]:${short(a.id)}/${f.path}` });
205
+ });
206
+ if (s.readsPrivate.length && s.outsideInput.length && s.sendsOut.length) {
207
+ add({ kind: "automation.private_outside_send", confidence: 0.5, severity: 0.7, message: `this shortcut reads ${s.readsPrivate.slice(0, 3).join(", ")}, takes in ${s.outsideInput.slice(0, 2).join(" and ")}, and can send things out (${s.sendsOut.slice(0, 2).join("; ")}): content from outside can steer where your data goes` });
208
+ }
209
+ else if (s.readsPrivate.length && s.sendsOut.some((x) => /web request|script on another|posts to|uploads to/.test(x))) {
210
+ add({ kind: "automation.private_outside_send", confidence: 0.3, severity: 0.6, message: `this shortcut reads ${s.readsPrivate.slice(0, 3).join(", ")} and can send to a server (${s.sendsOut.filter((x) => /web request|script on another|posts to|uploads to/.test(x)).slice(0, 2).join("; ")})` });
211
+ }
212
+ if (s.runsCode.length)
213
+ add({ kind: "automation.runs_code", confidence: 0.35, severity: 0.6, message: `this shortcut runs ${s.runsCode.join(", ")}: read the script before installing it` });
214
+ if (s.runsOtherShortcuts.length)
215
+ add({ kind: "automation.runs_code", confidence: 0.1, severity: 0.3, message: `this shortcut runs other shortcuts (${s.runsOtherShortcuts.slice(0, 4).join(", ")}), which were not read` });
216
+ const tricks = s.destinations.filter((x) => /^https?:/i.test(x)).map((u) => [u, linkTrick(u)]).filter(([, t]) => t);
217
+ if (tricks.length)
218
+ add({ kind: "automation.destination", confidence: 0.4, severity: 0.6, message: `an address written into the shortcut is ${tricks[0][1]}`, excerpt: tricks[0][0].slice(0, 120) });
219
+ return { segments, findings };
220
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Streams: content that arrives in pieces and is read as it arrives (server-sent events, agent-to-agent
3
+ * messages, live logs, chat feeds, live transcripts).
4
+ *
5
+ * There is no whole document to score before delivery, so the scanner keeps three promises instead:
6
+ *
7
+ * 1. Nothing is released before it has been scanned together with what came just before it. Each scan
8
+ * covers the unreleased text plus an overlap of text already released, so an instruction split across
9
+ * two chunks is seen whole.
10
+ * 2. The newest part is held back. A sentence that has only half arrived cannot be judged, so the last
11
+ * `holdBackChars` stay in the buffer until more arrives or the stream ends. An instruction shorter than
12
+ * the hold-back is never released at all once it is recognised.
13
+ * 3. A verdict that turns bad stops the stream. Nothing further is released, and the caller is told which
14
+ * already-released stretch belonged to the window that turned bad, so it can tell the agent that text
15
+ * is tainted too ("cut and retract").
16
+ *
17
+ * The cost is latency: the hold-back delays the newest text. Callers choose it.
18
+ */
19
+ import type { IngressEngine } from "./engine.js";
20
+ import type { ContentKind, Finding, SourceInfo, Tier } from "./types.js";
21
+ export interface StreamOptions {
22
+ /** receipt source, e.g. "stream:sse:https://feed.example/events" */
23
+ url?: string;
24
+ /** how the text should be read; streams are text-like (text, markdown, log, captions, message) */
25
+ kind?: ContentKind;
26
+ source?: SourceInfo;
27
+ /** newest characters kept back until more arrives (default 600) */
28
+ holdBackChars?: number;
29
+ /** already-released characters rescanned with each new piece (default 1200) */
30
+ overlapChars?: number;
31
+ /** do not scan until this much unreleased text has arrived, unless the stream ends (default 400) */
32
+ minScanChars?: number;
33
+ /** the stream is refused once it has carried this much (default 20 MB) */
34
+ maxTotalChars?: number;
35
+ }
36
+ export interface StreamStep {
37
+ /** text that may now be handed to the model; "" when nothing is ready or the stream was cut */
38
+ release: string;
39
+ tier: Tier;
40
+ /** true once the stream has been stopped; every later step is cut too */
41
+ cut: boolean;
42
+ /** set on the step that cut the stream */
43
+ retract?: {
44
+ /** offset (in released characters) from which earlier text belonged to the window that turned bad */
45
+ fromOffset: number;
46
+ /** how many already-released characters that is */
47
+ chars: number;
48
+ findings: Finding[];
49
+ /** what to tell the agent */
50
+ notice: string;
51
+ };
52
+ }
53
+ export interface StreamSummary {
54
+ tier: Tier;
55
+ cut: boolean;
56
+ receivedChars: number;
57
+ releasedChars: number;
58
+ windows: number;
59
+ /** sha256 of everything released, so the caller can prove what the model was given */
60
+ releasedDigest: string;
61
+ }
62
+ export declare class StreamScanner {
63
+ private readonly engine;
64
+ private pending;
65
+ private releasedTail;
66
+ private releasedChars;
67
+ private received;
68
+ private windows;
69
+ private isCut;
70
+ private tier;
71
+ private hash;
72
+ private chain;
73
+ private readonly o;
74
+ constructor(engine: IngressEngine, opts?: StreamOptions);
75
+ /** Add the next piece. Steps are processed in order even if push is called again before the last one resolved. */
76
+ push(chunk: string): Promise<StreamStep>;
77
+ /** The stream is over: scan and release what was held back. */
78
+ end(): Promise<StreamStep>;
79
+ summary(): StreamSummary;
80
+ private step;
81
+ private cut;
82
+ }
83
+ /** Scan a complete text as if it had arrived in the given pieces. Convenience for tests and replays. */
84
+ export declare function scanStream(engine: IngressEngine, chunks: Iterable<string> | AsyncIterable<string>, opts?: StreamOptions): Promise<{
85
+ released: string;
86
+ steps: StreamStep[];
87
+ summary: StreamSummary;
88
+ }>;