enseal 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/README.md +106 -2
- package/bin/enseal.mjs +366 -0
- package/lib/api.mjs +32 -0
- package/lib/clipboard.mjs +25 -0
- package/lib/config.mjs +110 -0
- package/lib/e2e.js +297 -0
- package/lib/errors.mjs +8 -0
- package/lib/filerisk.js +258 -0
- package/lib/handoffmail.js +74 -0
- package/lib/prompt.mjs +51 -0
- package/lib/secretstore.mjs +127 -0
- package/package.json +17 -4
package/lib/e2e.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// GENERATED from src/lib/e2e.ts by scripts/build-cli.mjs. Do not edit here.
|
|
2
|
+
/**
|
|
3
|
+
* End-to-end encryption core. Everything here runs in the BROWSER (and in
|
|
4
|
+
* Node ≥20 for tests) — the server only ever stores the outputs, none of
|
|
5
|
+
* which can decrypt anything on their own.
|
|
6
|
+
*
|
|
7
|
+
* Model
|
|
8
|
+
* - Each workspace has an X25519 keypair, generated in the requester's
|
|
9
|
+
* browser at workspace creation.
|
|
10
|
+
* - The PUBLIC key goes to the server; drop pages seal submissions to it
|
|
11
|
+
* (NaCl box with an ephemeral sender key — x25519-xsalsa20-poly1305).
|
|
12
|
+
* - The PRIVATE key never travels bare: it is AES-256-GCM-wrapped under a
|
|
13
|
+
* key derived from the requester's unlock passphrase (PBKDF2-SHA256,
|
|
14
|
+
* 600k iterations) and the wrapped blob is what the server stores.
|
|
15
|
+
* - Reveal = fetch envelope + wrapped key, unlock with the passphrase
|
|
16
|
+
* locally, open the box locally. The server never sees plaintext.
|
|
17
|
+
*
|
|
18
|
+
* Envelope format (base64 of JSON): { epk, nonce, ct } — ephemeral public
|
|
19
|
+
* key, random 24-byte nonce, box ciphertext. Equivalent construction to
|
|
20
|
+
* libsodium's sealed box, spelled out explicitly.
|
|
21
|
+
*/
|
|
22
|
+
import nacl from "tweetnacl";
|
|
23
|
+
const subtle = globalThis.crypto.subtle;
|
|
24
|
+
const CHUNK = 0x8000; // stays far below every engine's argument-count limit
|
|
25
|
+
export const toB64 = (u) => {
|
|
26
|
+
const native = u.toBase64;
|
|
27
|
+
if (typeof native === "function")
|
|
28
|
+
return native.call(u);
|
|
29
|
+
let s = "";
|
|
30
|
+
for (let i = 0; i < u.length; i += CHUNK) {
|
|
31
|
+
s += String.fromCharCode.apply(null, u.subarray(i, i + CHUNK));
|
|
32
|
+
}
|
|
33
|
+
return btoa(s);
|
|
34
|
+
};
|
|
35
|
+
export const fromB64 = (s) => {
|
|
36
|
+
const native = Uint8Array.fromBase64;
|
|
37
|
+
if (typeof native === "function") {
|
|
38
|
+
try {
|
|
39
|
+
return native.call(Uint8Array, s);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// stricter than atob about padding and whitespace: fall through
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const bin = atob(s);
|
|
46
|
+
const out = new Uint8Array(bin.length);
|
|
47
|
+
for (let i = 0; i < bin.length; i++)
|
|
48
|
+
out[i] = bin.charCodeAt(i);
|
|
49
|
+
return out;
|
|
50
|
+
};
|
|
51
|
+
const utf8 = {
|
|
52
|
+
enc: (s) => new TextEncoder().encode(s),
|
|
53
|
+
dec: (u) => new TextDecoder().decode(u),
|
|
54
|
+
};
|
|
55
|
+
// ---- passphrase → AES key (PBKDF2-SHA256, 600k) ----
|
|
56
|
+
const KDF_ITERATIONS = 600_000;
|
|
57
|
+
async function kdf(passphrase, salt) {
|
|
58
|
+
const material = await subtle.importKey("raw", utf8.enc(passphrase), "PBKDF2", false, [
|
|
59
|
+
"deriveKey",
|
|
60
|
+
]);
|
|
61
|
+
return subtle.deriveKey({ name: "PBKDF2", hash: "SHA-256", salt: salt, iterations: KDF_ITERATIONS }, material, { name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"]);
|
|
62
|
+
}
|
|
63
|
+
/** Workspace key ceremony — run once, in the browser, at creation. */
|
|
64
|
+
export async function generateWorkspaceKeys(passphrase) {
|
|
65
|
+
const pair = nacl.box.keyPair();
|
|
66
|
+
const salt = crypto.getRandomValues(new Uint8Array(16));
|
|
67
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
68
|
+
const key = await kdf(passphrase, salt);
|
|
69
|
+
const wrapped = new Uint8Array(await subtle.encrypt({ name: "AES-GCM", iv: iv }, key, pair.secretKey));
|
|
70
|
+
return {
|
|
71
|
+
publicKey: toB64(pair.publicKey),
|
|
72
|
+
encPrivKey: toB64(wrapped),
|
|
73
|
+
kdfSalt: toB64(salt),
|
|
74
|
+
wrapIv: toB64(iv),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** Re-wrap an unlocked private key under a NEW passphrase (passphrase
|
|
78
|
+
* change: the keypair itself never changes, so pending submissions stay
|
|
79
|
+
* readable and old links keep working). Runs entirely in the browser. */
|
|
80
|
+
export async function rewrapPrivateKey(newPassphrase, privateKey) {
|
|
81
|
+
const salt = crypto.getRandomValues(new Uint8Array(16));
|
|
82
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
83
|
+
const key = await kdf(newPassphrase, salt);
|
|
84
|
+
const wrapped = new Uint8Array(await subtle.encrypt({ name: "AES-GCM", iv: iv }, key, privateKey));
|
|
85
|
+
return { encPrivKey: toB64(wrapped), kdfSalt: toB64(salt), wrapIv: toB64(iv) };
|
|
86
|
+
}
|
|
87
|
+
/** Unlock the workspace private key locally. null = wrong passphrase. */
|
|
88
|
+
export async function unlockPrivateKey(passphrase, w) {
|
|
89
|
+
try {
|
|
90
|
+
const key = await kdf(passphrase, fromB64(w.kdfSalt));
|
|
91
|
+
const priv = await subtle.decrypt({ name: "AES-GCM", iv: fromB64(w.wrapIv) }, key, fromB64(w.encPrivKey));
|
|
92
|
+
return new Uint8Array(priv);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return null; // GCM authentication failed — wrong passphrase or tampering
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Seal a submission to a workspace — runs in the CLIENT's browser. */
|
|
99
|
+
export function sealForWorkspace(workspacePublicKeyB64, plaintext) {
|
|
100
|
+
const eph = nacl.box.keyPair();
|
|
101
|
+
const nonce = nacl.randomBytes(nacl.box.nonceLength);
|
|
102
|
+
const ct = nacl.box(utf8.enc(plaintext), nonce, fromB64(workspacePublicKeyB64), eph.secretKey);
|
|
103
|
+
return toB64(utf8.enc(JSON.stringify({ epk: toB64(eph.publicKey), nonce: toB64(nonce), ct: toB64(ct) })));
|
|
104
|
+
}
|
|
105
|
+
/** Does this unlocked private key belong to the public key the server is
|
|
106
|
+
* handing out for the workspace? Clients seal to whatever public key the
|
|
107
|
+
* server gives them, so a public key swapped in the database would quietly
|
|
108
|
+
* send every new submission to someone else. The server cannot be asked
|
|
109
|
+
* about that; the one party who can tell is whoever holds the private key,
|
|
110
|
+
* so the dashboard checks each time the workspace is unlocked. */
|
|
111
|
+
export function publicKeyMatches(privateKey, publicKeyB64) {
|
|
112
|
+
try {
|
|
113
|
+
const derived = nacl.box.keyPair.fromSecretKey(privateKey).publicKey;
|
|
114
|
+
const claimed = fromB64(publicKeyB64);
|
|
115
|
+
return claimed.length === derived.length && nacl.verify(derived, claimed);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** Open a sealed envelope with the unlocked private key. null = tampered/wrong key. */
|
|
122
|
+
export function openSealed(envelopeB64, privateKey) {
|
|
123
|
+
try {
|
|
124
|
+
const env = JSON.parse(utf8.dec(fromB64(envelopeB64)));
|
|
125
|
+
const msg = nacl.box.open(fromB64(env.ct), fromB64(env.nonce), fromB64(env.epk), privateKey);
|
|
126
|
+
return msg ? utf8.dec(msg) : null;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// ---- outbound sends: fragment-key AES-GCM ------------------------------
|
|
133
|
+
//
|
|
134
|
+
// A SEND travels the other way: we deliver a secret to someone with no
|
|
135
|
+
// account and no keypair. The secret is AES-256-GCM-encrypted here in the
|
|
136
|
+
// sender's browser under a random key that rides the URL FRAGMENT (`…#k`),
|
|
137
|
+
// which browsers never transmit — the server stores ciphertext it cannot
|
|
138
|
+
// read. An optional passphrase (shared over a second channel) is stretched
|
|
139
|
+
// with the same PBKDF2 used for workspace keys and mixed into the key, so an
|
|
140
|
+
// intercepted link alone opens nothing.
|
|
141
|
+
export const toB64url = (u) => toB64(u).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
142
|
+
export const fromB64url = (s) => fromB64(s.replace(/-/g, "+").replace(/_/g, "/"));
|
|
143
|
+
/** The slow part, done once per (passphrase, salt): PBKDF2 at 600k. */
|
|
144
|
+
async function stretch(passphrase, passSalt) {
|
|
145
|
+
const material = await subtle.importKey("raw", utf8.enc(passphrase), "PBKDF2", false, [
|
|
146
|
+
"deriveBits",
|
|
147
|
+
]);
|
|
148
|
+
return new Uint8Array(await subtle.deriveBits({ name: "PBKDF2", hash: "SHA-256", salt: passSalt, iterations: KDF_ITERATIONS }, material, 256));
|
|
149
|
+
}
|
|
150
|
+
const concat = (...parts) => {
|
|
151
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
152
|
+
let at = 0;
|
|
153
|
+
for (const p of parts) {
|
|
154
|
+
out.set(p, at);
|
|
155
|
+
at += p.length;
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
};
|
|
159
|
+
async function sendKeyFrom(fragKey, stretched) {
|
|
160
|
+
const raw = stretched
|
|
161
|
+
? new Uint8Array(await subtle.digest("SHA-256", concat(fragKey, stretched)))
|
|
162
|
+
: fragKey;
|
|
163
|
+
return subtle.importKey("raw", raw, "AES-GCM", false, ["encrypt", "decrypt"]);
|
|
164
|
+
}
|
|
165
|
+
async function sendKey(fragKey, passphrase, passSalt) {
|
|
166
|
+
if (passphrase && !passSalt)
|
|
167
|
+
throw new Error("passphrase requires a salt");
|
|
168
|
+
return sendKeyFrom(fragKey, passphrase ? await stretch(passphrase, passSalt) : null);
|
|
169
|
+
}
|
|
170
|
+
// ---- claim proof: show the server you can open it BEFORE it hands anything over ----
|
|
171
|
+
//
|
|
172
|
+
// The recipient's browser derives a proof from the same secrets that open
|
|
173
|
+
// the payload (the fragment key, plus the stretched passphrase when there is
|
|
174
|
+
// one) and presents it with the claim. The server stores only a hash of it,
|
|
175
|
+
// taken at creation. Until the right proof arrives nothing is released and
|
|
176
|
+
// nothing is burned, so:
|
|
177
|
+
// - a link whose key was cut off or corrupted can't destroy the delivery
|
|
178
|
+
// - someone holding only the link's path (a proxy log) can't either
|
|
179
|
+
// - someone who intercepted the WHOLE link still needs the passphrase, and
|
|
180
|
+
// gets a few online tries instead of unlimited offline ones
|
|
181
|
+
// The label keeps the proof unrelated to the encryption key derived from the
|
|
182
|
+
// same inputs, and the server never sees the key or the passphrase.
|
|
183
|
+
//
|
|
184
|
+
// Counting those few tries is only fair to someone who is actually guessing a
|
|
185
|
+
// passphrase, which means someone who has the key. So a passphrase-protected
|
|
186
|
+
// send carries a SECOND proof, of the key alone: a wrong claim proof from a
|
|
187
|
+
// caller who can't show the key proof either costs nothing and destroys
|
|
188
|
+
// nothing, however many times it is sent. (A send with no passphrase needs no
|
|
189
|
+
// second value: its claim proof is already a proof of the key.)
|
|
190
|
+
const PROOF_LABEL = utf8.enc("enseal/claim-proof/v1");
|
|
191
|
+
const KEY_PROOF_LABEL = utf8.enc("enseal/key-proof/v1");
|
|
192
|
+
async function keyProofFrom(fragKey) {
|
|
193
|
+
return toB64url(new Uint8Array(await subtle.digest("SHA-256", concat(KEY_PROOF_LABEL, fragKey))));
|
|
194
|
+
}
|
|
195
|
+
async function claimProofFrom(fragKey, stretched) {
|
|
196
|
+
const parts = stretched ? [PROOF_LABEL, fragKey, stretched] : [PROOF_LABEL, fragKey];
|
|
197
|
+
return toB64url(new Uint8Array(await subtle.digest("SHA-256", concat(...parts))));
|
|
198
|
+
}
|
|
199
|
+
/** What the server stores at creation: sha256 of the proof, hex. */
|
|
200
|
+
export async function claimVerifierFor(proof) {
|
|
201
|
+
const h = new Uint8Array(await subtle.digest("SHA-256", utf8.enc(proof)));
|
|
202
|
+
return Array.from(h, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
203
|
+
}
|
|
204
|
+
/** Recipient side: derive the proof to present, and an opener that reuses
|
|
205
|
+
* the same (slow) passphrase stretch for the decrypt that follows. */
|
|
206
|
+
export async function sendPrepare(fragKey, passphrase, passSaltB64) {
|
|
207
|
+
const stretched = passphrase && passSaltB64 ? await stretch(passphrase, fromB64(passSaltB64)) : null;
|
|
208
|
+
return {
|
|
209
|
+
proof: await claimProofFrom(fragKey, stretched),
|
|
210
|
+
// shows the server this browser holds the link's key, passphrase or not
|
|
211
|
+
keyProof: await keyProofFrom(fragKey),
|
|
212
|
+
open: async (sealed) => {
|
|
213
|
+
const key = await sendKeyFrom(fragKey, stretched);
|
|
214
|
+
const plain = await subtle.decrypt({ name: "AES-GCM", iv: fromB64(sealed.iv) }, key, fromB64(sealed.ciphertext));
|
|
215
|
+
return utf8.dec(new Uint8Array(plain));
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
export async function sendSeal(plaintext, fragKey, passphrase) {
|
|
220
|
+
const passSalt = passphrase ? crypto.getRandomValues(new Uint8Array(16)) : null;
|
|
221
|
+
const stretched = passphrase ? await stretch(passphrase, passSalt) : null;
|
|
222
|
+
const key = await sendKeyFrom(fragKey, stretched);
|
|
223
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
224
|
+
const ct = new Uint8Array(await subtle.encrypt({ name: "AES-GCM", iv: iv }, key, utf8.enc(plaintext)));
|
|
225
|
+
return {
|
|
226
|
+
ciphertext: toB64(ct),
|
|
227
|
+
iv: toB64(iv),
|
|
228
|
+
passSalt: passSalt ? toB64(passSalt) : null,
|
|
229
|
+
claimVerifier: await claimVerifierFor(await claimProofFrom(fragKey, stretched)),
|
|
230
|
+
keyVerifier: stretched ? await claimVerifierFor(await keyProofFrom(fragKey)) : null,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
/** Throws on a wrong key or passphrase (GCM authentication fails). */
|
|
234
|
+
export async function sendOpen(sealed, fragKey, passphrase) {
|
|
235
|
+
const key = await sendKey(fragKey, passphrase, sealed.passSalt ? fromB64(sealed.passSalt) : undefined);
|
|
236
|
+
const plain = await subtle.decrypt({ name: "AES-GCM", iv: fromB64(sealed.iv) }, key, fromB64(sealed.ciphertext));
|
|
237
|
+
return utf8.dec(new Uint8Array(plain));
|
|
238
|
+
}
|
|
239
|
+
export const envelopeForLogin = (f) => JSON.stringify({ v: 1, kind: "login", url: f.url, username: f.username, password: f.password, notes: f.notes });
|
|
240
|
+
export const envelopeForText = (text) => JSON.stringify({ v: 1, kind: "text", text });
|
|
241
|
+
export const envelopeForFile = (name, mime, bytes, note) => JSON.stringify({
|
|
242
|
+
v: 1,
|
|
243
|
+
kind: "file",
|
|
244
|
+
name,
|
|
245
|
+
mime: mime || "application/octet-stream",
|
|
246
|
+
data: toB64(bytes),
|
|
247
|
+
...(note ? { note } : {}),
|
|
248
|
+
});
|
|
249
|
+
export function inviteFrom(plain) {
|
|
250
|
+
try {
|
|
251
|
+
const o = JSON.parse(plain);
|
|
252
|
+
if (o && o.v === 1 && o.kind === "invite" && typeof o.code === "string" && typeof o.email === "string") {
|
|
253
|
+
return {
|
|
254
|
+
code: o.code.slice(0, 40),
|
|
255
|
+
email: o.email.slice(0, 254),
|
|
256
|
+
expires: typeof o.expires === "string" ? o.expires.slice(0, 60) : null,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
/* not an invitation */
|
|
262
|
+
}
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
export function openEnvelope(plain) {
|
|
266
|
+
try {
|
|
267
|
+
const o = JSON.parse(plain);
|
|
268
|
+
if (o && o.v === 1 && o.kind === "text" && typeof o.text === "string") {
|
|
269
|
+
return { kind: "text", text: o.text };
|
|
270
|
+
}
|
|
271
|
+
if (o && o.v === 1 && o.kind === "login" && typeof o.password === "string") {
|
|
272
|
+
// every part is whatever the other side typed: text, of bounded length
|
|
273
|
+
const part = (v, max) => (typeof v === "string" ? v.slice(0, max) : "");
|
|
274
|
+
return {
|
|
275
|
+
kind: "login",
|
|
276
|
+
login: { url: part(o.url, 2000), username: part(o.username, 1000), password: part(o.password, 10_000), notes: part(o.notes, 20_000) },
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
if (o && o.v === 1 && o.kind === "file" && typeof o.data === "string") {
|
|
280
|
+
return {
|
|
281
|
+
kind: "file",
|
|
282
|
+
// chosen by whoever sent the file: strip control characters and
|
|
283
|
+
// direction overrides (which can disguise an extension), cap the length
|
|
284
|
+
name: String(o.name ?? "")
|
|
285
|
+
.replace(/[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u202a-\u202e\u2060-\u2069\ufeff/\\]/g, "")
|
|
286
|
+
.slice(0, 200) || "download",
|
|
287
|
+
mime: String(o.mime ?? "application/octet-stream"),
|
|
288
|
+
bytes: fromB64(o.data),
|
|
289
|
+
note: typeof o.note === "string" && o.note ? o.note : null,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
/* not JSON: legacy plain-text payload */
|
|
295
|
+
}
|
|
296
|
+
return { kind: "text", text: plain };
|
|
297
|
+
}
|
package/lib/errors.mjs
ADDED
package/lib/filerisk.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// GENERATED from src/lib/filerisk.ts by scripts/build-cli.mjs. Do not edit here.
|
|
2
|
+
/**
|
|
3
|
+
* What kind of file is this, and how careful should the person opening it be?
|
|
4
|
+
*
|
|
5
|
+
* Enseal cannot virus-scan files: they are encrypted in the sender's browser
|
|
6
|
+
* and the server only ever holds ciphertext, which is the whole point. So the
|
|
7
|
+
* check happens where the plaintext exists, in the RECIPIENT's browser, after
|
|
8
|
+
* decryption and before anything is saved. It looks at the name AND the first
|
|
9
|
+
* bytes, because the name is chosen by the sender and can lie.
|
|
10
|
+
*
|
|
11
|
+
* This is a guard rail, not an antivirus: it tells people when they are
|
|
12
|
+
* holding a program instead of a document, and when a file is not what its
|
|
13
|
+
* name says. The device's own antivirus still does the scanning when the
|
|
14
|
+
* file is saved or opened.
|
|
15
|
+
*
|
|
16
|
+
* It also looks INSIDE the common containers, still entirely locally:
|
|
17
|
+
* - zip archives and modern Office files: the list of what they hold
|
|
18
|
+
* (programs inside, macro projects, password-protected entries)
|
|
19
|
+
* - legacy Office files: whether a macro project is present
|
|
20
|
+
* - PDFs: scripts and launch actions
|
|
21
|
+
* It does not, and cannot, recognise a specific virus: that takes a
|
|
22
|
+
* signature database of hundreds of megabytes, or sending the file (or a
|
|
23
|
+
* fingerprint of it) to a scanning service, which end-to-end encryption
|
|
24
|
+
* exists to avoid. The same check runs when a file is attached, so a risky
|
|
25
|
+
* file is refused or flagged before it is ever sent.
|
|
26
|
+
*/
|
|
27
|
+
/** Opening these runs code. */
|
|
28
|
+
const PROGRAM_EXT = new Set([
|
|
29
|
+
"exe", "scr", "com", "pif", "cpl", "msi", "msp", "dll", "sys", "bat", "cmd", "ps1", "psm1", "vbs", "vbe",
|
|
30
|
+
"js", "jse", "wsf", "wsh", "hta", "lnk", "scf", "url", "reg", "chm", "jar", "jnlp", "apk", "app", "command",
|
|
31
|
+
"dmg", "pkg", "deb", "rpm", "appimage", "run", "sh", "bash", "zsh", "py", "pl", "rb", "iso", "img", "vhd", "vhdx",
|
|
32
|
+
"xll", "msix", "appx", "appxbundle", "gadget", "inf", "ws", "application", "one",
|
|
33
|
+
]);
|
|
34
|
+
/** The same list, for other implementations' tests (the PowerShell module
|
|
35
|
+
* keeps its own copy, checked against this one through cli/test/filerisk.json). */
|
|
36
|
+
export const PROGRAM_EXTENSIONS = [...PROGRAM_EXT];
|
|
37
|
+
/** Office formats that can run macros. */
|
|
38
|
+
const MACRO_EXT = new Set(["docm", "dotm", "xlsm", "xltm", "xlam", "pptm", "potm", "ppam", "doc", "xls", "ppt"]);
|
|
39
|
+
/** Containers: the real contents are inside, unseen until unpacked. */
|
|
40
|
+
const ARCHIVE_EXT = new Set(["zip", "rar", "7z", "tar", "gz", "tgz", "bz2", "xz", "cab", "ace"]);
|
|
41
|
+
/** Open in a browser and can run script there. */
|
|
42
|
+
const WEB_EXT = new Set(["html", "htm", "xhtml", "mht", "mhtml", "svg", "xml"]);
|
|
43
|
+
export const extensionOf = (name) => {
|
|
44
|
+
const m = /\.([A-Za-z0-9]{1,12})\s*$/.exec(name);
|
|
45
|
+
return m ? m[1].toLowerCase() : "";
|
|
46
|
+
};
|
|
47
|
+
/** The danger categories a browser may report. */
|
|
48
|
+
export const DANGER_CODES = ["program", "disguised_program", "archive_program", "pdf_launch"];
|
|
49
|
+
/** True for names that are programs or scripts: refused at the SENDING end
|
|
50
|
+
* as a courtesy (the receiving end is where it is actually enforced). */
|
|
51
|
+
export const isProgramName = (name) => PROGRAM_EXT.has(extensionOf(name));
|
|
52
|
+
const starts = (b, sig) => sig.every((v, i) => b[i] === v);
|
|
53
|
+
/** What the first bytes say the file really is, when they say anything. */
|
|
54
|
+
function sniff(b) {
|
|
55
|
+
if (b.length < 4)
|
|
56
|
+
return null;
|
|
57
|
+
if (starts(b, [0x4d, 0x5a]))
|
|
58
|
+
return "program"; // Windows executable (MZ)
|
|
59
|
+
if (starts(b, [0x7f, 0x45, 0x4c, 0x46]))
|
|
60
|
+
return "program"; // ELF
|
|
61
|
+
if (starts(b, [0xfe, 0xed, 0xfa, 0xce]) || starts(b, [0xfe, 0xed, 0xfa, 0xcf]) ||
|
|
62
|
+
starts(b, [0xce, 0xfa, 0xed, 0xfe]) || starts(b, [0xcf, 0xfa, 0xed, 0xfe]) ||
|
|
63
|
+
starts(b, [0xca, 0xfe, 0xba, 0xbe])) {
|
|
64
|
+
return "program"; // Mach-O / universal binary / Java class
|
|
65
|
+
}
|
|
66
|
+
if (starts(b, [0x23, 0x21]))
|
|
67
|
+
return "script"; // #!
|
|
68
|
+
if (starts(b, [0x4c, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00]))
|
|
69
|
+
return "shortcut"; // .lnk
|
|
70
|
+
if (starts(b, [0xd0, 0xcf, 0x11, 0xe0]))
|
|
71
|
+
return "ole"; // legacy Office, MSI
|
|
72
|
+
if (starts(b, [0x50, 0x4b, 0x03, 0x04]))
|
|
73
|
+
return "zip"; // zip, modern Office, jar, apk
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
// ---- looking inside containers (all local, nothing is unpacked or run) ----
|
|
77
|
+
const u16 = (b, o) => b[o] | (b[o + 1] << 8);
|
|
78
|
+
const u32 = (b, o) => (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0;
|
|
79
|
+
/** The names a zip container lists, read from its central directory. The
|
|
80
|
+
* directory is stored uncompressed even when the files are not, so this
|
|
81
|
+
* needs no decompression. null = not a readable zip. */
|
|
82
|
+
export function zipListing(b) {
|
|
83
|
+
// the end-of-central-directory record sits in the last 64 KB
|
|
84
|
+
let eocd = -1;
|
|
85
|
+
for (let i = b.length - 22; i >= Math.max(0, b.length - 65_557); i--) {
|
|
86
|
+
if (b[i] === 0x50 && b[i + 1] === 0x4b && b[i + 2] === 0x05 && b[i + 3] === 0x06) {
|
|
87
|
+
eocd = i;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (eocd < 0)
|
|
92
|
+
return null;
|
|
93
|
+
let at = u32(b, eocd + 16);
|
|
94
|
+
const count = Math.min(u16(b, eocd + 10), 5000);
|
|
95
|
+
const names = [];
|
|
96
|
+
let encrypted = false;
|
|
97
|
+
const dec = new TextDecoder();
|
|
98
|
+
for (let n = 0; n < count; n++) {
|
|
99
|
+
if (at + 46 > b.length || u32(b, at) !== 0x02014b50)
|
|
100
|
+
break;
|
|
101
|
+
if (u16(b, at + 8) & 1)
|
|
102
|
+
encrypted = true;
|
|
103
|
+
const nameLen = u16(b, at + 28);
|
|
104
|
+
if (at + 46 + nameLen > b.length)
|
|
105
|
+
break;
|
|
106
|
+
names.push(dec.decode(b.subarray(at + 46, at + 46 + nameLen)));
|
|
107
|
+
at += 46 + nameLen + u16(b, at + 30) + u16(b, at + 32);
|
|
108
|
+
}
|
|
109
|
+
return { names, encrypted };
|
|
110
|
+
}
|
|
111
|
+
/** Does `needle` (ASCII) occur in the bytes, optionally as UTF-16LE? */
|
|
112
|
+
function contains(b, needle, utf16 = false) {
|
|
113
|
+
const pat = [];
|
|
114
|
+
for (const ch of needle) {
|
|
115
|
+
pat.push(ch.charCodeAt(0));
|
|
116
|
+
if (utf16)
|
|
117
|
+
pat.push(0);
|
|
118
|
+
}
|
|
119
|
+
const first = pat[0];
|
|
120
|
+
outer: for (let i = b.indexOf(first); i !== -1 && i + pat.length <= b.length; i = b.indexOf(first, i + 1)) {
|
|
121
|
+
for (let j = 1; j < pat.length; j++)
|
|
122
|
+
if (b[i + j] !== pat[j])
|
|
123
|
+
continue outer;
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const OFFICE_ZIP_EXT = new Set(["docx", "xlsx", "pptx", "docm", "dotm", "xlsm", "xltm", "xlam", "pptm", "potm", "ppam"]);
|
|
129
|
+
export function assessFile(name, bytes) {
|
|
130
|
+
const ext = extensionOf(name);
|
|
131
|
+
const kind = sniff(bytes);
|
|
132
|
+
// a zip container: judge it by what it lists
|
|
133
|
+
if (kind === "zip") {
|
|
134
|
+
const zip = zipListing(bytes);
|
|
135
|
+
if (zip) {
|
|
136
|
+
const program = zip.names.find((n) => PROGRAM_EXT.has(extensionOf(n)));
|
|
137
|
+
const macros = zip.names.some((n) => /(^|\/)vbaProject\.bin$/i.test(n));
|
|
138
|
+
if (OFFICE_ZIP_EXT.has(ext)) {
|
|
139
|
+
if (macros) {
|
|
140
|
+
return {
|
|
141
|
+
level: "caution",
|
|
142
|
+
title: MACRO_EXT.has(ext) ? "This document contains macros" : "This document contains macros, although its type says it should not",
|
|
143
|
+
detail: "Macros are small programs inside a document. If it asks you to enable macros or editing when you open it, say no unless you were told to expect exactly that.",
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (program) {
|
|
148
|
+
return {
|
|
149
|
+
level: "danger",
|
|
150
|
+
code: "archive_program",
|
|
151
|
+
title: "This archive contains a program",
|
|
152
|
+
detail: `Inside it is "${program.split("/").pop()}", which runs code when opened. Hiding a program in an archive is a common way to get it past people. Only continue if you asked for exactly this.`,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
else if (zip.encrypted) {
|
|
156
|
+
return {
|
|
157
|
+
level: "caution",
|
|
158
|
+
title: "This archive is password-protected, so its contents can't be checked",
|
|
159
|
+
detail: "Protected archives are often used to slip harmful files past checks. Open it only if you were expecting it, and be wary of programs inside.",
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (ext === "pdf" || starts(bytes, [0x25, 0x50, 0x44, 0x46])) {
|
|
165
|
+
if (contains(bytes, "/Launch")) {
|
|
166
|
+
return {
|
|
167
|
+
level: "danger",
|
|
168
|
+
code: "pdf_launch",
|
|
169
|
+
title: "This PDF tries to start another program when opened",
|
|
170
|
+
detail: "Ordinary documents don't do that. Do not open it unless the sender confirms, through a channel you trust, exactly what it is.",
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
if (contains(bytes, "/JavaScript") || contains(bytes, "/JS ") || contains(bytes, "/JS(")) {
|
|
174
|
+
return {
|
|
175
|
+
level: "caution",
|
|
176
|
+
title: "This PDF contains scripts",
|
|
177
|
+
detail: "Some forms use them legitimately. To be safe, open it in your web browser's built-in PDF viewer instead of a desktop PDF program.",
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (kind === "ole" && contains(bytes, "_VBA_PROJECT", true)) {
|
|
182
|
+
return {
|
|
183
|
+
level: "caution",
|
|
184
|
+
title: "This document contains macros",
|
|
185
|
+
detail: "Macros are small programs inside a document. If it asks you to enable macros or editing when you open it, say no unless you were told to expect exactly that.",
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
if (kind === "program" || kind === "script" || kind === "shortcut") {
|
|
189
|
+
const disguised = !PROGRAM_EXT.has(ext);
|
|
190
|
+
return {
|
|
191
|
+
level: "danger",
|
|
192
|
+
code: disguised ? "disguised_program" : "program",
|
|
193
|
+
title: disguised ? "This file is not what its name says" : "This is a program, not a document",
|
|
194
|
+
detail: disguised
|
|
195
|
+
? `It is named like ${ext ? `a .${ext} file` : "an ordinary file"}, but its contents are a program. That is a common way to trick people into running something harmful. Do not open it unless the sender confirms, through a channel you trust, exactly what it is.`
|
|
196
|
+
: "Opening it runs code on your computer. Only continue if you asked for this exact file and you are sure who sent it.",
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
if (PROGRAM_EXT.has(ext)) {
|
|
200
|
+
return {
|
|
201
|
+
level: "danger",
|
|
202
|
+
code: "program",
|
|
203
|
+
title: "This is a program or script, not a document",
|
|
204
|
+
detail: "Opening it runs code on your computer. Only continue if you asked for this exact file and you are sure who sent it.",
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
if (MACRO_EXT.has(ext) || kind === "ole") {
|
|
208
|
+
return {
|
|
209
|
+
level: "caution",
|
|
210
|
+
title: "This document can contain macros",
|
|
211
|
+
detail: "If it asks you to enable macros or editing when you open it, say no unless you were told to expect that.",
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
if (WEB_EXT.has(ext)) {
|
|
215
|
+
return {
|
|
216
|
+
level: "caution",
|
|
217
|
+
title: "This file opens in your browser and can run scripts there",
|
|
218
|
+
detail: "Open it only if you were expecting a web page or image of this kind from this sender.",
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (ARCHIVE_EXT.has(ext)) {
|
|
222
|
+
return {
|
|
223
|
+
level: "caution",
|
|
224
|
+
title: "This is an archive: its real contents are inside",
|
|
225
|
+
detail: "Look at what it contains before opening anything in it, and be wary of programs inside.",
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
if (kind === "zip" && !["docx", "xlsx", "pptx", "odt", "ods", "odp", "epub", "key", "pages", "numbers"].includes(ext)) {
|
|
229
|
+
return {
|
|
230
|
+
level: "caution",
|
|
231
|
+
title: "This file is an archive under another name",
|
|
232
|
+
detail: `It is named like ${ext ? `a .${ext} file` : "an ordinary file"}, but it is a container with other files inside. Check with the sender before opening it.`,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
level: "ok",
|
|
237
|
+
title: "Nothing unusual about this file type",
|
|
238
|
+
detail: "",
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
/** The check, run at the moment a file is ATTACHED (on either form), on the
|
|
242
|
+
* file's real bytes. Dangerous files are refused before they are sent;
|
|
243
|
+
* files the recipient will be cautioned about are allowed, and the person
|
|
244
|
+
* attaching is told so they are not surprised. */
|
|
245
|
+
export async function checkUpload(file) {
|
|
246
|
+
const risk = assessFile(file.name, new Uint8Array(await file.arrayBuffer()));
|
|
247
|
+
if (risk.level === "danger") {
|
|
248
|
+
return {
|
|
249
|
+
code: risk.code,
|
|
250
|
+
refuse: `"${file.name}" can't be sent. ${risk.title}. Files that run code when opened aren't carried, because the person opening it would be the one at risk. If it is text (a script or config), paste it as text instead.`,
|
|
251
|
+
caution: null,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
if (risk.level === "caution") {
|
|
255
|
+
return { refuse: null, caution: `${risk.title}. It will be sent, and the person opening it will see this warning too.` };
|
|
256
|
+
}
|
|
257
|
+
return { refuse: null, caution: null };
|
|
258
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// GENERATED from src/lib/handoffmail.ts by scripts/build-cli.mjs. Do not edit here.
|
|
2
|
+
/**
|
|
3
|
+
* The ready-to-send message for a link the user just created: used by the
|
|
4
|
+
* "Open in my email app" and "Copy message" buttons.
|
|
5
|
+
*
|
|
6
|
+
* Everything here runs in the user's browser. The message is handed to THEIR
|
|
7
|
+
* mail app through a mailto: link, so it goes out from their own address and
|
|
8
|
+
* their own domain, and it never touches our servers. That matters for sends,
|
|
9
|
+
* whose link carries the decryption key after the #: a message composed or
|
|
10
|
+
* relayed server-side would put that key where the design says it never goes.
|
|
11
|
+
*
|
|
12
|
+
* One rule is absolute: a send's passphrase is NEVER put in this message. The
|
|
13
|
+
* passphrase only protects anything if it travels separately from the link.
|
|
14
|
+
*/
|
|
15
|
+
const day = (iso) => new Date(iso).toLocaleDateString("en-US", { weekday: "long", month: "long", day: "numeric" });
|
|
16
|
+
const greeting = (clientName) => `Hi ${clientName.trim() || "there"},`;
|
|
17
|
+
/** Asking a client to send something through a request link. */
|
|
18
|
+
export function requestMessage(o) {
|
|
19
|
+
const list = o.items ?? [];
|
|
20
|
+
return {
|
|
21
|
+
subject: `${o.workspaceName}: a secure link to send us what we need`,
|
|
22
|
+
body: [
|
|
23
|
+
greeting(o.clientName),
|
|
24
|
+
"",
|
|
25
|
+
list.length > 0 ? o.note : `We need the following from you: ${o.note}`,
|
|
26
|
+
"",
|
|
27
|
+
`Please send ${list.length > 0 ? "them" : "it"} through this secure ${list.length > 0 ? "" : "one-time "}link, not by email or chat. ${list.length > 0 ? "Each one" : "It"} is encrypted in your browser before it leaves, and only we can read it, once:`,
|
|
28
|
+
"",
|
|
29
|
+
o.url,
|
|
30
|
+
"",
|
|
31
|
+
// For a list, what is asked for comes AFTER the link. A mail app handed
|
|
32
|
+
// a very long draft can cut it short (some stop near 2,000 characters),
|
|
33
|
+
// and twenty-five item names ahead of the link could push the link past
|
|
34
|
+
// that point. Cut here, a draft loses the end of the list; the page the
|
|
35
|
+
// link opens shows the whole of it.
|
|
36
|
+
...(list.length > 0 ? ["What the link asks for:", ...list.map((label) => ` - ${label}`), ""] : []),
|
|
37
|
+
(list.length > 0
|
|
38
|
+
? `You can send them one at a time and come back to the same link for the rest. It closes once everything is in, and expires on ${day(o.expiresAt)}.`
|
|
39
|
+
: `The link works once and expires on ${day(o.expiresAt)}.`) +
|
|
40
|
+
" If you weren't expecting this request, don't use the link, and tell me through a phone number or address you already had for us.",
|
|
41
|
+
"",
|
|
42
|
+
"Thanks,",
|
|
43
|
+
o.senderName,
|
|
44
|
+
].join("\n"),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Delivering something to a client through a send link. */
|
|
48
|
+
export function sendMessage(o) {
|
|
49
|
+
return {
|
|
50
|
+
subject: `${o.workspaceName} has sent you something securely`,
|
|
51
|
+
body: [
|
|
52
|
+
greeting(o.clientName),
|
|
53
|
+
"",
|
|
54
|
+
`I've sent you: ${o.label}`,
|
|
55
|
+
"",
|
|
56
|
+
"Open this secure one-time link to see it. It can only be opened once, so have somewhere safe to store it (your password manager) before you click Reveal:",
|
|
57
|
+
"",
|
|
58
|
+
o.url,
|
|
59
|
+
"",
|
|
60
|
+
...(o.withPassphrase
|
|
61
|
+
? ["You will also need a passphrase to open it. I'll give you that separately, by text or phone, so it never sits next to the link.", ""]
|
|
62
|
+
: []),
|
|
63
|
+
`The link expires on ${day(o.expiresAt)}. Please use the whole link, including everything after the # sign.`,
|
|
64
|
+
"",
|
|
65
|
+
"Thanks,",
|
|
66
|
+
o.senderName,
|
|
67
|
+
].join("\n"),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** A mailto: URL for the message. The recipient is left for the user to fill
|
|
71
|
+
* in from their own address book. CRLF line breaks, per the mailto spec. */
|
|
72
|
+
export const mailtoHref = (m) => `mailto:?subject=${encodeURIComponent(m.subject)}&body=${encodeURIComponent(m.body.replace(/\n/g, "\r\n"))}`;
|
|
73
|
+
/** The same message as plain text, for pasting into any mail or chat app. */
|
|
74
|
+
export const messageText = (m) => `Subject: ${m.subject}\n\n${m.body}`;
|