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
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { Icon, TextInput } from "@getpaseo/plugin/client/ui";
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { ActivityIndicator, Platform, Pressable, Text, View } from "react-native";
|
|
4
|
+
import { checkedFile, fileLine, fromPasted, takeFile, type FileValue } from "./file-entry";
|
|
5
|
+
import type { FormRow } from "./form-state";
|
|
6
|
+
import { A11Y, KEY_PLACEHOLDER, eye } from "./presentation";
|
|
7
|
+
import type { Tone } from "./tone";
|
|
8
|
+
import { canPickFiles, pickFile, useFileDrop } from "./web";
|
|
9
|
+
|
|
10
|
+
export const MONO = Platform.select({ ios: "Menlo", default: "monospace" });
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* What stops an input from suggesting, completing or remembering what is typed in it. Every one of these exists on the
|
|
14
|
+
* SDK's `TextInput` (React Native's own props).
|
|
15
|
+
*/
|
|
16
|
+
const PRIVATE_INPUT = {
|
|
17
|
+
autoCapitalize: "none",
|
|
18
|
+
autoCorrect: false,
|
|
19
|
+
autoComplete: "off",
|
|
20
|
+
spellCheck: false,
|
|
21
|
+
textContentType: "none",
|
|
22
|
+
importantForAutofill: "no",
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
/** The box of a row: red when the row has a line under it, dashed while a file row waits for its file. */
|
|
26
|
+
function boxStyle(colors: Tone, error: boolean, dashed = false) {
|
|
27
|
+
return {
|
|
28
|
+
flexDirection: "row",
|
|
29
|
+
alignItems: "center",
|
|
30
|
+
gap: 8,
|
|
31
|
+
minHeight: 42,
|
|
32
|
+
paddingHorizontal: 10,
|
|
33
|
+
borderWidth: 1,
|
|
34
|
+
borderRadius: 9,
|
|
35
|
+
borderStyle: dashed ? "dashed" : "solid",
|
|
36
|
+
borderColor: error ? colors.danger : colors.border,
|
|
37
|
+
backgroundColor: colors.surface,
|
|
38
|
+
} as const;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface IconButtonProps {
|
|
42
|
+
icon: string;
|
|
43
|
+
label: string;
|
|
44
|
+
/** What a screen reader says after the label: the agent's label of the field the button is for. None when empty. */
|
|
45
|
+
hint?: string;
|
|
46
|
+
color: string;
|
|
47
|
+
onPress(): void;
|
|
48
|
+
disabled?: boolean;
|
|
49
|
+
/** A background for a button that stands by itself, as «+» and «→» do. */
|
|
50
|
+
fill?: string;
|
|
51
|
+
/** While what it started is under way: it shows an indicator in place of its icon, and takes no press. */
|
|
52
|
+
busy?: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A control with an icon and no words: what it does is its accessibility label, in Ukrainian, and the field it is for its hint. */
|
|
56
|
+
export function IconButton({ icon, label, hint, color, onPress, disabled = false, fill, busy = false }: IconButtonProps) {
|
|
57
|
+
return (
|
|
58
|
+
<Pressable
|
|
59
|
+
accessibilityRole="button"
|
|
60
|
+
accessibilityLabel={label}
|
|
61
|
+
accessibilityHint={hint === undefined || hint === "" ? undefined : hint}
|
|
62
|
+
accessibilityState={{ disabled: disabled || busy, busy }}
|
|
63
|
+
disabled={disabled || busy}
|
|
64
|
+
onPress={onPress}
|
|
65
|
+
hitSlop={6}
|
|
66
|
+
style={{ width: 34, height: 34, borderRadius: 9, alignItems: "center", justifyContent: "center", backgroundColor: fill ?? "transparent", opacity: disabled && !busy ? 0.4 : 1 }}
|
|
67
|
+
>
|
|
68
|
+
{busy ? <ActivityIndicator size="small" color={color} /> : <Icon name={icon} size={16} color={color} />}
|
|
69
|
+
</Pressable>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Props that have a screen reader say a line the moment it appears, as a problem: the line under a row, and the one at
|
|
75
|
+
* the top of the form. The role `alert` is a live region in the web build (the desktop app), and Android reads a polite
|
|
76
|
+
* live region as it changes. iOS has no live region: VoiceOver reads the line when it comes to it.
|
|
77
|
+
*/
|
|
78
|
+
export const ANNOUNCED = { accessibilityRole: "alert", accessibilityLiveRegion: "polite" } as const;
|
|
79
|
+
|
|
80
|
+
/** A line under a row, in the colour of a problem, said to a screen reader as it appears. */
|
|
81
|
+
export function RowLine({ text, colors }: { text: string; colors: Tone }) {
|
|
82
|
+
return (
|
|
83
|
+
<Text {...ANNOUNCED} style={{ color: colors.danger, fontSize: 12, marginLeft: 4 }}>
|
|
84
|
+
{text}
|
|
85
|
+
</Text>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The key of a field the agent asked for, inside the box on the left: dimmer when the field may stay empty. */
|
|
90
|
+
function KeyText({ row, colors }: { row: FormRow; colors: Tone }) {
|
|
91
|
+
return (
|
|
92
|
+
<Text numberOfLines={1} style={{ color: colors.muted, opacity: row.optional ? 0.55 : 1, fontFamily: MONO, fontSize: 12, flexShrink: 1, maxWidth: "45%" }}>
|
|
93
|
+
{row.key}
|
|
94
|
+
</Text>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The name of a key the human adds. A name is not a secret, so it is shown as typed; a screen reader is told what it is. */
|
|
99
|
+
function NameInput({ row, colors, locked, onName }: { row: FormRow; colors: Tone; locked: boolean; onName(key: string): void }) {
|
|
100
|
+
return (
|
|
101
|
+
<TextInput
|
|
102
|
+
value={row.key}
|
|
103
|
+
onChangeText={onName}
|
|
104
|
+
accessibilityLabel={A11Y.name}
|
|
105
|
+
placeholder={KEY_PLACEHOLDER}
|
|
106
|
+
placeholderTextColor={colors.muted}
|
|
107
|
+
editable={!locked}
|
|
108
|
+
{...PRIVATE_INPUT}
|
|
109
|
+
autoCapitalize="characters"
|
|
110
|
+
style={{ width: 120, paddingVertical: 8, paddingRight: 8, borderRightWidth: 1, borderRightColor: colors.border, color: colors.text, fontFamily: MONO, fontSize: 12 }}
|
|
111
|
+
/>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface RowProps {
|
|
116
|
+
row: FormRow;
|
|
117
|
+
/** The agent's label, which a screen reader reads as the hint of the row's controls. It is shown nowhere. */
|
|
118
|
+
hint: string;
|
|
119
|
+
/** The line under the row, from `checkForm`, or null. */
|
|
120
|
+
error: string | null;
|
|
121
|
+
colors: Tone;
|
|
122
|
+
/** While the form is being sent: the row is faint and takes nothing. */
|
|
123
|
+
locked: boolean;
|
|
124
|
+
onName(key: string): void;
|
|
125
|
+
onRemove(): void;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
interface TextRowProps extends RowProps {
|
|
129
|
+
onText(text: string): void;
|
|
130
|
+
onToFile(): void;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A row for a line of text: the key (or, in a row of the human's, the name they type), the value hidden on one line,
|
|
135
|
+
* and the eye that shows it on the same line. A row of the human's has a paperclip that makes it a file row, and «✕».
|
|
136
|
+
* A value of several lines goes through a file row, where its line breaks are kept. While the form is locked no control
|
|
137
|
+
* of the row takes a press, the eye included.
|
|
138
|
+
*/
|
|
139
|
+
export function TextRow({ row, hint, error, colors, locked, onText, onName, onToFile, onRemove }: TextRowProps) {
|
|
140
|
+
const [shown, setShown] = useState(false);
|
|
141
|
+
const look = eye(shown);
|
|
142
|
+
const text = row.value.kind === "text" ? row.value.text : "";
|
|
143
|
+
return (
|
|
144
|
+
<View style={{ gap: 4, opacity: locked ? 0.5 : 1 }}>
|
|
145
|
+
<View style={boxStyle(colors, error !== null)}>
|
|
146
|
+
{row.declared ? <KeyText row={row} colors={colors} /> : <NameInput row={row} colors={colors} locked={locked} onName={onName} />}
|
|
147
|
+
<TextInput
|
|
148
|
+
value={text}
|
|
149
|
+
onChangeText={onText}
|
|
150
|
+
secureTextEntry={!shown}
|
|
151
|
+
editable={!locked}
|
|
152
|
+
accessibilityLabel={row.key.trim() === "" ? undefined : row.key.trim()}
|
|
153
|
+
accessibilityHint={hint === "" ? undefined : hint}
|
|
154
|
+
{...PRIVATE_INPUT}
|
|
155
|
+
style={{ flex: 1, paddingVertical: 8, color: colors.text, fontFamily: MONO, fontSize: 13 }}
|
|
156
|
+
/>
|
|
157
|
+
<IconButton icon={look.icon} label={look.label} color={colors.muted} disabled={locked} onPress={() => setShown((current) => !current)} />
|
|
158
|
+
{row.declared ? null : (
|
|
159
|
+
<>
|
|
160
|
+
<IconButton icon="Paperclip" label={A11Y.pick} color={colors.muted} disabled={locked} onPress={onToFile} />
|
|
161
|
+
<IconButton icon="X" label={A11Y.remove} color={colors.muted} disabled={locked} onPress={onRemove} />
|
|
162
|
+
</>
|
|
163
|
+
)}
|
|
164
|
+
</View>
|
|
165
|
+
{error === null ? null : <RowLine text={error} colors={colors} />}
|
|
166
|
+
</View>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
interface FileRowProps extends RowProps {
|
|
171
|
+
/** A file picked, dropped or pasted. The form takes it only into an empty row, and wipes any it does not take. */
|
|
172
|
+
onFile(file: FileValue): void;
|
|
173
|
+
onClearFile(): void;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* A row for a whole file. Empty, it has the paperclip (where the app can pick files), the clipboard, which opens an area
|
|
178
|
+
* under the row to paste into, and it takes a file dropped on it. Filled, it shows the file's name, size and check, and
|
|
179
|
+
* «✕» to take it out. Its content is never shown; the paste area shows what is pasted while it is open, and when it
|
|
180
|
+
* loses focus its text becomes the file and the area closes. The agent's label is the hint of each of its controls.
|
|
181
|
+
*/
|
|
182
|
+
export function FileRow({ row, hint, error, colors, locked, onName, onFile, onClearFile, onRemove }: FileRowProps) {
|
|
183
|
+
const box = useRef<View>(null);
|
|
184
|
+
const alive = useRef(true);
|
|
185
|
+
useEffect(() => {
|
|
186
|
+
alive.current = true;
|
|
187
|
+
return () => {
|
|
188
|
+
alive.current = false;
|
|
189
|
+
};
|
|
190
|
+
}, []);
|
|
191
|
+
const [pasting, setPasting] = useState(false);
|
|
192
|
+
const [pasted, setPasted] = useState("");
|
|
193
|
+
const file = row.value.kind === "file" ? row.value.file : null;
|
|
194
|
+
const line = useMemo(() => (file === null ? null : fileLine(file, row.format, checkedFile(file, row.format))), [file, row.format]);
|
|
195
|
+
|
|
196
|
+
// A file is handed on only while the row is unlocked and mounted, and wiped otherwise (`takeFile`).
|
|
197
|
+
const take = (value: FileValue) =>
|
|
198
|
+
takeFile(value, !locked && alive.current, (taken) => {
|
|
199
|
+
// Whatever filled the row, the paste area closes, and what was pasted into it is let go.
|
|
200
|
+
setPasting(false);
|
|
201
|
+
setPasted("");
|
|
202
|
+
onFile(taken);
|
|
203
|
+
});
|
|
204
|
+
useFileDrop(box, take);
|
|
205
|
+
const pick = () => {
|
|
206
|
+
void pickFile().then((value) => {
|
|
207
|
+
if (value !== null) take(value);
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
const endPaste = () => {
|
|
211
|
+
const value = fromPasted(pasted);
|
|
212
|
+
setPasted("");
|
|
213
|
+
setPasting(false);
|
|
214
|
+
if (value !== null) take(value);
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
return (
|
|
218
|
+
<View style={{ gap: 4, opacity: locked ? 0.5 : 1 }}>
|
|
219
|
+
<View ref={box} style={boxStyle(colors, error !== null, file === null)}>
|
|
220
|
+
{row.declared ? <KeyText row={row} colors={colors} /> : <NameInput row={row} colors={colors} locked={locked} onName={onName} />}
|
|
221
|
+
{row.format === "any" ? null : <Text style={{ color: colors.muted, opacity: 0.7, fontFamily: MONO, fontSize: 11 }}>{row.format}</Text>}
|
|
222
|
+
{line === null ? (
|
|
223
|
+
<>
|
|
224
|
+
<View style={{ flex: 1 }} />
|
|
225
|
+
{canPickFiles ? <IconButton icon="Paperclip" label={A11Y.pick} hint={hint} color={colors.muted} disabled={locked} onPress={pick} /> : null}
|
|
226
|
+
<IconButton icon="ClipboardPaste" label={A11Y.paste} hint={hint} color={colors.muted} disabled={locked} onPress={() => setPasting(true)} />
|
|
227
|
+
</>
|
|
228
|
+
) : (
|
|
229
|
+
<>
|
|
230
|
+
<Text numberOfLines={1} style={{ flex: 1, color: colors.text, fontSize: 12 }}>
|
|
231
|
+
{line}
|
|
232
|
+
</Text>
|
|
233
|
+
<IconButton icon="X" label={A11Y.removeFile} hint={hint} color={colors.muted} disabled={locked} onPress={onClearFile} />
|
|
234
|
+
</>
|
|
235
|
+
)}
|
|
236
|
+
{row.declared ? null : <IconButton icon="X" label={A11Y.remove} color={colors.muted} disabled={locked} onPress={onRemove} />}
|
|
237
|
+
</View>
|
|
238
|
+
{pasting ? (
|
|
239
|
+
<TextInput
|
|
240
|
+
value={pasted}
|
|
241
|
+
onChangeText={setPasted}
|
|
242
|
+
onBlur={endPaste}
|
|
243
|
+
accessibilityLabel={A11Y.pasteArea}
|
|
244
|
+
accessibilityHint={hint === "" ? undefined : hint}
|
|
245
|
+
multiline
|
|
246
|
+
autoFocus
|
|
247
|
+
editable={!locked}
|
|
248
|
+
{...PRIVATE_INPUT}
|
|
249
|
+
style={{ minHeight: 96, padding: 8, borderWidth: 1, borderRadius: 9, borderColor: colors.accent, color: colors.text, fontFamily: MONO, fontSize: 12, textAlignVertical: "top" }}
|
|
250
|
+
/>
|
|
251
|
+
) : null}
|
|
252
|
+
{error === null ? null : <RowLine text={error} colors={colors} />}
|
|
253
|
+
</View>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { toBase64Url, utf8 } from "../shared/bytes";
|
|
2
|
+
import { LIMITS } from "../shared/limits";
|
|
3
|
+
import { sanitizeText } from "../shared/sanitize";
|
|
4
|
+
import { bytesText } from "./text";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A file the human picked, dropped or pasted: its bytes, as they are, and its name. The name is null for a paste. The
|
|
8
|
+
* bytes are the form's to wipe, when the file leaves its row and when the form closes (`wipeForm`), and they are wiped
|
|
9
|
+
* through `wipeFile` alone. Nothing changes a file but its wipe: what is made of it once (`checkedFile`, `encodedFile`)
|
|
10
|
+
* holds until then.
|
|
11
|
+
*/
|
|
12
|
+
export interface FileValue {
|
|
13
|
+
name: string | null;
|
|
14
|
+
bytes: Uint8Array;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type Format = "json" | "pem" | "any";
|
|
18
|
+
|
|
19
|
+
/** What the app found of a file, before anything is sealed: it passes, or the one line under its row says why not. */
|
|
20
|
+
export type FileCheck = { ok: true } | { ok: false; message: "Це не JSON" | "Це не PEM" | "Не довше 128 КіБ" };
|
|
21
|
+
|
|
22
|
+
const PASSES: FileCheck = { ok: true };
|
|
23
|
+
/** `LIMITS.fileBytes`, 128 KiB, as the form words it: a literal, so that the type says which line it is. */
|
|
24
|
+
const TOO_LARGE: FileCheck = { ok: false, message: "Не довше 128 КіБ" };
|
|
25
|
+
const NOT_JSON: FileCheck = { ok: false, message: "Це не JSON" };
|
|
26
|
+
const NOT_PEM: FileCheck = { ok: false, message: "Це не PEM" };
|
|
27
|
+
|
|
28
|
+
/** A line of a PEM that opens a block, and one that closes it, ending in CR LF or in LF. */
|
|
29
|
+
const PEM_BEGIN = /^-----BEGIN [^\r\n]+-----\r?$/m;
|
|
30
|
+
const PEM_END = /^-----END [^\r\n]+-----\r?$/m;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The bytes as text when they are UTF-8, and null when they are not: no character is replaced, and a byte order mark at
|
|
34
|
+
* the start stays the first character. The daemon writes the bytes as they are, BOM and all, and a reader of that file
|
|
35
|
+
* meets the BOM first: a check that dropped it would pass a JSON or a PEM the reader refuses.
|
|
36
|
+
*/
|
|
37
|
+
function textOf(bytes: Uint8Array): string | null {
|
|
38
|
+
try {
|
|
39
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
40
|
+
} catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether a file is what its field asks for. The size comes first, for every format: a file over `LIMITS.fileBytes`
|
|
47
|
+
* is refused however it reads. `json` is UTF-8 that parses as JSON; `pem` is UTF-8 with a line that begins a block and
|
|
48
|
+
* one that ends one; `any` is not looked into, so a `.p12` or anything else binary passes. The bytes are only read.
|
|
49
|
+
*/
|
|
50
|
+
export function checkFile(file: FileValue, format: Format): FileCheck {
|
|
51
|
+
if (file.bytes.length > LIMITS.fileBytes) return TOO_LARGE;
|
|
52
|
+
if (format === "any") return PASSES;
|
|
53
|
+
const text = textOf(file.bytes);
|
|
54
|
+
if (format === "pem") return text !== null && PEM_BEGIN.test(text) && PEM_END.test(text) ? PASSES : NOT_PEM;
|
|
55
|
+
if (text === null) return NOT_JSON;
|
|
56
|
+
try {
|
|
57
|
+
JSON.parse(text);
|
|
58
|
+
return PASSES;
|
|
59
|
+
} catch {
|
|
60
|
+
return NOT_JSON;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The most of a file's name that is shown: the longest name a file system gives one. */
|
|
65
|
+
const NAME_MAX = 255;
|
|
66
|
+
|
|
67
|
+
/** «2,1 КіБ»: the size of a file, in bytes below a KiB and in KiB with one decimal above. */
|
|
68
|
+
export function sizeText(bytes: number): string {
|
|
69
|
+
if (bytes < 1024) return bytesText(bytes);
|
|
70
|
+
return `${(bytes / 1024).toFixed(1).replace(".", ",")} КіБ`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The line that stands for a file in its row: its name, or «вставлено» for a paste, its size, and for a format that
|
|
75
|
+
* is checked, the check. Never a byte of the file. The name is the human's file system's, and it is shown without what
|
|
76
|
+
* could hide or reorder it, which a name made to deceive could hold.
|
|
77
|
+
*/
|
|
78
|
+
export function fileLine(file: FileValue, format: "json" | "pem" | "any", check: FileCheck): string {
|
|
79
|
+
const name = file.name === null ? "вставлено" : sanitizeText(file.name, NAME_MAX);
|
|
80
|
+
const parts = [name, sizeText(file.bytes.length)];
|
|
81
|
+
if (format !== "any") parts.push(`${format} ${check.ok ? "✓" : "✗"}`);
|
|
82
|
+
return parts.filter((part) => part !== "").join(" · ");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** What was pasted into a file row's area, as the file's bytes: nothing pasted is no file, and the row stays empty. */
|
|
86
|
+
export function fromPasted(text: string): FileValue | null {
|
|
87
|
+
return text === "" ? null : { name: null, bytes: utf8.encode(text) };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* What is made of each file the form holds, made once: its check against each format it is held to, and its bytes as
|
|
92
|
+
* base64url, which is how it is sent. Every change of the form checks every row again, so without this every keystroke
|
|
93
|
+
* would read and parse every file anew and make another copy of its content as text. Keyed by the file itself, so that
|
|
94
|
+
* it goes with the file; `wipeFile` lets go of it at once. The text cannot be wiped, as no string can: it is held no
|
|
95
|
+
* longer than the file it was made from, which is no longer than the form.
|
|
96
|
+
*/
|
|
97
|
+
const made = new WeakMap<FileValue, { checks: Map<Format, FileCheck>; encoded: string | null }>();
|
|
98
|
+
|
|
99
|
+
function madeOf(file: FileValue): { checks: Map<Format, FileCheck>; encoded: string | null } {
|
|
100
|
+
let entry = made.get(file);
|
|
101
|
+
if (entry === undefined) made.set(file, (entry = { checks: new Map(), encoded: null }));
|
|
102
|
+
return entry;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** `checkFile`, made once for each file and format, and kept until the file is wiped. */
|
|
106
|
+
export function checkedFile(file: FileValue, format: Format): FileCheck {
|
|
107
|
+
const { checks } = madeOf(file);
|
|
108
|
+
let check = checks.get(format);
|
|
109
|
+
if (check === undefined) checks.set(format, (check = checkFile(file, format)));
|
|
110
|
+
return check;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The bytes of a file as base64url, as a file goes in a submission: made once, and kept until the file is wiped. */
|
|
114
|
+
export function encodedFile(file: FileValue): string {
|
|
115
|
+
const entry = madeOf(file);
|
|
116
|
+
entry.encoded ??= toBase64Url(file.bytes);
|
|
117
|
+
return entry.encoded;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Wipes a file's bytes, and lets go of what was made of them (`checkedFile`, `encodedFile`): nothing of the file is held after. */
|
|
121
|
+
export function wipeFile(file: FileValue): void {
|
|
122
|
+
file.bytes.fill(0);
|
|
123
|
+
made.delete(file);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A file the human gave a row, handed to `take` while `open` says the form may take one, and wiped otherwise. What
|
|
128
|
+
* `take` throws on is wiped too, and goes no further: a file that nothing takes leaves nothing behind. The form is open
|
|
129
|
+
* to a file while it is on screen and nothing is under way (the dialog), and a row while it is not locked and still
|
|
130
|
+
* mounted (a file row).
|
|
131
|
+
*/
|
|
132
|
+
export function takeFile(file: FileValue, open: boolean, take: (file: FileValue) => void): void {
|
|
133
|
+
if (!open) {
|
|
134
|
+
wipeFile(file);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
take(file);
|
|
139
|
+
} catch {
|
|
140
|
+
wipeFile(file);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { utf8Length } from "../shared/bytes";
|
|
2
|
+
import { KEY_PATTERN, LIMITS, RESERVED_KEYS } from "../shared/limits";
|
|
3
|
+
import { MAX_PAYLOAD_BYTES, payloadSize, type PayloadEntry } from "../shared/seal";
|
|
4
|
+
import { hasLoneSurrogate } from "../shared/text";
|
|
5
|
+
import type { RequestView } from "../shared/view";
|
|
6
|
+
import { checkedFile, encodedFile, wipeFile, type FileValue } from "./file-entry";
|
|
7
|
+
import { fieldId } from "./presentation";
|
|
8
|
+
|
|
9
|
+
/** What a row holds: a line of text, or a file, which is null until one is picked, dropped or pasted. */
|
|
10
|
+
export type RowValue = { kind: "text"; text: string } | { kind: "file"; file: FileValue | null };
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* One row of the form. A declared row is a field the agent asked for: its key is fixed and it cannot be removed. A row
|
|
14
|
+
* of the human's has the name they type in place of a key. `format` is what a file in the row is checked against; a
|
|
15
|
+
* row of the human's checks none (`any`).
|
|
16
|
+
*/
|
|
17
|
+
export interface FormRow {
|
|
18
|
+
id: string;
|
|
19
|
+
key: string;
|
|
20
|
+
declared: boolean;
|
|
21
|
+
optional: boolean;
|
|
22
|
+
format: "json" | "pem" | "any";
|
|
23
|
+
value: RowValue;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The declared rows first, in the view's order, with ids `fieldId.declared(key)`; then the human's, `fieldId.row(n)`. */
|
|
27
|
+
export interface FormState {
|
|
28
|
+
rows: FormRow[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The form for a view: a row for each field the agent asked for, empty. */
|
|
32
|
+
export function formFromView(view: RequestView): FormState {
|
|
33
|
+
return {
|
|
34
|
+
rows: view.fields.map((field) => ({
|
|
35
|
+
id: fieldId.declared(field.key),
|
|
36
|
+
key: field.key,
|
|
37
|
+
declared: true,
|
|
38
|
+
optional: field.optional,
|
|
39
|
+
format: field.format ?? "any",
|
|
40
|
+
value: field.kind === "file" ? { kind: "file", file: null } : { kind: "text", text: "" },
|
|
41
|
+
})),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// The lines under a row, as the form words them. A limit a line names is the plugin's own number (`LIMITS`, `MAX_PAYLOAD_BYTES`).
|
|
46
|
+
const INVALID_NAME = "Лише A–Z, 0–9, _";
|
|
47
|
+
const RESERVED_NAME = "Ім'я зарезервоване";
|
|
48
|
+
const TEXT_TOO_LARGE = `Не довше ${LIMITS.valueBytes / 1024} КіБ`;
|
|
49
|
+
const BROKEN_TEXT = "Некоректний символ";
|
|
50
|
+
const TOTAL_TOO_LARGE = `Разом не довше ${MAX_PAYLOAD_BYTES / 1024} КіБ`;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The key a row's name gives, and the line about it. A declared row's key is the agent's, which the plugin checked. A
|
|
54
|
+
* name of the human's is trimmed, and gives no key while it is empty (and no line), is of another shape than a key, is
|
|
55
|
+
* a name a plain object treats specially, or names a file an earlier row names already, exactly or by letter case: two
|
|
56
|
+
* such keys would be one file on a volume that ignores case. `taken` maps each earlier key, folded, to itself.
|
|
57
|
+
*/
|
|
58
|
+
function nameOf(row: FormRow, taken: ReadonlyMap<string, string>): { key: string | null; error: string | null } {
|
|
59
|
+
if (row.declared) return { key: row.key, error: null };
|
|
60
|
+
const key = row.key.trim();
|
|
61
|
+
if (key === "") return { key: null, error: null };
|
|
62
|
+
if (!KEY_PATTERN.test(key)) return { key: null, error: INVALID_NAME };
|
|
63
|
+
if (RESERVED_KEYS.includes(key)) return { key: null, error: RESERVED_NAME };
|
|
64
|
+
const earlier = taken.get(key.toLowerCase());
|
|
65
|
+
return earlier === undefined ? { key, error: null } : { key: null, error: `Вже є ${earlier}` };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* What a row's value comes to: a line, nothing to send (an empty text, no file, a file of no bytes), or an entry, made
|
|
70
|
+
* only when the form is sent. A text is measured without a copy of it being made, and must be whole UTF-8: a surrogate
|
|
71
|
+
* with no partner would be written as another character. A file is checked against its row's format, and encoded, once
|
|
72
|
+
* for as long as the row holds it (`checkedFile`, `encodedFile`).
|
|
73
|
+
*/
|
|
74
|
+
function valueOf(row: FormRow): { error: string | null; entry: ((key: string) => PayloadEntry) | null } {
|
|
75
|
+
const { value } = row;
|
|
76
|
+
if (value.kind === "text") {
|
|
77
|
+
const { text } = value;
|
|
78
|
+
if (text === "") return { error: null, entry: null };
|
|
79
|
+
if (utf8Length(text) > LIMITS.valueBytes) return { error: TEXT_TOO_LARGE, entry: null };
|
|
80
|
+
if (hasLoneSurrogate(text)) return { error: BROKEN_TEXT, entry: null };
|
|
81
|
+
return { error: null, entry: (key) => ({ key, value: text }) };
|
|
82
|
+
}
|
|
83
|
+
const { file } = value;
|
|
84
|
+
if (file === null) return { error: null, entry: null };
|
|
85
|
+
const check = checkedFile(file, row.format);
|
|
86
|
+
if (!check.ok) return { error: check.message, entry: null };
|
|
87
|
+
// The plugin gives a key whose file has no bytes no file: such a row is an empty one, here as there.
|
|
88
|
+
if (file.bytes.length === 0) return { error: null, entry: null };
|
|
89
|
+
return { error: null, entry: (key) => ({ key, file: encodedFile(file) }) };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* What the form would send, and what each row and the whole say about it. `entries` is null while «→» is disabled:
|
|
94
|
+
* while a row has a line under it, a field the agent needs is empty, a row of the human's lacks a name or a value, the
|
|
95
|
+
* rows together would not fit one submission (`total`), there are more of them than a request may carry, or nothing
|
|
96
|
+
* would be sent at all. The plugin refuses a submission that writes nothing, and the refusal closes the request.
|
|
97
|
+
*
|
|
98
|
+
* An empty row says nothing: «→» is just disabled. A row has one line, about its name before its value. Messages name
|
|
99
|
+
* keys, never values. Every file goes as `{ key, file }`, its bytes as base64url.
|
|
100
|
+
*/
|
|
101
|
+
export function checkForm(form: FormState): { entries: PayloadEntry[] | null; errors: ReadonlyMap<string, string>; total: string | null } {
|
|
102
|
+
const errors = new Map<string, string>();
|
|
103
|
+
const taken = new Map<string, string>();
|
|
104
|
+
const ready: Array<() => PayloadEntry> = [];
|
|
105
|
+
let complete = true;
|
|
106
|
+
for (const row of form.rows) {
|
|
107
|
+
const name = nameOf(row, taken);
|
|
108
|
+
const { key } = name;
|
|
109
|
+
if (key !== null && !taken.has(key.toLowerCase())) taken.set(key.toLowerCase(), key);
|
|
110
|
+
const value = valueOf(row);
|
|
111
|
+
const error = name.error ?? value.error;
|
|
112
|
+
if (error !== null) {
|
|
113
|
+
errors.set(row.id, error);
|
|
114
|
+
complete = false;
|
|
115
|
+
} else if (value.entry === null) {
|
|
116
|
+
// Nothing in the row. A field the agent made optional is left out; every other row is waited for.
|
|
117
|
+
if (!(row.declared && row.optional)) complete = false;
|
|
118
|
+
} else if (key === null) {
|
|
119
|
+
complete = false;
|
|
120
|
+
} else {
|
|
121
|
+
const entry = value.entry;
|
|
122
|
+
ready.push(() => entry(key));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!complete) return { entries: null, errors, total: null };
|
|
126
|
+
const entries = ready.map((make) => make());
|
|
127
|
+
if (payloadSize(entries) > MAX_PAYLOAD_BYTES) return { entries: null, errors, total: TOTAL_TOO_LARGE };
|
|
128
|
+
if (entries.length === 0 || entries.length > LIMITS.fields) return { entries: null, errors, total: null };
|
|
129
|
+
return { entries, errors, total: null };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Fills every file's bytes the form holds with zeros, and lets go of what was made of them (`wipeFile`). */
|
|
133
|
+
export function wipeForm(form: FormState): void {
|
|
134
|
+
for (const row of form.rows) wipeRow(row);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function wipeRow(row: FormRow): void {
|
|
138
|
+
if (row.value.kind === "file" && row.value.file !== null) wipeFile(row.value.file);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The form with one row changed, or the very form it was given when `change` leaves the row as it was or there is no such row. */
|
|
142
|
+
function changed(form: FormState, id: string, change: (row: FormRow) => FormRow): FormState {
|
|
143
|
+
const index = form.rows.findIndex((row) => row.id === id);
|
|
144
|
+
const row = form.rows[index];
|
|
145
|
+
if (row === undefined) return form;
|
|
146
|
+
const next = change(row);
|
|
147
|
+
if (next === row) return form;
|
|
148
|
+
return { rows: form.rows.map((other, at) => (at === index ? next : other)) };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** What was typed into a text row. A file row has no text. */
|
|
152
|
+
export function setText(form: FormState, id: string, text: string): FormState {
|
|
153
|
+
return changed(form, id, (row) => (row.value.kind === "text" ? { ...row, value: { kind: "text", text } } : row));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The name of a row of the human's. A field the agent asked for keeps its key. */
|
|
157
|
+
export function setName(form: FormState, id: string, key: string): FormState {
|
|
158
|
+
return changed(form, id, (row) => (row.declared ? row : { ...row, key }));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** «+»: a row of the human's, numbered `n`, a text with no name. None past the most a request may carry, and none under an id that is taken. */
|
|
162
|
+
export function addRow(form: FormState, n: number): FormState {
|
|
163
|
+
const id = fieldId.row(n);
|
|
164
|
+
if (form.rows.length >= LIMITS.fields || form.rows.some((row) => row.id === id)) return form;
|
|
165
|
+
return { rows: [...form.rows, { id, key: "", declared: false, optional: false, format: "any", value: { kind: "text", text: "" } }] };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** «✕» on a row of the human's: it goes, and its file is wiped. A field the agent asked for stays. */
|
|
169
|
+
export function removeRow(form: FormState, id: string): FormState {
|
|
170
|
+
const row = form.rows.find((candidate) => candidate.id === id);
|
|
171
|
+
if (row === undefined || row.declared) return form;
|
|
172
|
+
wipeRow(row);
|
|
173
|
+
return { rows: form.rows.filter((other) => other !== row) };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The paperclip on a text row of the human's: it becomes an empty file row, whose file no format is checked against. */
|
|
177
|
+
export function toFileRow(form: FormState, id: string): FormState {
|
|
178
|
+
return changed(form, id, (row) => (row.declared || row.value.kind !== "text" ? row : { ...row, format: "any", value: { kind: "file", file: null } }));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* A file picked, dropped or pasted into a row. Only an empty file row takes one: a row that holds a file takes no
|
|
183
|
+
* other until that one is taken out, and a file nothing takes is wiped here, so that no copy of it is left behind.
|
|
184
|
+
*/
|
|
185
|
+
export function fillFile(form: FormState, id: string, file: FileValue): FormState {
|
|
186
|
+
const next = changed(form, id, (row) => (row.value.kind === "file" && row.value.file === null ? { ...row, value: { kind: "file", file } } : row));
|
|
187
|
+
if (next === form) wipeFile(file);
|
|
188
|
+
return next;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** «✕» on a file: it is wiped and taken out of its row, which is empty again. */
|
|
192
|
+
export function clearFile(form: FormState, id: string): FormState {
|
|
193
|
+
return changed(form, id, (row) => {
|
|
194
|
+
if (row.value.kind !== "file" || row.value.file === null) return row;
|
|
195
|
+
wipeFile(row.value.file);
|
|
196
|
+
return { ...row, value: { kind: "file", file: null } };
|
|
197
|
+
});
|
|
198
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// What the plugin needs from the host it runs in, and no promise that every host has it. The client project is checked
|
|
2
|
+
// without Node's types and without the DOM library, so the compiler is told about exactly the three globals that
|
|
3
|
+
// `shared/bytes.ts` uses. Whether they are there depends on the app that loads this code (Hermes on a phone, a browser
|
|
4
|
+
// engine on the desktop and the web): `crypto.getRandomValues` in particular is the host app's to provide.
|
|
5
|
+
//
|
|
6
|
+
// Its absence fails closed. Without it no key pair and no nonce can be drawn, `seal` throws `SealError("sealing failed")`,
|
|
7
|
+
// the form sends nothing and asks for «→» again, and the self-test on the settings screen reports that the app could
|
|
8
|
+
// not seal. There is no fallback to another source of randomness and none to plaintext.
|
|
9
|
+
declare class TextEncoder {
|
|
10
|
+
encode(input?: string): Uint8Array;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare class TextDecoder {
|
|
14
|
+
/** `ignoreBOM`: a byte order mark at the start is kept as the character U+FEFF, and not taken away (`client/file-entry.ts`). */
|
|
15
|
+
constructor(label?: string, options?: { fatal?: boolean; ignoreBOM?: boolean });
|
|
16
|
+
decode(input?: Uint8Array): string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare var crypto: { getRandomValues<T extends Uint8Array>(array: T): T };
|