paseo-plugin-secure-input 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/OVERVIEW.md +30 -0
- package/README.md +252 -2
- package/SECURITY.md +79 -0
- package/assets/icon.png +0 -0
- package/client/card.tsx +74 -0
- package/client/codes.ts +32 -0
- package/client/diagnostics-rows.ts +139 -0
- package/client/dialog-state.ts +59 -0
- package/client/dialog.tsx +292 -0
- package/client/envelope-cache.ts +85 -0
- package/client/fields.tsx +255 -0
- package/client/file-entry.ts +142 -0
- package/client/form-state.ts +198 -0
- package/client/globals.d.ts +19 -0
- package/client/outcome.ts +96 -0
- package/client/presentation.ts +114 -0
- package/client/sealing.ts +27 -0
- package/client/self-test.ts +96 -0
- package/client/settings.tsx +160 -0
- package/client/submit.ts +92 -0
- package/client/text.ts +47 -0
- package/client/tone.ts +36 -0
- package/client/web.ts +184 -0
- package/index.client.tsx +29 -0
- package/index.server.ts +6 -0
- package/npm-shrinkwrap.json +60 -0
- package/package.json +68 -4
- package/paseo-plugin.json +6 -0
- package/server/core/continuation.ts +151 -0
- package/server/core/entries.ts +89 -0
- package/server/core/outbox.ts +555 -0
- package/server/core/registry-store.ts +185 -0
- package/server/core/registry.ts +1031 -0
- package/server/core/selftest.ts +60 -0
- package/server/core/view.ts +38 -0
- package/server/io/atomic.ts +119 -0
- package/server/io/deadline.ts +28 -0
- package/server/io/descriptor.ts +36 -0
- package/server/io/directory.ts +40 -0
- package/server/io/drop.ts +434 -0
- package/server/io/legacy-shim.ts +108 -0
- package/server/io/ownership.ts +8 -0
- package/server/io/private-file.ts +86 -0
- package/server/paseo/bounded.ts +13 -0
- package/server/paseo/handlers.ts +124 -0
- package/server/paseo/hooks.ts +106 -0
- package/server/paseo/plugin.ts +341 -0
- package/server/paseo/runtime.ts +409 -0
- package/server/paseo/tools.ts +180 -0
- package/shared/bytes.ts +100 -0
- package/shared/canonical.ts +32 -0
- package/shared/card.ts +41 -0
- package/shared/contracts.ts +128 -0
- package/shared/errors.ts +48 -0
- package/shared/limits.ts +118 -0
- package/shared/request-id.ts +9 -0
- package/shared/request.ts +66 -0
- package/shared/sanitize.ts +101 -0
- package/shared/seal.ts +292 -0
- package/shared/shown.ts +12 -0
- package/shared/text.ts +39 -0
- package/shared/view.ts +42 -0
- package/skills/secure-input/SKILL.md +148 -0
package/client/web.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { useEffect, useRef, type RefObject } from "react";
|
|
2
|
+
import { Platform } from "react-native";
|
|
3
|
+
import { LIMITS } from "../shared/limits";
|
|
4
|
+
import { wipeFile, type FileValue } from "./file-entry";
|
|
5
|
+
|
|
6
|
+
// The one module of the app that touches the browser's own objects. The desktop app is the web build of the app in
|
|
7
|
+
// Electron, and the SDK gives a plugin neither a file picker nor a way to take a file that is dropped: this module does
|
|
8
|
+
// both. It reaches the browser through `globalThis`, and describes below the few members it uses, so that no project is
|
|
9
|
+
// checked against the DOM's types. Every export is gated on the web platform and does nothing anywhere else: a phone has
|
|
10
|
+
// no picking and no dropping, only the paste area of a file row.
|
|
11
|
+
|
|
12
|
+
/** A file the browser holds, as far as this module reads it. */
|
|
13
|
+
interface BrowserFile {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
slice(start: number, end: number): { arrayBuffer(): Promise<ArrayBuffer> };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface BrowserFiles {
|
|
19
|
+
item(index: number): BrowserFile | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface FileInput {
|
|
23
|
+
type: string;
|
|
24
|
+
value: string;
|
|
25
|
+
readonly files: BrowserFiles | null;
|
|
26
|
+
addEventListener(type: "change" | "cancel", listener: () => void): void;
|
|
27
|
+
click(): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface Page {
|
|
31
|
+
createElement(tag: "input"): FileInput;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface DragEventLike {
|
|
35
|
+
preventDefault(): void;
|
|
36
|
+
stopPropagation(): void;
|
|
37
|
+
readonly dataTransfer: { readonly files: BrowserFiles; dropEffect: string } | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface DropTarget {
|
|
41
|
+
addEventListener(type: "dragover" | "drop", listener: (event: DragEventLike) => void): void;
|
|
42
|
+
removeEventListener(type: "dragover" | "drop", listener: (event: DragEventLike) => void): void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
46
|
+
return typeof value === "object" && value !== null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The page the app is drawn in, when there is one that can make an input. */
|
|
50
|
+
function page(): Page | null {
|
|
51
|
+
const found: unknown = Reflect.get(globalThis, "document");
|
|
52
|
+
return isObject(found) && typeof found.createElement === "function" ? (found as unknown as Page) : null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** What a row's ref holds on the web: the element it is drawn as, which takes listeners. Anything else is no target. */
|
|
56
|
+
function dropTarget(value: unknown): DropTarget | null {
|
|
57
|
+
return isObject(value) && typeof value.addEventListener === "function" && typeof value.removeEventListener === "function"
|
|
58
|
+
? (value as unknown as DropTarget)
|
|
59
|
+
: null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A file's bytes and name. At most one byte more than a file may have is read: enough for the check to say that it is
|
|
64
|
+
* too large, and nothing more of a large file taken into memory. Null when the browser cannot read it.
|
|
65
|
+
*/
|
|
66
|
+
function read(file: BrowserFile): Promise<FileValue | null> {
|
|
67
|
+
try {
|
|
68
|
+
return file
|
|
69
|
+
.slice(0, LIMITS.fileBytes + 1)
|
|
70
|
+
.arrayBuffer()
|
|
71
|
+
.then(
|
|
72
|
+
(buffer) => ({ name: file.name, bytes: new Uint8Array(buffer) }),
|
|
73
|
+
() => null,
|
|
74
|
+
);
|
|
75
|
+
} catch {
|
|
76
|
+
return Promise.resolve(null);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Whether this app can pick a file: the web build can, through an input of the page; a phone cannot. */
|
|
81
|
+
export const canPickFiles: boolean = Platform.OS === "web";
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Opens the browser's file picker and resolves with the file the human chose, or null when they chose none, the
|
|
85
|
+
* picker could not be opened, or the app is not the web build. It never rejects: every step that touches the page (the
|
|
86
|
+
* input made, its listeners, the click, the choice read) is in one path that settles with null when one of them throws.
|
|
87
|
+
* A file that comes after the answer was given is wiped, and the input lets go of what it was given.
|
|
88
|
+
*/
|
|
89
|
+
export function pickFile(): Promise<FileValue | null> {
|
|
90
|
+
if (Platform.OS !== "web") return Promise.resolve(null);
|
|
91
|
+
const found = page();
|
|
92
|
+
if (found === null) return Promise.resolve(null);
|
|
93
|
+
return new Promise((resolve) => {
|
|
94
|
+
let input: FileInput | null = null;
|
|
95
|
+
let settled = false;
|
|
96
|
+
const settle = (file: FileValue | null): void => {
|
|
97
|
+
if (settled) {
|
|
98
|
+
if (file !== null) wipeFile(file);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
settled = true;
|
|
102
|
+
try {
|
|
103
|
+
if (input !== null) input.value = "";
|
|
104
|
+
} catch {
|
|
105
|
+
// The input goes with this call, and what it was given with it.
|
|
106
|
+
}
|
|
107
|
+
resolve(file);
|
|
108
|
+
};
|
|
109
|
+
try {
|
|
110
|
+
const made = found.createElement("input");
|
|
111
|
+
input = made;
|
|
112
|
+
made.type = "file";
|
|
113
|
+
made.addEventListener("change", () => {
|
|
114
|
+
let chosen: BrowserFile | null;
|
|
115
|
+
try {
|
|
116
|
+
chosen = made.files?.item(0) ?? null;
|
|
117
|
+
} catch {
|
|
118
|
+
chosen = null;
|
|
119
|
+
}
|
|
120
|
+
if (chosen === null) settle(null);
|
|
121
|
+
else void read(chosen).then(settle, () => settle(null));
|
|
122
|
+
});
|
|
123
|
+
made.addEventListener("cancel", () => settle(null));
|
|
124
|
+
made.click();
|
|
125
|
+
} catch {
|
|
126
|
+
settle(null);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Takes a file dropped on the element `ref` holds: its bytes go to `onFile`, read as `pickFile` reads them. The drop is
|
|
133
|
+
* the row's alone: the browser does not open the file, and nothing around the row is given it. A file that is read after
|
|
134
|
+
* the row is gone is wiped. On a phone it does nothing.
|
|
135
|
+
*/
|
|
136
|
+
export function useFileDrop(ref: RefObject<unknown>, onFile: (file: FileValue) => void): void {
|
|
137
|
+
if (Platform.OS !== "web") return;
|
|
138
|
+
// The platform is the same on every render, so these hooks are called on every render of a web app and on none elsewhere.
|
|
139
|
+
const held = useRef({ onFile, mounted: true });
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
held.current.onFile = onFile;
|
|
142
|
+
});
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
const state = held.current;
|
|
145
|
+
state.mounted = true;
|
|
146
|
+
return () => {
|
|
147
|
+
state.mounted = false;
|
|
148
|
+
};
|
|
149
|
+
}, []);
|
|
150
|
+
// After every render: the element the ref holds may be another one.
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
const target = dropTarget(ref.current);
|
|
153
|
+
if (target === null) return undefined;
|
|
154
|
+
const over = (event: DragEventLike): void => {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
event.stopPropagation();
|
|
157
|
+
if (event.dataTransfer !== null) event.dataTransfer.dropEffect = "copy";
|
|
158
|
+
};
|
|
159
|
+
const drop = (event: DragEventLike): void => {
|
|
160
|
+
event.preventDefault();
|
|
161
|
+
event.stopPropagation();
|
|
162
|
+
const file = event.dataTransfer?.files.item(0) ?? null;
|
|
163
|
+
if (file === null) return;
|
|
164
|
+
void read(file).then((value) => {
|
|
165
|
+
if (value === null) return;
|
|
166
|
+
if (!held.current.mounted) {
|
|
167
|
+
wipeFile(value);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
held.current.onFile(value);
|
|
172
|
+
} catch {
|
|
173
|
+
wipeFile(value);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
target.addEventListener("dragover", over);
|
|
178
|
+
target.addEventListener("drop", drop);
|
|
179
|
+
return () => {
|
|
180
|
+
target.removeEventListener("dragover", over);
|
|
181
|
+
target.removeEventListener("drop", drop);
|
|
182
|
+
};
|
|
183
|
+
});
|
|
184
|
+
}
|
package/index.client.tsx
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { PluginClientContext } from "@getpaseo/plugin/client";
|
|
2
|
+
import { RequestCard } from "./client/card";
|
|
3
|
+
import { SettingsScreen } from "./client/settings";
|
|
4
|
+
import { CARD_KIND, CARD_VERSION, CardDataSchema } from "./shared/card";
|
|
5
|
+
import { readDiagnostics } from "./shared/contracts";
|
|
6
|
+
|
|
7
|
+
export default function contribute(client: PluginClientContext) {
|
|
8
|
+
const removeRenderer = client.addTimelineRenderer({
|
|
9
|
+
kind: CARD_KIND,
|
|
10
|
+
version: CARD_VERSION,
|
|
11
|
+
schema: CardDataSchema,
|
|
12
|
+
Component: RequestCard,
|
|
13
|
+
});
|
|
14
|
+
const removeSettings = client.addSettingsScreen({ id: "main", title: "Secure input", icon: "ShieldCheck", Component: SettingsScreen });
|
|
15
|
+
|
|
16
|
+
// The daemon half only receives its API handle inside a hook or an RPC. One
|
|
17
|
+
// harmless call when the app loads lets it publish cards that were waiting.
|
|
18
|
+
// A call that throws on the spot must not fail the whole contribution.
|
|
19
|
+
try {
|
|
20
|
+
void client.rpc(readDiagnostics, {}).catch(() => undefined);
|
|
21
|
+
} catch {
|
|
22
|
+
// nothing to do: the cards are published the next time the plugin is asked for anything
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return () => {
|
|
26
|
+
removeRenderer();
|
|
27
|
+
removeSettings();
|
|
28
|
+
};
|
|
29
|
+
}
|
package/index.server.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "paseo-plugin-secure-input",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"lockfileVersion": 3,
|
|
5
|
+
"requires": true,
|
|
6
|
+
"packages": {
|
|
7
|
+
"": {
|
|
8
|
+
"name": "paseo-plugin-secure-input",
|
|
9
|
+
"version": "0.2.0",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@noble/ciphers": "2.4.0",
|
|
13
|
+
"@noble/curves": "2.4.0",
|
|
14
|
+
"@noble/hashes": "2.4.0"
|
|
15
|
+
},
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=22.12.0"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"node_modules/@noble/ciphers": {
|
|
21
|
+
"version": "2.4.0",
|
|
22
|
+
"resolved": "https://registry.npmjs.org/@noble/ciphers/-/ciphers-2.4.0.tgz",
|
|
23
|
+
"integrity": "sha512-AnjFn0Jv92laAkvMrghlFZq4qQCIN/4DxFV/eooqtC2YTjB7kBeLMS2T9KJX4Dn+ZVXLOwK0lSgqDtx9gvxtiw==",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">= 20.19.0"
|
|
27
|
+
},
|
|
28
|
+
"funding": {
|
|
29
|
+
"url": "https://paulmillr.com/funding/"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"node_modules/@noble/curves": {
|
|
33
|
+
"version": "2.4.0",
|
|
34
|
+
"resolved": "https://registry.npmjs.org/@noble/curves/-/curves-2.4.0.tgz",
|
|
35
|
+
"integrity": "sha512-P4/62zrgfH33CneE3Dn4WhJVA22YUU0eR51wKIan4NVRvwsA0YnPTwWGpNbpuacSujmSFLvyzpyuR30+fbq2Ew==",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@noble/hashes": "2.4.0"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">= 20.19.0"
|
|
42
|
+
},
|
|
43
|
+
"funding": {
|
|
44
|
+
"url": "https://paulmillr.com/funding/"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"node_modules/@noble/hashes": {
|
|
48
|
+
"version": "2.4.0",
|
|
49
|
+
"resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-2.4.0.tgz",
|
|
50
|
+
"integrity": "sha512-X5XaVWZIBCT7HHZGm5I7ZQXDwLG+bGXuSrMQAW+7Zvl87h1kmc1ZB1VSRJcpUfoUrGQp4Fkoxm5kZ+Ms+aW+eA==",
|
|
51
|
+
"license": "MIT",
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">= 20.19.0"
|
|
54
|
+
},
|
|
55
|
+
"funding": {
|
|
56
|
+
"url": "https://paulmillr.com/funding/"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "paseo-plugin-secure-input",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Secure secret input for Paseo agents: a typed value or a whole file travels sealed to the daemon and lands in a short-lived file; the agent gets its path, never the value",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "dutchakdev",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/dutchakdev/paseo-plugin-secure-input.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/dutchakdev/paseo-plugin-secure-input#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/dutchakdev/paseo-plugin-secure-input/issues"
|
|
15
|
+
},
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=22.12.0"
|
|
18
|
+
},
|
|
19
|
+
"os": [
|
|
20
|
+
"darwin",
|
|
21
|
+
"linux"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [
|
|
24
|
+
"paseo",
|
|
25
|
+
"paseo-plugin",
|
|
26
|
+
"secrets",
|
|
27
|
+
"security"
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"typecheck": "tsc -p tsconfig.server.json && tsc -p tsconfig.client.json && tsc -p tsconfig.client-tests.json",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"check": "npm run typecheck && npm test",
|
|
33
|
+
"prepack": "node scripts/shrinkwrap.mjs write",
|
|
34
|
+
"postpack": "node scripts/shrinkwrap.mjs remove"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@noble/ciphers": "2.4.0",
|
|
38
|
+
"@noble/curves": "2.4.0",
|
|
39
|
+
"@noble/hashes": "2.4.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@getpaseo/client": "0.14.0-beta.1",
|
|
43
|
+
"@getpaseo/plugin": "0.14.0-beta.1",
|
|
44
|
+
"@getpaseo/protocol": "0.14.0-beta.1",
|
|
45
|
+
"@getpaseo/relay": "0.14.0-beta.1",
|
|
46
|
+
"@modelcontextprotocol/sdk": "1.32.1",
|
|
47
|
+
"@tanstack/react-query": "^5.90.11",
|
|
48
|
+
"@types/node": "^22.0.0",
|
|
49
|
+
"@types/react": "~19.2.0",
|
|
50
|
+
"react": "19.1.0",
|
|
51
|
+
"react-native": "0.81.5",
|
|
52
|
+
"semver": "7.8.5",
|
|
53
|
+
"typescript": "^5.9.3",
|
|
54
|
+
"vitest": "^4.1.11",
|
|
55
|
+
"zod": "^4.4.3"
|
|
56
|
+
},
|
|
57
|
+
"files": [
|
|
58
|
+
"paseo-plugin.json",
|
|
59
|
+
"npm-shrinkwrap.json",
|
|
60
|
+
"OVERVIEW.md",
|
|
61
|
+
"index.client.tsx",
|
|
62
|
+
"index.server.ts",
|
|
63
|
+
"client/",
|
|
64
|
+
"server/",
|
|
65
|
+
"shared/",
|
|
66
|
+
"skills/",
|
|
67
|
+
"assets/icon.png",
|
|
68
|
+
"SECURITY.md"
|
|
69
|
+
]
|
|
70
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { ErrorCode } from "../../shared/errors";
|
|
2
|
+
import { DROP_TTL_MS, MAX_RETRIES } from "../../shared/limits";
|
|
3
|
+
import { filePath, setPath } from "../io/drop";
|
|
4
|
+
import type { RequestRecord } from "./registry";
|
|
5
|
+
import type { ReleasedBy } from "./registry-store";
|
|
6
|
+
|
|
7
|
+
const PREFIX = "[secure-input]";
|
|
8
|
+
|
|
9
|
+
const COUNT_WORDS: readonly string[] = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* "Three submissions failed": how many submissions it takes to reach the retry limit, in words, as the start of a
|
|
13
|
+
* sentence. Built from the number the registry counts to, so the sentence cannot say another one. A number past the
|
|
14
|
+
* list is written in digits.
|
|
15
|
+
*/
|
|
16
|
+
export function submissionsFailed(count: number): string {
|
|
17
|
+
return `${COUNT_WORDS[count] ?? String(count)} ${count === 1 ? "submission" : "submissions"} failed`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** One sentence for every code a request can be rejected with, so a new code is a compile error until it has one. */
|
|
21
|
+
const HINTS: Record<ErrorCode, string> = {
|
|
22
|
+
invalid_request: "The request or the submitted values could not be read.",
|
|
23
|
+
request_already_pending: "Another request from you was still open.",
|
|
24
|
+
unknown_agent: "The plugin could not tell which agent asked.",
|
|
25
|
+
service_unavailable: "The plugin was not able to take the request.",
|
|
26
|
+
write_failed: "The plugin could not write the files.",
|
|
27
|
+
retry_limit: `${submissionsFailed(MAX_RETRIES)}: the sealed values could not be opened.`,
|
|
28
|
+
request_closed: "The request was no longer open when the user answered, for example because the plugin restarted.",
|
|
29
|
+
decrypt_failed: "The sealed values could not be opened.",
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const NO_REASON = "The plugin did not record a reason.";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Rejections the agent cannot repair by itself: a new request would only fail the same way, and every request makes the
|
|
36
|
+
* user type the values again. Their message ends by sending the agent to the user instead of to a new request.
|
|
37
|
+
*/
|
|
38
|
+
const ONLY_THE_USER_CAN_FIX: ReadonlySet<ErrorCode> = new Set([
|
|
39
|
+
"invalid_request",
|
|
40
|
+
"unknown_agent",
|
|
41
|
+
"service_unavailable",
|
|
42
|
+
"write_failed",
|
|
43
|
+
"retry_limit",
|
|
44
|
+
"decrypt_failed",
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
const FIX_AND_ASK = "Fix the cause and make a new request.";
|
|
48
|
+
const TELL_THE_USER = "Tell the user what happened. Do not make a new request yourself; each one makes the user type the values again.";
|
|
49
|
+
|
|
50
|
+
/** What a rejection says: the code (when this version knows it), its sentence and what to do next. */
|
|
51
|
+
function reasonOf(code: ErrorCode | null): { named: string; hint: string; next: string } {
|
|
52
|
+
// `Object.hasOwn`, because a code the table does not know must not reach `constructor` or `__proto__`.
|
|
53
|
+
if (code === null || typeof code !== "string" || !Object.hasOwn(HINTS, code)) return { named: "", hint: NO_REASON, next: TELL_THE_USER };
|
|
54
|
+
return { named: ` (${code})`, hint: HINTS[code], next: ONLY_THE_USER_CAN_FIX.has(code) ? TELL_THE_USER : FIX_AND_ASK };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* When the files of a set went, for each way of release that has a message: the agent is told this when the message
|
|
59
|
+
* about the files was still waiting when they went. A set the agent released, or that went at the end of the turn the
|
|
60
|
+
* message started, has none: the agent took them, or was told and had its turn.
|
|
61
|
+
*/
|
|
62
|
+
const GONE: Readonly<Partial<Record<ReleasedBy, string>>> = {
|
|
63
|
+
time: `${DROP_TTL_MS / 60_000} minutes after they were written`,
|
|
64
|
+
restart: "when the plugin restarted",
|
|
65
|
+
stop: "when the plugin stopped",
|
|
66
|
+
human: "by the user",
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* A path as a shell reads it: as it is when it is made of letters, digits and `_./-` alone, and otherwise in single
|
|
71
|
+
* quotes, a quote in it written as `'\''`. Nothing in a path quoted so is expanded or run.
|
|
72
|
+
*/
|
|
73
|
+
export function shellPath(path: string): string {
|
|
74
|
+
return /^[A-Za-z0-9_./-]+$/.test(path) ? path : `'${path.replaceAll("'", "'\\''")}'`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** What the agent is told of a delivery: where each value is, what never to do with it, what to do, and when the files go. */
|
|
78
|
+
function deliveredText(head: string, record: RequestRecord, drop: string): string | null {
|
|
79
|
+
const { written, deleteBy } = record;
|
|
80
|
+
if (written === null || deleteBy === null) return null;
|
|
81
|
+
const empty = record.request.fields.map((field) => field.key).filter((key) => !written.includes(key));
|
|
82
|
+
return [
|
|
83
|
+
`${head} delivered ${written.length} value(s) as files only your OS user can read:`,
|
|
84
|
+
...written.map((key) => ` ${key} → ${shellPath(filePath(drop, record.id, key))}`),
|
|
85
|
+
...(empty.length > 0 ? [`Left empty by the user, so no file: ${empty.join(", ")}.`] : []),
|
|
86
|
+
"The values were not shown to you. Never let the content of a file or directory whose name starts with si-secret- reach you: do not read, print, open, compare or log it, and do not pass it as an argument.",
|
|
87
|
+
"Move each value where it is needed only by redirection or by copy, for example `command < PATH` or `cp PATH DESTINATION`.",
|
|
88
|
+
`Then delete the directory (\`rm -r ${shellPath(setPath(drop, record.id))}\`) or call release_secret.`,
|
|
89
|
+
`The files are deleted at the end of the turn this message starts, and at ${new Date(deleteBy).toISOString()} at the latest.`,
|
|
90
|
+
].join("\n");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** What the agent is told of a set that went before its message reached it, or null when there is nothing to tell. */
|
|
94
|
+
function releasedText(head: string, by: ReleasedBy | undefined): string | null {
|
|
95
|
+
// `Object.hasOwn`, because a way this version does not know must not reach `constructor` or `__proto__`.
|
|
96
|
+
if (by === undefined || typeof by !== "string" || !Object.hasOwn(GONE, by)) return null;
|
|
97
|
+
return `${head} was delivered, but its files were deleted ${GONE[by] as string}, before this message reached you. Nothing is left to move. If the values are still needed, make a new request.`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The fixed text an agent receives about a request, or null when the record has nothing to tell. Built from the
|
|
102
|
+
* record's id, the keys it asked for and the keys that were written, its deadline, its error code and how it ended, and
|
|
103
|
+
* from the drop the files are in; nothing the agent wrote in the request (reason, labels) is repeated. The record holds
|
|
104
|
+
* no value, so no text can.
|
|
105
|
+
*/
|
|
106
|
+
export function continuationText(record: RequestRecord, drop: string): string | null {
|
|
107
|
+
const head = `${PREFIX} Request ${record.id}`;
|
|
108
|
+
switch (record.state) {
|
|
109
|
+
case "delivered":
|
|
110
|
+
return deliveredText(head, record, drop);
|
|
111
|
+
case "released":
|
|
112
|
+
return releasedText(head, record.releasedBy);
|
|
113
|
+
case "rejected": {
|
|
114
|
+
const { named, hint, next } = reasonOf(record.errorCode);
|
|
115
|
+
return `${head} was rejected before any file was written${named}. ${hint} ${next}`;
|
|
116
|
+
}
|
|
117
|
+
case "declined":
|
|
118
|
+
return `${head}: the user declined to provide the values. Do not ask again in a different way. Ask the user how they want to proceed.`;
|
|
119
|
+
case "expired":
|
|
120
|
+
if (record.expiredBy === "restart") return `${head} expired when the plugin restarted. Nothing was delivered. If the values are still needed, make a new request.`;
|
|
121
|
+
return `${head} expired before the user answered. If the values are still needed, make a new request.`;
|
|
122
|
+
default:
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The states whose arrival the agent is told of. `released` is not one: a message that still waits says it instead (`continuationText`). */
|
|
128
|
+
const ANNOUNCED: ReadonlySet<RequestRecord["state"]> = new Set(["delivered", "rejected", "declined", "expired"]);
|
|
129
|
+
|
|
130
|
+
/** Whether a request that has just come to its state is told to its agent. */
|
|
131
|
+
export function announces(record: RequestRecord): boolean {
|
|
132
|
+
return ANNOUNCED.has(record.state);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Sent when a second, different submission reached a request whose values were delivered. Built from the id alone. */
|
|
136
|
+
export function tamperText(record: RequestRecord): string {
|
|
137
|
+
return (
|
|
138
|
+
`${PREFIX} Warning for request ${record.id}: a second, different submission arrived after the values were delivered. ` +
|
|
139
|
+
"The files you were given may have held values the user did not type. Tell the user to check what you did with them and to rotate the secret."
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The message an outbox item stands for, or null when the record has nothing to say for it: the tamper warning only
|
|
145
|
+
* when the registry flagged the request as tampered with, the continuation only when the record has one at the moment
|
|
146
|
+
* it is sent. `drop` is where the files of a set are.
|
|
147
|
+
*/
|
|
148
|
+
export function messageFor(record: RequestRecord, kind: "continuation" | "tamper", drop: string): string | null {
|
|
149
|
+
if (kind === "tamper") return record.tamper ? tamperText(record) : null;
|
|
150
|
+
return continuationText(record, drop);
|
|
151
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { decodedLength, fromBase64Url, utf8, utf8Length } from "../../shared/bytes";
|
|
2
|
+
import { SecureInputError } from "../../shared/errors";
|
|
3
|
+
import { KEY_PATTERN, caseCollision, isValidKey } from "../../shared/limits";
|
|
4
|
+
import { LIMITS, type Field, type InputRequest } from "../../shared/request";
|
|
5
|
+
import type { FileEntry, PayloadEntry } from "../../shared/seal";
|
|
6
|
+
import { hasLoneSurrogate } from "../../shared/text";
|
|
7
|
+
import type { DropFile } from "../io/drop";
|
|
8
|
+
|
|
9
|
+
function invalid(detail: string): never {
|
|
10
|
+
throw new SecureInputError("invalid_request", detail);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isFileEntry(entry: PayloadEntry): entry is FileEntry {
|
|
14
|
+
return Object.hasOwn(entry, "file");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Checks what the app sent against what the agent asked for, and turns it into the files of the set: a text as its
|
|
19
|
+
* UTF-8, a file as its bytes. Errors name keys only: a value must never reach an error message.
|
|
20
|
+
*
|
|
21
|
+
* Every key is a valid one, said once, and no two name one file when case is ignored: the declared keys first, then the
|
|
22
|
+
* human's own, which are always taken. A declared field gets the kind it was declared with (a text when it names
|
|
23
|
+
* none). A text is at most `LIMITS.valueBytes` and holds no surrogate without its partner; a file is base64url and at
|
|
24
|
+
* most `LIMITS.fileBytes`. An empty text or an empty file is left empty: a declared optional field is then skipped, a
|
|
25
|
+
* required one refused, and a key of the human's gets no file. A submission that would write nothing is refused.
|
|
26
|
+
*
|
|
27
|
+
* The bytes that come out are the caller's to wipe. On a refusal every byte array made so far is wiped here.
|
|
28
|
+
*/
|
|
29
|
+
export function validateEntries(request: InputRequest, entries: readonly PayloadEntry[]): { written: DropFile[]; skipped: string[] } {
|
|
30
|
+
const written: DropFile[] = [];
|
|
31
|
+
try {
|
|
32
|
+
if (entries.length > LIMITS.fields) invalid("too many entries");
|
|
33
|
+
|
|
34
|
+
const seen = new Set<string>();
|
|
35
|
+
for (const entry of entries) {
|
|
36
|
+
if (!isValidKey(entry.key)) invalid(KEY_PATTERN.test(entry.key) ? "a key name is reserved" : "a key is not a valid identifier");
|
|
37
|
+
if (seen.has(entry.key)) invalid(`${entry.key} appears more than once`);
|
|
38
|
+
seen.add(entry.key);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const declared = new Map<string, Field>(request.fields.map((field) => [field.key, field] as const));
|
|
42
|
+
const theirs = entries.filter((entry) => !declared.has(entry.key)).map((entry) => entry.key);
|
|
43
|
+
const collision = caseCollision([...declared.keys(), ...theirs]);
|
|
44
|
+
if (collision !== null) invalid(`${collision[0]} and ${collision[1]} differ only in letter case`);
|
|
45
|
+
|
|
46
|
+
const filled = new Set<string>();
|
|
47
|
+
for (const entry of entries) {
|
|
48
|
+
const field = declared.get(entry.key);
|
|
49
|
+
if (field !== undefined && (field.kind === "file") !== isFileEntry(entry)) invalid(field.kind === "file" ? `${entry.key} must be a file` : `${entry.key} must be text`);
|
|
50
|
+
const bytes = isFileEntry(entry) ? fileBytes(entry) : textBytes(entry.key, entry.value);
|
|
51
|
+
if (bytes.length === 0) continue;
|
|
52
|
+
written.push({ key: entry.key, bytes });
|
|
53
|
+
filled.add(entry.key);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const skipped: string[] = [];
|
|
57
|
+
for (const field of request.fields) {
|
|
58
|
+
if (filled.has(field.key)) continue;
|
|
59
|
+
if (field.optional !== true) invalid(`${field.key} is required`);
|
|
60
|
+
skipped.push(field.key);
|
|
61
|
+
}
|
|
62
|
+
if (written.length === 0) invalid("nothing to write");
|
|
63
|
+
return { written, skipped };
|
|
64
|
+
} catch (error) {
|
|
65
|
+
for (const file of written) file.bytes.fill(0);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A text as the bytes of its file: its UTF-8, measured before it is made. */
|
|
71
|
+
function textBytes(key: string, value: string): Uint8Array {
|
|
72
|
+
if (utf8Length(value) > LIMITS.valueBytes) invalid(`${key} is larger than the limit`);
|
|
73
|
+
if (hasLoneSurrogate(value)) invalid(`${key} contains invalid Unicode`);
|
|
74
|
+
return utf8.encode(value);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A file as the bytes it was. One over the limit is refused from the length of its text, before it is decoded, so no
|
|
79
|
+
* array of it is ever made (`decodedLength` is the length of what the decoder would give). A text the decoder refuses
|
|
80
|
+
* leaves no array of it either (`fromBase64Url`), and what the decoder said is not passed on.
|
|
81
|
+
*/
|
|
82
|
+
function fileBytes(entry: FileEntry): Uint8Array {
|
|
83
|
+
if (decodedLength(entry.file) > LIMITS.fileBytes) invalid(`${entry.key} is larger than the limit`);
|
|
84
|
+
try {
|
|
85
|
+
return fromBase64Url(entry.file);
|
|
86
|
+
} catch {
|
|
87
|
+
invalid(`${entry.key} is not a file the plugin can read`);
|
|
88
|
+
}
|
|
89
|
+
}
|