@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
package/FORMAT.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Encrypted backup format, version 1
|
|
2
|
+
|
|
3
|
+
The byte layout of a backup written by `@cardonjs/backups` with `encryption` (scheme
|
|
4
|
+
`aes-256-gcm-stream-v1`). Enough to write an independent decrypter; two full vectors are at the
|
|
5
|
+
end of this file.
|
|
6
|
+
|
|
7
|
+
All integers are big-endian. `||` is concatenation. Every text field is ASCII.
|
|
8
|
+
|
|
9
|
+
## Object
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
object = header || chunk_0 || chunk_1 || … || chunk_(n-1)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Header
|
|
16
|
+
|
|
17
|
+
| Offset | Size | Field | Value |
|
|
18
|
+
| ---------------- | ---- | ------------------ | ------------------------------------------------------ |
|
|
19
|
+
| 0 | 8 | magic | ASCII `CARDONBK` (`43 41 52 44 4F 4E 42 4B`) |
|
|
20
|
+
| 8 | 1 | version | `0x01` |
|
|
21
|
+
| 9 | 4 | chunk size | `0x00010000` (65 536): plaintext bytes per chunk |
|
|
22
|
+
| 13 | 1 | key id length | `L`, 1 to 32 |
|
|
23
|
+
| 14 | L | key id | `^[a-z0-9][a-z0-9_-]{0,31}$` |
|
|
24
|
+
| 14 + L | 32 | salt | 32 random bytes, new for every backup |
|
|
25
|
+
| 46 + L | 1 | database id length | `D`, 1 to 64 |
|
|
26
|
+
| 47 + L | D | database id | `^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$` (a D1 id: a UUID) |
|
|
27
|
+
| 47 + L + D | 2 | object key length | `K`, 1 to 1024 |
|
|
28
|
+
| 49 + L + D | K | object key | `^[A-Za-z0-9._/-]{1,1024}$`: the full R2 key |
|
|
29
|
+
|
|
30
|
+
The header is `49 + L + D + K` bytes. The database id and the object key (the **context**) say
|
|
31
|
+
what the backup is of: the D1 database it was exported from and the R2 key the copy wrote it
|
|
32
|
+
to. A reader refuses any other magic (`not_encrypted`), version or chunk size
|
|
33
|
+
(`unsupported_format`), a length out of its range or a field outside its pattern
|
|
34
|
+
(`invalid_header`), and a key id it was not given (`unknown_key`).
|
|
35
|
+
|
|
36
|
+
The whole header, byte for byte, is the associated data of every chunk, so changing any of it
|
|
37
|
+
(the salt and the context included) makes every chunk fail authentication. **Nothing in the
|
|
38
|
+
header is trusted before the first chunk's tag verified**: only then does a reader compare the
|
|
39
|
+
context with what it expects (`wrong_backup` on a mismatch) and report it, and only then does
|
|
40
|
+
it release that chunk's plaintext.
|
|
41
|
+
|
|
42
|
+
### Keys
|
|
43
|
+
|
|
44
|
+
- **Configured key** `K`: 32 random bytes, given to the package as base64url (43 characters,
|
|
45
|
+
RFC 4648 §5, no padding; one `=` is accepted), under its key id.
|
|
46
|
+
- **Data key** of the backup: `HKDF-SHA-256(IKM = K, salt = header.salt, info = "cardon-backups/aes-256-gcm-stream/v1", L = 32)`
|
|
47
|
+
(RFC 5869; `info` is those 36 ASCII bytes), used as an AES-256 key. The configured key never
|
|
48
|
+
encrypts data directly, and a fresh salt gives every backup its own data key, so nonces never
|
|
49
|
+
repeat under one key.
|
|
50
|
+
|
|
51
|
+
### Chunks
|
|
52
|
+
|
|
53
|
+
The plaintext (the SQL dump) is cut into chunks of exactly 65 536 bytes; the last chunk holds
|
|
54
|
+
the rest, **0 to 65 535 bytes**. So there are `⌊P / 65536⌋ + 1` chunks for a plaintext of `P`
|
|
55
|
+
bytes: an empty dump is one empty last chunk, and a dump whose size is a multiple of 65 536 ends
|
|
56
|
+
with an empty last chunk. A full-size chunk is never the last one.
|
|
57
|
+
|
|
58
|
+
```text
|
|
59
|
+
chunk_i = AES-256-GCM-Encrypt(key = data key, nonce = nonce_i, aad = header, plaintext_i)
|
|
60
|
+
= ciphertext_i (same length as plaintext_i) || tag_i (16 bytes)
|
|
61
|
+
|
|
62
|
+
nonce_i = 0x00 × 7 || uint32(i) || last_i (12 bytes)
|
|
63
|
+
last_i = 0x01 for the last chunk, 0x00 for every other
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
This is the STREAM construction (Hoang, Reyhanitabar, Rogaway, Vizár, 2015): the counter in
|
|
67
|
+
the nonce makes reordering, dropping or duplicating a chunk fail authentication, and the
|
|
68
|
+
last-chunk flag makes truncation fail. Every chunk but the last is 65 552 bytes on disk; the
|
|
69
|
+
last one is 16 to 65 551 bytes.
|
|
70
|
+
|
|
71
|
+
### Size
|
|
72
|
+
|
|
73
|
+
```text
|
|
74
|
+
size(P) = (49 + L + D + K) + P + 16 × (⌊P / 65536⌋ + 1)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The counter is 32 bits, so a backup has at most 2³² chunks: a plaintext of at most
|
|
78
|
+
`2^48 − 1` bytes (just under 256 TiB). The writer refuses to go beyond (`too_large`); a reader
|
|
79
|
+
refuses a counter that would overflow. In practice the copy's own limits are far lower
|
|
80
|
+
(`partSize × 10 000`, at most about 312 GiB), and R2's object limit lower still.
|
|
81
|
+
|
|
82
|
+
## Reading
|
|
83
|
+
|
|
84
|
+
1. Read and check the header; derive the data key from the configured key named by the key id.
|
|
85
|
+
2. While at least 65 552 bytes remain, take 65 552 bytes as a middle chunk (`last = 0`),
|
|
86
|
+
decrypt and verify it, and only then release its plaintext (even when they are the last
|
|
87
|
+
65 552 bytes of the object: a full chunk is never the last one, so the object is then cut).
|
|
88
|
+
3. At the end of the object, what remains is the last chunk (`last = 1`). **Fewer than 16
|
|
89
|
+
bytes** (nothing at all, or 1 to 15 bytes) cannot even hold its tag: `truncated`. 16 bytes or
|
|
90
|
+
more: decrypt and verify it.
|
|
91
|
+
4. After the first chunk (middle or last) verified, and before releasing its plaintext, compare
|
|
92
|
+
the header's context with what is expected.
|
|
93
|
+
|
|
94
|
+
Since a whole chunk is never the last one, a reader can decrypt each full chunk as soon as it
|
|
95
|
+
has it, without looking ahead. Outcomes:
|
|
96
|
+
|
|
97
|
+
- `truncated`: the object ends inside the header, exactly at a chunk boundary, or with fewer
|
|
98
|
+
than 16 bytes of the last chunk.
|
|
99
|
+
- `authentication_failed`: a tag that does not verify, releasing nothing of that chunk. It is
|
|
100
|
+
what a modified byte, a wrong key, swapped, dropped or duplicated chunks, a moved last-chunk
|
|
101
|
+
flag, bytes after the last chunk, and a cut that leaves 16 bytes or more of a chunk all look
|
|
102
|
+
like (AES-GCM cannot tell them apart).
|
|
103
|
+
- `wrong_backup`: an authentic backup whose context (or key id) is not the expected one.
|
|
104
|
+
|
|
105
|
+
Chunks released before an error were authentic, but the object as a whole is not: discard
|
|
106
|
+
everything written.
|
|
107
|
+
|
|
108
|
+
## Metadata (not part of the format)
|
|
109
|
+
|
|
110
|
+
The R2 object and its `<key>.sha256` sidecar carry `encryption: aes-256-gcm-stream-v1` and
|
|
111
|
+
`key-id: <id>` as custom metadata, and `bytes`, the sidecar's digest and the results' `size`
|
|
112
|
+
and `sha256` are of the encrypted object. None of them is needed to decrypt: the header says
|
|
113
|
+
which key, and what the backup is of.
|
|
114
|
+
|
|
115
|
+
## Vectors
|
|
116
|
+
|
|
117
|
+
Key `000102…1f` (base64url `AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8`), key id `k1`, salt
|
|
118
|
+
`a0a1…bf`, database id `0b6d4b4a-4e43-4a7c-9a4e-3b1f2a6c9d01`, object key
|
|
119
|
+
`d1/main/2026/11/2026-11-20T04-00-03Z.sql`. The header (127 bytes) is the same for both:
|
|
120
|
+
|
|
121
|
+
```text
|
|
122
|
+
434152444f4e424b magic
|
|
123
|
+
01 version
|
|
124
|
+
00010000 chunk size
|
|
125
|
+
02 6b31 key id "k1"
|
|
126
|
+
a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf salt
|
|
127
|
+
24 30623664346234612d346534332d346137632d396134652d336231663261366339643031
|
|
128
|
+
database id (36)
|
|
129
|
+
0028 64312f6d61696e2f323032362f31312f323032362d31312d32305430342d30302d30335a2e73716c
|
|
130
|
+
object key (40)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Empty plaintext: one empty last chunk, its tag only:
|
|
134
|
+
|
|
135
|
+
```text
|
|
136
|
+
ab48f8dede99786b37397a3e1e902928
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Plaintext `PRAGMA x;\n` (10 bytes): one last chunk, 10 bytes of ciphertext then the tag:
|
|
140
|
+
|
|
141
|
+
```text
|
|
142
|
+
c15e2f335c1018b02332 1c31d71770d5a31d36f975e51984c092
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Each object is the header followed by its chunk. The package's tests decrypt both, and check
|
|
146
|
+
that an independent implementation (`node:crypto`) reproduces them and every output of the
|
|
147
|
+
writer. Never use a fixed salt: the vectors fix it only to pin the format.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matias Terradas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|