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
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { ERROR_CODES } from "../../shared/errors";
|
|
4
|
+
import { DIGEST_PATTERN, DIRECTORY_MAX, LIMITS, MAX_RETRIES, PUBLIC_KEY_PATTERN, STORED_AGENT_ID_MAX, caseCollision, isValidKey } from "../../shared/limits";
|
|
5
|
+
import { FULFILLED_STATES, InputRequestSchema, REQUEST_STATES } from "../../shared/request";
|
|
6
|
+
import { REQUEST_ID_PATTERN } from "../../shared/request-id";
|
|
7
|
+
import { hasUnsafeCharacters } from "../../shared/sanitize";
|
|
8
|
+
import { shown } from "../../shared/shown";
|
|
9
|
+
import { writePrivateFile } from "../io/atomic";
|
|
10
|
+
import { readPrivateFile } from "../io/private-file";
|
|
11
|
+
|
|
12
|
+
// Declared in `shared/limits`, where the app can read it too: it says the number to the human, in words.
|
|
13
|
+
export { MAX_RETRIES };
|
|
14
|
+
export const HISTORY = 200;
|
|
15
|
+
/**
|
|
16
|
+
* The most of `requests.json` that is read or written. The file is writable by the agent, so a larger one was not
|
|
17
|
+
* written by this module; and this module does not write one: when the records would not fit, the oldest closed ones
|
|
18
|
+
* are left out until they do (`encodeRecords`). A typical record is 0.7 KB (measured), the largest the schema allows
|
|
19
|
+
* is 18 KB (32 file fields with 128 character keys and 80 character labels, all 32 keys written, a 4096 character
|
|
20
|
+
* directory, all ASCII; measured). 2 MiB is the room for the 400 records the reader looks at at 5 KB each, seven times
|
|
21
|
+
* a typical one: everything ordinary fits, and a run of the largest still leaves the 113 newest.
|
|
22
|
+
*/
|
|
23
|
+
export const MAX_REQUESTS_BYTES = 2 * 1024 * 1024;
|
|
24
|
+
/** An honest file holds HISTORY records and a few open ones: only the end of a longer list is looked at. */
|
|
25
|
+
const MAX_RESTORED = 2 * HISTORY;
|
|
26
|
+
|
|
27
|
+
/** The most of an agent's title that is kept. The registry cuts a title to this before it makes a record. */
|
|
28
|
+
export const TITLE_MAX = 200;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* How a request came to be `expired`: nobody answered within its time (`time`), or the plugin started again while it
|
|
32
|
+
* was open and the key to open it was gone (`restart`). The agent is told the one or the other (see `continuationText`).
|
|
33
|
+
*/
|
|
34
|
+
export const EXPIRED_BY = ["time", "restart"] as const;
|
|
35
|
+
export type ExpiredBy = (typeof EXPIRED_BY)[number];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* How the files of a delivered request came to be gone, and so the request `released`: the agent released them or took
|
|
39
|
+
* the set away itself (`agent`), the turn the delivery message started ended (`turn`), their time was up (`time`), the
|
|
40
|
+
* plugin started again (`restart`) or stopped (`stop`), or the human deleted every set (`human`). A delivery message
|
|
41
|
+
* that still waits says the files are gone for the last four, and is dropped for the first two (see `continuationText`).
|
|
42
|
+
*/
|
|
43
|
+
export const RELEASED_BY = ["agent", "turn", "time", "restart", "stop", "human"] as const;
|
|
44
|
+
export type ReleasedBy = (typeof RELEASED_BY)[number];
|
|
45
|
+
|
|
46
|
+
/** The keys a set was written with, in the order they were written: valid keys, of which no two name one file. */
|
|
47
|
+
const WrittenKeys = z
|
|
48
|
+
.array(z.string().refine(isValidKey, "not a valid key name"))
|
|
49
|
+
.min(1)
|
|
50
|
+
.max(LIMITS.fields)
|
|
51
|
+
.refine((keys) => caseCollision(keys) === null, "keys that differ only in letter case");
|
|
52
|
+
|
|
53
|
+
/** What `requests.json` holds for one request, and so what a request is: there is no field a typed value could go into. */
|
|
54
|
+
export const RecordSchema = z
|
|
55
|
+
.object({
|
|
56
|
+
id: z.string().regex(REQUEST_ID_PATTERN),
|
|
57
|
+
agentId: shown(STORED_AGENT_ID_MAX).min(1),
|
|
58
|
+
// The root of the path the human reads: text, like the path itself.
|
|
59
|
+
agentCwd: shown(DIRECTORY_MAX)
|
|
60
|
+
.min(1)
|
|
61
|
+
.refine((path) => isAbsolute(path), "not an absolute path"),
|
|
62
|
+
agentTitle: shown(TITLE_MAX).nullable(),
|
|
63
|
+
request: InputRequestSchema.refine(
|
|
64
|
+
(request) => !hasUnsafeCharacters(request.reason) && request.fields.every((field) => !hasUnsafeCharacters(field.label ?? "")),
|
|
65
|
+
"unsafe characters in text shown to the human",
|
|
66
|
+
),
|
|
67
|
+
state: z.enum(REQUEST_STATES),
|
|
68
|
+
publicKey: z.string().regex(PUBLIC_KEY_PATTERN),
|
|
69
|
+
/** The digest of what the request asked for, which the submission is sealed to (`requestDigestOf`). */
|
|
70
|
+
requestDigest: z.string().regex(DIGEST_PATTERN),
|
|
71
|
+
retries: z.number().int().min(0).max(MAX_RETRIES),
|
|
72
|
+
tamper: z.boolean(),
|
|
73
|
+
errorCode: z.enum(ERROR_CODES).nullable(),
|
|
74
|
+
/** The digest of the submission that was delivered, so that the same one sent again is answered as taken. */
|
|
75
|
+
fulfilledDigest: z.string().regex(DIGEST_PATTERN).nullable(),
|
|
76
|
+
/** The keys of the set that was written. */
|
|
77
|
+
written: WrittenKeys.nullable(),
|
|
78
|
+
/** When the set is deleted at the latest, in the registry's clock. */
|
|
79
|
+
deleteBy: z.number().int().min(0).nullable(),
|
|
80
|
+
/** Exactly with the state `released`. */
|
|
81
|
+
releasedBy: z.enum(RELEASED_BY).optional(),
|
|
82
|
+
/** Only with the state `expired`. A record expired by a version that did not keep it has none, and reads as `time`. */
|
|
83
|
+
expiredBy: z.enum(EXPIRED_BY).optional(),
|
|
84
|
+
createdAt: z.number().int().min(0),
|
|
85
|
+
updatedAt: z.number().int().min(0),
|
|
86
|
+
})
|
|
87
|
+
.strict()
|
|
88
|
+
.superRefine((record, context) => {
|
|
89
|
+
// A delivery leaves its digest, its keys and its deadline together, and nothing else has any of them.
|
|
90
|
+
const delivery = [record.fulfilledDigest, record.written, record.deleteBy];
|
|
91
|
+
const fits =
|
|
92
|
+
(record.expiredBy === undefined || record.state === "expired") &&
|
|
93
|
+
(record.releasedBy !== undefined) === (record.state === "released") &&
|
|
94
|
+
(FULFILLED_STATES.has(record.state) ? delivery.every((part) => part !== null) : delivery.every((part) => part === null));
|
|
95
|
+
if (!fits) context.addIssue({ code: "custom", message: "the state and what the record says of its end do not fit together" });
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
export type RequestRecord = z.output<typeof RecordSchema>;
|
|
99
|
+
|
|
100
|
+
/** Open: waiting for the human, or delivered and its files not yet gone. An open record never leaves the history. */
|
|
101
|
+
export const isOpen = (record: RequestRecord): boolean => record.state === "pending" || record.state === "delivered";
|
|
102
|
+
|
|
103
|
+
/** A record, or null when it is not one this module could have written. */
|
|
104
|
+
export function parseRecord(candidate: unknown): RequestRecord | null {
|
|
105
|
+
const parsed = RecordSchema.safeParse(candidate);
|
|
106
|
+
return parsed.success ? parsed.data : null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Whether the lists of an item are no longer than their schemas allow, looked at before the schema is: a list of
|
|
111
|
+
* hundreds of thousands of empty objects costs the schema half a second to refuse, an issue for each. Anything else is
|
|
112
|
+
* left to the schema.
|
|
113
|
+
*/
|
|
114
|
+
function withinLimits(item: unknown): boolean {
|
|
115
|
+
if (typeof item !== "object" || item === null) return true;
|
|
116
|
+
const { request, written } = item as { request?: unknown; written?: unknown };
|
|
117
|
+
const tooLong = (list: unknown) => Array.isArray(list) && list.length > LIMITS.fields;
|
|
118
|
+
if (typeof request === "object" && request !== null && tooLong((request as { fields?: unknown }).fields)) return false;
|
|
119
|
+
return !tooLong(written);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface StoredState {
|
|
123
|
+
/** The records that pass, oldest first. */
|
|
124
|
+
records: RequestRecord[];
|
|
125
|
+
/** `unreadable`: there is a file, and it could not be read or is no list. Only a missing file is an empty state. */
|
|
126
|
+
status: "ok" | "unreadable";
|
|
127
|
+
/** Entries of the file that were not loaded: invalid, repeating an id, or older than the end of the list that is looked at. */
|
|
128
|
+
dropped: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The records of `requests.json` that pass. It is a file the agent can write: read capped and without following a link,
|
|
133
|
+
* checked record by record, and nothing else of it is used. A file that cannot be read is survived (the state starts
|
|
134
|
+
* empty) but reported.
|
|
135
|
+
*/
|
|
136
|
+
export async function readStored(file: string, limit = MAX_REQUESTS_BYTES): Promise<StoredState> {
|
|
137
|
+
let parsed: unknown;
|
|
138
|
+
try {
|
|
139
|
+
const text = await readPrivateFile(file, limit);
|
|
140
|
+
if (text === null) return { records: [], status: "ok", dropped: 0 };
|
|
141
|
+
parsed = JSON.parse(text);
|
|
142
|
+
} catch {
|
|
143
|
+
return { records: [], status: "unreadable", dropped: 0 };
|
|
144
|
+
}
|
|
145
|
+
if (!Array.isArray(parsed)) return { records: [], status: "unreadable", dropped: 0 };
|
|
146
|
+
const window = parsed.slice(-MAX_RESTORED);
|
|
147
|
+
let dropped = parsed.length - window.length;
|
|
148
|
+
const records = new Map<string, RequestRecord>();
|
|
149
|
+
for (const item of window) {
|
|
150
|
+
const record = withinLimits(item) ? parseRecord(item) : null;
|
|
151
|
+
// The first of two with one id is the one that is kept; the other is not reported on either.
|
|
152
|
+
if (record === null || records.has(record.id)) dropped += 1;
|
|
153
|
+
else records.set(record.id, record);
|
|
154
|
+
}
|
|
155
|
+
return { records: [...records.values()].sort((a, b) => a.createdAt - b.createdAt), status: "ok", dropped };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The text to save, and the closed records that had to be left out for it to fit under the cap, oldest first (the one
|
|
160
|
+
* closed longest ago goes first). Open records are never left out: when they alone are too many, there is no text.
|
|
161
|
+
*/
|
|
162
|
+
export function encodeRecords(
|
|
163
|
+
records: readonly RequestRecord[],
|
|
164
|
+
limit = MAX_REQUESTS_BYTES,
|
|
165
|
+
): { text: string | null; dropped: RequestRecord[] } {
|
|
166
|
+
const texts = records.map((record) => JSON.stringify(record));
|
|
167
|
+
const sizes = texts.map((text) => Buffer.byteLength(text));
|
|
168
|
+
const left = new Set(records.keys());
|
|
169
|
+
// `[`, `]` and a comma between neighbours.
|
|
170
|
+
const total = (): number => 2 + [...left].reduce((sum, index) => sum + (sizes[index] as number), 0) + Math.max(0, left.size - 1);
|
|
171
|
+
const dropped: RequestRecord[] = [];
|
|
172
|
+
const closed = [...records.keys()].filter((index) => !isOpen(records[index] as RequestRecord));
|
|
173
|
+
closed.sort((a, b) => (records[a] as RequestRecord).updatedAt - (records[b] as RequestRecord).updatedAt);
|
|
174
|
+
for (const index of closed) {
|
|
175
|
+
if (total() <= limit) break;
|
|
176
|
+
left.delete(index);
|
|
177
|
+
dropped.push(records[index] as RequestRecord);
|
|
178
|
+
}
|
|
179
|
+
if (total() > limit) return { text: null, dropped };
|
|
180
|
+
return { text: `[${[...left].map((index) => texts[index]).join(",")}]`, dropped };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function saveStored(file: string, text: string): Promise<void> {
|
|
184
|
+
return writePrivateFile(file, text);
|
|
185
|
+
}
|