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/shared/seal.ts
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { chacha20poly1305 } from "@noble/ciphers/chacha.js";
|
|
2
|
+
import { x25519 } from "@noble/curves/ed25519.js";
|
|
3
|
+
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
4
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
5
|
+
import { fromBase64Url, randomBytes, toBase64Url, toHex, utf8 } from "./bytes";
|
|
6
|
+
import { canonicalJson } from "./canonical";
|
|
7
|
+
import { LIMITS, PASEO_RPC_MAX_BYTES, RPC_FRAME_MARGIN } from "./limits";
|
|
8
|
+
|
|
9
|
+
export const SEAL_INFO = "paseo-secure-input/v1";
|
|
10
|
+
export const PAD_BLOCK = 1024;
|
|
11
|
+
/** The tag ChaCha20-Poly1305 adds to the ciphertext. */
|
|
12
|
+
export const AEAD_TAG_BYTES = 16;
|
|
13
|
+
|
|
14
|
+
/** A text field's value, as it was typed. */
|
|
15
|
+
export interface Entry {
|
|
16
|
+
key: string;
|
|
17
|
+
value: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A file field's content: the bytes of the file, as base64url. */
|
|
21
|
+
export interface FileEntry {
|
|
22
|
+
key: string;
|
|
23
|
+
file: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** One entry of a submission: a text, or a file. */
|
|
27
|
+
export type PayloadEntry = Entry | FileEntry;
|
|
28
|
+
|
|
29
|
+
export interface SealedEnvelope {
|
|
30
|
+
v: 1;
|
|
31
|
+
epk: string;
|
|
32
|
+
nonce: string;
|
|
33
|
+
ct: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface RequestKeyPair {
|
|
37
|
+
secretKey: Uint8Array;
|
|
38
|
+
publicKey: Uint8Array;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class SealError extends Error {}
|
|
42
|
+
|
|
43
|
+
function concat(a: Uint8Array, b: Uint8Array): Uint8Array {
|
|
44
|
+
const out = new Uint8Array(a.length + b.length);
|
|
45
|
+
out.set(a);
|
|
46
|
+
out.set(b, a.length);
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function deriveKey(shared: Uint8Array, ephemeralPublic: Uint8Array, recipientPublic: Uint8Array): Uint8Array {
|
|
51
|
+
if (shared.every((byte) => byte === 0)) throw new SealError("degenerate shared secret");
|
|
52
|
+
return hkdf(sha256, shared, concat(ephemeralPublic, recipientPublic), utf8.encode(SEAL_INFO), 32);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A key pair for one request. The secret key it drew is wiped if the public key cannot be derived from it: nobody else holds it yet. */
|
|
56
|
+
export function generateRequestKeyPair(): RequestKeyPair {
|
|
57
|
+
const secretKey = randomBytes(32);
|
|
58
|
+
try {
|
|
59
|
+
return { secretKey, publicKey: x25519.getPublicKey(secretKey) };
|
|
60
|
+
} catch (error) {
|
|
61
|
+
secretKey.fill(0);
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Seal to a one-time recipient key. A fresh ephemeral key per call makes the AEAD key single-use.
|
|
68
|
+
* Every secret this call creates is wiped on every exit, and no library message escapes: the ephemeral key is drawn
|
|
69
|
+
* inside the guard too, so a host without usable randomness is a sealing failure like any other and not a raw
|
|
70
|
+
* error of the platform.
|
|
71
|
+
*/
|
|
72
|
+
export function seal(recipientPublic: Uint8Array, plaintext: Uint8Array, aad: Uint8Array): SealedEnvelope {
|
|
73
|
+
let ephemeral: RequestKeyPair | undefined;
|
|
74
|
+
let shared: Uint8Array | undefined;
|
|
75
|
+
let key: Uint8Array | undefined;
|
|
76
|
+
try {
|
|
77
|
+
ephemeral = generateRequestKeyPair();
|
|
78
|
+
shared = x25519.getSharedSecret(ephemeral.secretKey, recipientPublic);
|
|
79
|
+
key = deriveKey(shared, ephemeral.publicKey, recipientPublic);
|
|
80
|
+
const nonce = randomBytes(12);
|
|
81
|
+
const ciphertext = chacha20poly1305(key, nonce, aad).encrypt(plaintext);
|
|
82
|
+
return { v: 1, epk: toBase64Url(ephemeral.publicKey), nonce: toBase64Url(nonce), ct: toBase64Url(ciphertext) };
|
|
83
|
+
} catch (error) {
|
|
84
|
+
// Library messages are not ours to vouch for; never forward them.
|
|
85
|
+
throw new SealError(error instanceof SealError ? error.message : "sealing failed");
|
|
86
|
+
} finally {
|
|
87
|
+
ephemeral?.secretKey.fill(0);
|
|
88
|
+
shared?.fill(0);
|
|
89
|
+
key?.fill(0);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function open(secretKey: Uint8Array, envelope: SealedEnvelope, aad: Uint8Array): Uint8Array {
|
|
94
|
+
let shared: Uint8Array | undefined;
|
|
95
|
+
let key: Uint8Array | undefined;
|
|
96
|
+
try {
|
|
97
|
+
if (envelope.v !== 1) throw new SealError("unsupported envelope version");
|
|
98
|
+
const ephemeralPublic = fromBase64Url(envelope.epk);
|
|
99
|
+
const nonce = fromBase64Url(envelope.nonce);
|
|
100
|
+
if (ephemeralPublic.length !== 32 || nonce.length !== 12) throw new SealError("malformed envelope");
|
|
101
|
+
shared = x25519.getSharedSecret(secretKey, ephemeralPublic);
|
|
102
|
+
key = deriveKey(shared, ephemeralPublic, x25519.getPublicKey(secretKey));
|
|
103
|
+
return chacha20poly1305(key, nonce, aad).decrypt(fromBase64Url(envelope.ct));
|
|
104
|
+
} catch (error) {
|
|
105
|
+
// Library messages are not ours to vouch for; never forward them.
|
|
106
|
+
throw new SealError(error instanceof SealError ? error.message : "authentication failed");
|
|
107
|
+
} finally {
|
|
108
|
+
shared?.fill(0);
|
|
109
|
+
key?.fill(0);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Binds a ciphertext to one request and to what it asked for: the digest of its declared keys and whether each is
|
|
115
|
+
* optional (`requestDigestOf` in the registry). The server builds this from its own record, the app from the view.
|
|
116
|
+
*/
|
|
117
|
+
export function buildAad(requestId: string, requestDigest: string): Uint8Array {
|
|
118
|
+
// canonicalJson drops undefined properties, so a missing digest would otherwise yield an AAD with no digest in it.
|
|
119
|
+
if (typeof requestId !== "string" || requestId === "" || typeof requestDigest !== "string" || requestDigest === "") {
|
|
120
|
+
throw new SealError("malformed binding");
|
|
121
|
+
}
|
|
122
|
+
return utf8.encode(canonicalJson({ v: 2, requestId, requestDigest }));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The letter the padding is made of. */
|
|
126
|
+
const PAD_LETTER = "x";
|
|
127
|
+
|
|
128
|
+
/** A control character is one byte of UTF-8 and six of canonical JSON (`\u0001`): the worst a byte of a text can grow to. */
|
|
129
|
+
const WORST_JSON_BYTES_PER_BYTE = 6;
|
|
130
|
+
|
|
131
|
+
/** How long the unpadded base64url of `bytes` bytes is: three bytes make four characters, a remainder of n bytes makes n + 1. */
|
|
132
|
+
function base64UrlLength(bytes: number): number {
|
|
133
|
+
return Math.ceil((bytes * 4) / 3);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The frame of a payload with no entry, and of each kind of entry with an empty key and nothing in it: `{"pad":"","values":[]}` and the like. */
|
|
137
|
+
const FRAME_BYTES = canonicalJson({ pad: "", values: [] }).length;
|
|
138
|
+
const TEXT_ENTRY_BYTES = canonicalJson({ key: "", value: "" }).length;
|
|
139
|
+
const FILE_ENTRY_BYTES = canonicalJson({ file: "", key: "" }).length;
|
|
140
|
+
/** What the quotes of an empty string add, which the frames above count once for each string. */
|
|
141
|
+
const QUOTES = 2;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The most a payload may be. Two bounds, and the smaller one with it, rounded down to a block:
|
|
145
|
+
*
|
|
146
|
+
* - what the per-field limits allow: `LIMITS.fields` entries, each with a key of `LIMITS.key` and the larger of the two
|
|
147
|
+
* kinds at their worst (a text of `LIMITS.valueBytes`, every byte written as six; a file of `LIMITS.fileBytes`, as
|
|
148
|
+
* base64url), in the encoder's frame and padded to a block as the encoder pads it;
|
|
149
|
+
* - what one Paseo RPC carries: its bytes less the frame of the fulfil call, as the base64url of a ciphertext, less the
|
|
150
|
+
* tag the cipher adds.
|
|
151
|
+
*
|
|
152
|
+
* Here and not in `./limits`, because it is measured on the encoder's frame. A test seals a payload of this size and
|
|
153
|
+
* holds the RPC that carries it to `PASEO_RPC_MAX_BYTES`.
|
|
154
|
+
*/
|
|
155
|
+
function largestPayload(): number {
|
|
156
|
+
const text = TEXT_ENTRY_BYTES + WORST_JSON_BYTES_PER_BYTE * LIMITS.valueBytes;
|
|
157
|
+
const file = FILE_ENTRY_BYTES + base64UrlLength(LIMITS.fileBytes);
|
|
158
|
+
const entries = LIMITS.fields * (LIMITS.key + Math.max(text, file)) + (LIMITS.fields - 1);
|
|
159
|
+
const allowed = Math.ceil((FRAME_BYTES + entries) / PAD_BLOCK) * PAD_BLOCK;
|
|
160
|
+
const carried = Math.floor(((PASEO_RPC_MAX_BYTES - RPC_FRAME_MARGIN) * 3) / 4) - AEAD_TAG_BYTES;
|
|
161
|
+
return Math.min(allowed, Math.floor(carried / PAD_BLOCK) * PAD_BLOCK);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The largest padded payload one submission may have: a multiple of `PAD_BLOCK` (see `largestPayload`). */
|
|
165
|
+
export const MAX_PAYLOAD_BYTES = largestPayload();
|
|
166
|
+
|
|
167
|
+
function isFileEntry(entry: PayloadEntry): entry is FileEntry {
|
|
168
|
+
return typeof (entry as Partial<FileEntry>).file === "string";
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* How many bytes of UTF-8 the JSON of `text` takes, quotes included, as `JSON.stringify` writes it: `"` and `\` and the
|
|
173
|
+
* five control characters that have a short escape take two, every other control character and every surrogate with
|
|
174
|
+
* no partner six (`\u0001`, `\ud800`); every other character its UTF-8, a well-formed pair four. Nothing is built.
|
|
175
|
+
*/
|
|
176
|
+
function jsonTextBytes(text: string): number {
|
|
177
|
+
let bytes = QUOTES;
|
|
178
|
+
for (let index = 0; index < text.length; index++) {
|
|
179
|
+
const unit = text.charCodeAt(index);
|
|
180
|
+
if (unit === 0x22 || unit === 0x5c || unit === 0x08 || unit === 0x09 || unit === 0x0a || unit === 0x0c || unit === 0x0d) bytes += 2;
|
|
181
|
+
else if (unit < 0x20) bytes += 6;
|
|
182
|
+
else if (unit < 0x80) bytes += 1;
|
|
183
|
+
else if (unit < 0x800) bytes += 2;
|
|
184
|
+
else if (unit >= 0xd800 && unit <= 0xdbff) {
|
|
185
|
+
const next = text.charCodeAt(index + 1);
|
|
186
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
187
|
+
bytes += 4;
|
|
188
|
+
index++;
|
|
189
|
+
} else bytes += 6;
|
|
190
|
+
} else if (unit >= 0xdc00 && unit <= 0xdfff) bytes += 6;
|
|
191
|
+
else bytes += 3;
|
|
192
|
+
}
|
|
193
|
+
return bytes;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The size `encodeValues` would give these entries, padding included, without building the payload: no copy of a value
|
|
198
|
+
* is made to measure it. The app says it before anything is sealed; the encoder refuses on it.
|
|
199
|
+
*/
|
|
200
|
+
export function payloadSize(values: readonly PayloadEntry[]): number {
|
|
201
|
+
let bytes = FRAME_BYTES + Math.max(0, values.length - 1);
|
|
202
|
+
for (const entry of values) {
|
|
203
|
+
bytes += isFileEntry(entry)
|
|
204
|
+
? FILE_ENTRY_BYTES - 2 * QUOTES + jsonTextBytes(entry.file) + jsonTextBytes(entry.key)
|
|
205
|
+
: TEXT_ENTRY_BYTES - 2 * QUOTES + jsonTextBytes(entry.value) + jsonTextBytes(entry.key);
|
|
206
|
+
}
|
|
207
|
+
return Math.ceil(bytes / PAD_BLOCK) * PAD_BLOCK;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Canonical JSON padded to a block multiple, so message size in a trace does not reveal value length.
|
|
212
|
+
*
|
|
213
|
+
* A payload larger than `MAX_PAYLOAD_BYTES` is refused before anything is built: it would not fit the RPC that carries
|
|
214
|
+
* it. Otherwise the payload is built once to be measured and, unless it fills its blocks already, once more with the
|
|
215
|
+
* padding it needs. Both buildings hold every value, so the first one is wiped on every way out, also when the second
|
|
216
|
+
* fails; the one that is returned is the caller's to wipe. Of an entry only its key and its value, or its key and its
|
|
217
|
+
* file, are written, whatever else it was handed with: the decoder refuses a member it does not know, and what it
|
|
218
|
+
* refuses is never written. An entry is a file entry when it has a file.
|
|
219
|
+
*/
|
|
220
|
+
export function encodeValues(values: readonly PayloadEntry[]): Uint8Array {
|
|
221
|
+
if (payloadSize(values) > MAX_PAYLOAD_BYTES) throw new SealError("payload too large");
|
|
222
|
+
const entries = values.map((entry) => (isFileEntry(entry) ? { key: entry.key, file: entry.file } : { key: entry.key, value: entry.value }));
|
|
223
|
+
const body = (pad: string): Uint8Array => utf8.encode(canonicalJson({ pad, values: entries }));
|
|
224
|
+
const bare = body("");
|
|
225
|
+
const missing = (PAD_BLOCK - (bare.length % PAD_BLOCK)) % PAD_BLOCK;
|
|
226
|
+
if (missing === 0) return bare;
|
|
227
|
+
try {
|
|
228
|
+
return body(PAD_LETTER.repeat(missing));
|
|
229
|
+
} finally {
|
|
230
|
+
bare.fill(0);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The members of a payload, and of each kind of entry in it. Sets, so that a name from a prototype is no member. */
|
|
235
|
+
const PAYLOAD_MEMBERS: ReadonlySet<string> = new Set(["pad", "values"]);
|
|
236
|
+
const TEXT_MEMBERS: ReadonlySet<string> = new Set(["key", "value"]);
|
|
237
|
+
const FILE_MEMBERS: ReadonlySet<string> = new Set(["key", "file"]);
|
|
238
|
+
|
|
239
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
240
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function hasOnly(record: Record<string, unknown>, members: ReadonlySet<string>): boolean {
|
|
244
|
+
return Object.keys(record).every((name) => members.has(name));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** The padding the encoder writes: its own letter and nothing else, and less than a block of it, because a whole block is never needed. */
|
|
248
|
+
function isEncoderPad(pad: unknown): boolean {
|
|
249
|
+
if (typeof pad !== "string" || pad.length >= PAD_BLOCK) return false;
|
|
250
|
+
for (let index = 0; index < pad.length; index++) {
|
|
251
|
+
if (pad[index] !== PAD_LETTER) return false;
|
|
252
|
+
}
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The entries of a payload. Strict: a payload that does not have the shape `encodeValues` writes is refused, always
|
|
258
|
+
* with the same words and nothing of the payload in them. It fills its blocks; it is an object with a `pad` and the
|
|
259
|
+
* `values`, and no other member; the pad is the encoder's (its letter, less than a block of it); every entry is exactly
|
|
260
|
+
* a key and a value, or exactly a key and a file, each of them text, and no other member: an entry that is both, or
|
|
261
|
+
* neither, is refused.
|
|
262
|
+
*
|
|
263
|
+
* What is held to the encoder is the shape, not the spelling: the bytes are not compared with a second encoding of
|
|
264
|
+
* what was read (the same members in another order, or JSON whitespace, are read as the same payload). Comparing
|
|
265
|
+
* them would put one more copy of every value into the daemon's memory, for a payload the AEAD has already
|
|
266
|
+
* authenticated and whose entries the registry checks against the request.
|
|
267
|
+
*/
|
|
268
|
+
export function decodeValues(plaintext: Uint8Array): PayloadEntry[] {
|
|
269
|
+
// encodeValues always pads to a block; anything else did not come from it.
|
|
270
|
+
if (plaintext.length === 0 || plaintext.length % PAD_BLOCK !== 0) throw new SealError("malformed payload");
|
|
271
|
+
let parsed: unknown;
|
|
272
|
+
try {
|
|
273
|
+
parsed = JSON.parse(utf8.decode(plaintext));
|
|
274
|
+
} catch {
|
|
275
|
+
throw new SealError("malformed payload");
|
|
276
|
+
}
|
|
277
|
+
if (!isRecord(parsed) || !hasOnly(parsed, PAYLOAD_MEMBERS)) throw new SealError("malformed payload");
|
|
278
|
+
if (!isEncoderPad(parsed.pad)) throw new SealError("malformed payload");
|
|
279
|
+
const values = parsed.values;
|
|
280
|
+
if (!Array.isArray(values)) throw new SealError("malformed payload");
|
|
281
|
+
return values.map((item: unknown): PayloadEntry => {
|
|
282
|
+
if (!isRecord(item) || typeof item.key !== "string") throw new SealError("malformed payload");
|
|
283
|
+
const { key } = item;
|
|
284
|
+
if (hasOnly(item, TEXT_MEMBERS) && typeof item.value === "string") return { key, value: item.value };
|
|
285
|
+
if (hasOnly(item, FILE_MEMBERS) && typeof item.file === "string") return { key, file: item.file };
|
|
286
|
+
throw new SealError("malformed payload");
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function digestHex(bytes: Uint8Array): string {
|
|
291
|
+
return toHex(sha256(bytes));
|
|
292
|
+
}
|
package/shared/shown.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { hasUnsafeCharacters } from "./sanitize";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Text a human reads: at most `max` code points, at least `min` (none by default), and free of anything that hides,
|
|
6
|
+
* reorders or restyles it. The one definition of that refinement; a caller that wants other words for the refusal gives
|
|
7
|
+
* them, and one that wants a minimum on a schema it already built can still call `.min` on the result.
|
|
8
|
+
*/
|
|
9
|
+
export function shown(max: number, min = 0, message = "unsafe characters") {
|
|
10
|
+
const text = min > 0 ? z.string().min(min) : z.string();
|
|
11
|
+
return text.max(max).refine((value) => !hasUnsafeCharacters(value), message);
|
|
12
|
+
}
|
package/shared/text.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How long a text is, in code points: a pair of surrogates counts once, a lone surrogate as itself. This is the unit
|
|
3
|
+
* the schemas state their limits in (Zod's string maximum counts code points, and so does `maxLength` of JSON Schema),
|
|
4
|
+
* so a check written by hand beside a schema counts with this and not with `.length`, which counts UTF-16 units and
|
|
5
|
+
* would refuse half of what the schema accepts of text outside the basic plane. Nothing is allocated.
|
|
6
|
+
*
|
|
7
|
+
* It is for limits on text an agent supplies and a human reads. A limit that guards a file, a volume or a socket is
|
|
8
|
+
* in bytes, and says so where it is declared.
|
|
9
|
+
*/
|
|
10
|
+
export function codePointLength(text: string): number {
|
|
11
|
+
let length = 0;
|
|
12
|
+
for (let index = 0; index < text.length; index++) {
|
|
13
|
+
const unit = text.charCodeAt(index);
|
|
14
|
+
if (unit >= 0xd800 && unit <= 0xdbff && index + 1 < text.length) {
|
|
15
|
+
const next = text.charCodeAt(index + 1);
|
|
16
|
+
if (next >= 0xdc00 && next <= 0xdfff) index++;
|
|
17
|
+
}
|
|
18
|
+
length++;
|
|
19
|
+
}
|
|
20
|
+
return length;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Whether a string holds a surrogate code unit with no partner. Such a value would be written as U+FFFD and silently
|
|
25
|
+
* change the secret: the daemon refuses it, and the dialog says so before anything is sent.
|
|
26
|
+
*/
|
|
27
|
+
export function hasLoneSurrogate(text: string): boolean {
|
|
28
|
+
for (let index = 0; index < text.length; index++) {
|
|
29
|
+
const unit = text.charCodeAt(index);
|
|
30
|
+
if (unit >= 0xd800 && unit <= 0xdbff) {
|
|
31
|
+
const next = text.charCodeAt(index + 1);
|
|
32
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) return true;
|
|
33
|
+
index++;
|
|
34
|
+
} else if (unit >= 0xdc00 && unit <= 0xdfff) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
package/shared/view.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ERROR_CODES } from "./errors";
|
|
3
|
+
import { DIGEST_PATTERN, PUBLIC_KEY_PATTERN } from "./limits";
|
|
4
|
+
import { KEY_PATTERN, LIMITS, REQUEST_STATES } from "./request";
|
|
5
|
+
import { REQUEST_ID_PATTERN } from "./request-id";
|
|
6
|
+
import { shown } from "./shown";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* One field of the form: the key the agent asked for, its label (the agent's words, scrubbed, which a screen reader
|
|
10
|
+
* reads and nobody sees), whether it may stay empty, and whether it is one line of text or a whole file. A file field
|
|
11
|
+
* has the format the app checks it against (`any` when the agent named none); a text field has none.
|
|
12
|
+
*/
|
|
13
|
+
const FieldViewSchema = z
|
|
14
|
+
.object({
|
|
15
|
+
key: z.string().regex(KEY_PATTERN),
|
|
16
|
+
label: shown(LIMITS.label),
|
|
17
|
+
optional: z.boolean(),
|
|
18
|
+
kind: z.enum(["text", "file"]),
|
|
19
|
+
format: z.enum(["json", "pem", "any"]).nullable(),
|
|
20
|
+
})
|
|
21
|
+
.strict()
|
|
22
|
+
.refine((field) => (field.kind === "file") === (field.format !== null), "a file field has a format, and a text field none");
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* What the form receives: the request's state and tamper flag, the code it was rejected with, the fields, and the key
|
|
26
|
+
* and digest a submission is sealed to. Nothing of the agent's but the scrubbed labels: the reason is kept with the
|
|
27
|
+
* request and shown nowhere, and the agent's title is not shown either.
|
|
28
|
+
*/
|
|
29
|
+
export const RequestViewSchema = z
|
|
30
|
+
.object({
|
|
31
|
+
id: z.string().regex(REQUEST_ID_PATTERN),
|
|
32
|
+
state: z.enum(REQUEST_STATES),
|
|
33
|
+
tamper: z.boolean(),
|
|
34
|
+
errorCode: z.enum(ERROR_CODES).nullable(),
|
|
35
|
+
fields: z.array(FieldViewSchema).max(LIMITS.fields),
|
|
36
|
+
publicKey: z.string().regex(PUBLIC_KEY_PATTERN),
|
|
37
|
+
requestDigest: z.string().regex(DIGEST_PATTERN),
|
|
38
|
+
createdAt: z.number().int().min(0),
|
|
39
|
+
})
|
|
40
|
+
.strict();
|
|
41
|
+
|
|
42
|
+
export type RequestView = z.output<typeof RequestViewSchema>;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: secure-input
|
|
3
|
+
description: Use when a task needs a secret from the human — a password, API key, token, private key, credentials file or connection string with credentials — or when you are about to tell the user to paste, export or type such a value themselves. Opens a secure form in the Paseo app; each value or file arrives as a short-lived file whose path you get, and you move it where it belongs without ever reading it.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Secure input
|
|
7
|
+
|
|
8
|
+
When you need a secret, make a request instead of asking in chat. The user fills in a form in the Paseo app, each value lands in a short-lived file that only your OS user can read, and you get its path. You move the value where it belongs without reading it, then delete the file.
|
|
9
|
+
|
|
10
|
+
## Hard rules
|
|
11
|
+
|
|
12
|
+
- **Never ask for a secret in chat.** Not "paste it here", not "export it and tell me", not "put it in `.env` yourself". Make a request.
|
|
13
|
+
- **Never let a `si-secret-` file's content reach you.** Never read, print, open, compare, hash, measure or log a file or directory whose name starts with `si-secret-`, and never pass its content as an argument: no `cat PATH`, `head`, `less`, `grep`, `diff`, `wc`, `xxd`, `base64`, no editor or file viewer, and no `ls -l` of its files. Such a path may stand in a command only after `<`, as the source of `cp`, or after `rm -r`: a value moves by redirection or by copy, and a command that reads it from its stdin into its destination, `cat` included, moves it blind.
|
|
14
|
+
- **Move, then delete or release.** Move every value in the turn the message starts. Then delete the directory, as the message shows, or call `release_secret`. At the end of that turn the plugin deletes the files anyway.
|
|
15
|
+
- **Do not poll.** A request answers `End your turn now. You will receive a message when the user has submitted. Do not poll.` Do that. Call `request_status` once, and only when the user says they submitted and no message came, or that no form appeared.
|
|
16
|
+
- **Say the reason yourself.** The form shows neither your reason nor your labels. In your own message, tell the user what you need, why, and that the form opens from the pill in this chat.
|
|
17
|
+
- **A plugin that is down is no reason to take a secret in chat.** When the tool is missing, or keeps answering `service_unavailable`, tell the user and stop.
|
|
18
|
+
- **One pending request at a time.** Ask for every value a task needs in a single request.
|
|
19
|
+
|
|
20
|
+
## Asking
|
|
21
|
+
|
|
22
|
+
The tool is `request_secret`, and its full name depends on your harness:
|
|
23
|
+
|
|
24
|
+
- Claude Code: `mcp__paseo__request_secret`, deferred. Load it with `ToolSearch` (`select:mcp__paseo__request_secret`) before the first call; the other three load the same way.
|
|
25
|
+
- Codex: a nested tool of `exec`. Find it in `ALL_TOOLS` and call `tools.mcp__paseo__request_secret(…)`.
|
|
26
|
+
- Elsewhere, look for a tool whose name ends in `request_secret`.
|
|
27
|
+
|
|
28
|
+
If there is none, this session cannot use the plugin: tell the user, and do not ask for the secret in chat. Your harness may ask the user to allow each call of these tools; that is expected.
|
|
29
|
+
|
|
30
|
+
The input is `{ reason, fields }`, each field `{ key, label?, optional?, kind?, format? }`:
|
|
31
|
+
|
|
32
|
+
- `reason`: one sentence, at most 300 characters. It is kept with the request and shown nowhere.
|
|
33
|
+
- `label`: at most 80 characters. A screen reader reads it with its field, a text or a file; it is not shown.
|
|
34
|
+
- `key`: `[A-Za-z_][A-Za-z0-9_]*`, at most 128 characters, not `__proto__`, `constructor` or `prototype`. It becomes the file's name, so keys that differ only in letter case are refused.
|
|
35
|
+
- `kind`: `"text"` (the default) for one line, such as a token or a password, at most 64 KiB; `"file"` for a whole file, such as a JSON key, a PEM, a kubeconfig or a `.p12`, at most 128 KiB, binary included.
|
|
36
|
+
- `format`: for a file only. `"json"` or `"pem"` is checked in the app; `"any"` (the default) is not.
|
|
37
|
+
- `optional`: the user may leave it empty, and it then gets no file.
|
|
38
|
+
- At most 32 fields, or none. The user can always add keys and files of their own.
|
|
39
|
+
|
|
40
|
+
One key:
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
{ "reason": "OpenAI key for the summariser's integration tests", "fields": [{ "key": "OPENAI_API_KEY" }] }
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Several, one of them optional:
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"reason": "Stripe test keys for the checkout tests",
|
|
51
|
+
"fields": [
|
|
52
|
+
{ "key": "STRIPE_KEY", "label": "Secret key, starts with sk_test_" },
|
|
53
|
+
{ "key": "STRIPE_WEBHOOK_SECRET", "optional": true }
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
A whole file:
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{ "reason": "GCP service account for the deploy script", "fields": [{ "key": "GCP_SA", "kind": "file", "format": "json" }] }
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
No keys, when you do not know their names. The user adds each key and its value:
|
|
65
|
+
|
|
66
|
+
```json
|
|
67
|
+
{ "reason": "Credentials for the staging database", "fields": [] }
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Moving a value blind
|
|
71
|
+
|
|
72
|
+
Each path in the delivery message is already quoted for a shell: use it as it stands (`PATH` below). A path from `request_status` (`files`) is not: it is the path as it is, and may hold spaces or an apostrophe. Quote it yourself before a shell sees it, in single quotes, each `'` in it written as `'\''`. Run every move before your turn ends.
|
|
73
|
+
|
|
74
|
+
**Into `.env`**, as one line `KEY=value`, into a file that git does not track and only your user can read, with nothing printed:
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
{ ! git rev-parse --is-inside-work-tree >/dev/null 2>&1 || git check-ignore -q .env.local; } &&
|
|
78
|
+
( umask 077; touch .env.local ) && chmod 600 .env.local &&
|
|
79
|
+
{ printf '\nSTRIPE_KEY='; cat; printf '\n'; } < PATH >> .env.local
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The first line goes on only outside a git repository, or when git ignores the file. When it stops there, nothing is written: ask the user where the value may go, and never add the file to git. The second line makes the file private: a new one is created `0600`, one that is there already is set to `0600`. The path stands only after `<`, and the output goes only into the file. The leading newline starts a line of its own even when the file does not end with one. The value goes in as typed, so a reader that treats `#`, quotes or spaces specially may read it differently. To learn whether the key is already there, count it by name, `grep -c '^STRIPE_KEY=' .env.local`, never by any part of a value; to replace it, ask the user. A value of several lines (a PEM, a JSON key) has no safe form on one `.env` line: ask for it as a file field and give it a file of its own, next.
|
|
83
|
+
|
|
84
|
+
**A whole file:**
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
cp PATH config/gcp-sa.json && chmod 600 config/gcp-sa.json
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`cp` keeps the mode of a destination that already existed, hence the `chmod`. Inside a git repository, make sure the destination is ignored first (`git check-ignore -q config/gcp-sa.json` succeeds), and never stage it.
|
|
91
|
+
|
|
92
|
+
**On stdin**, to a tool that reads its secret there:
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
gh secret set STRIPE_KEY < PATH
|
|
96
|
+
docker login --username USER --password-stdin REGISTRY < PATH
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**To a remote server:**
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
ssh HOST 'umask 077; mkdir -p ~/.config/app && touch ~/.config/app/token && chmod 600 ~/.config/app/token && cat > ~/.config/app/token' < PATH
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`umask 077` makes a new file private; `chmod 600` makes one that was there already private too.
|
|
106
|
+
|
|
107
|
+
**A credentials file a tool reads by path.** Copy it where it will stay, as above, and point the tool at the copy by its absolute path (`GOOGLE_APPLICATION_CREDENTIALS`, `KUBECONFIG`, a config file). Never point anything at the `si-secret-` path: it is gone at the end of the turn.
|
|
108
|
+
|
|
109
|
+
**Not blind**, so never:
|
|
110
|
+
|
|
111
|
+
- anything that shows the file: `cat PATH`, `echo "$(cat PATH)"`, an editor or a file viewer;
|
|
112
|
+
- the value as an argument (`--password "$(cat PATH)"`): other processes see arguments, and a tool may repeat them in an error;
|
|
113
|
+
- `export TOKEN="$(cat PATH)"`: every later command inherits it, and `env` prints it;
|
|
114
|
+
- a command that echoes what it stored, a verbose flag, `set -x`: read the tool's help first;
|
|
115
|
+
- comparing, counting or hashing the value (`diff`, `grep`, `wc -c`, `shasum`): each tells you something about it.
|
|
116
|
+
|
|
117
|
+
## What comes back
|
|
118
|
+
|
|
119
|
+
| You receive | Meaning | Do |
|
|
120
|
+
|---|---|---|
|
|
121
|
+
| `"status": "pending"` | The form is waiting for the user | End your turn |
|
|
122
|
+
| `… delivered N value(s) as files only your OS user can read:`, then `KEY → <path>` for each file | The values are files now | Move each one in this turn, then delete the directory as the message shows, or call `release_secret`. A key you did not ask for is one the user added: ask what it is for, without opening it |
|
|
123
|
+
| `Left empty by the user, so no file: …` | Optional keys the user left empty | Carry on without them |
|
|
124
|
+
| `… was delivered, but its files were deleted 15 minutes after they were written, …` | The files went before this message reached you | If the values are still needed, make a new request |
|
|
125
|
+
| `… deleted when the plugin restarted, …` | As above: the plugin restarted | The same |
|
|
126
|
+
| `… deleted when the plugin stopped, …` | As above: the plugin stopped | The same |
|
|
127
|
+
| `… deleted by the user, …` | As above: the user deleted every file in the plugin's settings | Ask the user before you ask again |
|
|
128
|
+
| `… was rejected before any file was written (code). …` | Nothing was written | If it ends `Fix the cause and make a new request.`, do that. If it ends `Tell the user what happened. Do not make a new request yourself; …`, tell the user and stop |
|
|
129
|
+
| `…: the user declined to provide the values.` | The user said no | Ask how they want to proceed; do not rephrase and retry |
|
|
130
|
+
| `… expired before the user answered.` | Nobody answered in 8 hours | Ask again if the values are still needed |
|
|
131
|
+
| `… expired when the plugin restarted. Nothing was delivered.` | The plugin restarted before the user submitted | Ask again if the values are still needed |
|
|
132
|
+
| `Warning for request …: a second, different submission arrived after the values were delivered.` | Someone may have submitted before the user: the files may not have held what the user typed | Tell the user what you did with the values, and that the secret should be rotated |
|
|
133
|
+
|
|
134
|
+
`request_status` gives `state`, `keys`, `written`, `tamper` and `errorCode`, and while the files exist, `files` (key → path) and `deleteBy`. Never a value. If it says `delivered`, move the files at those paths as you would from the message, each quoted for the shell first, as above. If it says `pending`, the form is still waiting: the user has not submitted, or the form refused what they sent and shows why. Either way the pill in this chat opens it; do not cancel and ask again, or the user types everything twice.
|
|
135
|
+
|
|
136
|
+
Nothing comes when the files go the ordinary way: when you delete them or call `release_secret`, or at the end of your turn. A request you cancel ends without a message.
|
|
137
|
+
|
|
138
|
+
## When a request is refused at once
|
|
139
|
+
|
|
140
|
+
The tool answers with an error whose text is `{"error": "<code>", "message": "<code>: <detail>"}`.
|
|
141
|
+
|
|
142
|
+
| `error` | Do |
|
|
143
|
+
|---|---|
|
|
144
|
+
| `invalid_request` | The message says what is wrong. A member of your request: fix it. `unknown request`: the id is not one of your requests. `the request has no files to release`: it was never delivered |
|
|
145
|
+
| `request_already_pending` | The message is the id of your pending request. Wait for it. `cancel_request` it only when its values are no longer needed, never to make room for a new request: the user would type everything again |
|
|
146
|
+
| `request_closed` | `cancel_request` on a request that no longer waits. `request_status` shows its state |
|
|
147
|
+
| `service_unavailable` | `the plugin has not finished starting; retry in a few seconds`: retry, twice at most. `the plugin is busy; retry in a few seconds`: nothing of the call was done; retry, twice at most, then tell the user. `the plugin did not start (…). Tell the user. Do not ask for the secret in chat instead.`: do that. `the plugin is shutting down`: tell the user. `the request could not be handled`: the plugin failed over the call; ask once more, and if that answers `request_already_pending`, wait for that request |
|
|
148
|
+
| `write_failed` | From `release_secret`: `the files could not be deleted; the plugin tries again`. Tell the user; do not call it in a loop |
|