@cardonjs/backups 0.0.0-stage → 0.3.2
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/FORMAT.md +147 -0
- package/LICENSE +21 -0
- package/README.md +1237 -2
- package/dist/copy.d.ts +175 -0
- package/dist/copy.d.ts.map +1 -0
- package/dist/copy.js +863 -0
- package/dist/copy.js.map +1 -0
- package/dist/encryption.d.ts +203 -0
- package/dist/encryption.d.ts.map +1 -0
- package/dist/encryption.js +609 -0
- package/dist/encryption.js.map +1 -0
- package/dist/export.d.ts +201 -0
- package/dist/export.d.ts.map +1 -0
- package/dist/export.js +447 -0
- package/dist/export.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/keys.d.ts +28 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +97 -0
- package/dist/keys.js.map +1 -0
- package/dist/metadata.d.ts +49 -0
- package/dist/metadata.d.ts.map +1 -0
- package/dist/metadata.js +51 -0
- package/dist/metadata.js.map +1 -0
- package/dist/restore.d.ts +97 -0
- package/dist/restore.d.ts.map +1 -0
- package/dist/restore.js +276 -0
- package/dist/restore.js.map +1 -0
- package/dist/retention.d.ts +153 -0
- package/dist/retention.d.ts.map +1 -0
- package/dist/retention.js +295 -0
- package/dist/retention.js.map +1 -0
- package/dist/run.d.ts +94 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +129 -0
- package/dist/run.js.map +1 -0
- package/dist/sniff.d.ts +15 -0
- package/dist/sniff.d.ts.map +1 -0
- package/dist/sniff.js +47 -0
- package/dist/sniff.js.map +1 -0
- package/dist/workflow.d.ts +102 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +298 -0
- package/dist/workflow.js.map +1 -0
- package/llms.txt +249 -0
- package/package.json +76 -3
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side encryption of backups (opt-in): AES-256-GCM over 64 KiB plaintext chunks in the
|
|
3
|
+
* STREAM construction, with a key derived for each backup (HKDF-SHA-256 from the configured key
|
|
4
|
+
* and a random 32-byte salt), the header bound to every chunk as associated data. The header
|
|
5
|
+
* also holds what the backup is of (the D1 database id and the object key it was written to), so
|
|
6
|
+
* an authentic backup cannot stand in for another one. The byte layout and two full vectors are
|
|
7
|
+
* in `FORMAT.md`, shipped with the package; `encryption.test.ts` pins more vectors.
|
|
8
|
+
*
|
|
9
|
+
* Standard Web Crypto only (`crypto.subtle`): runs on Workers and Node. Streams with
|
|
10
|
+
* backpressure: at most one 64 KiB chunk (plus what the source handed over) is held at a time.
|
|
11
|
+
*
|
|
12
|
+
* Key material: a configured key's decoded bytes are kept in a `WeakMap` (invisible to logging
|
|
13
|
+
* and `JSON.stringify`) and zeroed right after that key's first import as a non-extractable
|
|
14
|
+
* `CryptoKey`; the bytes of keys never used in a run stay in memory until the settings object is
|
|
15
|
+
* collected, and the base64 strings the app passed in (from its environment) are not ours to
|
|
16
|
+
* erase. Derived keys are non-extractable. Errors are fixed strings with a reason code.
|
|
17
|
+
*/
|
|
18
|
+
/** The scheme recorded in a backup's metadata (`encryption`) and its sidecar. */
|
|
19
|
+
export const ENCRYPTION_SCHEME = "aes-256-gcm-stream-v1";
|
|
20
|
+
/** `CARDONBK`: the first 8 bytes of every encrypted backup. A plain dump starts with SQL. */
|
|
21
|
+
const MAGIC = new Uint8Array([0x43, 0x41, 0x52, 0x44, 0x4f, 0x4e, 0x42, 0x4b]);
|
|
22
|
+
const FORMAT_VERSION = 1;
|
|
23
|
+
/** Plaintext bytes per chunk; every chunk but the last holds exactly this many. */
|
|
24
|
+
export const CHUNK_SIZE = 65_536;
|
|
25
|
+
const TAG_SIZE = 16;
|
|
26
|
+
const SALT_SIZE = 32;
|
|
27
|
+
const KEY_SIZE = 32;
|
|
28
|
+
/** magic (8) + version (1) + chunk size (4) + key id length (1). */
|
|
29
|
+
const FIXED_HEADER = 14;
|
|
30
|
+
const MAX_KEY_ID = 32;
|
|
31
|
+
/** The counter is 32 bits: at most 2^32 chunks, so a plaintext below 2^48 bytes (256 TiB). */
|
|
32
|
+
const MAX_CHUNKS = 2 ** 32;
|
|
33
|
+
/** The largest plaintext one backup can hold: 2^32 chunks, the last one short. */
|
|
34
|
+
export const MAX_PLAINTEXT_SIZE = MAX_CHUNKS * CHUNK_SIZE - 1;
|
|
35
|
+
const INFO = new TextEncoder().encode("cardon-backups/aes-256-gcm-stream/v1");
|
|
36
|
+
/** Key ids: not secret, but they go in metadata, step results and the header. */
|
|
37
|
+
const KEY_ID = /^[a-z0-9][a-z0-9_-]{0,31}$/;
|
|
38
|
+
/** The context's database id (a D1 id is a UUID; the format takes any id of this shape). */
|
|
39
|
+
const CONTEXT_DATABASE_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/;
|
|
40
|
+
/** The context's object key (`backupKey` builds `<prefix>/<yyyy>/<mm>/<…>Z.sql`). */
|
|
41
|
+
const CONTEXT_KEY = /^[A-Za-z0-9._/-]{1,1024}$/;
|
|
42
|
+
/** The longest header: fixed part, key id, salt, both context fields and their lengths. */
|
|
43
|
+
const MAX_HEADER = FIXED_HEADER + MAX_KEY_ID + SALT_SIZE + 1 + 64 + 2 + 1024;
|
|
44
|
+
/** 32 bytes in base64url: 43 characters, an optional `=` of padding. */
|
|
45
|
+
const KEY_TEXT = /^[A-Za-z0-9_-]{43}=?$/;
|
|
46
|
+
const MAX_KEYS = 32;
|
|
47
|
+
/** Internal: whether `value` is a valid context (`databaseId` and `key` of their patterns). */
|
|
48
|
+
export function isIdentity(value) {
|
|
49
|
+
if (typeof value !== "object" || value === null)
|
|
50
|
+
return false;
|
|
51
|
+
const { databaseId, key } = value;
|
|
52
|
+
return (typeof databaseId === "string" &&
|
|
53
|
+
CONTEXT_DATABASE_ID.test(databaseId) &&
|
|
54
|
+
typeof key === "string" &&
|
|
55
|
+
CONTEXT_KEY.test(key));
|
|
56
|
+
}
|
|
57
|
+
/** The secrets of each `BackupEncryption`, out of reach of whoever holds the object. */
|
|
58
|
+
const keyrings = new WeakMap();
|
|
59
|
+
function decodeKey(text) {
|
|
60
|
+
if (!KEY_TEXT.test(text))
|
|
61
|
+
return null;
|
|
62
|
+
const body = text.endsWith("=") ? text.slice(0, -1) : text;
|
|
63
|
+
let binary;
|
|
64
|
+
try {
|
|
65
|
+
binary = atob(`${body.replaceAll("-", "+").replaceAll("_", "/")}=`);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
if (binary.length !== KEY_SIZE)
|
|
71
|
+
return null;
|
|
72
|
+
const bytes = new Uint8Array(KEY_SIZE);
|
|
73
|
+
for (let i = 0; i < KEY_SIZE; i++)
|
|
74
|
+
bytes[i] = binary.charCodeAt(i);
|
|
75
|
+
// Canonical only: the last character must carry no stray bits (one text per key).
|
|
76
|
+
const again = btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
77
|
+
if (again !== body)
|
|
78
|
+
return null;
|
|
79
|
+
return bytes;
|
|
80
|
+
}
|
|
81
|
+
function sameBytes(a, b) {
|
|
82
|
+
if (a.byteLength !== b.byteLength)
|
|
83
|
+
return false;
|
|
84
|
+
let diff = 0;
|
|
85
|
+
for (let i = 0; i < a.byteLength; i++)
|
|
86
|
+
diff |= a[i] ^ b[i];
|
|
87
|
+
return diff === 0;
|
|
88
|
+
}
|
|
89
|
+
function buildKeyring(options, who, requireCurrent) {
|
|
90
|
+
if (typeof options !== "object" || options === null) {
|
|
91
|
+
throw new TypeError(`${who}: encryption options { keys, current } are required.`);
|
|
92
|
+
}
|
|
93
|
+
const { keys, current } = options;
|
|
94
|
+
if (typeof keys !== "object" || keys === null || Array.isArray(keys)) {
|
|
95
|
+
throw new TypeError(`${who}: encryption keys must be an object of key id → key.`);
|
|
96
|
+
}
|
|
97
|
+
const ids = Object.keys(keys);
|
|
98
|
+
if (ids.length === 0 || ids.length > MAX_KEYS) {
|
|
99
|
+
throw new TypeError(`${who}: encryption keys must hold 1 to ${MAX_KEYS} keys.`);
|
|
100
|
+
}
|
|
101
|
+
const slots = new Map();
|
|
102
|
+
for (const id of ids) {
|
|
103
|
+
if (!KEY_ID.test(id)) {
|
|
104
|
+
throw new TypeError(`${who}: every encryption key id must be 1 to 32 lowercase letters, digits, '-' or '_'.`);
|
|
105
|
+
}
|
|
106
|
+
const value = keys[id];
|
|
107
|
+
const raw = typeof value === "string" ? decodeKey(value) : null;
|
|
108
|
+
if (raw === null) {
|
|
109
|
+
throw new TypeError(`${who}: every encryption key must be 32 random bytes in base64url (43 characters).`);
|
|
110
|
+
}
|
|
111
|
+
for (const slot of slots.values()) {
|
|
112
|
+
if (slot.raw !== null && sameBytes(slot.raw, raw)) {
|
|
113
|
+
throw new TypeError(`${who}: the same encryption key is listed under two ids.`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
slots.set(id, { raw, imported: null });
|
|
117
|
+
}
|
|
118
|
+
if (current === undefined && !requireCurrent)
|
|
119
|
+
return { current: null, ids, slots };
|
|
120
|
+
if (typeof current !== "string" || !slots.has(current)) {
|
|
121
|
+
throw new TypeError(`${who}: encryption current must be the id of one of the keys.`);
|
|
122
|
+
}
|
|
123
|
+
return { current, ids, slots };
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Validates encryption settings once, when the backup is configured: every key 32 bytes in
|
|
127
|
+
* base64url, ids of the strict pattern, `current` one of them, no key listed twice. Throws a
|
|
128
|
+
* `TypeError` that never includes a key or an id. The result holds the keys out of sight (no
|
|
129
|
+
* property, no `toJSON` exposes them); pass it to `copyExport`, `runBackup`, the Workflow,
|
|
130
|
+
* `decryptBackup` or `checkRestore`.
|
|
131
|
+
*/
|
|
132
|
+
export function backupEncryption(options) {
|
|
133
|
+
return makeEncryption(options, "backupEncryption");
|
|
134
|
+
}
|
|
135
|
+
function makeEncryption(options, who) {
|
|
136
|
+
const { current, ids, slots } = buildKeyring(options, who, true);
|
|
137
|
+
const encryption = Object.freeze({
|
|
138
|
+
scheme: ENCRYPTION_SCHEME,
|
|
139
|
+
current: current,
|
|
140
|
+
keyIds: Object.freeze([...ids]),
|
|
141
|
+
});
|
|
142
|
+
keyrings.set(encryption, slots);
|
|
143
|
+
return encryption;
|
|
144
|
+
}
|
|
145
|
+
/** Whether `value` came from `backupEncryption`. */
|
|
146
|
+
function isBackupEncryption(value) {
|
|
147
|
+
return typeof value === "object" && value !== null && keyrings.has(value);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Internal: `value` as validated settings (`backupEncryption`'s result as it is; options are
|
|
151
|
+
* validated). It never fails open: `null` or `undefined` is a `TypeError`, never "plain".
|
|
152
|
+
*/
|
|
153
|
+
export function requireEncryption(value, who) {
|
|
154
|
+
if (value === undefined || value === null) {
|
|
155
|
+
throw new TypeError(`${who}: encryption is empty; pass backupEncryption(…) settings, or leave the property out for plain backups.`);
|
|
156
|
+
}
|
|
157
|
+
if (isBackupEncryption(value))
|
|
158
|
+
return value;
|
|
159
|
+
return makeEncryption(value, who);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Internal: the `encryption` property of `options`. Only an **absent** property means plain
|
|
163
|
+
* (`null`); present, it must be valid settings (a `null` or `undefined` value is a `TypeError`),
|
|
164
|
+
* so a missing secret can never turn encryption off silently.
|
|
165
|
+
*/
|
|
166
|
+
export function encryptionOption(options, who) {
|
|
167
|
+
if (!("encryption" in options))
|
|
168
|
+
return null;
|
|
169
|
+
return requireEncryption(options.encryption, who);
|
|
170
|
+
}
|
|
171
|
+
/** Internal: the keys for decryption (`current` optional). */
|
|
172
|
+
function resolveDecryption(value, who) {
|
|
173
|
+
if (value === undefined || value === null) {
|
|
174
|
+
throw new TypeError(`${who}: the keys are required.`);
|
|
175
|
+
}
|
|
176
|
+
if (isBackupEncryption(value))
|
|
177
|
+
return value;
|
|
178
|
+
const { slots } = buildKeyring(value, who, false);
|
|
179
|
+
const ring = Object.freeze({});
|
|
180
|
+
keyrings.set(ring, slots);
|
|
181
|
+
return ring;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The configured key `id` as a non-extractable HKDF key, imported once; its decoded bytes are
|
|
185
|
+
* zeroed right after that import (`importKey` copies them when called).
|
|
186
|
+
*/
|
|
187
|
+
function baseKey(ring, id) {
|
|
188
|
+
const slot = keyrings.get(ring)?.get(id);
|
|
189
|
+
if (slot === undefined)
|
|
190
|
+
return null;
|
|
191
|
+
if (slot.imported === null) {
|
|
192
|
+
const raw = slot.raw;
|
|
193
|
+
slot.imported = crypto.subtle
|
|
194
|
+
.importKey("raw", raw, "HKDF", false, ["deriveKey"])
|
|
195
|
+
.finally(() => raw.fill(0));
|
|
196
|
+
slot.raw = null;
|
|
197
|
+
}
|
|
198
|
+
return slot.imported;
|
|
199
|
+
}
|
|
200
|
+
/** The backup's own AES-256-GCM key: HKDF-SHA-256(configured key, salt, INFO), non-extractable. */
|
|
201
|
+
function dataKey(base, salt, usage) {
|
|
202
|
+
return crypto.subtle.deriveKey({ name: "HKDF", hash: "SHA-256", salt, info: INFO }, base, { name: "AES-GCM", length: 256 }, false, [usage]);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Internal (exported for the tests): the 12-byte nonce of chunk `counter`: 7 zero bytes, the
|
|
206
|
+
* counter as a 32-bit big-endian integer, then 1 for the last chunk and 0 for the others. A
|
|
207
|
+
* counter outside 0 to 2^32 − 1 is refused (a `RangeError`): a nonce is never reused.
|
|
208
|
+
*/
|
|
209
|
+
export function chunkNonce(counter, last) {
|
|
210
|
+
if (!Number.isSafeInteger(counter) || counter < 0 || counter >= MAX_CHUNKS) {
|
|
211
|
+
throw new RangeError("the backup is too large for its nonce space");
|
|
212
|
+
}
|
|
213
|
+
const nonce = new Uint8Array(12);
|
|
214
|
+
new DataView(nonce.buffer).setUint32(7, counter, false);
|
|
215
|
+
nonce[11] = last ? 1 : 0;
|
|
216
|
+
return nonce;
|
|
217
|
+
}
|
|
218
|
+
/** The header's length for `keyId` and `identity` (all ASCII: one byte per character). */
|
|
219
|
+
function headerLength(keyId, identity) {
|
|
220
|
+
return (FIXED_HEADER +
|
|
221
|
+
keyId.length +
|
|
222
|
+
SALT_SIZE +
|
|
223
|
+
1 +
|
|
224
|
+
identity.databaseId.length +
|
|
225
|
+
2 +
|
|
226
|
+
identity.key.length);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* magic ‖ version ‖ chunk size ‖ key id length ‖ key id ‖ salt ‖ database id length (1 byte) ‖
|
|
230
|
+
* database id ‖ object key length (2 bytes) ‖ object key. All of it is every chunk's associated
|
|
231
|
+
* data. Inputs are validated by the callers (ASCII only).
|
|
232
|
+
*/
|
|
233
|
+
function headerOf(keyId, salt, identity) {
|
|
234
|
+
const ascii = new TextEncoder();
|
|
235
|
+
const header = new Uint8Array(headerLength(keyId, identity));
|
|
236
|
+
const view = new DataView(header.buffer);
|
|
237
|
+
header.set(MAGIC, 0);
|
|
238
|
+
header[8] = FORMAT_VERSION;
|
|
239
|
+
view.setUint32(9, CHUNK_SIZE, false);
|
|
240
|
+
header[13] = keyId.length;
|
|
241
|
+
header.set(ascii.encode(keyId), FIXED_HEADER);
|
|
242
|
+
let at = FIXED_HEADER + keyId.length;
|
|
243
|
+
header.set(salt, at);
|
|
244
|
+
at += SALT_SIZE;
|
|
245
|
+
header[at] = identity.databaseId.length;
|
|
246
|
+
header.set(ascii.encode(identity.databaseId), at + 1);
|
|
247
|
+
at += 1 + identity.databaseId.length;
|
|
248
|
+
view.setUint16(at, identity.key.length, false);
|
|
249
|
+
header.set(ascii.encode(identity.key), at + 2);
|
|
250
|
+
return header;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* The exact size of an encrypted backup of `plaintextSize` bytes under `keyId`, written for
|
|
254
|
+
* `identity`: the header (49 + the lengths of the key id, the database id and the object key),
|
|
255
|
+
* the plaintext, and a 16-byte tag per chunk (⌊size / 64 KiB⌋ + 1 chunks: the last one is
|
|
256
|
+
* shorter than 64 KiB, possibly empty). `null` for a size that is not a whole number from 0 to
|
|
257
|
+
* `MAX_PLAINTEXT_SIZE`, a key id outside its pattern, or an invalid identity.
|
|
258
|
+
*/
|
|
259
|
+
export function encryptedSize(plaintextSize, keyId, identity) {
|
|
260
|
+
if (!Number.isSafeInteger(plaintextSize) || plaintextSize < 0)
|
|
261
|
+
return null;
|
|
262
|
+
if (plaintextSize > MAX_PLAINTEXT_SIZE)
|
|
263
|
+
return null;
|
|
264
|
+
if (typeof keyId !== "string" || !KEY_ID.test(keyId) || !isIdentity(identity))
|
|
265
|
+
return null;
|
|
266
|
+
const chunks = Math.floor(plaintextSize / CHUNK_SIZE) + 1;
|
|
267
|
+
return headerLength(keyId, identity) + plaintextSize + chunks * TAG_SIZE;
|
|
268
|
+
}
|
|
269
|
+
/** Thrown into the encrypting stream when the plaintext outgrows the nonce space. */
|
|
270
|
+
export class EncryptionTooLarge extends Error {
|
|
271
|
+
constructor() {
|
|
272
|
+
super("the backup is too large for its nonce space");
|
|
273
|
+
this.name = "EncryptionTooLarge";
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Internal (not exported from the package): a stream that encrypts a dump with the current key
|
|
278
|
+
* of `encryption`, for the backup `identity` (written into the header), re-chunked to exactly
|
|
279
|
+
* 64 KiB whatever sizes arrive; an empty dump gives the header and one authenticated, empty last
|
|
280
|
+
* chunk. The salt is always 32 fresh random bytes: there is no way to pass one (the tests pin
|
|
281
|
+
* the format by decrypting fixed vectors and by recomputing this stream's output, from the salt
|
|
282
|
+
* it wrote, with an independent implementation).
|
|
283
|
+
*/
|
|
284
|
+
export function createEncryptStream(encryption, identity) {
|
|
285
|
+
if (!isBackupEncryption(encryption)) {
|
|
286
|
+
throw new TypeError("createEncryptStream: encryption must come from backupEncryption.");
|
|
287
|
+
}
|
|
288
|
+
if (!isIdentity(identity)) {
|
|
289
|
+
throw new TypeError("createEncryptStream: identity must be a valid { databaseId, key }.");
|
|
290
|
+
}
|
|
291
|
+
const salt = crypto.getRandomValues(new Uint8Array(SALT_SIZE));
|
|
292
|
+
const header = headerOf(encryption.current, salt, identity);
|
|
293
|
+
const base = baseKey(encryption, encryption.current);
|
|
294
|
+
let key;
|
|
295
|
+
const buffer = new Uint8Array(CHUNK_SIZE);
|
|
296
|
+
let filled = 0;
|
|
297
|
+
let counter = 0;
|
|
298
|
+
async function seal(last) {
|
|
299
|
+
if (counter >= MAX_CHUNKS)
|
|
300
|
+
throw new EncryptionTooLarge();
|
|
301
|
+
const iv = chunkNonce(counter, last);
|
|
302
|
+
counter += 1;
|
|
303
|
+
const sealed = await crypto.subtle.encrypt({ name: "AES-GCM", iv, additionalData: header, tagLength: 128 }, key, buffer.subarray(0, filled));
|
|
304
|
+
filled = 0;
|
|
305
|
+
return new Uint8Array(sealed);
|
|
306
|
+
}
|
|
307
|
+
return new TransformStream({
|
|
308
|
+
async start(controller) {
|
|
309
|
+
key = await dataKey(await base, salt, "encrypt");
|
|
310
|
+
controller.enqueue(header.slice());
|
|
311
|
+
},
|
|
312
|
+
async transform(chunk, controller) {
|
|
313
|
+
if (!(chunk instanceof Uint8Array))
|
|
314
|
+
throw new TypeError("expected bytes");
|
|
315
|
+
let offset = 0;
|
|
316
|
+
while (offset < chunk.byteLength) {
|
|
317
|
+
const take = Math.min(CHUNK_SIZE - filled, chunk.byteLength - offset);
|
|
318
|
+
buffer.set(chunk.subarray(offset, offset + take), filled);
|
|
319
|
+
filled += take;
|
|
320
|
+
offset += take;
|
|
321
|
+
// A full chunk is never the last: the last one is always shorter (possibly empty).
|
|
322
|
+
if (filled === CHUNK_SIZE)
|
|
323
|
+
controller.enqueue(await seal(false));
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
async flush(controller) {
|
|
327
|
+
controller.enqueue(await seal(true));
|
|
328
|
+
buffer.fill(0);
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
const DECRYPT_MESSAGES = {
|
|
333
|
+
not_encrypted: "the backup is not encrypted",
|
|
334
|
+
encrypted: "the backup is encrypted; pass its keys",
|
|
335
|
+
unsupported_format: "the backup's encryption format is not supported by this version",
|
|
336
|
+
invalid_header: "the backup's encryption header is invalid",
|
|
337
|
+
unknown_key: "the backup was encrypted with a key that was not given",
|
|
338
|
+
authentication_failed: "the backup failed authentication (wrong key or modified data)",
|
|
339
|
+
truncated: "the backup is truncated",
|
|
340
|
+
too_large: "the backup is too large for its nonce space",
|
|
341
|
+
wrong_backup: "the backup is authentic but is not the backup expected",
|
|
342
|
+
};
|
|
343
|
+
/** A backup that cannot be decrypted. The message is fixed per `reason`; nothing is echoed. */
|
|
344
|
+
export class BackupDecryptError extends Error {
|
|
345
|
+
reason;
|
|
346
|
+
constructor(reason) {
|
|
347
|
+
super(`backup decryption failed: ${DECRYPT_MESSAGES[reason]} (${reason})`);
|
|
348
|
+
this.name = "BackupDecryptError";
|
|
349
|
+
this.reason = reason;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/** Bytes held until a whole header or chunk is there; `take` copies exactly `n` out. */
|
|
353
|
+
class ByteQueue {
|
|
354
|
+
parts = [];
|
|
355
|
+
head = 0;
|
|
356
|
+
length = 0;
|
|
357
|
+
push(bytes) {
|
|
358
|
+
if (bytes.byteLength === 0)
|
|
359
|
+
return;
|
|
360
|
+
this.parts.push(bytes);
|
|
361
|
+
this.length += bytes.byteLength;
|
|
362
|
+
}
|
|
363
|
+
/** The first `n` bytes (at most `length`), without consuming them. */
|
|
364
|
+
peek(n) {
|
|
365
|
+
return this.read(n, false);
|
|
366
|
+
}
|
|
367
|
+
take(n) {
|
|
368
|
+
return this.read(n, true);
|
|
369
|
+
}
|
|
370
|
+
read(n, consume) {
|
|
371
|
+
const out = new Uint8Array(Math.min(n, this.length));
|
|
372
|
+
let written = 0;
|
|
373
|
+
let index = 0;
|
|
374
|
+
let head = this.head;
|
|
375
|
+
while (written < out.byteLength) {
|
|
376
|
+
const part = this.parts[index];
|
|
377
|
+
const take = Math.min(part.byteLength - head, out.byteLength - written);
|
|
378
|
+
out.set(part.subarray(head, head + take), written);
|
|
379
|
+
written += take;
|
|
380
|
+
head += take;
|
|
381
|
+
if (head === part.byteLength) {
|
|
382
|
+
index += 1;
|
|
383
|
+
head = 0;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (consume) {
|
|
387
|
+
this.parts.splice(0, index);
|
|
388
|
+
this.head = head;
|
|
389
|
+
this.length -= written;
|
|
390
|
+
}
|
|
391
|
+
return out;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
/** Whether `bytes` start like an encrypted backup (its magic). */
|
|
395
|
+
export function startsEncrypted(bytes) {
|
|
396
|
+
return bytes.byteLength >= MAGIC.byteLength && sameBytes(bytes.subarray(0, 8), MAGIC);
|
|
397
|
+
}
|
|
398
|
+
/** How many first bytes of an object `claimedKeyId` needs at most: the fixed header and a key id. */
|
|
399
|
+
export const KEY_ID_PROBE_BYTES = FIXED_HEADER + MAX_KEY_ID;
|
|
400
|
+
/**
|
|
401
|
+
* The key id an encrypted backup's header **claims**, read from the object's first bytes (at
|
|
402
|
+
* least `KEY_ID_PROBE_BYTES`, or the whole object), **without a key and without authenticating
|
|
403
|
+
* anything**: whoever can write the bucket can write any header. `null` when the bytes do not
|
|
404
|
+
* start like this format (a plain dump, another file). Use it to find which keys objects say they
|
|
405
|
+
* need (a range read of the first bytes of each), never as proof.
|
|
406
|
+
*/
|
|
407
|
+
export function claimedKeyId(firstBytes) {
|
|
408
|
+
if (!(firstBytes instanceof Uint8Array) || !startsEncrypted(firstBytes))
|
|
409
|
+
return null;
|
|
410
|
+
if (firstBytes.byteLength < FIXED_HEADER)
|
|
411
|
+
return null;
|
|
412
|
+
const view = new DataView(firstBytes.buffer, firstBytes.byteOffset, firstBytes.byteLength);
|
|
413
|
+
if (firstBytes[8] !== FORMAT_VERSION || view.getUint32(9, false) !== CHUNK_SIZE)
|
|
414
|
+
return null;
|
|
415
|
+
const length = firstBytes[13];
|
|
416
|
+
if (length < 1 || length > MAX_KEY_ID || firstBytes.byteLength < FIXED_HEADER + length) {
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
419
|
+
const id = String.fromCharCode(...firstBytes.subarray(FIXED_HEADER, FIXED_HEADER + length));
|
|
420
|
+
return KEY_ID.test(id) ? id : null;
|
|
421
|
+
}
|
|
422
|
+
const ascii = (bytes) => String.fromCharCode(...bytes);
|
|
423
|
+
/**
|
|
424
|
+
* Parses the header at the start of `start` (the first bytes of the object, at most
|
|
425
|
+
* `MAX_HEADER`): the header, or `null` when more bytes are needed. Throws a `BackupDecryptError`
|
|
426
|
+
* for anything malformed. Nothing in it is trusted before the first chunk authenticated.
|
|
427
|
+
*/
|
|
428
|
+
function parseHeader(start) {
|
|
429
|
+
const magic = start.subarray(0, Math.min(start.byteLength, MAGIC.byteLength));
|
|
430
|
+
if (!sameBytes(magic, MAGIC.subarray(0, magic.byteLength))) {
|
|
431
|
+
throw new BackupDecryptError("not_encrypted");
|
|
432
|
+
}
|
|
433
|
+
if (start.byteLength < FIXED_HEADER)
|
|
434
|
+
return null;
|
|
435
|
+
const view = new DataView(start.buffer, start.byteOffset, start.byteLength);
|
|
436
|
+
if (start[8] !== FORMAT_VERSION || view.getUint32(9, false) !== CHUNK_SIZE) {
|
|
437
|
+
throw new BackupDecryptError("unsupported_format");
|
|
438
|
+
}
|
|
439
|
+
const idLength = start[13];
|
|
440
|
+
if (idLength < 1 || idLength > MAX_KEY_ID)
|
|
441
|
+
throw new BackupDecryptError("invalid_header");
|
|
442
|
+
let at = FIXED_HEADER + idLength + SALT_SIZE;
|
|
443
|
+
if (start.byteLength < at + 1)
|
|
444
|
+
return null;
|
|
445
|
+
const databaseIdLength = start[at];
|
|
446
|
+
if (databaseIdLength < 1 || databaseIdLength > 64) {
|
|
447
|
+
throw new BackupDecryptError("invalid_header");
|
|
448
|
+
}
|
|
449
|
+
at += 1 + databaseIdLength;
|
|
450
|
+
if (start.byteLength < at + 2)
|
|
451
|
+
return null;
|
|
452
|
+
const keyLength = view.getUint16(at, false);
|
|
453
|
+
if (keyLength < 1 || keyLength > 1024)
|
|
454
|
+
throw new BackupDecryptError("invalid_header");
|
|
455
|
+
const size = at + 2 + keyLength;
|
|
456
|
+
if (start.byteLength < size)
|
|
457
|
+
return null;
|
|
458
|
+
const keyId = ascii(start.subarray(FIXED_HEADER, FIXED_HEADER + idLength));
|
|
459
|
+
const salt = start.slice(FIXED_HEADER + idLength, FIXED_HEADER + idLength + SALT_SIZE);
|
|
460
|
+
const databaseIdAt = FIXED_HEADER + idLength + SALT_SIZE + 1;
|
|
461
|
+
const identity = {
|
|
462
|
+
databaseId: ascii(start.subarray(databaseIdAt, databaseIdAt + databaseIdLength)),
|
|
463
|
+
key: ascii(start.subarray(at + 2, size)),
|
|
464
|
+
};
|
|
465
|
+
if (!KEY_ID.test(keyId) || !isIdentity(identity))
|
|
466
|
+
throw new BackupDecryptError("invalid_header");
|
|
467
|
+
return { bytes: start.slice(0, size), keyId, salt, identity };
|
|
468
|
+
}
|
|
469
|
+
function validExpected(expected) {
|
|
470
|
+
if (expected === undefined)
|
|
471
|
+
return {};
|
|
472
|
+
if (typeof expected !== "object" || expected === null) {
|
|
473
|
+
throw new TypeError("decryptBackup: expected must be { databaseId?, key?, keyId? }.");
|
|
474
|
+
}
|
|
475
|
+
for (const field of ["databaseId", "key", "keyId"]) {
|
|
476
|
+
const value = expected[field];
|
|
477
|
+
if (value !== undefined && typeof value !== "string") {
|
|
478
|
+
throw new TypeError("decryptBackup: expected must be { databaseId?, key?, keyId? }.");
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return expected;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* A stream that decrypts an encrypted backup (the object's bytes, from R2 or a file) with the
|
|
485
|
+
* keys given; the header names the key id. Each chunk's plaintext is released only after its tag
|
|
486
|
+
* verified.
|
|
487
|
+
*
|
|
488
|
+
* The header also names what the backup is of (database id and object key). It is trusted only
|
|
489
|
+
* once the first chunk authenticated: then, before any plaintext is released, it is compared
|
|
490
|
+
* with `expected` (any of `databaseId`, `key`, `keyId`), and `authenticated` resolves with it.
|
|
491
|
+
* Pass what you know: an authentic backup of another database, environment or date under the
|
|
492
|
+
* same key would otherwise decrypt fine in its place.
|
|
493
|
+
*
|
|
494
|
+
* The stream errors with a `BackupDecryptError` (fixed message, a `reason`) on the first
|
|
495
|
+
* problem, releasing nothing of the failed chunk: a plain dump (`not_encrypted`), an unknown
|
|
496
|
+
* format (`unsupported_format`, `invalid_header`), a key that was not given (`unknown_key`), a
|
|
497
|
+
* modified, reordered, duplicated or dropped chunk, a wrong key, a cut inside a chunk that
|
|
498
|
+
* leaves at least 16 bytes, or bytes after the last chunk (`authentication_failed`), an object
|
|
499
|
+
* that ends before its last chunk (`truncated`), another backup than expected (`wrong_backup`).
|
|
500
|
+
* Chunks before the failure were authentic and were already released: on an error, discard
|
|
501
|
+
* everything written (the README's script writes to a `.partial` file and deletes it).
|
|
502
|
+
*
|
|
503
|
+
* Throws a `TypeError` (never including a key) for invalid keys or `expected`.
|
|
504
|
+
*/
|
|
505
|
+
export function decryptBackup(keys, options = {}) {
|
|
506
|
+
const ring = resolveDecryption(keys, "decryptBackup");
|
|
507
|
+
if (typeof options !== "object" || options === null) {
|
|
508
|
+
throw new TypeError("decryptBackup: options must be { expected? }.");
|
|
509
|
+
}
|
|
510
|
+
const expected = validExpected(options.expected);
|
|
511
|
+
const queue = new ByteQueue();
|
|
512
|
+
let header = null;
|
|
513
|
+
let key = null;
|
|
514
|
+
let counter = 0;
|
|
515
|
+
let resolveAuthenticated = () => undefined;
|
|
516
|
+
let rejectAuthenticated = () => undefined;
|
|
517
|
+
const authenticated = new Promise((resolve, reject) => {
|
|
518
|
+
resolveAuthenticated = resolve;
|
|
519
|
+
rejectAuthenticated = reject;
|
|
520
|
+
});
|
|
521
|
+
// Rejected along with the stream: whoever does not await it must not see an unhandled one.
|
|
522
|
+
authenticated.catch(() => undefined);
|
|
523
|
+
/** Reads the header once enough bytes are there; at the end, missing bytes are `truncated`. */
|
|
524
|
+
async function readHeader(atEnd) {
|
|
525
|
+
const parsed = parseHeader(queue.peek(MAX_HEADER));
|
|
526
|
+
if (parsed === null) {
|
|
527
|
+
if (atEnd)
|
|
528
|
+
throw new BackupDecryptError("truncated");
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
const base = baseKey(ring, parsed.keyId);
|
|
532
|
+
if (base === null)
|
|
533
|
+
throw new BackupDecryptError("unknown_key");
|
|
534
|
+
key = await dataKey(await base, parsed.salt, "decrypt");
|
|
535
|
+
queue.take(parsed.bytes.byteLength);
|
|
536
|
+
header = parsed;
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
async function open(sealed, last) {
|
|
540
|
+
if (counter >= MAX_CHUNKS)
|
|
541
|
+
throw new BackupDecryptError("too_large");
|
|
542
|
+
const first = counter === 0;
|
|
543
|
+
const iv = chunkNonce(counter, last);
|
|
544
|
+
counter += 1;
|
|
545
|
+
const parsed = header;
|
|
546
|
+
let plain;
|
|
547
|
+
try {
|
|
548
|
+
plain = new Uint8Array(await crypto.subtle.decrypt({ name: "AES-GCM", iv, additionalData: parsed.bytes, tagLength: 128 }, key, sealed));
|
|
549
|
+
}
|
|
550
|
+
catch {
|
|
551
|
+
throw new BackupDecryptError("authentication_failed");
|
|
552
|
+
}
|
|
553
|
+
if (first) {
|
|
554
|
+
// The header is authentic now (it is this chunk's associated data): check it before the
|
|
555
|
+
// first plaintext leaves.
|
|
556
|
+
const found = { keyId: parsed.keyId, ...parsed.identity };
|
|
557
|
+
if ((expected.databaseId !== undefined && expected.databaseId !== found.databaseId) ||
|
|
558
|
+
(expected.key !== undefined && expected.key !== found.key) ||
|
|
559
|
+
(expected.keyId !== undefined && expected.keyId !== found.keyId)) {
|
|
560
|
+
plain.fill(0);
|
|
561
|
+
throw new BackupDecryptError("wrong_backup");
|
|
562
|
+
}
|
|
563
|
+
resolveAuthenticated(found);
|
|
564
|
+
}
|
|
565
|
+
return plain;
|
|
566
|
+
}
|
|
567
|
+
/** Runs a step of the stream; its error also rejects `authenticated`. */
|
|
568
|
+
async function guarded(step) {
|
|
569
|
+
try {
|
|
570
|
+
await step();
|
|
571
|
+
}
|
|
572
|
+
catch (error) {
|
|
573
|
+
rejectAuthenticated(error);
|
|
574
|
+
throw error;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
const stream = new TransformStream({
|
|
578
|
+
// The writable side aborted (the source failed, say) or the readable side cancelled: the
|
|
579
|
+
// first chunk will never authenticate, so `authenticated` is settled with that reason.
|
|
580
|
+
// Runtimes that predate the `cancel` hook ignore it: see `DecryptBackupStream`.
|
|
581
|
+
cancel: (reason) => {
|
|
582
|
+
rejectAuthenticated(reason ?? new BackupDecryptError("truncated"));
|
|
583
|
+
},
|
|
584
|
+
transform: (chunk, controller) => guarded(async () => {
|
|
585
|
+
if (!(chunk instanceof Uint8Array)) {
|
|
586
|
+
throw new TypeError("decryptBackup: the stream must carry bytes (Uint8Array).");
|
|
587
|
+
}
|
|
588
|
+
queue.push(chunk);
|
|
589
|
+
if (header === null && !(await readHeader(false)))
|
|
590
|
+
return;
|
|
591
|
+
// A whole sealed chunk is never the last one (the last is always shorter): open it now.
|
|
592
|
+
while (queue.length >= CHUNK_SIZE + TAG_SIZE) {
|
|
593
|
+
controller.enqueue(await open(queue.take(CHUNK_SIZE + TAG_SIZE), false));
|
|
594
|
+
}
|
|
595
|
+
}),
|
|
596
|
+
flush: (controller) => guarded(async () => {
|
|
597
|
+
if (header === null)
|
|
598
|
+
await readHeader(true);
|
|
599
|
+
// The last chunk is at least its tag: nothing, or fewer than 16 bytes, is a cut.
|
|
600
|
+
if (queue.length < TAG_SIZE)
|
|
601
|
+
throw new BackupDecryptError("truncated");
|
|
602
|
+
const plain = await open(queue.take(queue.length), true);
|
|
603
|
+
if (plain.byteLength > 0)
|
|
604
|
+
controller.enqueue(plain);
|
|
605
|
+
}),
|
|
606
|
+
});
|
|
607
|
+
return Object.assign(stream, { authenticated });
|
|
608
|
+
}
|
|
609
|
+
//# sourceMappingURL=encryption.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encryption.js","sourceRoot":"","sources":["../src/encryption.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,iFAAiF;AACjF,MAAM,CAAC,MAAM,iBAAiB,GAAG,uBAAuB,CAAC;AAGzD,6FAA6F;AAC7F,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAC/E,MAAM,cAAc,GAAG,CAAC,CAAC;AACzB,mFAAmF;AACnF,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC;AACjC,MAAM,QAAQ,GAAG,EAAE,CAAC;AACpB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,QAAQ,GAAG,EAAE,CAAC;AACpB,oEAAoE;AACpE,MAAM,YAAY,GAAG,EAAE,CAAC;AACxB,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,8FAA8F;AAC9F,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,CAAC;AAC3B,kFAAkF;AAClF,MAAM,CAAC,MAAM,kBAAkB,GAAG,UAAU,GAAG,UAAU,GAAG,CAAC,CAAC;AAC9D,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,sCAAsC,CAAC,CAAC;AAC9E,iFAAiF;AACjF,MAAM,MAAM,GAAG,4BAA4B,CAAC;AAC5C,4FAA4F;AAC5F,MAAM,mBAAmB,GAAG,oCAAoC,CAAC;AACjE,qFAAqF;AACrF,MAAM,WAAW,GAAG,2BAA2B,CAAC;AAChD,2FAA2F;AAC3F,MAAM,UAA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