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
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.snapshotSection = exports.hashFile = exports.hashBytes = exports.parseManifest = exports.digestOf = exports.serializeManifest = exports.assertSafePath = exports.MANIFEST_VERSION = exports.MANIFEST_PATH = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const promises_1 = require("fs/promises");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const bytes_1 = require("../utils/bytes");
|
|
9
|
+
const errors_1 = require("../utils/errors");
|
|
10
|
+
/**
|
|
11
|
+
* What a section contains: every file's path, size and SHA-256.
|
|
12
|
+
*
|
|
13
|
+
* The manifest is the first entry of a section's archive, inside the encryption. It is what makes
|
|
14
|
+
* "nothing changed" cheap to establish (two sections are the same exactly when their manifests
|
|
15
|
+
* are), and it is what an extraction is checked against before any plaintext is replaced.
|
|
16
|
+
*/
|
|
17
|
+
exports.MANIFEST_PATH = '.dcrypt-manifest.json';
|
|
18
|
+
exports.MANIFEST_VERSION = 1;
|
|
19
|
+
/** Finder metadata, rewritten whenever a folder is opened: never part of a section. */
|
|
20
|
+
const IGNORED_NAMES = new Set(['.DS_Store']);
|
|
21
|
+
const OWNER_EXECUTE = 0o100;
|
|
22
|
+
const invalid = (message) => new errors_1.CliError(`the manifest is invalid: ${message}`, errors_1.EXIT.corrupt);
|
|
23
|
+
/**
|
|
24
|
+
* Throws unless `path` names a file strictly inside the directory it will be extracted into.
|
|
25
|
+
* Applied to every path that comes out of a bundle, before anything is written.
|
|
26
|
+
*/
|
|
27
|
+
const assertSafePath = (path) => {
|
|
28
|
+
const reason = path.length === 0
|
|
29
|
+
? 'it is empty'
|
|
30
|
+
: /[\0\\]/.test(path)
|
|
31
|
+
? 'it contains a NUL or a backslash'
|
|
32
|
+
: path.split('/').some((segment) => segment === '' || segment === '.' || segment === '..')
|
|
33
|
+
? 'it is absolute or has an empty, "." or ".." segment'
|
|
34
|
+
: path === exports.MANIFEST_PATH
|
|
35
|
+
? 'that name is reserved'
|
|
36
|
+
: path !== path.normalize('NFC')
|
|
37
|
+
? 'it is not NFC-normalized'
|
|
38
|
+
: undefined;
|
|
39
|
+
if (reason)
|
|
40
|
+
throw invalid(`path ${JSON.stringify(path)} is not acceptable (${reason})`);
|
|
41
|
+
};
|
|
42
|
+
exports.assertSafePath = assertSafePath;
|
|
43
|
+
const byPath = (a, b) => Buffer.compare(Buffer.from(a.path, 'utf8'), Buffer.from(b.path, 'utf8'));
|
|
44
|
+
/** The one byte sequence a manifest is written as: fixed key order, sorted files. */
|
|
45
|
+
const serializeManifest = (manifest) => {
|
|
46
|
+
const files = manifest.files.map(({ path, size, sha256, executable }) => ({
|
|
47
|
+
path,
|
|
48
|
+
size,
|
|
49
|
+
sha256,
|
|
50
|
+
executable,
|
|
51
|
+
}));
|
|
52
|
+
return Buffer.from(JSON.stringify({ version: manifest.version, files }, null, 2) + '\n', 'utf8');
|
|
53
|
+
};
|
|
54
|
+
exports.serializeManifest = serializeManifest;
|
|
55
|
+
const digestOf = (manifest) => (0, crypto_1.createHash)('sha256').update((0, exports.serializeManifest)(manifest)).digest('hex');
|
|
56
|
+
exports.digestOf = digestOf;
|
|
57
|
+
const isManifestFile = (value) => {
|
|
58
|
+
const file = value;
|
|
59
|
+
return (typeof file === 'object' &&
|
|
60
|
+
file !== null &&
|
|
61
|
+
typeof file.path === 'string' &&
|
|
62
|
+
Number.isSafeInteger(file.size) &&
|
|
63
|
+
file.size >= 0 &&
|
|
64
|
+
typeof file.sha256 === 'string' &&
|
|
65
|
+
/^[0-9a-f]{64}$/.test(file.sha256) &&
|
|
66
|
+
typeof file.executable === 'boolean');
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Parses a manifest read from a bundle. Strict about every field it reads; a field it does not
|
|
70
|
+
* know is dropped, so that nothing outside the format can reach the digest.
|
|
71
|
+
*/
|
|
72
|
+
const parseManifest = (bytes) => {
|
|
73
|
+
let parsed;
|
|
74
|
+
try {
|
|
75
|
+
parsed = JSON.parse(Buffer.from(bytes).toString('utf8'));
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
throw invalid('it is not JSON');
|
|
79
|
+
}
|
|
80
|
+
if (parsed?.version !== exports.MANIFEST_VERSION) {
|
|
81
|
+
throw invalid(`version ${JSON.stringify(parsed?.version)} is not supported`);
|
|
82
|
+
}
|
|
83
|
+
if (!Array.isArray(parsed.files) || !parsed.files.every(isManifestFile)) {
|
|
84
|
+
throw invalid('the file list is malformed');
|
|
85
|
+
}
|
|
86
|
+
const files = parsed.files.map(({ path, size, sha256, executable }) => ({
|
|
87
|
+
path,
|
|
88
|
+
size,
|
|
89
|
+
sha256,
|
|
90
|
+
executable,
|
|
91
|
+
}));
|
|
92
|
+
files.forEach((file, index) => {
|
|
93
|
+
(0, exports.assertSafePath)(file.path);
|
|
94
|
+
// Strictly ascending order also rules out duplicates.
|
|
95
|
+
if (index > 0 && byPath(files[index - 1], file) >= 0)
|
|
96
|
+
throw invalid('the files are not sorted');
|
|
97
|
+
});
|
|
98
|
+
return { version: exports.MANIFEST_VERSION, files };
|
|
99
|
+
};
|
|
100
|
+
exports.parseManifest = parseManifest;
|
|
101
|
+
const readBytes = (path) => (0, fs_1.createReadStream)(path, { highWaterMark: bytes_1.IO_CHUNK_SIZE });
|
|
102
|
+
/** SHA-256 of a byte stream, as lowercase hex. */
|
|
103
|
+
const hashBytes = async (bytes) => {
|
|
104
|
+
const hash = (0, crypto_1.createHash)('sha256');
|
|
105
|
+
for await (const piece of bytes)
|
|
106
|
+
hash.update(piece);
|
|
107
|
+
return hash.digest('hex');
|
|
108
|
+
};
|
|
109
|
+
exports.hashBytes = hashBytes;
|
|
110
|
+
const hashFile = (path) => (0, exports.hashBytes)(readBytes(path));
|
|
111
|
+
exports.hashFile = hashFile;
|
|
112
|
+
/** Every regular file under `directory`. Anything that is not a file or a folder is refused. */
|
|
113
|
+
const findFiles = async (directory, prefix = '') => {
|
|
114
|
+
const found = [];
|
|
115
|
+
for (const name of await (0, promises_1.readdir)(directory)) {
|
|
116
|
+
if (IGNORED_NAMES.has(name))
|
|
117
|
+
continue;
|
|
118
|
+
const source = (0, path_1.join)(directory, name);
|
|
119
|
+
// The name on disk is used to read the file; the manifest records its normalized form, so
|
|
120
|
+
// that the same section has the same digest on file systems that store names differently.
|
|
121
|
+
const path = prefix + name.normalize('NFC');
|
|
122
|
+
const stats = await (0, promises_1.lstat)(source);
|
|
123
|
+
if (stats.isDirectory()) {
|
|
124
|
+
found.push(...(await findFiles(source, `${path}/`)));
|
|
125
|
+
}
|
|
126
|
+
else if (stats.isFile()) {
|
|
127
|
+
found.push({ path, source, size: stats.size, executable: (stats.mode & OWNER_EXECUTE) !== 0 });
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
throw new errors_1.CliError(`${source} is a link or a special file; a section holds regular files only (empty folders are not kept)`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return found;
|
|
134
|
+
};
|
|
135
|
+
/** Reads a section's plaintext: finds its files and hashes each one. */
|
|
136
|
+
const snapshotSection = async (directory) => {
|
|
137
|
+
const found = (await findFiles(directory)).sort(byPath);
|
|
138
|
+
const files = [];
|
|
139
|
+
for (const file of found) {
|
|
140
|
+
try {
|
|
141
|
+
(0, exports.assertSafePath)(file.path);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
throw new errors_1.CliError(`${file.source} cannot be stored: its name is reserved, or contains a backslash or a NUL`);
|
|
145
|
+
}
|
|
146
|
+
if (files.length && files[files.length - 1].path === file.path) {
|
|
147
|
+
throw new errors_1.CliError(`${file.source} has the same name as another file once normalized`);
|
|
148
|
+
}
|
|
149
|
+
files.push({ ...file, sha256: await (0, exports.hashFile)(file.source) });
|
|
150
|
+
}
|
|
151
|
+
const manifest = {
|
|
152
|
+
version: exports.MANIFEST_VERSION,
|
|
153
|
+
files: files.map(({ path, size, sha256, executable }) => ({ path, size, sha256, executable })),
|
|
154
|
+
};
|
|
155
|
+
return { manifest, digest: (0, exports.digestOf)(manifest), files };
|
|
156
|
+
};
|
|
157
|
+
exports.snapshotSection = snapshotSection;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RecipientEntry } from '../utils/recipients';
|
|
2
|
+
import { Workspace } from './layout';
|
|
3
|
+
/** The fingerprint a bundle's header lists for this `dcrypt1…` recipient. */
|
|
4
|
+
export declare const fingerprintOf: (recipient: string) => string;
|
|
5
|
+
/** This machine's fingerprint, when it has an identity. Public: no passphrase is involved. */
|
|
6
|
+
export declare const ownFingerprint: () => string | undefined;
|
|
7
|
+
/** Everyone the workspace's sections are encrypted to. */
|
|
8
|
+
export declare const readWorkspaceRecipients: (workspace: Workspace) => RecipientEntry[];
|
|
9
|
+
/**
|
|
10
|
+
* A recovery recipient is a key that lives on no working machine: kept offline, for the day
|
|
11
|
+
* every laptop is gone. It is recognised by the word "recovery" in its label.
|
|
12
|
+
*/
|
|
13
|
+
export declare const isRecovery: (entry: RecipientEntry) => boolean;
|
|
14
|
+
export declare const NO_RECOVERY_WARNING: string;
|
|
15
|
+
/** What is worth saying about a set of recipients before encrypting to them. */
|
|
16
|
+
export declare const recipientWarnings: (entries: RecipientEntry[]) => string[];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.recipientWarnings = exports.NO_RECOVERY_WARNING = exports.isRecovery = exports.readWorkspaceRecipients = exports.ownFingerprint = exports.fingerprintOf = void 0;
|
|
4
|
+
const keys_1 = require("@decryption/keys");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const errors_1 = require("../utils/errors");
|
|
7
|
+
const recipients_1 = require("../utils/recipients");
|
|
8
|
+
const stash_1 = require("../utils/stash");
|
|
9
|
+
/** The fingerprint a bundle's header lists for this `dcrypt1…` recipient. */
|
|
10
|
+
const fingerprintOf = (recipient) => (0, keys_1.fingerprint)((0, keys_1.recipientFromString)(recipient));
|
|
11
|
+
exports.fingerprintOf = fingerprintOf;
|
|
12
|
+
/** This machine's fingerprint, when it has an identity. Public: no passphrase is involved. */
|
|
13
|
+
const ownFingerprint = () => (0, stash_1.hasIdentity)() ? (0, exports.fingerprintOf)((0, stash_1.readStoredIdentity)().recipient) : undefined;
|
|
14
|
+
exports.ownFingerprint = ownFingerprint;
|
|
15
|
+
/** Everyone the workspace's sections are encrypted to. */
|
|
16
|
+
const readWorkspaceRecipients = (workspace) => {
|
|
17
|
+
if (!(0, fs_1.existsSync)(workspace.recipientsFile)) {
|
|
18
|
+
throw new errors_1.CliError(`no recipients.txt in ${workspace.root}; create one with a "dcrypt1…" public key per line ("dcrypt keys show" prints yours)`, errors_1.EXIT.notFound);
|
|
19
|
+
}
|
|
20
|
+
const entries = (0, recipients_1.readRecipientsFile)(workspace.recipientsFile);
|
|
21
|
+
if (!entries.length)
|
|
22
|
+
throw new errors_1.CliError(`${workspace.recipientsFile} lists no recipients`);
|
|
23
|
+
return entries;
|
|
24
|
+
};
|
|
25
|
+
exports.readWorkspaceRecipients = readWorkspaceRecipients;
|
|
26
|
+
/**
|
|
27
|
+
* A recovery recipient is a key that lives on no working machine: kept offline, for the day
|
|
28
|
+
* every laptop is gone. It is recognised by the word "recovery" in its label.
|
|
29
|
+
*/
|
|
30
|
+
const isRecovery = (entry) => /\brecovery\b/i.test(entry.label);
|
|
31
|
+
exports.isRecovery = isRecovery;
|
|
32
|
+
exports.NO_RECOVERY_WARNING = 'no recovery recipient: if every machine key is lost, nothing can decrypt these sections. ' +
|
|
33
|
+
'Add a key that is kept offline to recipients.txt, with "recovery" in its label.';
|
|
34
|
+
/** What is worth saying about a set of recipients before encrypting to them. */
|
|
35
|
+
const recipientWarnings = (entries) => {
|
|
36
|
+
const warnings = [];
|
|
37
|
+
if (!entries.some(exports.isRecovery))
|
|
38
|
+
warnings.push(exports.NO_RECOVERY_WARNING);
|
|
39
|
+
const mine = (0, exports.ownFingerprint)();
|
|
40
|
+
if (mine && !entries.some((entry) => (0, exports.fingerprintOf)(entry.recipient) === mine)) {
|
|
41
|
+
warnings.push(`this machine's identity (${mine}) is not in recipients.txt: it cannot decrypt what it encrypts.`);
|
|
42
|
+
}
|
|
43
|
+
return warnings;
|
|
44
|
+
};
|
|
45
|
+
exports.recipientWarnings = recipientWarnings;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Files a workspace's repository needs, ready to paste. `dcrypt guard snippet <name>` prints one.
|
|
3
|
+
*/
|
|
4
|
+
/** Marks a pre-commit hook as ours, so that installing again replaces it and nothing else. */
|
|
5
|
+
export declare const HOOK_MARKER = "# dcrypt-guard";
|
|
6
|
+
export declare const SNIPPETS: {
|
|
7
|
+
readonly gitignore: {
|
|
8
|
+
readonly file: ".gitignore";
|
|
9
|
+
readonly text: "# Allow-list: ignore everything, then name what may be committed.\n# A new file is private until a line here says otherwise.\n/*\n!/.gitignore\n!/.gitattributes\n!/README.md\n!/recipients.txt\n!/*.tar.dcrypt\n\n# For bundles in a sub-folder, open the folder, close its contents, allow its bundles:\n# !/clients/\n# /clients/*\n# !/clients/*.tar.dcrypt\n";
|
|
10
|
+
};
|
|
11
|
+
readonly gitattributes: {
|
|
12
|
+
readonly file: ".gitattributes";
|
|
13
|
+
readonly text: "# Bundles are large and change completely on every encryption: keep them in Git LFS.\n*.tar.dcrypt filter=lfs diff=lfs merge=lfs -text\n";
|
|
14
|
+
};
|
|
15
|
+
readonly hook: {
|
|
16
|
+
readonly file: ".git/hooks/pre-commit";
|
|
17
|
+
readonly text: "#!/bin/sh\n# dcrypt-guard: installed by \"dcrypt guard install\"\n# Refuses a commit that would record plaintext from an encrypted section.\nif ! command -v dcrypt >/dev/null 2>&1; then\n echo \"pre-commit: dcrypt is not installed, so this commit cannot be checked.\" >&2\n echo \"pre-commit: install it with: npm i -g @decryption/cli\" >&2\n exit 1\nfi\nexec dcrypt guard --staged\n";
|
|
18
|
+
};
|
|
19
|
+
readonly ci: {
|
|
20
|
+
readonly file: "a CI workflow";
|
|
21
|
+
readonly text: "# A step for CI: fails the build if plaintext was ever committed.\n- name: dcrypt guard\n run: npx --yes @decryption/cli guard --all\n";
|
|
22
|
+
};
|
|
23
|
+
readonly recipients: {
|
|
24
|
+
readonly file: "recipients.txt";
|
|
25
|
+
readonly text: "# Who can decrypt the sections in this folder. One public key per line, then a label.\n# Public keys are safe to commit. \"dcrypt keys show\" prints yours.\n#\n# Keep one key that lives on no working machine, with \"recovery\" in its label:\n# it is what opens the sections on the day every laptop is gone.\n#\n# dcrypt1… ada (laptop)\n# dcrypt1… ada (desktop)\n# dcrypt1… recovery (printed, in the safe)\n";
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export type SnippetName = keyof typeof SNIPPETS;
|
|
29
|
+
export declare const isSnippetName: (name: string) => name is SnippetName;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Files a workspace's repository needs, ready to paste. `dcrypt guard snippet <name>` prints one.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isSnippetName = exports.SNIPPETS = exports.HOOK_MARKER = void 0;
|
|
7
|
+
/** Marks a pre-commit hook as ours, so that installing again replaces it and nothing else. */
|
|
8
|
+
exports.HOOK_MARKER = '# dcrypt-guard';
|
|
9
|
+
const gitignore = `# Allow-list: ignore everything, then name what may be committed.
|
|
10
|
+
# A new file is private until a line here says otherwise.
|
|
11
|
+
/*
|
|
12
|
+
!/.gitignore
|
|
13
|
+
!/.gitattributes
|
|
14
|
+
!/README.md
|
|
15
|
+
!/recipients.txt
|
|
16
|
+
!/*.tar.dcrypt
|
|
17
|
+
|
|
18
|
+
# For bundles in a sub-folder, open the folder, close its contents, allow its bundles:
|
|
19
|
+
# !/clients/
|
|
20
|
+
# /clients/*
|
|
21
|
+
# !/clients/*.tar.dcrypt
|
|
22
|
+
`;
|
|
23
|
+
const gitattributes = `# Bundles are large and change completely on every encryption: keep them in Git LFS.
|
|
24
|
+
*.tar.dcrypt filter=lfs diff=lfs merge=lfs -text
|
|
25
|
+
`;
|
|
26
|
+
const hook = `#!/bin/sh
|
|
27
|
+
${exports.HOOK_MARKER}: installed by "dcrypt guard install"
|
|
28
|
+
# Refuses a commit that would record plaintext from an encrypted section.
|
|
29
|
+
if ! command -v dcrypt >/dev/null 2>&1; then
|
|
30
|
+
echo "pre-commit: dcrypt is not installed, so this commit cannot be checked." >&2
|
|
31
|
+
echo "pre-commit: install it with: npm i -g @decryption/cli" >&2
|
|
32
|
+
exit 1
|
|
33
|
+
fi
|
|
34
|
+
exec dcrypt guard --staged
|
|
35
|
+
`;
|
|
36
|
+
const ci = `# A step for CI: fails the build if plaintext was ever committed.
|
|
37
|
+
- name: dcrypt guard
|
|
38
|
+
run: npx --yes @decryption/cli guard --all
|
|
39
|
+
`;
|
|
40
|
+
const recipients = `# Who can decrypt the sections in this folder. One public key per line, then a label.
|
|
41
|
+
# Public keys are safe to commit. "dcrypt keys show" prints yours.
|
|
42
|
+
#
|
|
43
|
+
# Keep one key that lives on no working machine, with "recovery" in its label:
|
|
44
|
+
# it is what opens the sections on the day every laptop is gone.
|
|
45
|
+
#
|
|
46
|
+
# dcrypt1… ada (laptop)
|
|
47
|
+
# dcrypt1… ada (desktop)
|
|
48
|
+
# dcrypt1… recovery (printed, in the safe)
|
|
49
|
+
`;
|
|
50
|
+
exports.SNIPPETS = {
|
|
51
|
+
gitignore: { file: '.gitignore', text: gitignore },
|
|
52
|
+
gitattributes: { file: '.gitattributes', text: gitattributes },
|
|
53
|
+
hook: { file: '.git/hooks/pre-commit', text: hook },
|
|
54
|
+
ci: { file: 'a CI workflow', text: ci },
|
|
55
|
+
recipients: { file: 'recipients.txt', text: recipients },
|
|
56
|
+
};
|
|
57
|
+
const isSnippetName = (name) => Object.hasOwn(exports.SNIPPETS, name);
|
|
58
|
+
exports.isSnippetName = isSnippetName;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Workspace } from './layout';
|
|
2
|
+
export interface SectionRecord {
|
|
3
|
+
/** Digest of the manifest the plaintext and the bundle agreed on. */
|
|
4
|
+
digest: string;
|
|
5
|
+
/** SHA-256 of the bundle file at that moment. */
|
|
6
|
+
bundle: string;
|
|
7
|
+
}
|
|
8
|
+
export type State = Record<string, SectionRecord>;
|
|
9
|
+
export declare const statePath: (workspace: Workspace) => string;
|
|
10
|
+
/** Reads the state. A missing or unreadable file is an empty one: every section is then unknown. */
|
|
11
|
+
export declare const readState: (workspace: Workspace) => State;
|
|
12
|
+
/** Records that `name`'s plaintext and bundle agree, replacing what was known about it. */
|
|
13
|
+
export declare const recordSection: (workspace: Workspace, name: string, record: SectionRecord) => Promise<void>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.recordSection = exports.readState = exports.statePath = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const promises_1 = require("fs/promises");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const bytes_1 = require("../utils/bytes");
|
|
8
|
+
const layout_1 = require("./layout");
|
|
9
|
+
/**
|
|
10
|
+
* What this machine last encrypted or decrypted, per section.
|
|
11
|
+
*
|
|
12
|
+
* Comparing a section against its bundle would otherwise need the bundle decrypted, and so a
|
|
13
|
+
* passphrase. Remembering the two hashes that were in agreement the last time lets `status` and
|
|
14
|
+
* an unchanged `encrypt-section` answer without one. The file is local, holds only hashes, and is
|
|
15
|
+
* never committed; losing it loses nothing but that shortcut.
|
|
16
|
+
*/
|
|
17
|
+
const STATE_VERSION = 1;
|
|
18
|
+
const statePath = (workspace) => (0, path_1.join)(workspace.root, layout_1.STATE_DIRECTORY, 'state.json');
|
|
19
|
+
exports.statePath = statePath;
|
|
20
|
+
const isRecord = (value) => {
|
|
21
|
+
const record = value;
|
|
22
|
+
return (typeof record === 'object' &&
|
|
23
|
+
record !== null &&
|
|
24
|
+
typeof record.digest === 'string' &&
|
|
25
|
+
typeof record.bundle === 'string');
|
|
26
|
+
};
|
|
27
|
+
/** Reads the state. A missing or unreadable file is an empty one: every section is then unknown. */
|
|
28
|
+
const readState = (workspace) => {
|
|
29
|
+
const path = (0, exports.statePath)(workspace);
|
|
30
|
+
if (!(0, fs_1.existsSync)(path))
|
|
31
|
+
return {};
|
|
32
|
+
try {
|
|
33
|
+
const parsed = JSON.parse((0, fs_1.readFileSync)(path, 'utf8'));
|
|
34
|
+
if (parsed?.version !== STATE_VERSION || typeof parsed.sections !== 'object')
|
|
35
|
+
return {};
|
|
36
|
+
return Object.fromEntries(Object.entries(parsed.sections ?? {}).filter(([, value]) => isRecord(value)));
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exports.readState = readState;
|
|
43
|
+
/** Records that `name`'s plaintext and bundle agree, replacing what was known about it. */
|
|
44
|
+
const recordSection = async (workspace, name, record) => {
|
|
45
|
+
const sections = { ...(0, exports.readState)(workspace), [name]: record };
|
|
46
|
+
const sorted = Object.fromEntries(Object.entries(sections).sort(([a], [b]) => (a < b ? -1 : 1)));
|
|
47
|
+
const path = (0, exports.statePath)(workspace);
|
|
48
|
+
await (0, promises_1.mkdir)((0, path_1.join)(workspace.root, layout_1.STATE_DIRECTORY), { recursive: true });
|
|
49
|
+
const text = JSON.stringify({ version: STATE_VERSION, sections: sorted }, null, 2) + '\n';
|
|
50
|
+
await (0, bytes_1.writeFileAtomically)(path, [Buffer.from(text, 'utf8')]);
|
|
51
|
+
};
|
|
52
|
+
exports.recordSection = recordSection;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { BundleInfo } from './bundle';
|
|
2
|
+
import { Section } from './layout';
|
|
3
|
+
import { Snapshot } from './manifest';
|
|
4
|
+
import { SectionRecord } from './state';
|
|
5
|
+
/** Where a section's plaintext and its bundle stand relative to each other. */
|
|
6
|
+
export type SectionState =
|
|
7
|
+
/** Plaintext and bundle are the pair this machine last wrote or read. */
|
|
8
|
+
'in sync'
|
|
9
|
+
/** The plaintext was edited and has not been encrypted again. */
|
|
10
|
+
| 'plaintext changed'
|
|
11
|
+
/** The bundle is newer than the plaintext: someone else encrypted it. */
|
|
12
|
+
| 'bundle changed'
|
|
13
|
+
/** Both changed since they last agreed. */
|
|
14
|
+
| 'conflict'
|
|
15
|
+
/** There is a bundle and no plaintext. */
|
|
16
|
+
| 'not decrypted'
|
|
17
|
+
/** There is plaintext and no bundle. */
|
|
18
|
+
| 'not encrypted'
|
|
19
|
+
/** Both exist, and this machine has no record of them agreeing. */
|
|
20
|
+
| 'unknown'
|
|
21
|
+
/** The bundle is a Git LFS pointer. */
|
|
22
|
+
| 'not downloaded'
|
|
23
|
+
/** The bundle is not a file dcrypt can read: its header is damaged, cut short or unsupported. */
|
|
24
|
+
| 'bundle unreadable'
|
|
25
|
+
/** The folder cannot be recorded: it holds a link, or a name a bundle cannot store. */
|
|
26
|
+
| 'plaintext unreadable'
|
|
27
|
+
/** Neither exists. */
|
|
28
|
+
| 'missing';
|
|
29
|
+
export interface SectionReport {
|
|
30
|
+
section: Section;
|
|
31
|
+
state: SectionState;
|
|
32
|
+
snapshot?: Snapshot;
|
|
33
|
+
bundle?: BundleInfo;
|
|
34
|
+
/** Why it could not be read, in the two states that say it could not. */
|
|
35
|
+
problem?: string;
|
|
36
|
+
/** Fingerprints in `recipients.txt` that the bundle is not encrypted to. */
|
|
37
|
+
missingRecipients: string[];
|
|
38
|
+
/** Fingerprints the bundle is encrypted to that are not in `recipients.txt`. */
|
|
39
|
+
extraRecipients: string[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Compares a section's plaintext, its bundle and what this machine remembers of them. Needs no
|
|
43
|
+
* key: the plaintext is hashed, the bundle is hashed, and its header names its recipients.
|
|
44
|
+
*
|
|
45
|
+
* `recipients` is the fingerprints in `recipients.txt`, when there is one to compare against.
|
|
46
|
+
*
|
|
47
|
+
* It does not throw for a section it cannot read: that is a state of that section, with the
|
|
48
|
+
* reason in `problem`, so that one bad section never hides the state of the others.
|
|
49
|
+
*/
|
|
50
|
+
export declare const inspectSection: (section: Section, record?: SectionRecord, recipients?: string[]) => Promise<SectionReport>;
|
|
51
|
+
/** True when the bundle is not encrypted to exactly the people in `recipients.txt`. */
|
|
52
|
+
export declare const recipientsDiffer: (report: SectionReport) => boolean;
|
|
53
|
+
/** One line on what a report means and what to do about it. Empty when there is nothing to do. */
|
|
54
|
+
export declare const adviceFor: (report: SectionReport) => string;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.adviceFor = exports.recipientsDiffer = exports.inspectSection = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const errors_1 = require("../utils/errors");
|
|
6
|
+
const text_1 = require("../utils/text");
|
|
7
|
+
const bundle_1 = require("./bundle");
|
|
8
|
+
const manifest_1 = require("./manifest");
|
|
9
|
+
const exists = (path, kind) => {
|
|
10
|
+
try {
|
|
11
|
+
return (0, fs_1.statSync)(path)[kind]();
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const stateOf = (snapshot, bundle, record) => {
|
|
18
|
+
if (!bundle)
|
|
19
|
+
return snapshot ? 'not encrypted' : 'missing';
|
|
20
|
+
if (!snapshot)
|
|
21
|
+
return 'not decrypted';
|
|
22
|
+
if (!record)
|
|
23
|
+
return 'unknown';
|
|
24
|
+
const plaintextSame = snapshot.digest === record.digest;
|
|
25
|
+
const bundleSame = bundle.sha256 === record.bundle;
|
|
26
|
+
if (plaintextSame)
|
|
27
|
+
return bundleSame ? 'in sync' : 'bundle changed';
|
|
28
|
+
return bundleSame ? 'plaintext changed' : 'conflict';
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Compares a section's plaintext, its bundle and what this machine remembers of them. Needs no
|
|
32
|
+
* key: the plaintext is hashed, the bundle is hashed, and its header names its recipients.
|
|
33
|
+
*
|
|
34
|
+
* `recipients` is the fingerprints in `recipients.txt`, when there is one to compare against.
|
|
35
|
+
*
|
|
36
|
+
* It does not throw for a section it cannot read: that is a state of that section, with the
|
|
37
|
+
* reason in `problem`, so that one bad section never hides the state of the others.
|
|
38
|
+
*/
|
|
39
|
+
const inspectSection = async (section, record, recipients) => {
|
|
40
|
+
const unread = { section, missingRecipients: [], extraRecipients: [] };
|
|
41
|
+
let snapshot;
|
|
42
|
+
try {
|
|
43
|
+
if (exists(section.directory, 'isDirectory'))
|
|
44
|
+
snapshot = await (0, manifest_1.snapshotSection)(section.directory);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
return { ...unread, state: 'plaintext unreadable', problem: (0, errors_1.messageFor)(error) };
|
|
48
|
+
}
|
|
49
|
+
const report = { ...unread, snapshot };
|
|
50
|
+
if (!exists(section.bundle, 'isFile'))
|
|
51
|
+
return { ...report, state: stateOf(snapshot, undefined, record) };
|
|
52
|
+
let bundle;
|
|
53
|
+
try {
|
|
54
|
+
bundle = await (0, bundle_1.inspectBundle)(section.bundle);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (error instanceof bundle_1.LfsPointerError)
|
|
58
|
+
return { ...report, state: 'not downloaded' };
|
|
59
|
+
return { ...report, state: 'bundle unreadable', problem: (0, errors_1.messageFor)(error) };
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
...report,
|
|
63
|
+
bundle,
|
|
64
|
+
state: stateOf(snapshot, bundle, record),
|
|
65
|
+
missingRecipients: (recipients ?? []).filter((id) => !bundle.recipients.includes(id)),
|
|
66
|
+
extraRecipients: recipients ? bundle.recipients.filter((id) => !recipients.includes(id)) : [],
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
exports.inspectSection = inspectSection;
|
|
70
|
+
/** True when the bundle is not encrypted to exactly the people in `recipients.txt`. */
|
|
71
|
+
const recipientsDiffer = (report) => report.missingRecipients.length + report.extraRecipients.length > 0;
|
|
72
|
+
exports.recipientsDiffer = recipientsDiffer;
|
|
73
|
+
const ADVICE = {
|
|
74
|
+
'in sync': '',
|
|
75
|
+
'plaintext changed': 'run `dcrypt encrypt-section` to encrypt the changes',
|
|
76
|
+
'bundle changed': 'run `dcrypt decrypt-workspace` to update the plaintext',
|
|
77
|
+
conflict: 'the plaintext and the bundle have both changed since they last agreed',
|
|
78
|
+
'not decrypted': 'run `dcrypt decrypt-workspace`',
|
|
79
|
+
'not encrypted': 'run `dcrypt encrypt-section`',
|
|
80
|
+
unknown: 'this machine has no record of it; run `dcrypt decrypt-workspace` to compare',
|
|
81
|
+
'not downloaded': 'the bundle is a Git LFS pointer; run `git lfs pull`',
|
|
82
|
+
'bundle unreadable': 'restore the bundle from git, or replace it with `dcrypt encrypt-section --force`',
|
|
83
|
+
'plaintext unreadable': '',
|
|
84
|
+
missing: 'there is no such folder and no such bundle',
|
|
85
|
+
};
|
|
86
|
+
/** What to say when the bundle is not encrypted to exactly the people in `recipients.txt`. */
|
|
87
|
+
const recipientAdvice = ({ missingRecipients, extraRecipients }) => {
|
|
88
|
+
const differences = [];
|
|
89
|
+
if (missingRecipients.length) {
|
|
90
|
+
differences.push(`${(0, text_1.count)(missingRecipients.length, 'recipient')} in recipients.txt cannot open it yet`);
|
|
91
|
+
}
|
|
92
|
+
if (extraRecipients.length) {
|
|
93
|
+
differences.push(`it is still encrypted to ${(0, text_1.count)(extraRecipients.length, 'key')} no longer in recipients.txt`);
|
|
94
|
+
}
|
|
95
|
+
return differences.length ? `${differences.join(', and ')}; encrypt it again` : '';
|
|
96
|
+
};
|
|
97
|
+
/** One line on what a report means and what to do about it. Empty when there is nothing to do. */
|
|
98
|
+
const adviceFor = (report) => [report.problem, ADVICE[report.state], recipientAdvice(report)].filter(Boolean).join('; ');
|
|
99
|
+
exports.adviceFor = adviceFor;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ByteStream } from '../utils/bytes';
|
|
2
|
+
/** The largest file an 11-digit octal size field can describe: one byte under 8 GiB. */
|
|
3
|
+
export declare const MAX_TAR_FILE_SIZE = 8589934591;
|
|
4
|
+
/** A file to archive. `content` must yield exactly `size` bytes. */
|
|
5
|
+
export interface TarFile {
|
|
6
|
+
/** Path inside the archive, `/`-separated. */
|
|
7
|
+
path: string;
|
|
8
|
+
size: number;
|
|
9
|
+
executable: boolean;
|
|
10
|
+
content: ByteStream;
|
|
11
|
+
}
|
|
12
|
+
/** A file read back from an archive. `content` is only valid until the next entry is requested. */
|
|
13
|
+
export interface TarEntry {
|
|
14
|
+
path: string;
|
|
15
|
+
size: number;
|
|
16
|
+
executable: boolean;
|
|
17
|
+
content: AsyncIterable<Uint8Array>;
|
|
18
|
+
}
|
|
19
|
+
/** Writes `files` as a tar archive, in the order given. */
|
|
20
|
+
export declare function writeTar(files: Iterable<TarFile> | AsyncIterable<TarFile>): AsyncGenerator<Uint8Array>;
|
|
21
|
+
/**
|
|
22
|
+
* Reads a tar archive one file at a time. An entry's `content` has to be consumed before the
|
|
23
|
+
* next entry is requested; whatever is left of it then is skipped.
|
|
24
|
+
*
|
|
25
|
+
* The generator only finishes once `source` itself has ended. When the source is a decryptor,
|
|
26
|
+
* that is what makes it check its final chunk: an archive that completes normally came from a
|
|
27
|
+
* ciphertext that was complete.
|
|
28
|
+
*/
|
|
29
|
+
export declare function readTar(source: ByteStream): AsyncGenerator<TarEntry>;
|