decryption 0.5.1 → 0.6.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 +17 -325
- package/index.js +3 -66
- package/package.json +8 -24
- package/commands/account.d.ts +0 -4
- package/commands/account.js +0 -470
- package/commands/cosmology.d.ts +0 -4
- package/commands/cosmology.js +0 -97
- package/commands/doctor.d.ts +0 -3
- package/commands/doctor.js +0 -42
- package/commands/encrypt.d.ts +0 -12
- package/commands/encrypt.js +0 -251
- package/commands/guard.d.ts +0 -3
- package/commands/guard.js +0 -92
- package/commands/keychain.d.ts +0 -4
- package/commands/keychain.js +0 -92
- package/commands/keys.d.ts +0 -4
- package/commands/keys.js +0 -74
- package/commands/passkey.d.ts +0 -4
- package/commands/passkey.js +0 -135
- package/commands/salt.d.ts +0 -4
- package/commands/salt.js +0 -113
- package/commands/secrets.d.ts +0 -4
- package/commands/secrets.js +0 -214
- package/commands/shamir.d.ts +0 -4
- package/commands/shamir.js +0 -62
- package/commands/vault.d.ts +0 -7
- package/commands/vault.js +0 -382
- package/commands/wallet.d.ts +0 -5
- package/commands/wallet.js +0 -129
- package/commands/workspace.d.ts +0 -8
- package/commands/workspace.js +0 -289
- package/commands.d.ts +0 -8
- package/commands.js +0 -126
- package/esm/commands/account.js +0 -466
- package/esm/commands/cosmology.js +0 -93
- package/esm/commands/doctor.js +0 -38
- package/esm/commands/encrypt.js +0 -245
- package/esm/commands/guard.js +0 -88
- package/esm/commands/keychain.js +0 -88
- package/esm/commands/keys.js +0 -70
- package/esm/commands/passkey.js +0 -131
- package/esm/commands/salt.js +0 -109
- package/esm/commands/secrets.js +0 -210
- package/esm/commands/shamir.js +0 -58
- package/esm/commands/vault.js +0 -376
- package/esm/commands/wallet.js +0 -125
- package/esm/commands/workspace.js +0 -283
- package/esm/commands.js +0 -120
- package/esm/index.js +0 -50
- package/esm/utils/bytes.js +0 -286
- package/esm/utils/dispatch.js +0 -52
- package/esm/utils/env.js +0 -44
- package/esm/utils/errors.js +0 -44
- package/esm/utils/header.js +0 -14
- package/esm/utils/io.js +0 -49
- package/esm/utils/passphrase.js +0 -49
- package/esm/utils/recipients.js +0 -74
- package/esm/utils/stash.js +0 -61
- package/esm/utils/temp.js +0 -51
- package/esm/utils/text.js +0 -2
- package/esm/workspace/bundle.js +0 -239
- package/esm/workspace/doctor.js +0 -104
- package/esm/workspace/git.js +0 -72
- package/esm/workspace/guard.js +0 -111
- package/esm/workspace/layout.js +0 -106
- package/esm/workspace/manifest.js +0 -147
- package/esm/workspace/recipients.js +0 -37
- package/esm/workspace/snippets.js +0 -54
- package/esm/workspace/state.js +0 -46
- package/esm/workspace/status.js +0 -93
- package/esm/workspace/tar.js +0 -235
- package/index.d.ts +0 -12
- package/utils/bytes.d.ts +0 -86
- package/utils/bytes.js +0 -301
- package/utils/dispatch.d.ts +0 -24
- package/utils/dispatch.js +0 -58
- package/utils/env.d.ts +0 -26
- package/utils/env.js +0 -49
- package/utils/errors.d.ts +0 -25
- package/utils/errors.js +0 -50
- package/utils/header.d.ts +0 -4
- package/utils/header.js +0 -18
- package/utils/io.d.ts +0 -10
- package/utils/io.js +0 -57
- package/utils/passphrase.d.ts +0 -15
- package/utils/passphrase.js +0 -53
- package/utils/recipients.d.ts +0 -28
- package/utils/recipients.js +0 -80
- package/utils/stash.d.ts +0 -40
- package/utils/stash.js +0 -76
- package/utils/temp.d.ts +0 -10
- package/utils/temp.js +0 -59
- package/utils/text.d.ts +0 -2
- package/utils/text.js +0 -6
- package/workspace/bundle.d.ts +0 -50
- package/workspace/bundle.js +0 -248
- package/workspace/doctor.d.ts +0 -8
- package/workspace/doctor.js +0 -108
- package/workspace/git.d.ts +0 -21
- package/workspace/git.js +0 -83
- package/workspace/guard.d.ts +0 -23
- package/workspace/guard.js +0 -118
- package/workspace/layout.d.ts +0 -47
- package/workspace/layout.js +0 -113
- package/workspace/manifest.d.ts +0 -52
- package/workspace/manifest.js +0 -157
- package/workspace/recipients.d.ts +0 -16
- package/workspace/recipients.js +0 -45
- package/workspace/snippets.d.ts +0 -29
- package/workspace/snippets.js +0 -58
- package/workspace/state.d.ts +0 -13
- package/workspace/state.js +0 -52
- package/workspace/status.d.ts +0 -54
- package/workspace/status.js +0 -99
- package/workspace/tar.d.ts +0 -29
- package/workspace/tar.js +0 -240
package/utils/temp.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.discardTemp = exports.untrackTemp = exports.trackTemp = exports.isTempName = exports.tempPathFor = void 0;
|
|
4
|
-
const crypto_1 = require("crypto");
|
|
5
|
-
const fs_1 = require("fs");
|
|
6
|
-
const path_1 = require("path");
|
|
7
|
-
/**
|
|
8
|
-
* Temporary files and directories that must not outlive the command.
|
|
9
|
-
*
|
|
10
|
-
* Output is always built next to its destination and renamed into place, so an interrupted run
|
|
11
|
-
* leaves the destination exactly as it was. What an interrupt would leave behind is the
|
|
12
|
-
* half-written temporary itself, which may hold plaintext; everything registered here is removed
|
|
13
|
-
* on Ctrl-C and on termination.
|
|
14
|
-
*/
|
|
15
|
-
const live = new Set();
|
|
16
|
-
const remove = (path) => {
|
|
17
|
-
try {
|
|
18
|
-
(0, fs_1.rmSync)(path, { recursive: true, force: true });
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
// best effort: there is nothing useful to do about a temp file that will not delete
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
const onSignal = (signal) => {
|
|
25
|
-
for (const path of live)
|
|
26
|
-
remove(path);
|
|
27
|
-
live.clear();
|
|
28
|
-
process.exit(signal === 'SIGINT' ? 130 : 143);
|
|
29
|
-
};
|
|
30
|
-
/** A sibling path of `target`, so that renaming onto `target` never crosses a filesystem. */
|
|
31
|
-
const tempPathFor = (target) => (0, path_1.join)((0, path_1.dirname)(target), `.${(0, path_1.basename)(target)}.dcrypt-tmp-${(0, crypto_1.randomBytes)(6).toString('hex')}`);
|
|
32
|
-
exports.tempPathFor = tempPathFor;
|
|
33
|
-
/** True for names produced by {@link tempPathFor}. */
|
|
34
|
-
const isTempName = (name) => /\.dcrypt-tmp-[0-9a-f]{12}$/.test(name);
|
|
35
|
-
exports.isTempName = isTempName;
|
|
36
|
-
/** Registers `path` for removal if the process is interrupted. */
|
|
37
|
-
const trackTemp = (path) => {
|
|
38
|
-
if (live.size === 0) {
|
|
39
|
-
process.on('SIGINT', onSignal);
|
|
40
|
-
process.on('SIGTERM', onSignal);
|
|
41
|
-
}
|
|
42
|
-
live.add(path);
|
|
43
|
-
};
|
|
44
|
-
exports.trackTemp = trackTemp;
|
|
45
|
-
/** Forgets `path`: call once it has been renamed into place or removed. */
|
|
46
|
-
const untrackTemp = (path) => {
|
|
47
|
-
live.delete(path);
|
|
48
|
-
if (live.size === 0) {
|
|
49
|
-
process.removeListener('SIGINT', onSignal);
|
|
50
|
-
process.removeListener('SIGTERM', onSignal);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
exports.untrackTemp = untrackTemp;
|
|
54
|
-
/** Removes a temporary path now and forgets it. */
|
|
55
|
-
const discardTemp = (path) => {
|
|
56
|
-
remove(path);
|
|
57
|
-
(0, exports.untrackTemp)(path);
|
|
58
|
-
};
|
|
59
|
-
exports.discardTemp = discardTemp;
|
package/utils/text.d.ts
DELETED
package/utils/text.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.count = void 0;
|
|
4
|
-
/** "1 file", "3 files": a number with its noun, pluralised the regular way. */
|
|
5
|
-
const count = (n, noun) => `${n} ${noun}${n === 1 ? '' : 's'}`;
|
|
6
|
-
exports.count = count;
|
package/workspace/bundle.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { Identity } from '@decryption/keys';
|
|
2
|
-
import { CliError } from '../utils/errors';
|
|
3
|
-
import { Section } from './layout';
|
|
4
|
-
import { Manifest, Snapshot } from './manifest';
|
|
5
|
-
/** The bundle on disk is a Git LFS pointer: the real file has not been downloaded. */
|
|
6
|
-
export declare class LfsPointerError extends CliError {
|
|
7
|
-
constructor(path: string);
|
|
8
|
-
}
|
|
9
|
-
export interface BundleInfo {
|
|
10
|
-
/** SHA-256 of the bundle file. */
|
|
11
|
-
sha256: string;
|
|
12
|
-
/** Fingerprints of the recipients it is encrypted to. */
|
|
13
|
-
recipients: string[];
|
|
14
|
-
}
|
|
15
|
-
/** Reads what a bundle shows without a key: its hash, and who can open it. */
|
|
16
|
-
export declare const inspectBundle: (path: string) => Promise<BundleInfo>;
|
|
17
|
-
/**
|
|
18
|
-
* Encrypts a section to `recipients` and replaces its bundle atomically. Resolves to the new
|
|
19
|
-
* bundle's SHA-256. If a file changes underneath it, the old bundle is left as it was.
|
|
20
|
-
*/
|
|
21
|
-
export declare const encryptSection: (section: Section, snapshot: Snapshot, recipients: string[]) => Promise<string>;
|
|
22
|
-
export interface Extraction {
|
|
23
|
-
/** A new folder beside the section, holding the bundle's files. Not yet the section itself. */
|
|
24
|
-
directory: string;
|
|
25
|
-
manifest: Manifest;
|
|
26
|
-
digest: string;
|
|
27
|
-
/** SHA-256 of the bundle that was read. */
|
|
28
|
-
bundle: string;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Decrypts a bundle into a new folder beside the section's own, which is not touched.
|
|
32
|
-
*
|
|
33
|
-
* It resolves only once every chunk has authenticated, the ciphertext is known to be complete,
|
|
34
|
-
* and every file matches the manifest. On any failure the new folder is removed and the error is
|
|
35
|
-
* rethrown: there is no partial result.
|
|
36
|
-
*/
|
|
37
|
-
export declare const extractSection: (section: Section, identity: Identity) => Promise<Extraction>;
|
|
38
|
-
/**
|
|
39
|
-
* Makes an extracted folder the section's plaintext, replacing what was there.
|
|
40
|
-
*
|
|
41
|
-
* The two renames run back to back without returning to the event loop, so Ctrl-C cannot land
|
|
42
|
-
* between them. If the machine dies there instead, the previous plaintext is still on disk under
|
|
43
|
-
* its set-aside name, and {@link recoverSection} puts it back.
|
|
44
|
-
*/
|
|
45
|
-
export declare const installExtraction: (section: Section, extraction: Extraction) => Promise<void>;
|
|
46
|
-
/**
|
|
47
|
-
* Cleans up after a run that was killed: restores a section's plaintext if it had been set aside
|
|
48
|
-
* and nothing took its place, and removes half-written leftovers. Returns true if it restored.
|
|
49
|
-
*/
|
|
50
|
-
export declare const recoverSection: (section: Section) => boolean;
|
package/workspace/bundle.js
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.recoverSection = exports.installExtraction = exports.extractSection = exports.encryptSection = exports.inspectBundle = exports.LfsPointerError = void 0;
|
|
4
|
-
const keys_1 = require("@decryption/keys");
|
|
5
|
-
const crypto_1 = require("crypto");
|
|
6
|
-
const fs_1 = require("fs");
|
|
7
|
-
const promises_1 = require("fs/promises");
|
|
8
|
-
const path_1 = require("path");
|
|
9
|
-
const promises_2 = require("stream/promises");
|
|
10
|
-
const bytes_1 = require("../utils/bytes");
|
|
11
|
-
const errors_1 = require("../utils/errors");
|
|
12
|
-
const header_1 = require("../utils/header");
|
|
13
|
-
const temp_1 = require("../utils/temp");
|
|
14
|
-
const layout_1 = require("./layout");
|
|
15
|
-
const manifest_1 = require("./manifest");
|
|
16
|
-
const tar_1 = require("./tar");
|
|
17
|
-
/**
|
|
18
|
-
* A bundle is a section's files as one encrypted file: a deterministic tar whose first entry is
|
|
19
|
-
* the manifest, inside the streamed envelope `dcrypt encrypt --binary --recipient` writes.
|
|
20
|
-
*
|
|
21
|
-
* Everything about the section is inside the encryption: file names, sizes, how many files there
|
|
22
|
-
* are. From outside, a bundle shows its own size and the fingerprints of its recipients.
|
|
23
|
-
*
|
|
24
|
-
* A bundle says nothing about who made it. Recipient keys are public, so anyone who has them can
|
|
25
|
-
* produce a bundle that decrypts cleanly. That is why extraction trusts nothing in one: every
|
|
26
|
-
* path is checked before a byte is written, and nothing is written outside a fresh folder.
|
|
27
|
-
*/
|
|
28
|
-
const LFS_POINTER_PREFIX = 'version https://git-lfs.github.com/spec/';
|
|
29
|
-
/** Far more than any real manifest; it is held in memory, so it has a bound. */
|
|
30
|
-
const MAX_MANIFEST_SIZE = 256 * 1024 * 1024;
|
|
31
|
-
/** The bundle on disk is a Git LFS pointer: the real file has not been downloaded. */
|
|
32
|
-
class LfsPointerError extends errors_1.CliError {
|
|
33
|
-
constructor(path) {
|
|
34
|
-
super(`${path} is a Git LFS pointer, not the bundle itself; run \`git lfs pull\``, errors_1.EXIT.notFound);
|
|
35
|
-
this.name = 'LfsPointerError';
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
exports.LfsPointerError = LfsPointerError;
|
|
39
|
-
const mismatch = (message) => new errors_1.CliError(`the bundle does not match its manifest: ${message}`, errors_1.EXIT.corrupt);
|
|
40
|
-
const readFile = (path) => (0, fs_1.createReadStream)(path, { highWaterMark: bytes_1.IO_CHUNK_SIZE });
|
|
41
|
-
/** Passes bytes through unchanged, feeding each piece to `hash` on the way. */
|
|
42
|
-
async function* hashing(source, hash) {
|
|
43
|
-
for await (const piece of source) {
|
|
44
|
-
hash.update(piece);
|
|
45
|
-
yield piece;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
/** Reads what a bundle shows without a key: its hash, and who can open it. */
|
|
49
|
-
const inspectBundle = async (path) => {
|
|
50
|
-
const input = readFile(path);
|
|
51
|
-
const hash = (0, crypto_1.createHash)('sha256');
|
|
52
|
-
try {
|
|
53
|
-
const reader = new bytes_1.ByteReader(hashing(input, hash));
|
|
54
|
-
const head = await reader.fill(LFS_POINTER_PREFIX.length);
|
|
55
|
-
if (head.toString('latin1').startsWith(LFS_POINTER_PREFIX))
|
|
56
|
-
throw new LfsPointerError(path);
|
|
57
|
-
const header = await (0, header_1.readFileHeader)(reader);
|
|
58
|
-
for await (const piece of reader.rest())
|
|
59
|
-
void piece;
|
|
60
|
-
return { sha256: hash.digest('hex'), recipients: (0, keys_1.fileRecipients)(header) };
|
|
61
|
-
}
|
|
62
|
-
finally {
|
|
63
|
-
input.destroy();
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
exports.inspectBundle = inspectBundle;
|
|
67
|
-
/** A file's bytes, with a check at the end that they are still the ones the manifest describes. */
|
|
68
|
-
async function* unchanged(file) {
|
|
69
|
-
const hash = (0, crypto_1.createHash)('sha256');
|
|
70
|
-
yield* hashing(readFile(file.source), hash);
|
|
71
|
-
if (hash.digest('hex') !== file.sha256) {
|
|
72
|
-
throw new errors_1.CliError(`${file.source} changed while it was being encrypted; run the command again`);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
/** The archive's entries: the manifest first, then every file in manifest order. */
|
|
76
|
-
function* archiveEntries(snapshot) {
|
|
77
|
-
const manifest = (0, manifest_1.serializeManifest)(snapshot.manifest);
|
|
78
|
-
yield { path: manifest_1.MANIFEST_PATH, size: manifest.length, executable: false, content: [manifest] };
|
|
79
|
-
for (const file of snapshot.files) {
|
|
80
|
-
yield { path: file.path, size: file.size, executable: file.executable, content: unchanged(file) };
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Encrypts a section to `recipients` and replaces its bundle atomically. Resolves to the new
|
|
85
|
-
* bundle's SHA-256. If a file changes underneath it, the old bundle is left as it was.
|
|
86
|
-
*/
|
|
87
|
-
const encryptSection = async (section, snapshot, recipients) => {
|
|
88
|
-
const { header, stream } = (0, keys_1.createRecipientFileEncryptor)(recipients);
|
|
89
|
-
const hash = (0, crypto_1.createHash)('sha256');
|
|
90
|
-
try {
|
|
91
|
-
const archive = (0, bytes_1.transformBytes)((0, tar_1.writeTar)(archiveEntries(snapshot)), stream);
|
|
92
|
-
await (0, bytes_1.writeFileAtomically)(section.bundle, hashing((0, bytes_1.chainBytes)([header], archive), hash));
|
|
93
|
-
}
|
|
94
|
-
finally {
|
|
95
|
-
stream.destroy();
|
|
96
|
-
}
|
|
97
|
-
return hash.digest('hex');
|
|
98
|
-
};
|
|
99
|
-
exports.encryptSection = encryptSection;
|
|
100
|
-
const readManifest = async (entry) => {
|
|
101
|
-
if (entry.path !== manifest_1.MANIFEST_PATH)
|
|
102
|
-
throw mismatch('it does not begin with one');
|
|
103
|
-
if (entry.size > MAX_MANIFEST_SIZE)
|
|
104
|
-
throw mismatch('the manifest is implausibly large');
|
|
105
|
-
const pieces = [];
|
|
106
|
-
for await (const piece of entry.content)
|
|
107
|
-
pieces.push(piece);
|
|
108
|
-
return (0, manifest_1.parseManifest)(Buffer.concat(pieces));
|
|
109
|
-
};
|
|
110
|
-
/** Writes one archive entry to its place under `directory`, and checks it against the manifest. */
|
|
111
|
-
const writeEntry = async (directory, entry, expected) => {
|
|
112
|
-
const target = (0, path_1.join)(directory, ...expected.path.split('/'));
|
|
113
|
-
const hash = (0, crypto_1.createHash)('sha256');
|
|
114
|
-
const handle = await (0, promises_1.mkdir)((0, path_1.dirname)(target), { recursive: true, mode: 0o700 })
|
|
115
|
-
// "wx" refuses a name that is already there: two paths the file system considers the same.
|
|
116
|
-
.then(() => (0, promises_1.open)(target, 'wx', expected.executable ? 0o700 : 0o600))
|
|
117
|
-
.catch((error) => {
|
|
118
|
-
throw new errors_1.CliError(`cannot create "${expected.path}" (${error.code ?? 'error'}); two paths in the bundle may be the same on this file system`, errors_1.EXIT.corrupt);
|
|
119
|
-
});
|
|
120
|
-
await (0, promises_2.pipeline)(hashing(entry.content, hash), handle.createWriteStream());
|
|
121
|
-
if (hash.digest('hex') !== expected.sha256) {
|
|
122
|
-
throw mismatch(`"${expected.path}" does not have the content it lists`);
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
/**
|
|
126
|
-
* Writes an archive's files under `directory`, accepting exactly what the manifest lists, in the
|
|
127
|
-
* order it lists it. Paths come from the manifest, which {@link parseManifest} has vetted.
|
|
128
|
-
*/
|
|
129
|
-
const extractArchive = async (entries, directory) => {
|
|
130
|
-
let manifest;
|
|
131
|
-
let index = 0;
|
|
132
|
-
for await (const entry of entries) {
|
|
133
|
-
if (!manifest) {
|
|
134
|
-
manifest = await readManifest(entry);
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
const expected = manifest.files[index++];
|
|
138
|
-
const listed = expected !== undefined &&
|
|
139
|
-
entry.path === expected.path &&
|
|
140
|
-
entry.size === expected.size &&
|
|
141
|
-
entry.executable === expected.executable;
|
|
142
|
-
if (!listed)
|
|
143
|
-
throw mismatch(`"${entry.path}" is not the file it lists next`);
|
|
144
|
-
await writeEntry(directory, entry, expected);
|
|
145
|
-
}
|
|
146
|
-
if (!manifest)
|
|
147
|
-
throw mismatch('it is empty');
|
|
148
|
-
if (index !== manifest.files.length)
|
|
149
|
-
throw mismatch('files it lists are missing');
|
|
150
|
-
return manifest;
|
|
151
|
-
};
|
|
152
|
-
/**
|
|
153
|
-
* Forces an extracted folder to disk: every file, then every folder, before it may replace
|
|
154
|
-
* anything. One pass at the end, not a sync as each file is closed: that waits for the device
|
|
155
|
-
* once per file, milliseconds each, where this pass costs next to nothing after its first sync.
|
|
156
|
-
*/
|
|
157
|
-
const syncExtraction = async (directory, manifest) => {
|
|
158
|
-
const folders = new Set([directory]);
|
|
159
|
-
for (const { path } of manifest.files) {
|
|
160
|
-
const target = (0, path_1.join)(directory, ...path.split('/'));
|
|
161
|
-
await (0, bytes_1.syncFile)(target);
|
|
162
|
-
for (let folder = (0, path_1.dirname)(target); !folders.has(folder); folder = (0, path_1.dirname)(folder))
|
|
163
|
-
folders.add(folder);
|
|
164
|
-
}
|
|
165
|
-
for (const folder of folders)
|
|
166
|
-
await (0, bytes_1.syncDirectory)(folder);
|
|
167
|
-
};
|
|
168
|
-
/**
|
|
169
|
-
* Decrypts a bundle into a new folder beside the section's own, which is not touched.
|
|
170
|
-
*
|
|
171
|
-
* It resolves only once every chunk has authenticated, the ciphertext is known to be complete,
|
|
172
|
-
* and every file matches the manifest. On any failure the new folder is removed and the error is
|
|
173
|
-
* rethrown: there is no partial result.
|
|
174
|
-
*/
|
|
175
|
-
const extractSection = async (section, identity) => {
|
|
176
|
-
const directory = (0, temp_1.tempPathFor)(section.directory);
|
|
177
|
-
await (0, promises_1.mkdir)(directory, { mode: 0o700 });
|
|
178
|
-
(0, temp_1.trackTemp)(directory);
|
|
179
|
-
const input = readFile(section.bundle);
|
|
180
|
-
const hash = (0, crypto_1.createHash)('sha256');
|
|
181
|
-
let decryptor;
|
|
182
|
-
try {
|
|
183
|
-
const reader = new bytes_1.ByteReader(hashing(input, hash));
|
|
184
|
-
decryptor = (0, keys_1.createIdentityFileDecryptor)(await (0, header_1.readFileHeader)(reader), identity);
|
|
185
|
-
const archive = (0, tar_1.readTar)((0, bytes_1.transformBytes)(reader.rest(), decryptor));
|
|
186
|
-
const manifest = await extractArchive(archive, directory);
|
|
187
|
-
await syncExtraction(directory, manifest);
|
|
188
|
-
return { directory, manifest, digest: (0, manifest_1.digestOf)(manifest), bundle: hash.digest('hex') };
|
|
189
|
-
}
|
|
190
|
-
catch (error) {
|
|
191
|
-
(0, temp_1.discardTemp)(directory);
|
|
192
|
-
throw error;
|
|
193
|
-
}
|
|
194
|
-
finally {
|
|
195
|
-
decryptor?.destroy();
|
|
196
|
-
input.destroy();
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
exports.extractSection = extractSection;
|
|
200
|
-
const setAsidePathFor = (directory) => (0, path_1.join)((0, path_1.dirname)(directory), `.${(0, path_1.basename)(directory)}${layout_1.REPLACED_MARKER}${(0, crypto_1.randomBytes)(6).toString('hex')}`);
|
|
201
|
-
/**
|
|
202
|
-
* Makes an extracted folder the section's plaintext, replacing what was there.
|
|
203
|
-
*
|
|
204
|
-
* The two renames run back to back without returning to the event loop, so Ctrl-C cannot land
|
|
205
|
-
* between them. If the machine dies there instead, the previous plaintext is still on disk under
|
|
206
|
-
* its set-aside name, and {@link recoverSection} puts it back.
|
|
207
|
-
*/
|
|
208
|
-
const installExtraction = async (section, extraction) => {
|
|
209
|
-
const setAside = (0, fs_1.existsSync)(section.directory) ? setAsidePathFor(section.directory) : undefined;
|
|
210
|
-
if (setAside)
|
|
211
|
-
(0, fs_1.renameSync)(section.directory, setAside);
|
|
212
|
-
try {
|
|
213
|
-
(0, fs_1.renameSync)(extraction.directory, section.directory);
|
|
214
|
-
}
|
|
215
|
-
catch (error) {
|
|
216
|
-
if (setAside)
|
|
217
|
-
(0, fs_1.renameSync)(setAside, section.directory);
|
|
218
|
-
(0, temp_1.discardTemp)(extraction.directory);
|
|
219
|
-
throw error;
|
|
220
|
-
}
|
|
221
|
-
(0, temp_1.untrackTemp)(extraction.directory);
|
|
222
|
-
// The swap is on disk before the old plaintext goes, and before anyone records it as done.
|
|
223
|
-
await (0, bytes_1.syncDirectory)((0, path_1.dirname)(section.directory));
|
|
224
|
-
if (setAside)
|
|
225
|
-
(0, fs_1.rmSync)(setAside, { recursive: true, force: true });
|
|
226
|
-
};
|
|
227
|
-
exports.installExtraction = installExtraction;
|
|
228
|
-
/**
|
|
229
|
-
* Cleans up after a run that was killed: restores a section's plaintext if it had been set aside
|
|
230
|
-
* and nothing took its place, and removes half-written leftovers. Returns true if it restored.
|
|
231
|
-
*/
|
|
232
|
-
const recoverSection = (section) => {
|
|
233
|
-
const parent = (0, path_1.dirname)(section.directory);
|
|
234
|
-
if (!(0, fs_1.existsSync)(parent))
|
|
235
|
-
return false;
|
|
236
|
-
const own = (name, target) => name.startsWith(`.${(0, path_1.basename)(target)}.dcrypt-`);
|
|
237
|
-
const names = (0, fs_1.readdirSync)(parent);
|
|
238
|
-
const setAside = names.filter((name) => own(name, section.directory) && name.includes(layout_1.REPLACED_MARKER));
|
|
239
|
-
const temporary = names.filter((name) => (0, temp_1.isTempName)(name) && (own(name, section.directory) || own(name, section.bundle)));
|
|
240
|
-
const restored = !(0, fs_1.existsSync)(section.directory) && setAside.length > 0;
|
|
241
|
-
if (restored)
|
|
242
|
-
(0, fs_1.renameSync)((0, path_1.join)(parent, setAside.shift()), section.directory);
|
|
243
|
-
for (const name of [...setAside, ...temporary]) {
|
|
244
|
-
(0, fs_1.rmSync)((0, path_1.join)(parent, name), { recursive: true, force: true });
|
|
245
|
-
}
|
|
246
|
-
return restored;
|
|
247
|
-
};
|
|
248
|
-
exports.recoverSection = recoverSection;
|
package/workspace/doctor.d.ts
DELETED
package/workspace/doctor.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.runDoctor = void 0;
|
|
4
|
-
const fs_1 = require("fs");
|
|
5
|
-
const errors_1 = require("../utils/errors");
|
|
6
|
-
const recipients_1 = require("../utils/recipients");
|
|
7
|
-
const text_1 = require("../utils/text");
|
|
8
|
-
const git_1 = require("./git");
|
|
9
|
-
const guard_1 = require("./guard");
|
|
10
|
-
const layout_1 = require("./layout");
|
|
11
|
-
const recipients_2 = require("./recipients");
|
|
12
|
-
const state_1 = require("./state");
|
|
13
|
-
const status_1 = require("./status");
|
|
14
|
-
/**
|
|
15
|
-
* `dcrypt doctor`: everything that should be true of a machine and a workspace before either is
|
|
16
|
-
* trusted with a section. It reads; it changes nothing, and it never asks for a passphrase.
|
|
17
|
-
*/
|
|
18
|
-
const MINIMUM_NODE_MAJOR = 20;
|
|
19
|
-
const check = (name, level, message) => ({ name, level, message });
|
|
20
|
-
const nodeCheck = () => {
|
|
21
|
-
const version = process.versions.node;
|
|
22
|
-
return Number(version.split('.')[0]) >= MINIMUM_NODE_MAJOR
|
|
23
|
-
? check('node', 'ok', `Node.js ${version}`)
|
|
24
|
-
: check('node', 'fail', `Node.js ${version} is too old; ${MINIMUM_NODE_MAJOR} or newer is needed`);
|
|
25
|
-
};
|
|
26
|
-
const identityCheck = (mine) => mine
|
|
27
|
-
? check('identity', 'ok', `this machine's identity is ${mine}`)
|
|
28
|
-
: check('identity', 'fail', 'no identity on this machine; run `dcrypt keys generate`');
|
|
29
|
-
const readRecipients = (workspace) => {
|
|
30
|
-
if (!(0, fs_1.existsSync)(workspace.recipientsFile)) {
|
|
31
|
-
return check('recipients', 'fail', `no recipients.txt in ${workspace.root}`);
|
|
32
|
-
}
|
|
33
|
-
try {
|
|
34
|
-
return (0, recipients_1.readRecipientsFile)(workspace.recipientsFile);
|
|
35
|
-
}
|
|
36
|
-
catch (error) {
|
|
37
|
-
return check('recipients', 'fail', (0, errors_1.messageFor)(error));
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
const recipientChecks = (entries, mine) => {
|
|
41
|
-
if (!entries.length)
|
|
42
|
-
return [check('recipients', 'fail', 'recipients.txt lists no recipients')];
|
|
43
|
-
const checks = [check('recipients', 'ok', `recipients.txt lists ${(0, text_1.count)(entries.length, 'recipient')}`)];
|
|
44
|
-
checks.push(entries.some(recipients_2.isRecovery)
|
|
45
|
-
? check('recovery', 'ok', 'there is a recovery recipient')
|
|
46
|
-
: check('recovery', 'warn', recipients_2.NO_RECOVERY_WARNING));
|
|
47
|
-
if (entries.length === 1) {
|
|
48
|
-
checks.push(check('recipients', 'warn', 'there is only one recipient: one lost key loses everything'));
|
|
49
|
-
}
|
|
50
|
-
if (mine && !entries.some((entry) => (0, recipients_2.fingerprintOf)(entry.recipient) === mine)) {
|
|
51
|
-
checks.push(check('recipients', 'warn', `this machine's identity (${mine}) is not in recipients.txt`));
|
|
52
|
-
}
|
|
53
|
-
return checks;
|
|
54
|
-
};
|
|
55
|
-
const sectionChecks = async (workspace, sections, recipients) => {
|
|
56
|
-
if (!sections.length)
|
|
57
|
-
return [check('sections', 'ok', 'no sections are encrypted yet')];
|
|
58
|
-
const state = (0, state_1.readState)(workspace);
|
|
59
|
-
const checks = [];
|
|
60
|
-
for (const section of sections) {
|
|
61
|
-
const report = await (0, status_1.inspectSection)(section, state[section.name], recipients);
|
|
62
|
-
const advice = (0, status_1.adviceFor)(report);
|
|
63
|
-
const level = report.problem ? 'fail' : advice ? 'warn' : 'ok';
|
|
64
|
-
checks.push(check(`section ${section.name}`, level, advice ? `${report.state}: ${advice}` : report.state));
|
|
65
|
-
}
|
|
66
|
-
return checks;
|
|
67
|
-
};
|
|
68
|
-
const isIgnored = (workspace, path) => (0, git_1.ignoredPaths)(workspace.root, [path]).length > 0;
|
|
69
|
-
const repositoryChecks = async (workspace, sections) => {
|
|
70
|
-
if (!(0, git_1.isRepository)(workspace.root)) {
|
|
71
|
-
return [check('git', 'warn', 'not a git repository: nothing here checks what gets committed')];
|
|
72
|
-
}
|
|
73
|
-
const checks = [
|
|
74
|
-
(0, guard_1.isHookInstalled)(workspace)
|
|
75
|
-
? check('guard', 'ok', 'the pre-commit guard is installed')
|
|
76
|
-
: check('guard', 'warn', 'no pre-commit guard; run `dcrypt guard install`'),
|
|
77
|
-
];
|
|
78
|
-
const exposed = sections.filter((section) => !isIgnored(workspace, `${section.name}/file`));
|
|
79
|
-
checks.push(exposed.length
|
|
80
|
-
? check('gitignore', 'fail', `git does not ignore the plaintext of: ${exposed.map((section) => section.name).join(', ')} (see \`dcrypt guard snippet gitignore\`)`)
|
|
81
|
-
: check('gitignore', 'ok', 'git ignores the plaintext of every section'));
|
|
82
|
-
if (!isIgnored(workspace, `${layout_1.STATE_DIRECTORY}/state.json`)) {
|
|
83
|
-
checks.push(check('gitignore', 'warn', `git does not ignore ${layout_1.STATE_DIRECTORY}/`));
|
|
84
|
-
}
|
|
85
|
-
const outsideLfs = sections.filter((section) => (0, git_1.attributeOf)(workspace.root, 'filter', section.name + layout_1.BUNDLE_SUFFIX) !== 'lfs');
|
|
86
|
-
if (outsideLfs.length) {
|
|
87
|
-
checks.push(check('lfs', 'warn', 'bundles are not stored with Git LFS (see `dcrypt guard snippet gitattributes`)'));
|
|
88
|
-
}
|
|
89
|
-
const { violations } = await (0, guard_1.runGuard)(workspace, 'all');
|
|
90
|
-
checks.push(violations.length
|
|
91
|
-
? check('tracked files', 'fail', `${(0, text_1.count)(violations.length, 'tracked file')} should not be in the repository; run \`dcrypt guard --all\``)
|
|
92
|
-
: check('tracked files', 'ok', 'nothing tracked is plaintext'));
|
|
93
|
-
return checks;
|
|
94
|
-
};
|
|
95
|
-
const runDoctor = async (workspace) => {
|
|
96
|
-
const mine = (0, recipients_2.ownFingerprint)();
|
|
97
|
-
const recipients = readRecipients(workspace);
|
|
98
|
-
const entries = Array.isArray(recipients) ? recipients : undefined;
|
|
99
|
-
const sections = (0, layout_1.discoverSections)(workspace);
|
|
100
|
-
return [
|
|
101
|
-
nodeCheck(),
|
|
102
|
-
identityCheck(mine),
|
|
103
|
-
...(entries ? recipientChecks(entries, mine) : [recipients]),
|
|
104
|
-
...(await sectionChecks(workspace, sections, entries?.map((entry) => (0, recipients_2.fingerprintOf)(entry.recipient)))),
|
|
105
|
-
...(await repositoryChecks(workspace, sections)),
|
|
106
|
-
];
|
|
107
|
-
};
|
|
108
|
-
exports.runDoctor = runDoctor;
|
package/workspace/git.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export declare const isRepository: (root: string) => boolean;
|
|
2
|
-
/** The top of the working tree `root` is in. */
|
|
3
|
-
export declare const repositoryRoot: (root: string) => string;
|
|
4
|
-
/** Where this repository's hooks live, honouring `core.hooksPath`. */
|
|
5
|
-
export declare const hooksDirectory: (root: string) => string;
|
|
6
|
-
/** Files added or changed in the index: what the next commit would record. */
|
|
7
|
-
export declare const stagedPaths: (root: string) => string[];
|
|
8
|
-
/** Every file in the index. */
|
|
9
|
-
export declare const trackedPaths: (root: string) => string[];
|
|
10
|
-
/**
|
|
11
|
-
* The subset of `paths` the ignore rules match. `--no-index` asks about the rules alone, so a
|
|
12
|
-
* file that was force-added, and is therefore tracked, is still reported.
|
|
13
|
-
*/
|
|
14
|
-
export declare const ignoredPaths: (root: string, paths: string[]) => string[];
|
|
15
|
-
/** The value of a gitattribute for a path: "lfs", "unspecified", … */
|
|
16
|
-
export declare const attributeOf: (root: string, attribute: string, path: string) => string;
|
|
17
|
-
/**
|
|
18
|
-
* The first bytes of a file as it is in the index: what a commit would record, which is not
|
|
19
|
-
* always what is in the working tree. Only the head is read, however large the blob.
|
|
20
|
-
*/
|
|
21
|
-
export declare const indexedHead: (root: string, path: string, length: number) => Promise<Buffer>;
|
package/workspace/git.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.indexedHead = exports.attributeOf = exports.ignoredPaths = exports.trackedPaths = exports.stagedPaths = exports.hooksDirectory = exports.repositoryRoot = exports.isRepository = void 0;
|
|
4
|
-
const child_process_1 = require("child_process");
|
|
5
|
-
const fs_1 = require("fs");
|
|
6
|
-
const path_1 = require("path");
|
|
7
|
-
const bytes_1 = require("../utils/bytes");
|
|
8
|
-
const errors_1 = require("../utils/errors");
|
|
9
|
-
/**
|
|
10
|
-
* The few questions the guard and `doctor` ask of git, each answered by git itself. Nothing here
|
|
11
|
-
* changes a repository. Paths are relative to `root`, which is the workspace root.
|
|
12
|
-
*/
|
|
13
|
-
const MAX_OUTPUT = 256 * 1024 * 1024;
|
|
14
|
-
const git = (root, args, input) => {
|
|
15
|
-
const result = (0, child_process_1.spawnSync)('git', args, { cwd: root, input, encoding: 'utf8', maxBuffer: MAX_OUTPUT });
|
|
16
|
-
if (result.error)
|
|
17
|
-
throw new errors_1.CliError(`cannot run git: ${result.error.message}`);
|
|
18
|
-
return { status: result.status ?? 1, stdout: result.stdout };
|
|
19
|
-
};
|
|
20
|
-
const gitOrThrow = (root, args) => {
|
|
21
|
-
const { status, stdout } = git(root, args);
|
|
22
|
-
if (status !== 0)
|
|
23
|
-
throw new errors_1.CliError(`git ${args[0]} failed in ${root}`);
|
|
24
|
-
return stdout;
|
|
25
|
-
};
|
|
26
|
-
const nulSeparated = (output) => output.split('\0').filter(Boolean);
|
|
27
|
-
const isRepository = (root) => git(root, ['rev-parse', '--is-inside-work-tree']).stdout.trim() === 'true';
|
|
28
|
-
exports.isRepository = isRepository;
|
|
29
|
-
/** The top of the working tree `root` is in. */
|
|
30
|
-
const repositoryRoot = (root) => (0, fs_1.realpathSync)(gitOrThrow(root, ['rev-parse', '--show-toplevel']).trim());
|
|
31
|
-
exports.repositoryRoot = repositoryRoot;
|
|
32
|
-
/** Where this repository's hooks live, honouring `core.hooksPath`. */
|
|
33
|
-
const hooksDirectory = (root) => (0, path_1.resolve)(root, gitOrThrow(root, ['rev-parse', '--git-path', 'hooks']).trim());
|
|
34
|
-
exports.hooksDirectory = hooksDirectory;
|
|
35
|
-
/** Files added or changed in the index: what the next commit would record. */
|
|
36
|
-
const stagedPaths = (root) => nulSeparated(gitOrThrow(root, ['diff', '--cached', '--name-only', '--diff-filter=ACMR', '--relative', '-z']));
|
|
37
|
-
exports.stagedPaths = stagedPaths;
|
|
38
|
-
/** Every file in the index. */
|
|
39
|
-
const trackedPaths = (root) => nulSeparated(gitOrThrow(root, ['ls-files', '-z']));
|
|
40
|
-
exports.trackedPaths = trackedPaths;
|
|
41
|
-
/**
|
|
42
|
-
* The subset of `paths` the ignore rules match. `--no-index` asks about the rules alone, so a
|
|
43
|
-
* file that was force-added, and is therefore tracked, is still reported.
|
|
44
|
-
*/
|
|
45
|
-
const ignoredPaths = (root, paths) => {
|
|
46
|
-
if (!paths.length)
|
|
47
|
-
return [];
|
|
48
|
-
const { status, stdout } = git(root, ['check-ignore', '--no-index', '-z', '--stdin'], paths.join('\0'));
|
|
49
|
-
// 0: some are ignored. 1: none are. Anything else is git failing.
|
|
50
|
-
if (status > 1)
|
|
51
|
-
throw new errors_1.CliError(`git check-ignore failed in ${root}`);
|
|
52
|
-
return nulSeparated(stdout);
|
|
53
|
-
};
|
|
54
|
-
exports.ignoredPaths = ignoredPaths;
|
|
55
|
-
/** The value of a gitattribute for a path: "lfs", "unspecified", … */
|
|
56
|
-
const attributeOf = (root, attribute, path) => gitOrThrow(root, ['check-attr', attribute, '--', path]).trim().split(': ').pop() ?? '';
|
|
57
|
-
exports.attributeOf = attributeOf;
|
|
58
|
-
/**
|
|
59
|
-
* The first bytes of a file as it is in the index: what a commit would record, which is not
|
|
60
|
-
* always what is in the working tree. Only the head is read, however large the blob.
|
|
61
|
-
*/
|
|
62
|
-
const indexedHead = async (root, path, length) => {
|
|
63
|
-
const child = (0, child_process_1.spawn)('git', ['cat-file', 'blob', `:./${path}`], {
|
|
64
|
-
cwd: root,
|
|
65
|
-
stdio: ['ignore', 'pipe', 'ignore'],
|
|
66
|
-
});
|
|
67
|
-
const exited = new Promise((resolve) => {
|
|
68
|
-
child.on('error', () => resolve(null));
|
|
69
|
-
child.on('close', resolve);
|
|
70
|
-
});
|
|
71
|
-
try {
|
|
72
|
-
const head = Buffer.from((await new bytes_1.ByteReader(child.stdout).fill(length)).subarray(0, length));
|
|
73
|
-
// Nothing read is an empty file only if git says so: a git that failed must not pass for one.
|
|
74
|
-
if (!head.length && (await exited) !== 0)
|
|
75
|
-
throw new errors_1.CliError(`cannot read ${path} from the git index`);
|
|
76
|
-
return head;
|
|
77
|
-
}
|
|
78
|
-
finally {
|
|
79
|
-
child.stdout.destroy();
|
|
80
|
-
child.kill();
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
exports.indexedHead = indexedHead;
|
package/workspace/guard.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Workspace } from './layout';
|
|
2
|
-
export type GuardScope = 'staged' | 'all';
|
|
3
|
-
export interface Violation {
|
|
4
|
-
path: string;
|
|
5
|
-
rule: 'plaintext' | 'not encrypted' | 'ignored';
|
|
6
|
-
message: string;
|
|
7
|
-
}
|
|
8
|
-
export interface GuardResult {
|
|
9
|
-
scope: GuardScope;
|
|
10
|
-
checked: number;
|
|
11
|
-
violations: Violation[];
|
|
12
|
-
}
|
|
13
|
-
/** Checks the given paths (relative to the workspace root) as they stand in the index. */
|
|
14
|
-
export declare const checkPaths: (workspace: Workspace, paths: string[]) => Promise<Violation[]>;
|
|
15
|
-
/** Runs the guard over what is staged, or over everything tracked. */
|
|
16
|
-
export declare const runGuard: (workspace: Workspace, scope: GuardScope) => Promise<GuardResult>;
|
|
17
|
-
/** True when this repository's pre-commit hook is the one `dcrypt guard install` writes. */
|
|
18
|
-
export declare const isHookInstalled: (workspace: Workspace) => boolean;
|
|
19
|
-
/**
|
|
20
|
-
* Installs the pre-commit hook. A hook that is already there and is not ours is left alone
|
|
21
|
-
* unless `force` is set.
|
|
22
|
-
*/
|
|
23
|
-
export declare const installHook: (workspace: Workspace, force?: boolean) => string;
|