decryption 0.0.0-stage → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +348 -2
- package/commands/account.d.ts +4 -0
- package/commands/account.js +470 -0
- package/commands/cosmology.d.ts +4 -0
- package/commands/cosmology.js +97 -0
- package/commands/doctor.d.ts +3 -0
- package/commands/doctor.js +42 -0
- package/commands/encrypt.d.ts +12 -0
- package/commands/encrypt.js +251 -0
- package/commands/guard.d.ts +3 -0
- package/commands/guard.js +92 -0
- package/commands/keychain.d.ts +4 -0
- package/commands/keychain.js +92 -0
- package/commands/keys.d.ts +4 -0
- package/commands/keys.js +74 -0
- package/commands/passkey.d.ts +4 -0
- package/commands/passkey.js +135 -0
- package/commands/salt.d.ts +4 -0
- package/commands/salt.js +113 -0
- package/commands/secrets.d.ts +4 -0
- package/commands/secrets.js +214 -0
- package/commands/shamir.d.ts +4 -0
- package/commands/shamir.js +62 -0
- package/commands/vault.d.ts +7 -0
- package/commands/vault.js +382 -0
- package/commands/wallet.d.ts +5 -0
- package/commands/wallet.js +129 -0
- package/commands/workspace.d.ts +8 -0
- package/commands/workspace.js +289 -0
- package/commands.d.ts +8 -0
- package/commands.js +126 -0
- package/esm/commands/account.js +466 -0
- package/esm/commands/cosmology.js +93 -0
- package/esm/commands/doctor.js +38 -0
- package/esm/commands/encrypt.js +245 -0
- package/esm/commands/guard.js +88 -0
- package/esm/commands/keychain.js +88 -0
- package/esm/commands/keys.js +70 -0
- package/esm/commands/passkey.js +131 -0
- package/esm/commands/salt.js +109 -0
- package/esm/commands/secrets.js +210 -0
- package/esm/commands/shamir.js +58 -0
- package/esm/commands/vault.js +376 -0
- package/esm/commands/wallet.js +125 -0
- package/esm/commands/workspace.js +283 -0
- package/esm/commands.js +120 -0
- package/esm/index.js +50 -0
- package/esm/utils/bytes.js +286 -0
- package/esm/utils/dispatch.js +52 -0
- package/esm/utils/env.js +44 -0
- package/esm/utils/errors.js +44 -0
- package/esm/utils/header.js +14 -0
- package/esm/utils/io.js +49 -0
- package/esm/utils/passphrase.js +49 -0
- package/esm/utils/recipients.js +74 -0
- package/esm/utils/stash.js +61 -0
- package/esm/utils/temp.js +51 -0
- package/esm/utils/text.js +2 -0
- package/esm/workspace/bundle.js +239 -0
- package/esm/workspace/doctor.js +104 -0
- package/esm/workspace/git.js +72 -0
- package/esm/workspace/guard.js +111 -0
- package/esm/workspace/layout.js +106 -0
- package/esm/workspace/manifest.js +147 -0
- package/esm/workspace/recipients.js +37 -0
- package/esm/workspace/snippets.js +54 -0
- package/esm/workspace/state.js +46 -0
- package/esm/workspace/status.js +93 -0
- package/esm/workspace/tar.js +235 -0
- package/index.d.ts +12 -0
- package/index.js +67 -0
- package/package.json +53 -4
- package/utils/bytes.d.ts +86 -0
- package/utils/bytes.js +301 -0
- package/utils/dispatch.d.ts +24 -0
- package/utils/dispatch.js +58 -0
- package/utils/env.d.ts +26 -0
- package/utils/env.js +49 -0
- package/utils/errors.d.ts +25 -0
- package/utils/errors.js +50 -0
- package/utils/header.d.ts +4 -0
- package/utils/header.js +18 -0
- package/utils/io.d.ts +10 -0
- package/utils/io.js +57 -0
- package/utils/passphrase.d.ts +15 -0
- package/utils/passphrase.js +53 -0
- package/utils/recipients.d.ts +28 -0
- package/utils/recipients.js +80 -0
- package/utils/stash.d.ts +40 -0
- package/utils/stash.js +76 -0
- package/utils/temp.d.ts +10 -0
- package/utils/temp.js +59 -0
- package/utils/text.d.ts +2 -0
- package/utils/text.js +6 -0
- package/workspace/bundle.d.ts +50 -0
- package/workspace/bundle.js +248 -0
- package/workspace/doctor.d.ts +8 -0
- package/workspace/doctor.js +108 -0
- package/workspace/git.d.ts +21 -0
- package/workspace/git.js +83 -0
- package/workspace/guard.d.ts +23 -0
- package/workspace/guard.js +118 -0
- package/workspace/layout.d.ts +47 -0
- package/workspace/layout.js +113 -0
- package/workspace/manifest.d.ts +52 -0
- package/workspace/manifest.js +157 -0
- package/workspace/recipients.d.ts +16 -0
- package/workspace/recipients.js +45 -0
- package/workspace/snippets.d.ts +29 -0
- package/workspace/snippets.js +58 -0
- package/workspace/state.d.ts +13 -0
- package/workspace/state.js +52 -0
- package/workspace/status.d.ts +54 -0
- package/workspace/status.js +99 -0
- package/workspace/tar.d.ts +29 -0
- package/workspace/tar.js +240 -0
package/workspace/tar.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MAX_TAR_FILE_SIZE = void 0;
|
|
4
|
+
exports.writeTar = writeTar;
|
|
5
|
+
exports.readTar = readTar;
|
|
6
|
+
const bytes_1 = require("../utils/bytes");
|
|
7
|
+
const errors_1 = require("../utils/errors");
|
|
8
|
+
/**
|
|
9
|
+
* A deterministic subset of POSIX tar (ustar, plus a pax `path` record where a name does not fit).
|
|
10
|
+
*
|
|
11
|
+
* The same files always produce the same bytes: entries are written in the order given, and
|
|
12
|
+
* every field that describes the machine rather than the content is fixed. There is no owner, no
|
|
13
|
+
* group, no timestamp, and the mode is one of two values. Only regular files are stored.
|
|
14
|
+
*
|
|
15
|
+
* The output is ordinary tar. `dcrypt decrypt --in section.tar.dcrypt --out section.tar` followed
|
|
16
|
+
* by `tar -xf` recovers a section without any of the code in this folder.
|
|
17
|
+
*/
|
|
18
|
+
const BLOCK_SIZE = 512;
|
|
19
|
+
const NAME_LENGTH = 100;
|
|
20
|
+
const MAX_PAX_LENGTH = 64 * 1024;
|
|
21
|
+
/** The largest file an 11-digit octal size field can describe: one byte under 8 GiB. */
|
|
22
|
+
exports.MAX_TAR_FILE_SIZE = 0o77777777777;
|
|
23
|
+
const TYPE_FILE = '0';
|
|
24
|
+
const TYPE_PAX = 'x';
|
|
25
|
+
const MODE_FILE = 0o644;
|
|
26
|
+
const MODE_EXECUTABLE = 0o755;
|
|
27
|
+
const OWNER_EXECUTE = 0o100;
|
|
28
|
+
/** Offset and length of each header field this format reads or writes. */
|
|
29
|
+
const FIELD = {
|
|
30
|
+
name: [0, NAME_LENGTH],
|
|
31
|
+
mode: [100, 8],
|
|
32
|
+
uid: [108, 8],
|
|
33
|
+
gid: [116, 8],
|
|
34
|
+
size: [124, 12],
|
|
35
|
+
mtime: [136, 12],
|
|
36
|
+
checksum: [148, 8],
|
|
37
|
+
type: [156, 1],
|
|
38
|
+
magic: [257, 6],
|
|
39
|
+
version: [263, 2],
|
|
40
|
+
prefix: [345, 155],
|
|
41
|
+
};
|
|
42
|
+
const corrupt = (message) => new errors_1.CliError(`the archive is corrupt: ${message}`, errors_1.EXIT.corrupt);
|
|
43
|
+
const paddingFor = (length) => (BLOCK_SIZE - (length % BLOCK_SIZE)) % BLOCK_SIZE;
|
|
44
|
+
/** Sum of the header's bytes, with the checksum field itself counted as spaces. */
|
|
45
|
+
const checksumOf = (block) => {
|
|
46
|
+
const [start, length] = FIELD.checksum;
|
|
47
|
+
let sum = 0;
|
|
48
|
+
for (let i = 0; i < BLOCK_SIZE; i++) {
|
|
49
|
+
sum += i >= start && i < start + length ? 0x20 : block[i];
|
|
50
|
+
}
|
|
51
|
+
return sum;
|
|
52
|
+
};
|
|
53
|
+
const octal = (value, [, length]) => value.toString(8).padStart(length - 1, '0') + '\0';
|
|
54
|
+
const headerBlock = (name, size, mode, type) => {
|
|
55
|
+
const block = Buffer.alloc(BLOCK_SIZE);
|
|
56
|
+
const put = (field, value) => void block.write(value, field[0], 'latin1');
|
|
57
|
+
put(FIELD.name, name);
|
|
58
|
+
put(FIELD.mode, octal(mode, FIELD.mode));
|
|
59
|
+
put(FIELD.uid, octal(0, FIELD.uid));
|
|
60
|
+
put(FIELD.gid, octal(0, FIELD.gid));
|
|
61
|
+
put(FIELD.size, octal(size, FIELD.size));
|
|
62
|
+
put(FIELD.mtime, octal(0, FIELD.mtime));
|
|
63
|
+
put(FIELD.type, type);
|
|
64
|
+
put(FIELD.magic, 'ustar\0');
|
|
65
|
+
put(FIELD.version, '00');
|
|
66
|
+
put(FIELD.checksum, checksumOf(block).toString(8).padStart(6, '0') + '\0 ');
|
|
67
|
+
return block;
|
|
68
|
+
};
|
|
69
|
+
/** True when `path` can be stored in the 100-byte name field as it is. */
|
|
70
|
+
const fitsNameField = (path) => /^[\x20-\x7e]{1,100}$/.test(path);
|
|
71
|
+
/** A pax record is `<length> <key>=<value>\n`, where the length counts its own digits. */
|
|
72
|
+
const paxRecord = (key, value) => {
|
|
73
|
+
const body = Buffer.from(` ${key}=${value}\n`, 'utf8');
|
|
74
|
+
let length = body.length;
|
|
75
|
+
while (String(length).length + body.length !== length) {
|
|
76
|
+
length = String(length).length + body.length;
|
|
77
|
+
}
|
|
78
|
+
return Buffer.concat([Buffer.from(String(length), 'latin1'), body]);
|
|
79
|
+
};
|
|
80
|
+
/** The header blocks that introduce one file: a pax header first when the path needs one. */
|
|
81
|
+
const entryHeader = ({ path, size, executable }) => {
|
|
82
|
+
const mode = executable ? MODE_EXECUTABLE : MODE_FILE;
|
|
83
|
+
if (fitsNameField(path))
|
|
84
|
+
return headerBlock(path, size, mode, TYPE_FILE);
|
|
85
|
+
const record = paxRecord('path', path);
|
|
86
|
+
// What a reader without pax support would see: the tail of the path, in printable ASCII.
|
|
87
|
+
const fallback = path.replace(/[^\x20-\x7e]/g, '_').slice(-NAME_LENGTH);
|
|
88
|
+
return Buffer.concat([
|
|
89
|
+
headerBlock('PaxHeader', record.length, MODE_FILE, TYPE_PAX),
|
|
90
|
+
record,
|
|
91
|
+
Buffer.alloc(paddingFor(record.length)),
|
|
92
|
+
headerBlock(fallback, size, mode, TYPE_FILE),
|
|
93
|
+
]);
|
|
94
|
+
};
|
|
95
|
+
/** Writes `files` as a tar archive, in the order given. */
|
|
96
|
+
async function* writeTar(files) {
|
|
97
|
+
for await (const file of files) {
|
|
98
|
+
if (!Number.isSafeInteger(file.size) || file.size < 0 || file.size > exports.MAX_TAR_FILE_SIZE) {
|
|
99
|
+
throw new errors_1.CliError(`${file.path} is too large to archive (the limit is 8 GiB per file)`);
|
|
100
|
+
}
|
|
101
|
+
yield entryHeader(file);
|
|
102
|
+
let written = 0;
|
|
103
|
+
for await (const piece of file.content) {
|
|
104
|
+
written += piece.length;
|
|
105
|
+
if (written > file.size)
|
|
106
|
+
break;
|
|
107
|
+
yield piece;
|
|
108
|
+
}
|
|
109
|
+
if (written !== file.size) {
|
|
110
|
+
throw new errors_1.CliError(`${file.path} changed size while it was being archived; run the command again`);
|
|
111
|
+
}
|
|
112
|
+
if (paddingFor(file.size))
|
|
113
|
+
yield Buffer.alloc(paddingFor(file.size));
|
|
114
|
+
}
|
|
115
|
+
yield Buffer.alloc(2 * BLOCK_SIZE);
|
|
116
|
+
}
|
|
117
|
+
const takeBlock = async (reader) => {
|
|
118
|
+
const held = await reader.fill(BLOCK_SIZE);
|
|
119
|
+
if (held.length < BLOCK_SIZE)
|
|
120
|
+
throw corrupt('it ends in the middle of a block');
|
|
121
|
+
const block = held.subarray(0, BLOCK_SIZE);
|
|
122
|
+
reader.skip(BLOCK_SIZE);
|
|
123
|
+
return block;
|
|
124
|
+
};
|
|
125
|
+
/** Streams the next `count.remaining` bytes, keeping the count current as they are handed out. */
|
|
126
|
+
async function* takeBytes(reader, count) {
|
|
127
|
+
while (count.remaining > 0) {
|
|
128
|
+
const held = await reader.fill(1);
|
|
129
|
+
if (!held.length)
|
|
130
|
+
throw corrupt('it ends in the middle of a file');
|
|
131
|
+
const piece = held.subarray(0, Math.min(held.length, count.remaining));
|
|
132
|
+
reader.skip(piece.length);
|
|
133
|
+
count.remaining -= piece.length;
|
|
134
|
+
yield piece;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const skipBytes = async (reader, length) => {
|
|
138
|
+
for await (const piece of takeBytes(reader, { remaining: length }))
|
|
139
|
+
void piece;
|
|
140
|
+
};
|
|
141
|
+
const textOf = (block, [offset, length]) => {
|
|
142
|
+
const end = block.indexOf(0, offset);
|
|
143
|
+
const stop = end === -1 || end > offset + length ? offset + length : end;
|
|
144
|
+
return block.toString('utf8', offset, stop);
|
|
145
|
+
};
|
|
146
|
+
const numberOf = (block, field, name) => {
|
|
147
|
+
const digits = textOf(block, field).trim();
|
|
148
|
+
if (!/^[0-7]*$/.test(digits))
|
|
149
|
+
throw corrupt(`a header has an unreadable ${name}`);
|
|
150
|
+
return parseInt(digits || '0', 8);
|
|
151
|
+
};
|
|
152
|
+
const parseHeader = (block) => {
|
|
153
|
+
if (textOf(block, FIELD.magic) !== 'ustar')
|
|
154
|
+
throw corrupt('a header is not in ustar format');
|
|
155
|
+
if (numberOf(block, FIELD.checksum, 'checksum') !== checksumOf(block)) {
|
|
156
|
+
throw corrupt('a header fails its checksum');
|
|
157
|
+
}
|
|
158
|
+
const name = textOf(block, FIELD.name);
|
|
159
|
+
const prefix = textOf(block, FIELD.prefix);
|
|
160
|
+
return {
|
|
161
|
+
path: prefix ? `${prefix}/${name}` : name,
|
|
162
|
+
size: numberOf(block, FIELD.size, 'size'),
|
|
163
|
+
mode: numberOf(block, FIELD.mode, 'mode'),
|
|
164
|
+
// Archives older than ustar mark a regular file with a NUL instead of "0".
|
|
165
|
+
type: textOf(block, FIELD.type) || TYPE_FILE,
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
/** Reads the one pax record this format writes. Anything else is refused, not skipped. */
|
|
169
|
+
const paxPath = (data) => {
|
|
170
|
+
let path;
|
|
171
|
+
for (let offset = 0; offset < data.length;) {
|
|
172
|
+
const space = data.indexOf(0x20, offset);
|
|
173
|
+
const length = space === -1 ? NaN : Number(data.toString('latin1', offset, space));
|
|
174
|
+
const end = offset + length;
|
|
175
|
+
if (!Number.isInteger(length) || end <= space + 1 || end > data.length || data[end - 1] !== 0x0a) {
|
|
176
|
+
throw corrupt('an extended header is malformed');
|
|
177
|
+
}
|
|
178
|
+
const record = data.toString('utf8', space + 1, end - 1);
|
|
179
|
+
const equals = record.indexOf('=');
|
|
180
|
+
if (record.slice(0, Math.max(equals, 0)) !== 'path') {
|
|
181
|
+
throw corrupt('an extended header carries something other than a path');
|
|
182
|
+
}
|
|
183
|
+
path = record.slice(equals + 1);
|
|
184
|
+
offset = end;
|
|
185
|
+
}
|
|
186
|
+
if (path === undefined)
|
|
187
|
+
throw corrupt('an extended header is empty');
|
|
188
|
+
return path;
|
|
189
|
+
};
|
|
190
|
+
const readPaxPath = async (reader, size) => {
|
|
191
|
+
if (size > MAX_PAX_LENGTH)
|
|
192
|
+
throw corrupt('an extended header is implausibly large');
|
|
193
|
+
const pieces = [];
|
|
194
|
+
for await (const piece of takeBytes(reader, { remaining: size }))
|
|
195
|
+
pieces.push(piece);
|
|
196
|
+
await skipBytes(reader, paddingFor(size));
|
|
197
|
+
return paxPath(Buffer.concat(pieces));
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* Reads a tar archive one file at a time. An entry's `content` has to be consumed before the
|
|
201
|
+
* next entry is requested; whatever is left of it then is skipped.
|
|
202
|
+
*
|
|
203
|
+
* The generator only finishes once `source` itself has ended. When the source is a decryptor,
|
|
204
|
+
* that is what makes it check its final chunk: an archive that completes normally came from a
|
|
205
|
+
* ciphertext that was complete.
|
|
206
|
+
*/
|
|
207
|
+
async function* readTar(source) {
|
|
208
|
+
const reader = new bytes_1.ByteReader(source);
|
|
209
|
+
let path;
|
|
210
|
+
for (;;) {
|
|
211
|
+
const block = await takeBlock(reader);
|
|
212
|
+
if (block.every((byte) => byte === 0))
|
|
213
|
+
break;
|
|
214
|
+
const header = parseHeader(block);
|
|
215
|
+
if (header.type === TYPE_PAX) {
|
|
216
|
+
path = await readPaxPath(reader, header.size);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (header.type !== TYPE_FILE) {
|
|
220
|
+
throw corrupt(`"${header.path}" is not a regular file (links and devices are not accepted)`);
|
|
221
|
+
}
|
|
222
|
+
const count = { remaining: header.size };
|
|
223
|
+
yield {
|
|
224
|
+
path: path ?? header.path,
|
|
225
|
+
size: header.size,
|
|
226
|
+
executable: (header.mode & OWNER_EXECUTE) !== 0,
|
|
227
|
+
content: takeBytes(reader, count),
|
|
228
|
+
};
|
|
229
|
+
path = undefined;
|
|
230
|
+
await skipBytes(reader, count.remaining + paddingFor(header.size));
|
|
231
|
+
}
|
|
232
|
+
if (path !== undefined)
|
|
233
|
+
throw corrupt('an extended header is not followed by a file');
|
|
234
|
+
// The end marker is two empty blocks; nothing but padding may follow it.
|
|
235
|
+
do {
|
|
236
|
+
if (reader.buffered.some((byte) => byte !== 0))
|
|
237
|
+
throw corrupt('data follows the end marker');
|
|
238
|
+
reader.skip(reader.buffered.length);
|
|
239
|
+
} while (await reader.more());
|
|
240
|
+
}
|