paseo-plugin-secure-input 0.0.0-stage → 0.2.1
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 +28 -0
- package/README.md +105 -2
- package/SECURITY.md +84 -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 +178 -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 +150 -0
package/shared/bytes.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
2
|
+
const LOOKUP = new Map([...ALPHABET].map((char, index) => [char, index] as const));
|
|
3
|
+
|
|
4
|
+
/** base64url without padding. Hand-rolled because Hermes has neither Buffer nor a guaranteed btoa. */
|
|
5
|
+
export function toBase64Url(bytes: Uint8Array): string {
|
|
6
|
+
let out = "";
|
|
7
|
+
for (let index = 0; index < bytes.length; index += 3) {
|
|
8
|
+
const a = bytes[index] as number;
|
|
9
|
+
const b = bytes[index + 1];
|
|
10
|
+
const c = bytes[index + 2];
|
|
11
|
+
out += ALPHABET[a >> 2];
|
|
12
|
+
out += ALPHABET[((a & 3) << 4) | ((b ?? 0) >> 4)];
|
|
13
|
+
if (b !== undefined) out += ALPHABET[((b & 15) << 2) | ((c ?? 0) >> 6)];
|
|
14
|
+
if (c !== undefined) out += ALPHABET[c & 63];
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* How many bytes a text of base64url without padding stands for, from its length alone: every four characters are three
|
|
21
|
+
* bytes, and the two or three left over are one or two. For every text `fromBase64Url` takes, its array is this long, so
|
|
22
|
+
* a limit on the bytes can be held to a text before anything of it is decoded.
|
|
23
|
+
*/
|
|
24
|
+
export function decodedLength(text: string): number {
|
|
25
|
+
return Math.floor((text.length * 3) / 4);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The bytes of a text of base64url without padding. The text is checked whole before anything is decoded: a length an
|
|
30
|
+
* encoder makes, every character of the alphabet, and no bits set after the last byte (otherwise two different texts
|
|
31
|
+
* would decode to the same bytes). So a text that is refused leaves no array behind with any of its bytes, which the
|
|
32
|
+
* caller could not wipe, having never been given it; and nothing is refused once the array exists.
|
|
33
|
+
*/
|
|
34
|
+
export function fromBase64Url(text: string): Uint8Array {
|
|
35
|
+
if (text.length % 4 === 1) throw new Error("invalid base64url length");
|
|
36
|
+
let last = 0;
|
|
37
|
+
for (const char of text) {
|
|
38
|
+
const value = LOOKUP.get(char);
|
|
39
|
+
if (value === undefined) throw new Error("invalid base64url character");
|
|
40
|
+
last = value;
|
|
41
|
+
}
|
|
42
|
+
// The bits of the last character that are past the last byte: four after two characters of a group, two after three.
|
|
43
|
+
const spare = text.length % 4 === 2 ? 0b1111 : text.length % 4 === 3 ? 0b11 : 0;
|
|
44
|
+
if ((last & spare) !== 0) throw new Error("invalid base64url trailing bits");
|
|
45
|
+
const out = new Uint8Array(decodedLength(text));
|
|
46
|
+
let buffer = 0;
|
|
47
|
+
let bits = 0;
|
|
48
|
+
let offset = 0;
|
|
49
|
+
for (const char of text) {
|
|
50
|
+
buffer = ((buffer << 6) | (LOOKUP.get(char) as number)) & 0xffff;
|
|
51
|
+
bits += 6;
|
|
52
|
+
if (bits >= 8) {
|
|
53
|
+
bits -= 8;
|
|
54
|
+
out[offset++] = (buffer >> bits) & 0xff;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function toHex(bytes: Uint8Array): string {
|
|
61
|
+
let out = "";
|
|
62
|
+
for (const byte of bytes) out += byte.toString(16).padStart(2, "0");
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const utf8 = {
|
|
67
|
+
encode: (text: string): Uint8Array => new TextEncoder().encode(text),
|
|
68
|
+
decode: (bytes: Uint8Array): string => new TextDecoder("utf-8", { fatal: true }).decode(bytes),
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* How many bytes `utf8.encode(text)` would have, without making them: a limit in bytes can be checked on a typed
|
|
73
|
+
* value with no second copy of it left in memory. A pair of surrogates is one character of four bytes; a surrogate
|
|
74
|
+
* with no partner counts three, because an encoder writes U+FFFD in its place.
|
|
75
|
+
*/
|
|
76
|
+
export function utf8Length(text: string): number {
|
|
77
|
+
let bytes = 0;
|
|
78
|
+
for (let index = 0; index < text.length; index++) {
|
|
79
|
+
const unit = text.charCodeAt(index);
|
|
80
|
+
if (unit < 0x80) bytes += 1;
|
|
81
|
+
else if (unit < 0x800) bytes += 2;
|
|
82
|
+
else if (unit >= 0xd800 && unit <= 0xdbff && index + 1 < text.length && isLowSurrogate(text.charCodeAt(index + 1))) {
|
|
83
|
+
bytes += 4;
|
|
84
|
+
index++;
|
|
85
|
+
} else bytes += 3;
|
|
86
|
+
}
|
|
87
|
+
return bytes;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function isLowSurrogate(unit: number): boolean {
|
|
91
|
+
return unit >= 0xdc00 && unit <= 0xdfff;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** One getRandomValues call fills at most 65 536 bytes on every platform we run on. */
|
|
95
|
+
export function randomBytes(length: number): Uint8Array {
|
|
96
|
+
if (!Number.isInteger(length) || length < 0 || length > 65_536) {
|
|
97
|
+
throw new Error("randomBytes: length must be an integer from 0 to 65536");
|
|
98
|
+
}
|
|
99
|
+
return globalThis.crypto.getRandomValues(new Uint8Array(length));
|
|
100
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const UNSUPPORTED = "canonicalJson: unsupported value";
|
|
2
|
+
|
|
3
|
+
function isPlainObject(value: object): boolean {
|
|
4
|
+
const prototype = Object.getPrototypeOf(value);
|
|
5
|
+
return prototype === Object.prototype || prototype === null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Deterministic JSON for authenticated data and digests: keys sorted at every depth, no whitespace.
|
|
10
|
+
* Strict on purpose, so two different inputs never render to the same text: only null, booleans,
|
|
11
|
+
* strings, finite numbers, arrays and plain objects are accepted. Properties whose value is
|
|
12
|
+
* undefined are dropped; an undefined array element, or any other value, throws without echoing it.
|
|
13
|
+
*/
|
|
14
|
+
export function canonicalJson(value: unknown): string {
|
|
15
|
+
if (value === null || typeof value === "boolean" || typeof value === "string") {
|
|
16
|
+
return JSON.stringify(value);
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === "number") {
|
|
19
|
+
if (!Number.isFinite(value)) throw new TypeError(UNSUPPORTED);
|
|
20
|
+
return JSON.stringify(value);
|
|
21
|
+
}
|
|
22
|
+
// Array.from visits holes of a sparse array as undefined, so they throw like any undefined element.
|
|
23
|
+
if (Array.isArray(value)) return `[${Array.from(value, (item) => canonicalJson(item)).join(",")}]`;
|
|
24
|
+
if (typeof value === "object" && isPlainObject(value)) {
|
|
25
|
+
const record = value as Record<string, unknown>;
|
|
26
|
+
const keys = Object.keys(record)
|
|
27
|
+
.filter((key) => record[key] !== undefined)
|
|
28
|
+
.sort();
|
|
29
|
+
return `{${keys.map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`).join(",")}}`;
|
|
30
|
+
}
|
|
31
|
+
throw new TypeError(UNSUPPORTED);
|
|
32
|
+
}
|
package/shared/card.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { KEY_PATTERN, LIMITS, REQUEST_STATES, type InputRequest, type RequestState } from "./request";
|
|
3
|
+
import { REQUEST_ID_PATTERN } from "./request-id";
|
|
4
|
+
|
|
5
|
+
export const CARD_KIND = "secure-input-request";
|
|
6
|
+
export const CARD_VERSION = 2;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The timeline row, and the contract with the client that draws it. Agents can read timelines, so this carries names
|
|
10
|
+
* and state only, and the client checks what it receives against this schema: strict, so nothing else can ride along,
|
|
11
|
+
* and bounded, so a row cannot be made to grow. `z.number()` already refuses NaN and the infinities.
|
|
12
|
+
*/
|
|
13
|
+
export const CardDataSchema = z
|
|
14
|
+
.object({
|
|
15
|
+
requestId: z.string().regex(REQUEST_ID_PATTERN),
|
|
16
|
+
state: z.enum(REQUEST_STATES),
|
|
17
|
+
keys: z.array(z.string().regex(KEY_PATTERN)).max(LIMITS.fields),
|
|
18
|
+
tamper: z.boolean(),
|
|
19
|
+
updatedAt: z.number().min(0),
|
|
20
|
+
})
|
|
21
|
+
.strict();
|
|
22
|
+
|
|
23
|
+
export type CardData = z.output<typeof CardDataSchema>;
|
|
24
|
+
|
|
25
|
+
/** The row of a request: the keys it asked for, and once its values are delivered the keys that were written as files. */
|
|
26
|
+
export function cardData(record: {
|
|
27
|
+
id: string;
|
|
28
|
+
state: RequestState;
|
|
29
|
+
request: InputRequest;
|
|
30
|
+
written: string[] | null;
|
|
31
|
+
tamper: boolean;
|
|
32
|
+
updatedAt: number;
|
|
33
|
+
}): CardData {
|
|
34
|
+
return {
|
|
35
|
+
requestId: record.id,
|
|
36
|
+
state: record.state,
|
|
37
|
+
keys: record.written ?? record.request.fields.map((field) => field.key),
|
|
38
|
+
tamper: record.tamper,
|
|
39
|
+
updatedAt: record.updatedAt,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { defineRpc } from "@getpaseo/plugin";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { ERROR_CODES } from "./errors";
|
|
4
|
+
import { DETAIL_MAX, DIGEST_PATTERN, DIRECTORY_MAX, PUBLIC_KEY_PATTERN } from "./limits";
|
|
5
|
+
import { REQUEST_STATES } from "./request";
|
|
6
|
+
import { REQUEST_ID_PATTERN } from "./request-id";
|
|
7
|
+
import { AEAD_TAG_BYTES, MAX_PAYLOAD_BYTES } from "./seal";
|
|
8
|
+
import { RequestViewSchema } from "./view";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The most characters the `ct` of a submission can have: the unpadded base64url text of the largest payload
|
|
12
|
+
* (`MAX_PAYLOAD_BYTES`) and the tag (three bytes make four characters, a remainder of n bytes makes n + 1). A test
|
|
13
|
+
* builds the largest payload the per-field limits allow, seals it, and finds this number exactly.
|
|
14
|
+
*/
|
|
15
|
+
export const ENVELOPE_CT_MAX = Math.ceil(((MAX_PAYLOAD_BYTES + AEAD_TAG_BYTES) * 4) / 3);
|
|
16
|
+
|
|
17
|
+
/** An ephemeral X25519 key is 32 bytes, 43 characters; the ChaCha20-Poly1305 nonce is 12 bytes, 16 characters. */
|
|
18
|
+
export const EnvelopeSchema = z
|
|
19
|
+
.object({
|
|
20
|
+
v: z.literal(1),
|
|
21
|
+
epk: z.string().regex(PUBLIC_KEY_PATTERN),
|
|
22
|
+
nonce: z.string().regex(/^[A-Za-z0-9_-]{16}$/),
|
|
23
|
+
ct: z
|
|
24
|
+
.string()
|
|
25
|
+
.max(ENVELOPE_CT_MAX)
|
|
26
|
+
.regex(/^[A-Za-z0-9_-]+$/),
|
|
27
|
+
})
|
|
28
|
+
.strict();
|
|
29
|
+
|
|
30
|
+
const RequestIdInput = z.object({ requestId: z.string().regex(REQUEST_ID_PATTERN) }).strict();
|
|
31
|
+
|
|
32
|
+
const PATH_MAX = DIRECTORY_MAX + 64;
|
|
33
|
+
|
|
34
|
+
export const selfTestBegin = defineRpc({
|
|
35
|
+
name: "secure-input.selftest.begin",
|
|
36
|
+
input: z.object({}).strict(),
|
|
37
|
+
output: z.object({ probeId: z.string().regex(/^p_[0-9a-f]{16}$/), publicKey: z.string().regex(PUBLIC_KEY_PATTERN) }).strict(),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export const selfTestFinish = defineRpc({
|
|
41
|
+
name: "secure-input.selftest.finish",
|
|
42
|
+
input: z
|
|
43
|
+
.object({ probeId: z.string().regex(/^p_[0-9a-f]{16}$/), envelope: EnvelopeSchema, digest: z.string().regex(DIGEST_PATTERN) })
|
|
44
|
+
.strict(),
|
|
45
|
+
output: z.object({ ok: z.boolean(), bytes: z.number().int().min(0) }).strict(),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export const getRequest = defineRpc({ name: "secure-input.request.get", input: RequestIdInput, output: RequestViewSchema });
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Carries ciphertext only: the request it is for, and the sealed values. A refusal is data, not an exception, so no
|
|
52
|
+
* error text travels through the daemon but the `detail` an earlier module wrote for a person (key names, an errno
|
|
53
|
+
* name, a fixed phrase; never a value).
|
|
54
|
+
*/
|
|
55
|
+
export const fulfillRequest = defineRpc({
|
|
56
|
+
name: "secure-input.request.fulfill",
|
|
57
|
+
input: z.object({ requestId: z.string().regex(REQUEST_ID_PATTERN), envelope: EnvelopeSchema }).strict(),
|
|
58
|
+
output: z.union([
|
|
59
|
+
z.object({ ok: z.literal(true), state: z.enum(REQUEST_STATES) }).strict(),
|
|
60
|
+
z
|
|
61
|
+
.object({ ok: z.literal(false), code: z.enum(ERROR_CODES), retryable: z.boolean(), detail: z.string().max(DETAIL_MAX).optional() })
|
|
62
|
+
.strict(),
|
|
63
|
+
]),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
export const declineRequest = defineRpc({
|
|
67
|
+
name: "secure-input.request.decline",
|
|
68
|
+
input: RequestIdInput,
|
|
69
|
+
output: z.object({ state: z.enum(REQUEST_STATES) }).strict(),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const Persistence = z.enum(["ok", "failing"]);
|
|
73
|
+
const Count = z.number().int().min(0);
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Names, counts and codes. No value of a request. `registry.drop` is `failing` while the last sweep of `drop/` left
|
|
77
|
+
* something it could not remove, or a set could not be removed when it was released.
|
|
78
|
+
*/
|
|
79
|
+
export const DiagnosticsSchema = z
|
|
80
|
+
.object({
|
|
81
|
+
attached: z.boolean(),
|
|
82
|
+
dataDir: z.string().max(PATH_MAX),
|
|
83
|
+
registry: z
|
|
84
|
+
.object({ persistence: Persistence, drop: Persistence, stored: z.enum(["ok", "unreadable"]), dropped: Count })
|
|
85
|
+
.strict(),
|
|
86
|
+
outbox: z.object({ persistence: Persistence, unsaved: Count, dropped: Count }).strict(),
|
|
87
|
+
/**
|
|
88
|
+
* How many requests wait for the human (`pending`), how many sets of files are on the daemon (`delivered`), and when
|
|
89
|
+
* the nearest of those sets goes at the latest, in the plugin's clock (`nextDeleteBy`, null while none is there).
|
|
90
|
+
*/
|
|
91
|
+
requests: z.object({ pending: Count, delivered: Count, nextDeleteBy: z.number().int().min(0).nullable() }).strict(),
|
|
92
|
+
/** Why the plugin did not start, as a code and a fixed phrase; null when it did. */
|
|
93
|
+
startup: z.object({ code: z.enum([...ERROR_CODES, "unexpected"]), detail: z.string().max(300) }).strict().nullable(),
|
|
94
|
+
/**
|
|
95
|
+
* How many requests have a card that the timeline is not known to show yet. It is tried again once a minute, and
|
|
96
|
+
* once in five while the daemon has left three appends of it unanswered.
|
|
97
|
+
*/
|
|
98
|
+
cardsOwed: Count,
|
|
99
|
+
/**
|
|
100
|
+
* Whether messages to agents are held back: true from the moment the plugin starts until its start-up has read
|
|
101
|
+
* the requests, and for good when the start-up failed before that or never got there. What waits then stays in
|
|
102
|
+
* the outbox file for the next start.
|
|
103
|
+
*/
|
|
104
|
+
messagesHeld: z.boolean(),
|
|
105
|
+
/** `<hook>: <code>` of the last hook or event handler that failed. */
|
|
106
|
+
lastHookFailure: z.string().max(128).nullable(),
|
|
107
|
+
})
|
|
108
|
+
.strict();
|
|
109
|
+
|
|
110
|
+
/** What `readDiagnostics` answers. */
|
|
111
|
+
export type Diagnostics = z.output<typeof DiagnosticsSchema>;
|
|
112
|
+
|
|
113
|
+
export const readDiagnostics = defineRpc({
|
|
114
|
+
name: "secure-input.diagnostics.read",
|
|
115
|
+
input: z.object({}).strict(),
|
|
116
|
+
output: DiagnosticsSchema,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The human's word to delete every set of files at once (the settings screen). It answers with how many requests it
|
|
121
|
+
* released and how many entries of `drop/` it could not remove; no path, no name, no value. A refusal (the plugin is
|
|
122
|
+
* stopping, the drop cannot be swept) is an error of the call.
|
|
123
|
+
*/
|
|
124
|
+
export const clearDrop = defineRpc({
|
|
125
|
+
name: "secure-input.drop.clear",
|
|
126
|
+
input: z.object({}).strict(),
|
|
127
|
+
output: z.object({ released: Count, stuck: Count }).strict(),
|
|
128
|
+
});
|
package/shared/errors.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { sanitizeText } from "./sanitize";
|
|
2
|
+
|
|
3
|
+
/** Every code the plugin answers with. The codes of v1's destinations (a path, git, an existing file, a dotenv format) went with them. */
|
|
4
|
+
export const ERROR_CODES = [
|
|
5
|
+
"invalid_request",
|
|
6
|
+
"request_already_pending",
|
|
7
|
+
"unknown_agent",
|
|
8
|
+
"service_unavailable",
|
|
9
|
+
"write_failed",
|
|
10
|
+
"retry_limit",
|
|
11
|
+
"request_closed",
|
|
12
|
+
"decrypt_failed",
|
|
13
|
+
] as const;
|
|
14
|
+
|
|
15
|
+
export type ErrorCode = (typeof ERROR_CODES)[number];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The only error the plugin raises on purpose. `detail` may name a key or a path.
|
|
19
|
+
* It must never contain a typed value: the daemon writes handler error text into
|
|
20
|
+
* its activity log.
|
|
21
|
+
*/
|
|
22
|
+
export class SecureInputError extends Error {
|
|
23
|
+
constructor(
|
|
24
|
+
readonly code: ErrorCode,
|
|
25
|
+
detail?: string,
|
|
26
|
+
) {
|
|
27
|
+
super(detail ? `${code}: ${detail}` : code);
|
|
28
|
+
this.name = "SecureInputError";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function errorCodeOf(error: unknown, fallback: ErrorCode): ErrorCode {
|
|
33
|
+
return error instanceof SecureInputError ? error.code : fallback;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* What an error of the plugin's own says besides its code: the detail its author wrote for a person (key names, a
|
|
38
|
+
* path, a fixed phrase; never a value), cleaned of anything that hides or reorders text and cut to `max` characters.
|
|
39
|
+
* Undefined when the error has none, and for every error that is not the plugin's own: what a library, the system
|
|
40
|
+
* or a schema wrote in a message is never passed on, to an agent or to the dialog.
|
|
41
|
+
*/
|
|
42
|
+
export function errorDetailOf(error: unknown, max: number): string | undefined {
|
|
43
|
+
if (!(error instanceof SecureInputError)) return undefined;
|
|
44
|
+
const prefix = `${error.code}: `;
|
|
45
|
+
if (!error.message.startsWith(prefix)) return undefined;
|
|
46
|
+
const detail = sanitizeText(error.message.slice(prefix.length), max);
|
|
47
|
+
return detail === "" ? undefined : detail;
|
|
48
|
+
}
|
package/shared/limits.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// The patterns and the limits that more than one module goes by, and nothing else: no schema library, no import at all.
|
|
2
|
+
// The request schemas (`./request`) are built from these. What both halves of the plugin must agree on (a count, a path
|
|
3
|
+
// length, an id) is declared here once, and the daemon and the app import it.
|
|
4
|
+
|
|
5
|
+
export const KEY_PATTERN = /^[A-Za-z_][A-Za-z0-9_]{0,127}$/;
|
|
6
|
+
|
|
7
|
+
/** Names a plain object would treat specially. A key like these never reaches a file. */
|
|
8
|
+
export const RESERVED_KEYS: readonly string[] = ["__proto__", "constructor", "prototype"];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The limits on what a request may hold. Those on text (a reason, a label, a key) are in code points, which is what the
|
|
12
|
+
* schemas count and what a check written by hand counts with `codePointLength`; the two that guard what is written to
|
|
13
|
+
* a file, `valueBytes` (a text field) and `fileBytes` (a file field), are in bytes, as their names say: of UTF-8 for a
|
|
14
|
+
* text, the file's own bytes for a file.
|
|
15
|
+
*/
|
|
16
|
+
export const LIMITS = {
|
|
17
|
+
reason: 300,
|
|
18
|
+
label: 80,
|
|
19
|
+
fields: 32,
|
|
20
|
+
key: 128,
|
|
21
|
+
valueBytes: 64 * 1024,
|
|
22
|
+
fileBytes: 128 * 1024,
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
/** How long a delivered set of files may stay on the daemon at the most, from the moment it was written: 15 minutes. */
|
|
26
|
+
export const DROP_TTL_MS = 900_000;
|
|
27
|
+
|
|
28
|
+
/** The version of the plugin, as the settings screen says it. `tests/mirrored-constants.test.ts` holds it equal to the version in `package.json`. */
|
|
29
|
+
export const PLUGIN_VERSION = "0.2.1";
|
|
30
|
+
|
|
31
|
+
/** The release of Paseo the plugin needs, as the settings screen says it. The same test holds it to the requirement in `paseo-plugin.json`. */
|
|
32
|
+
export const PASEO_SINCE = "Paseo 0.14+";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The most bytes one Paseo RPC message may have on its way from either app to the daemon: the smallest limit on that
|
|
36
|
+
* path, as Paseo 0.14.0-beta.1 sets it (getpaseo/paseo at the tag `v0.14.0-beta.1`).
|
|
37
|
+
*
|
|
38
|
+
* - The desktop app talks to the daemon's WebSocket. Its limit is `MAX_WEBSOCKET_MESSAGE_BYTES`, 100 MiB
|
|
39
|
+
* (packages/protocol/src/websocket-message-limit.ts:2): the daemon's `maxPayload`
|
|
40
|
+
* (packages/server/src/server/websocket-server.ts:879), which the client checks before it sends a frame
|
|
41
|
+
* (packages/client/src/daemon-client.ts:1174).
|
|
42
|
+
* - The phone usually talks through the relay. Its limit is `MAX_RELAY_WEBSOCKET_MESSAGE_BYTES`, 32 MiB of encrypted
|
|
43
|
+
* frame (packages/protocol/src/websocket-message-limit.ts:5), which the client checks against the frame's size on
|
|
44
|
+
* the wire (packages/client/src/daemon-client-relay-e2ee-transport.ts:236). The plaintext that fits in it is
|
|
45
|
+
* `maxSecuredPlaintextByteLength(MAX_RELAY_WEBSOCKET_MESSAGE_BYTES)` (packages/relay/src/secure-channel.ts:716), the
|
|
46
|
+
* size the daemon itself gives a message that must cross the relay (packages/server/src/utils/checkout-git.ts:2187):
|
|
47
|
+
* the smaller of the legacy bound, `floor(32 MiB / 4) * 3 - 40` = 25,165,784
|
|
48
|
+
* (packages/relay/src/encrypted-channel.ts:127), and the protected-record bound, 24 bytes of each chunk of 65,513
|
|
49
|
+
* (packages/relay/src/protected-records.ts:32-42), which is 25,156,584.
|
|
50
|
+
* - A plugin RPC has no cap of its own on the way: the message takes any input (`PluginRpcInvokeRequestSchema`,
|
|
51
|
+
* packages/protocol/src/messages.ts:1587), the session hands it on (packages/server/src/server/session.ts:2596), and
|
|
52
|
+
* the runtime sends it to the plugin's process over Node's IPC (packages/server/src/server/plugins/runtime.ts:495 and
|
|
53
|
+
* :236-241).
|
|
54
|
+
*
|
|
55
|
+
* So the relay's plaintext is the limit. `tests/mirrored-constants.test.ts` holds this number to the installed packages.
|
|
56
|
+
*/
|
|
57
|
+
export const PASEO_RPC_MAX_BYTES = 25_156_584;
|
|
58
|
+
|
|
59
|
+
/** What the fulfil RPC adds around the ciphertext (the request id, the envelope's other members, the frame of the message), with margin. */
|
|
60
|
+
export const RPC_FRAME_MARGIN = 4096;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* How many submissions to one request may fail in a way that counts (the sealed values could not be opened) before
|
|
64
|
+
* the request is closed with `retry_limit`. Both halves say this number in words, the app to the human and the plugin
|
|
65
|
+
* to the agent, and both build the sentence from it.
|
|
66
|
+
*/
|
|
67
|
+
export const MAX_RETRIES = 3;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The most of a refusal's detail that is kept: the plugin's own words about a problem (a key, a path, a fixed phrase),
|
|
71
|
+
* never a library's. The daemon cuts a detail to this, and the contracts bound it by it.
|
|
72
|
+
*/
|
|
73
|
+
export const DETAIL_MAX = 1024;
|
|
74
|
+
|
|
75
|
+
/** A directory the plugin stores or shows: the working directory of a request. PATH_MAX on Linux. */
|
|
76
|
+
export const DIRECTORY_MAX = 4096;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The bound on an agent id in what the plugin keeps (a stored request, a queued message). A record or an item that was
|
|
80
|
+
* valid once must stay valid, and the daemon's own ids are far shorter.
|
|
81
|
+
*/
|
|
82
|
+
export const STORED_AGENT_ID_MAX = 256;
|
|
83
|
+
|
|
84
|
+
/** A SHA-256 as 64 lowercase hex characters: the digest of what a request asked for, or of a submission. */
|
|
85
|
+
export const DIGEST_PATTERN = /^[0-9a-f]{64}$/;
|
|
86
|
+
|
|
87
|
+
/** A public key (32 bytes of X25519: a request's key, a sealed envelope's ephemeral key) as 43 characters of unpadded base64url. */
|
|
88
|
+
export const PUBLIC_KEY_PATTERN = /^[A-Za-z0-9_-]{43}$/;
|
|
89
|
+
|
|
90
|
+
/** Whether `key` may name a value that is written to a file: the pattern, and not a name a plain object treats specially. */
|
|
91
|
+
export function isValidKey(key: unknown): key is string {
|
|
92
|
+
return typeof key === "string" && KEY_PATTERN.test(key) && !RESERVED_KEYS.includes(key);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* What a request's set of files is called on the daemon: `<data>/drop/si-secret-<requestId>/`. It is declared here because
|
|
97
|
+
* more than the store goes by it: the skill tells an agent never to read a path that has it.
|
|
98
|
+
*/
|
|
99
|
+
export const DROP_PREFIX = "si-secret-";
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The first two keys that name one file when letter case is ignored (an exact repeat counts), or null. A set has one file
|
|
103
|
+
* for each key, and on a volume that ignores case (the macOS default) `API_KEY` and `api_key` are one file, so the second
|
|
104
|
+
* would not be a second file. The keys are refused where a request is made, where a submission arrives and where the set
|
|
105
|
+
* is written, and so they are refused alike on every volume. The pair comes in the order the keys were given: the key that
|
|
106
|
+
* closes the first collision, and the earlier one it collides with. A map does the looking up, so that a key an object
|
|
107
|
+
* would treat specially (`constructor`) collides with nothing but itself.
|
|
108
|
+
*/
|
|
109
|
+
export function caseCollision(keys: readonly string[]): readonly [string, string] | null {
|
|
110
|
+
const seen = new Map<string, string>();
|
|
111
|
+
for (const key of keys) {
|
|
112
|
+
const folded = key.toLowerCase();
|
|
113
|
+
const earlier = seen.get(folded);
|
|
114
|
+
if (earlier !== undefined) return [earlier, key];
|
|
115
|
+
seen.set(folded, key);
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape of the id the registry gives a request: `r_` and 128 random bits in hex. It lives in `shared/` so the
|
|
3
|
+
* card contract and the outbox can check an id without importing the registry. `server/core/registry.ts` generates
|
|
4
|
+
* ids of this shape (`r_${randomBytes(16).toString("hex")}`).
|
|
5
|
+
*
|
|
6
|
+
* It is the pattern of the ids the registry gives, and so of what is stored and shown: the input of a tool or of an RPC
|
|
7
|
+
* that names a request, and the drop store that makes a directory of the id, hold an id to it.
|
|
8
|
+
*/
|
|
9
|
+
export const REQUEST_ID_PATTERN = /^r_[0-9a-f]{32}$/;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { KEY_PATTERN, LIMITS, RESERVED_KEYS, caseCollision } from "./limits";
|
|
3
|
+
|
|
4
|
+
// The patterns and the limits are declared in `./limits`, which has no schema library in it and imports nothing, so that
|
|
5
|
+
// a module that needs only a pattern or a number does not take the schemas along. They are part of this module as well.
|
|
6
|
+
export { KEY_PATTERN, LIMITS, RESERVED_KEYS };
|
|
7
|
+
|
|
8
|
+
// What the agent is told about each member. The daemon builds the input schema of `request_secret` from the shape
|
|
9
|
+
// below, descriptions included, and that schema is what the agent reads.
|
|
10
|
+
const DESCRIBED = {
|
|
11
|
+
reason: "Why you need the values, in one sentence. It is kept with the request and not shown in the form: say it in your own message too.",
|
|
12
|
+
fields:
|
|
13
|
+
"The keys you need; each becomes a file named after it. The human can add keys and files of their own. Keys that differ only in letter case are refused.",
|
|
14
|
+
key: "The name of the value and of its file: letters, digits and _, not starting with a digit.",
|
|
15
|
+
label: "A short hint about the value. It is not shown on screen; a screen reader reads it.",
|
|
16
|
+
optional: "The human may leave it empty; it then gets no file.",
|
|
17
|
+
kind: '"text" (default): one value on one line, such as a token or a password. "file": the content of a whole file, such as a JSON key, a PEM, a kubeconfig or a .p12; it may be binary.',
|
|
18
|
+
format: 'For a file only. "json" or "pem" is checked in the app before it is sent; "any" (default) is not.',
|
|
19
|
+
} as const;
|
|
20
|
+
|
|
21
|
+
/** One key the agent asks for. A field that names no kind is a text field. */
|
|
22
|
+
export const FieldSchema = z
|
|
23
|
+
.object({
|
|
24
|
+
key: z
|
|
25
|
+
.string()
|
|
26
|
+
.regex(KEY_PATTERN)
|
|
27
|
+
.refine((key) => !RESERVED_KEYS.includes(key), "this key name is reserved")
|
|
28
|
+
.describe(DESCRIBED.key),
|
|
29
|
+
label: z.string().max(LIMITS.label).optional().describe(DESCRIBED.label),
|
|
30
|
+
optional: z.boolean().optional().describe(DESCRIBED.optional),
|
|
31
|
+
kind: z.enum(["text", "file"]).optional().describe(DESCRIBED.kind),
|
|
32
|
+
format: z.enum(["json", "pem", "any"]).optional().describe(DESCRIBED.format),
|
|
33
|
+
})
|
|
34
|
+
.strict()
|
|
35
|
+
.refine((field) => field.format === undefined || field.kind === "file", "format is only for a file field");
|
|
36
|
+
|
|
37
|
+
/** The members of a request, as the agent writes them: the input of the `request_secret` tool. */
|
|
38
|
+
export const SecretRequestShape = z
|
|
39
|
+
.object({
|
|
40
|
+
reason: z.string().max(LIMITS.reason).default("").describe(DESCRIBED.reason),
|
|
41
|
+
fields: z.array(FieldSchema).max(LIMITS.fields).default([]).describe(DESCRIBED.fields),
|
|
42
|
+
})
|
|
43
|
+
.strict();
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A request as the registry takes it: the shape, and what no shape can say. Keys are unique, and they differ in more than
|
|
47
|
+
* letter case: each one becomes a file, and on a volume that ignores case two such keys would be one file. A pair that
|
|
48
|
+
* is an exact repeat is said once, as a repeat.
|
|
49
|
+
*/
|
|
50
|
+
export const InputRequestSchema = SecretRequestShape.superRefine((request, context) => {
|
|
51
|
+
const keys = request.fields.map((field) => field.key);
|
|
52
|
+
const issue = (message: string) => context.addIssue({ code: "custom", message });
|
|
53
|
+
const distinct = [...new Set(keys)];
|
|
54
|
+
if (distinct.length !== keys.length) issue("field keys must be unique");
|
|
55
|
+
if (caseCollision(distinct) !== null) issue("field keys must differ in more than letter case");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export type InputRequest = z.output<typeof InputRequestSchema>;
|
|
59
|
+
export type Field = z.output<typeof FieldSchema>;
|
|
60
|
+
|
|
61
|
+
export const REQUEST_STATES = ["pending", "delivered", "released", "rejected", "declined", "cancelled", "expired"] as const;
|
|
62
|
+
|
|
63
|
+
export type RequestState = (typeof REQUEST_STATES)[number];
|
|
64
|
+
|
|
65
|
+
/** States reached by a successful submission. A second, different submission to one of these is tampering. */
|
|
66
|
+
export const FULFILLED_STATES: ReadonlySet<RequestState> = new Set(["delivered", "released"]);
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// The one table of characters that can hide, reorder, restyle or impersonate text. What a human is shown as a fact
|
|
2
|
+
// (a path, a title) is refused when it holds one of them; what an agent says in its own words (a reason, a label) has
|
|
3
|
+
// each of them replaced by a space.
|
|
4
|
+
//
|
|
5
|
+
// What the table is: an explicit list of inclusive code point ranges, as numbers, and the text is walked code point
|
|
6
|
+
// by code point. It covers every code point that Unicode 17.0 gives the property Default_Ignorable_Code_Point or
|
|
7
|
+
// the general category Cf (format), Cc (control), Zl or Zp (line and paragraph separator), or Zs (space) other than
|
|
8
|
+
// U+0020, the ordinary space; where Unicode reserves a whole block for more of them, the block is listed whole.
|
|
9
|
+
// It also covers the general category Co, private use, whole (the area of the basic plane and the two supplementary
|
|
10
|
+
// ones): a private-use code point has no appearance of its own, and what a font makes of one is the font's affair.
|
|
11
|
+
// Eleven of them were measured to draw nothing at all with Apple's typesetter, in the very font the destination is
|
|
12
|
+
// shown in, so that a name with one in it could not be told from the name without it.
|
|
13
|
+
// Besides those it lists three things Unicode does not put in these classes: the blank braille pattern and the
|
|
14
|
+
// object replacement character, symbols that draw nothing (the second was measured with the same typesetter), and
|
|
15
|
+
// the surrogates, because a surrogate with no partner is text nobody wrote on purpose (a well-formed pair is read as
|
|
16
|
+
// the one code point it stands for, and never as these).
|
|
17
|
+
//
|
|
18
|
+
// Property escapes (`\p{…}`) are not used here: this module ships to the app, and Hermes is not known to have them.
|
|
19
|
+
// `tests/sanitize-table.test.ts` is the proof instead. It runs in Node, asks the engine for those properties for
|
|
20
|
+
// every code point there is, and holds each one the engine names to be refused by this table; so the table may know
|
|
21
|
+
// more than an engine does (a newer Unicode), and never less. When Unicode adds a character of these classes outside
|
|
22
|
+
// the ranges below, that test fails on an engine that knows it, and the range is added here.
|
|
23
|
+
//
|
|
24
|
+
// What it does not do: tell a letter from a look-alike of another script. A Cyrillic letter in place of a Latin one
|
|
25
|
+
// is an ordinary visible letter and passes (see SECURITY.md). And it is a list: it was found incomplete twice, by
|
|
26
|
+
// characters that draw nothing and are in no class that says so. So it is not the only thing between a human and a
|
|
27
|
+
// name that is not what it looks like: the keys the form shows are held to their pattern, which is ASCII alone.
|
|
28
|
+
const UNSAFE_RANGES: ReadonlyArray<readonly [number, number]> = [
|
|
29
|
+
[0x0000, 0x001f], // C0 controls, tab and the line breaks among them
|
|
30
|
+
[0x007f, 0x00a0], // DEL, the C1 controls and the no-break space
|
|
31
|
+
[0x00ad, 0x00ad], // soft hyphen
|
|
32
|
+
[0x034f, 0x034f], // combining grapheme joiner
|
|
33
|
+
[0x0600, 0x0605], // Arabic number signs (format characters that take the digits after them)
|
|
34
|
+
[0x061c, 0x061c], // Arabic letter mark
|
|
35
|
+
[0x06dd, 0x06dd], // Arabic end of ayah
|
|
36
|
+
[0x070f, 0x070f], // Syriac abbreviation mark
|
|
37
|
+
[0x0890, 0x0891], // Arabic pound and piastre marks above
|
|
38
|
+
[0x08e2, 0x08e2], // Arabic disputed end of ayah
|
|
39
|
+
[0x115f, 0x1160], // Hangul choseong and jungseong fillers
|
|
40
|
+
[0x1680, 0x1680], // Ogham space mark
|
|
41
|
+
[0x17b4, 0x17b5], // Khmer inherent vowels
|
|
42
|
+
[0x180b, 0x180f], // Mongolian free variation selectors and the Mongolian vowel separator
|
|
43
|
+
[0x2000, 0x200f], // en and em spaces, zero-width characters, directional marks
|
|
44
|
+
[0x2028, 0x202f], // line and paragraph separators, bidi embeddings and overrides, the narrow no-break space
|
|
45
|
+
[0x205f, 0x206f], // medium mathematical space, word joiner, invisible operators, bidi isolates, deprecated format characters
|
|
46
|
+
[0x2800, 0x2800], // blank braille pattern
|
|
47
|
+
[0x3000, 0x3000], // ideographic space
|
|
48
|
+
[0x3164, 0x3164], // Hangul filler
|
|
49
|
+
[0xd800, 0xdfff], // a surrogate with no partner
|
|
50
|
+
[0xe000, 0xf8ff], // the private-use area
|
|
51
|
+
[0xfe00, 0xfe0f], // variation selectors
|
|
52
|
+
[0xfeff, 0xfeff], // byte order mark
|
|
53
|
+
[0xffa0, 0xffa0], // halfwidth Hangul filler
|
|
54
|
+
[0xfff0, 0xfffc], // reserved specials, the interlinear annotation marks and the object replacement character
|
|
55
|
+
[0x110bd, 0x110bd], // Kaithi number sign
|
|
56
|
+
[0x110cd, 0x110cd], // Kaithi number sign above
|
|
57
|
+
[0x13430, 0x1343f], // Egyptian hieroglyph format controls
|
|
58
|
+
[0x1bca0, 0x1bca3], // shorthand format controls
|
|
59
|
+
[0x1d173, 0x1d17a], // musical formatting characters
|
|
60
|
+
[0xe0000, 0xe0fff], // tags, the variation selectors supplement and the reserved rest of that block
|
|
61
|
+
[0xf0000, 0xffffd], // supplementary private-use area A
|
|
62
|
+
[0x100000, 0x10fffd], // supplementary private-use area B
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
/** `codePointAt` gives a well-formed pair as one astral code point and a surrogate with no partner as itself. */
|
|
66
|
+
function isUnsafeCodePoint(codePoint: number): boolean {
|
|
67
|
+
// Printable ASCII is nearly all of what is ever asked about, and none of it is in the table.
|
|
68
|
+
if (codePoint >= 0x20 && codePoint < 0x7f) return false;
|
|
69
|
+
for (const [first, last] of UNSAFE_RANGES) {
|
|
70
|
+
if (codePoint >= first && codePoint <= last) return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** True when `text` holds any character from the unsafe set. */
|
|
76
|
+
export function hasUnsafeCharacters(text: string): boolean {
|
|
77
|
+
for (let index = 0; index < text.length; ) {
|
|
78
|
+
const codePoint = text.codePointAt(index) as number;
|
|
79
|
+
if (isUnsafeCodePoint(codePoint)) return true;
|
|
80
|
+
index += codePoint > 0xffff ? 2 : 1;
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Replaces every character from the unsafe set with a space. */
|
|
86
|
+
function replaceUnsafe(text: string): string {
|
|
87
|
+
let result = "";
|
|
88
|
+
for (let index = 0; index < text.length; ) {
|
|
89
|
+
const codePoint = text.codePointAt(index) as number;
|
|
90
|
+
const width = codePoint > 0xffff ? 2 : 1;
|
|
91
|
+
result += isUnsafeCodePoint(codePoint) ? " " : text.slice(index, index + width);
|
|
92
|
+
index += width;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Agent-supplied text is shown as plain text with nothing that can restyle the dialog. */
|
|
98
|
+
export function sanitizeText(input: string, maxLength: number): string {
|
|
99
|
+
const cleaned = replaceUnsafe(input).replace(/ {2,}/g, " ").trim();
|
|
100
|
+
return [...cleaned].slice(0, maxLength).join("");
|
|
101
|
+
}
|