@git-stunts/git-cas 5.3.2 → 6.0.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/ADVANCED_GUIDE.md +1006 -0
- package/ARCHITECTURE.md +514 -0
- package/CHANGELOG.md +393 -5
- package/CODE_OF_CONDUCT.md +33 -0
- package/GUIDE.md +872 -0
- package/README.md +219 -325
- package/SECURITY.md +993 -0
- package/SUPPORT.md +54 -0
- package/UPGRADING.md +299 -0
- package/bin/actions.js +70 -21
- package/bin/agent/cli.js +51 -0
- package/bin/agent/commands/doctor.js +99 -0
- package/bin/agent/commands/index.js +1489 -0
- package/bin/agent/input.js +619 -0
- package/bin/agent/passphrase-source.js +123 -0
- package/bin/agent/protocol.js +193 -0
- package/bin/build-version.js +64 -0
- package/bin/credentials.js +425 -0
- package/bin/git-cas.js +595 -449
- package/bin/passphrase-source.js +266 -0
- package/bin/restore-output-target.js +28 -0
- package/bin/ui/blocks/asset-card.js +109 -0
- package/bin/ui/blocks/health-dashboard.js +149 -0
- package/bin/ui/blocks/heatmap-block.js +67 -0
- package/bin/ui/blocks/merkle-explorer.js +163 -0
- package/bin/ui/blocks/operation-feed.js +176 -0
- package/bin/ui/blocks/wizard-block.js +168 -0
- package/bin/ui/components/short-sha.js +36 -0
- package/bin/ui/context.js +56 -1
- package/bin/ui/dashboard-cmds.js +1578 -13
- package/bin/ui/dashboard-view.js +612 -145
- package/bin/ui/dashboard.js +998 -214
- package/bin/ui/encryption-card.js +69 -26
- package/bin/ui/heatmap.js +26 -54
- package/bin/ui/history-timeline.js +51 -19
- package/bin/ui/manifest-view.js +115 -60
- package/bin/ui/merkle-dag.js +83 -0
- package/bin/ui/progress.js +15 -9
- package/bin/ui/repo-treemap.js +328 -0
- package/bin/ui/shaders/bak-sneppen.js +180 -0
- package/bin/ui/shaders/fractal-zoom.js +99 -0
- package/bin/ui/shaders/merkle-tree.js +142 -0
- package/bin/ui/shaders/organic-flow.js +96 -0
- package/bin/ui/shaders/phase-portrait.js +93 -0
- package/bin/ui/store-wizard.js +296 -0
- package/bin/ui/theme.js +155 -0
- package/bin/ui/vault-report.js +495 -0
- package/build-info.json +4 -0
- package/docs/API.md +2224 -0
- package/docs/ENCRYPTION_MODES.md +33 -0
- package/docs/EXTENDING.md +174 -0
- package/docs/STORE_RESTORE_PIPELINE.md +199 -0
- package/docs/THREAT_MODEL.md +269 -0
- package/docs/VAULT_INTERNALS.md +251 -0
- package/docs/WALKTHROUGH.md +1868 -0
- package/docs/demo.gif +0 -0
- package/docs/releases/v6.0.0.md +104 -0
- package/examples/README.md +260 -0
- package/index.d.ts +208 -65
- package/index.js +163 -54
- package/package.json +35 -6
- package/scripts/migrate-encryption.js +680 -0
- package/src/domain/bytes/ByteLayout.js +96 -0
- package/src/domain/encoding/base64.js +91 -0
- package/src/domain/encoding/hex.js +14 -0
- package/src/domain/encoding/utf8.js +123 -0
- package/src/domain/encryption/schemes.js +132 -0
- package/src/domain/errors/CannotRemoveLastRecipientError.js +10 -0
- package/src/domain/errors/CasError.d.ts +28 -0
- package/src/domain/errors/CasError.js +43 -6
- package/src/domain/errors/Codes.js +54 -0
- package/src/domain/errors/DekUnwrapFailedError.js +10 -0
- package/src/domain/errors/GitError.js +10 -0
- package/src/domain/errors/GitPlumbingInitializationError.js +10 -0
- package/src/domain/errors/IntegrityError.js +10 -0
- package/src/domain/errors/InvalidChunkingStrategyError.js +10 -0
- package/src/domain/errors/InvalidOidError.js +10 -0
- package/src/domain/errors/InvalidOptionsError.js +10 -0
- package/src/domain/errors/ManifestIntegrityError.js +10 -0
- package/src/domain/errors/ManifestNotFoundError.js +10 -0
- package/src/domain/errors/NoMatchingRecipientError.js +10 -0
- package/src/domain/errors/PersistenceCapabilityRequiredError.js +10 -0
- package/src/domain/errors/PortNotImplementedError.js +10 -0
- package/src/domain/errors/RecipientAlreadyExistsError.js +10 -0
- package/src/domain/errors/RecipientNotFoundError.js +10 -0
- package/src/domain/errors/RestoreTooLargeError.js +10 -0
- package/src/domain/errors/RotationNotSupportedError.js +10 -0
- package/src/domain/errors/createCasError.js +68 -0
- package/src/domain/errors/index.js +20 -0
- package/src/domain/helpers/buildKdfMetadata.js +4 -2
- package/src/domain/helpers/codecBytes.js +34 -0
- package/src/domain/helpers/gitRefErrors.js +84 -0
- package/src/domain/helpers/scryptMaxmem.js +22 -0
- package/src/domain/outcomes/RestoreFailure.js +15 -0
- package/src/domain/outcomes/RestoreOutcome.js +11 -0
- package/src/domain/outcomes/RestoreSuccess.js +34 -0
- package/src/domain/outcomes/StoreFailure.js +15 -0
- package/src/domain/outcomes/StoreOutcome.js +11 -0
- package/src/domain/outcomes/StoreSuccess.js +17 -0
- package/src/domain/schemas/ManifestSchema.d.ts +40 -9
- package/src/domain/schemas/ManifestSchema.js +67 -13
- package/src/domain/services/CasService.d.ts +123 -38
- package/src/domain/services/CasService.js +298 -834
- package/src/domain/services/ChunkRepository.js +230 -0
- package/src/domain/services/CompressionStreams.js +72 -0
- package/src/domain/services/ConvergentEncryption.js +128 -0
- package/src/domain/services/GitTreeBuilder.js +86 -0
- package/src/domain/services/IntegrityVerifier.js +199 -0
- package/src/domain/services/KeyResolver.js +49 -42
- package/src/domain/services/ManifestDiff.js +56 -0
- package/src/domain/services/ManifestRepository.js +286 -0
- package/src/domain/services/PrefetchWindow.js +48 -0
- package/src/domain/services/RecipientService.js +157 -0
- package/src/domain/services/RedactingObservability.js +169 -0
- package/src/domain/services/RestorePipeline.js +68 -0
- package/src/domain/services/StorePipeline.js +201 -0
- package/src/domain/services/VaultKeyVerifier.js +101 -0
- package/src/domain/services/VaultMetadataCodec.js +206 -0
- package/src/domain/services/VaultMutationRetryPolicy.js +105 -0
- package/src/domain/services/VaultPersistence.js +410 -0
- package/src/domain/services/VaultPrivacyIndex.js +191 -0
- package/src/domain/services/VaultService.js +749 -251
- package/src/domain/services/VaultStateCache.js +225 -0
- package/src/domain/services/VaultTreeCodec.js +121 -0
- package/src/domain/services/rotateVaultPassphrase.js +37 -20
- package/src/domain/strategies/Aad.js +39 -0
- package/src/domain/strategies/FramedRecordCodec.js +204 -0
- package/src/domain/strategies/RestoreCompressed.js +40 -0
- package/src/domain/strategies/RestoreConvergent.js +44 -0
- package/src/domain/strategies/RestoreFramed.js +55 -0
- package/src/domain/strategies/RestorePlain.js +37 -0
- package/src/domain/strategies/RestoreStrategy.js +35 -0
- package/src/domain/strategies/RestoreWhole.js +135 -0
- package/src/domain/strategies/StoreConvergent.js +28 -0
- package/src/domain/strategies/StoreFramed.js +39 -0
- package/src/domain/strategies/StorePlain.js +20 -0
- package/src/domain/strategies/StoreStrategy.js +61 -0
- package/src/domain/strategies/StoreWhole.js +34 -0
- package/src/domain/value-objects/Chunk.js +1 -2
- package/src/domain/value-objects/EncryptionMetadata.js +87 -0
- package/src/domain/value-objects/Manifest.d.ts +40 -4
- package/src/domain/value-objects/Manifest.js +61 -20
- package/src/domain/value-objects/Oid.js +45 -0
- package/src/domain/value-objects/Slug.js +124 -0
- package/src/domain/value-objects/StoreEncryptionConfig.js +114 -0
- package/src/helpers/aesGcmMeta.js +55 -0
- package/src/helpers/canonicalBase64.js +1 -0
- package/src/helpers/kdfPolicy.js +194 -0
- package/src/infrastructure/adapters/BunCryptoAdapter.js +124 -8
- package/src/infrastructure/adapters/FileIOHelper.js +178 -13
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +235 -42
- package/src/infrastructure/adapters/GitPlumbingFactoryAdapter.js +62 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +19 -6
- package/src/infrastructure/adapters/NodeCompressionAdapter.js +64 -0
- package/src/infrastructure/adapters/NodeCryptoAdapter.js +130 -10
- package/src/infrastructure/adapters/WebCryptoAdapter.js +187 -77
- package/src/infrastructure/chunkers/CdcChunker.js +53 -19
- package/src/infrastructure/chunkers/FixedChunker.js +15 -7
- package/src/infrastructure/chunkers/resolveChunker.js +3 -1
- package/src/infrastructure/codecs/CborCodec.js +3 -3
- package/src/infrastructure/codecs/JsonCodec.js +5 -5
- package/src/infrastructure/createGitPlumbing.js +22 -0
- package/src/package-version.js +1 -0
- package/src/ports/ChunkingPort.js +3 -3
- package/src/ports/CodecPort.js +4 -4
- package/src/ports/CompressionPort.js +57 -0
- package/src/ports/CryptoPort.js +138 -50
- package/src/ports/GitPersistencePort.js +32 -2
- package/src/ports/GitPlumbingFactoryPort.js +16 -0
- package/src/ports/GitRefPort.js +1 -1
- package/src/types/ambient.d.ts +10 -5
- package/test/CONVENTIONS.md +94 -0
- package/test/cycles/README.md +13 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { CasError, ErrorCodes } from '../errors/index.js';
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_VAULT_STATE_CACHE_ENTRIES = 128;
|
|
4
|
+
|
|
5
|
+
const MAX_ENTRIES_OPTION = 'maxEntries';
|
|
6
|
+
const MIN_CACHE_ENTRIES = 1;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Cache for parse-stable vault tree snapshots keyed by immutable tree OID.
|
|
10
|
+
*/
|
|
11
|
+
export default class VaultStateCache {
|
|
12
|
+
/** @type {Map<string, object>} */
|
|
13
|
+
#trees = new Map();
|
|
14
|
+
|
|
15
|
+
/** @type {number} */
|
|
16
|
+
#maxEntries;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {object} [options]
|
|
20
|
+
* @param {number} [options.maxEntries=DEFAULT_VAULT_STATE_CACHE_ENTRIES]
|
|
21
|
+
*/
|
|
22
|
+
constructor({ maxEntries = DEFAULT_VAULT_STATE_CACHE_ENTRIES } = {}) {
|
|
23
|
+
assertMaxEntries(maxEntries);
|
|
24
|
+
this.#maxEntries = maxEntries;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {string} treeOid
|
|
29
|
+
* @returns {object|undefined}
|
|
30
|
+
*/
|
|
31
|
+
get(treeOid) {
|
|
32
|
+
const cached = this.#trees.get(treeOid);
|
|
33
|
+
if (cached) {
|
|
34
|
+
this.#rememberRecentTree(treeOid, cached);
|
|
35
|
+
}
|
|
36
|
+
return cached;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {string} treeOid
|
|
41
|
+
* @param {{ rawEntries: Array<object>, metadata: object|null }} snapshot
|
|
42
|
+
* @returns {object}
|
|
43
|
+
*/
|
|
44
|
+
rememberTree(treeOid, snapshot) {
|
|
45
|
+
const cached = {
|
|
46
|
+
rawEntries: snapshot.rawEntries.map((entry) => ({ ...entry })),
|
|
47
|
+
metadata: cloneMetadata(snapshot.metadata),
|
|
48
|
+
plainEntries: null,
|
|
49
|
+
privacyEntriesByKey: new WeakMap(),
|
|
50
|
+
verifiedEncryptionKeys: new WeakMap(),
|
|
51
|
+
};
|
|
52
|
+
if (this.#trees.has(treeOid)) {
|
|
53
|
+
this.#trees.delete(treeOid);
|
|
54
|
+
}
|
|
55
|
+
this.#trees.set(treeOid, cached);
|
|
56
|
+
this.#evictOldestTrees();
|
|
57
|
+
return cached;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {string} treeOid
|
|
62
|
+
* @param {object} cached
|
|
63
|
+
*/
|
|
64
|
+
#rememberRecentTree(treeOid, cached) {
|
|
65
|
+
this.#trees.delete(treeOid);
|
|
66
|
+
this.#trees.set(treeOid, cached);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#evictOldestTrees() {
|
|
70
|
+
while (this.#trees.size > this.#maxEntries) {
|
|
71
|
+
this.#trees.delete(this.#trees.keys().next().value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {object} snapshot
|
|
77
|
+
* @param {(rawEntries: Array<object>) => Map<string, string>} parseEntries
|
|
78
|
+
* @returns {Map<string, string>}
|
|
79
|
+
*/
|
|
80
|
+
plainEntries(snapshot, parseEntries) {
|
|
81
|
+
if (!snapshot.plainEntries) {
|
|
82
|
+
snapshot.plainEntries = parseEntries(snapshot.rawEntries);
|
|
83
|
+
}
|
|
84
|
+
return new Map(snapshot.plainEntries);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @param {object} snapshot
|
|
89
|
+
* @param {Uint8Array} encryptionKey
|
|
90
|
+
* @param {(rawEntries: Array<object>, metadata: object|null, encryptionKey: Uint8Array) => Promise<Map<string, string>>} resolveEntries
|
|
91
|
+
* @returns {Promise<Map<string, string>>}
|
|
92
|
+
*/
|
|
93
|
+
async privacyEntries(snapshot, encryptionKey, resolveEntries) {
|
|
94
|
+
let cached = snapshot.privacyEntriesByKey.get(encryptionKey);
|
|
95
|
+
if (!cached || !bytesEqual(cached.keyBytes, encryptionKey)) {
|
|
96
|
+
cached = this.#startPrivacyEntryResolution(snapshot, encryptionKey, resolveEntries);
|
|
97
|
+
snapshot.privacyEntriesByKey.set(encryptionKey, cached);
|
|
98
|
+
}
|
|
99
|
+
const entries = cached.entries || await cached.pending;
|
|
100
|
+
return new Map(entries);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param {object} snapshot
|
|
105
|
+
* @param {Uint8Array} encryptionKey
|
|
106
|
+
* @param {(rawEntries: Array<object>, metadata: object|null, encryptionKey: Uint8Array) => Promise<Map<string, string>>} resolveEntries
|
|
107
|
+
* @returns {{ keyBytes: Uint8Array, entries: Map<string, string>|null, pending: Promise<Map<string, string>> }}
|
|
108
|
+
*/
|
|
109
|
+
#startPrivacyEntryResolution(snapshot, encryptionKey, resolveEntries) {
|
|
110
|
+
const cached = {
|
|
111
|
+
entries: null,
|
|
112
|
+
keyBytes: cloneBytes(encryptionKey),
|
|
113
|
+
pending: null,
|
|
114
|
+
};
|
|
115
|
+
cached.pending = this.#resolvePrivacyEntries({
|
|
116
|
+
cached,
|
|
117
|
+
encryptionKey,
|
|
118
|
+
resolveEntries,
|
|
119
|
+
snapshot,
|
|
120
|
+
});
|
|
121
|
+
return cached;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @param {object} context
|
|
126
|
+
* @param {{ entries: Map<string, string>|null, pending: Promise<Map<string, string>>|null }} context.cached
|
|
127
|
+
* @param {Uint8Array} context.encryptionKey
|
|
128
|
+
* @param {(rawEntries: Array<object>, metadata: object|null, encryptionKey: Uint8Array) => Promise<Map<string, string>>} context.resolveEntries
|
|
129
|
+
* @param {object} context.snapshot
|
|
130
|
+
* @returns {Promise<Map<string, string>>}
|
|
131
|
+
*/
|
|
132
|
+
async #resolvePrivacyEntries({ cached, encryptionKey, resolveEntries, snapshot }) {
|
|
133
|
+
try {
|
|
134
|
+
const entries = await resolveEntries(snapshot.rawEntries, snapshot.metadata, encryptionKey);
|
|
135
|
+
cached.entries = entries;
|
|
136
|
+
return entries;
|
|
137
|
+
} catch (err) {
|
|
138
|
+
if (snapshot.privacyEntriesByKey.get(encryptionKey) === cached) {
|
|
139
|
+
snapshot.privacyEntriesByKey.delete(encryptionKey);
|
|
140
|
+
}
|
|
141
|
+
throw err;
|
|
142
|
+
} finally {
|
|
143
|
+
cached.pending = null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {object} snapshot
|
|
149
|
+
* @param {Uint8Array} encryptionKey
|
|
150
|
+
* @returns {boolean}
|
|
151
|
+
*/
|
|
152
|
+
hasVerifiedEncryptionKey(snapshot, encryptionKey) {
|
|
153
|
+
const verifiedKeyBytes = snapshot.verifiedEncryptionKeys.get(encryptionKey);
|
|
154
|
+
return Boolean(verifiedKeyBytes && bytesEqual(verifiedKeyBytes, encryptionKey));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @param {object} snapshot
|
|
159
|
+
* @param {Uint8Array} encryptionKey
|
|
160
|
+
*/
|
|
161
|
+
rememberVerifiedEncryptionKey(snapshot, encryptionKey) {
|
|
162
|
+
snapshot.verifiedEncryptionKeys.set(encryptionKey, cloneBytes(encryptionKey));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @param {object} options
|
|
167
|
+
* @param {Map<string, string>} options.entries
|
|
168
|
+
* @param {object|null} options.metadata
|
|
169
|
+
* @param {string|null} options.parentCommitOid
|
|
170
|
+
* @returns {{ entries: Map<string, string>, parentCommitOid: string|null, metadata: object|null }}
|
|
171
|
+
*/
|
|
172
|
+
toState({ entries, metadata, parentCommitOid }) {
|
|
173
|
+
return {
|
|
174
|
+
entries: new Map(entries),
|
|
175
|
+
parentCommitOid,
|
|
176
|
+
metadata: cloneMetadata(metadata),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* @param {object|null} metadata
|
|
183
|
+
* @returns {object|null}
|
|
184
|
+
*/
|
|
185
|
+
function cloneMetadata(metadata) {
|
|
186
|
+
return metadata ? JSON.parse(JSON.stringify(metadata)) : null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @param {Uint8Array} bytes
|
|
191
|
+
* @returns {Uint8Array}
|
|
192
|
+
*/
|
|
193
|
+
function cloneBytes(bytes) {
|
|
194
|
+
return Uint8Array.from(bytes);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @param {Uint8Array} left
|
|
199
|
+
* @param {Uint8Array} right
|
|
200
|
+
* @returns {boolean}
|
|
201
|
+
*/
|
|
202
|
+
function bytesEqual(left, right) {
|
|
203
|
+
if (left.byteLength !== right.byteLength) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
let diff = 0;
|
|
207
|
+
for (let i = 0; i < left.byteLength; i += 1) {
|
|
208
|
+
diff |= left[i] ^ right[i];
|
|
209
|
+
}
|
|
210
|
+
return diff === 0;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @param {number} maxEntries
|
|
215
|
+
*/
|
|
216
|
+
function assertMaxEntries(maxEntries) {
|
|
217
|
+
if (Number.isSafeInteger(maxEntries) && maxEntries >= MIN_CACHE_ENTRIES) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
throw new CasError(
|
|
221
|
+
'VaultStateCache maxEntries must be a positive safe integer',
|
|
222
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
223
|
+
{ option: MAX_ENTRIES_OPTION, maxEntries },
|
|
224
|
+
);
|
|
225
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import Slug from '../value-objects/Slug.js';
|
|
3
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
4
|
+
|
|
5
|
+
export const VAULT_METADATA_ENTRY = '.vault.json';
|
|
6
|
+
export const VAULT_PRIVACY_INDEX_ENTRY = '.privacy-index';
|
|
7
|
+
export const GIT_TREE_MODE = '040000';
|
|
8
|
+
export const GIT_BLOB_MODE = '100644';
|
|
9
|
+
export const GIT_TREE_TYPE = 'tree';
|
|
10
|
+
export const GIT_BLOB_TYPE = 'blob';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Pure codec for the vault's structured Git tree records.
|
|
14
|
+
*/
|
|
15
|
+
export default class VaultTreeCodec {
|
|
16
|
+
/**
|
|
17
|
+
* @param {Map<string, string>} entries
|
|
18
|
+
* @returns {Array<{ mode: string, type: string, oid: string, name: string }>}
|
|
19
|
+
*/
|
|
20
|
+
assetRecordsFromPlainEntries(entries) {
|
|
21
|
+
const records = [];
|
|
22
|
+
for (const [slug, treeOid] of entries) {
|
|
23
|
+
records.push(this.assetRecord(Slug.from(slug).toTreePath(), treeOid));
|
|
24
|
+
}
|
|
25
|
+
return records;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {Map<string, string>} entries
|
|
30
|
+
* @param {Map<string, string>} persistedNameBySlug
|
|
31
|
+
* @returns {Array<{ mode: string, type: string, oid: string, name: string }>}
|
|
32
|
+
*/
|
|
33
|
+
assetRecordsFromPersistedNames(entries, persistedNameBySlug) {
|
|
34
|
+
const records = [];
|
|
35
|
+
for (const [slug, treeOid] of entries) {
|
|
36
|
+
const persistedName = persistedNameBySlug.get(slug);
|
|
37
|
+
if (!persistedName) {
|
|
38
|
+
throw new CasError(
|
|
39
|
+
`Vault persisted name missing for slug "${slug}"`,
|
|
40
|
+
ErrorCodes.VAULT_PRIVACY_INDEX_MISSING,
|
|
41
|
+
{ slug },
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
records.push(this.assetRecord(persistedName, treeOid));
|
|
45
|
+
}
|
|
46
|
+
return records;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {string} name
|
|
51
|
+
* @param {string} treeOid
|
|
52
|
+
* @returns {{ mode: string, type: string, oid: string, name: string }}
|
|
53
|
+
*/
|
|
54
|
+
assetRecord(name, treeOid) {
|
|
55
|
+
return { mode: GIT_TREE_MODE, type: GIT_TREE_TYPE, oid: treeOid, name };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} blobOid
|
|
60
|
+
* @returns {{ mode: string, type: string, oid: string, name: string }}
|
|
61
|
+
*/
|
|
62
|
+
metadataRecord(blobOid) {
|
|
63
|
+
return { mode: GIT_BLOB_MODE, type: GIT_BLOB_TYPE, oid: blobOid, name: VAULT_METADATA_ENTRY };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {string} blobOid
|
|
68
|
+
* @returns {{ mode: string, type: string, oid: string, name: string }}
|
|
69
|
+
*/
|
|
70
|
+
privacyIndexRecord(blobOid) {
|
|
71
|
+
return { mode: GIT_BLOB_MODE, type: GIT_BLOB_TYPE, oid: blobOid, name: VAULT_PRIVACY_INDEX_ENTRY };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {Array<{ mode: string, type: string, oid: string, name: string }>} records
|
|
76
|
+
* @returns {string[]}
|
|
77
|
+
*/
|
|
78
|
+
toTreeLines(records) {
|
|
79
|
+
return records.map((record) => (
|
|
80
|
+
`${record.mode} ${record.type} ${record.oid}\t${this.#validatePersistedName(record.name)}`
|
|
81
|
+
));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {Array<{ mode: string, type: string, oid: string, name: string }>} treeEntries
|
|
86
|
+
* @param {object} [options]
|
|
87
|
+
* @param {boolean} [options.privacyEnabled]
|
|
88
|
+
* @returns {{ entries: Map<string, string>, metadataBlobOid: string|null, privacyIndexBlobOid: string|null }}
|
|
89
|
+
*/
|
|
90
|
+
parseTreeEntries(treeEntries, { privacyEnabled = false } = {}) {
|
|
91
|
+
const entries = new Map();
|
|
92
|
+
let metadataBlobOid = null;
|
|
93
|
+
let privacyIndexBlobOid = null;
|
|
94
|
+
for (const entry of treeEntries) {
|
|
95
|
+
if (entry.name === VAULT_METADATA_ENTRY) {
|
|
96
|
+
metadataBlobOid = entry.oid;
|
|
97
|
+
} else if (entry.name === VAULT_PRIVACY_INDEX_ENTRY) {
|
|
98
|
+
privacyIndexBlobOid = entry.oid;
|
|
99
|
+
} else {
|
|
100
|
+
const key = privacyEnabled ? entry.name : Slug.from(Slug.decode(entry.name)).toString();
|
|
101
|
+
entries.set(key, entry.oid);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return { entries, metadataBlobOid, privacyIndexBlobOid };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {string} name
|
|
109
|
+
* @returns {string}
|
|
110
|
+
*/
|
|
111
|
+
#validatePersistedName(name) {
|
|
112
|
+
if (typeof name !== 'string' || name.length === 0 || Slug.hasControlChars(name)) {
|
|
113
|
+
throw new CasError(
|
|
114
|
+
'Vault tree entry name is invalid for git mktree',
|
|
115
|
+
ErrorCodes.INVALID_SLUG,
|
|
116
|
+
{ treePath: name },
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return name;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import CasError from '../errors/CasError.js';
|
|
2
2
|
import buildKdfMetadata from '../helpers/buildKdfMetadata.js';
|
|
3
|
+
import { prepareKdfOptions, prepareStoredKdfOptions } from '../../helpers/kdfPolicy.js';
|
|
4
|
+
import { decodeBase64 } from '../encoding/base64.js';
|
|
5
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
6
|
|
|
4
7
|
const DEFAULT_MAX_RETRIES = 3;
|
|
5
8
|
const DEFAULT_RETRY_BASE_MS = 50;
|
|
@@ -10,18 +13,14 @@ const DEFAULT_RETRY_BASE_MS = 50;
|
|
|
10
13
|
* @param {import('./CasService.js').default} service - CasService instance.
|
|
11
14
|
* @param {string} passphrase - The passphrase.
|
|
12
15
|
* @param {Object} kdf - Stored KDF params (algorithm, salt, iterations, etc.).
|
|
13
|
-
* @returns {Promise<
|
|
16
|
+
* @returns {Promise<Uint8Array>} The derived KEK.
|
|
14
17
|
*/
|
|
15
18
|
async function deriveKekFromKdf(service, passphrase, kdf) {
|
|
19
|
+
const params = prepareStoredKdfOptions(kdf, { source: 'vault-metadata' });
|
|
16
20
|
const { key } = await service.deriveKey({
|
|
17
21
|
passphrase,
|
|
18
|
-
salt:
|
|
19
|
-
|
|
20
|
-
iterations: kdf.iterations,
|
|
21
|
-
cost: kdf.cost,
|
|
22
|
-
blockSize: kdf.blockSize,
|
|
23
|
-
parallelization: kdf.parallelization,
|
|
24
|
-
keyLength: kdf.keyLength,
|
|
22
|
+
salt: decodeBase64(kdf.salt),
|
|
23
|
+
...params,
|
|
25
24
|
});
|
|
26
25
|
return key;
|
|
27
26
|
}
|
|
@@ -32,8 +31,8 @@ async function deriveKekFromKdf(service, passphrase, kdf) {
|
|
|
32
31
|
* @param {Object} options
|
|
33
32
|
* @param {import('./CasService.js').default} options.service - CasService instance.
|
|
34
33
|
* @param {Map<string, string>} options.entries - Vault entries (slug → treeOid).
|
|
35
|
-
* @param {
|
|
36
|
-
* @param {
|
|
34
|
+
* @param {Uint8Array} options.oldKek - Old key-encryption key.
|
|
35
|
+
* @param {Uint8Array} options.newKek - New key-encryption key.
|
|
37
36
|
* @returns {Promise<{ updatedEntries: Map<string, string>, rotatedSlugs: string[], skippedSlugs: string[] }>}
|
|
38
37
|
*/
|
|
39
38
|
async function rotateEntries({ service, entries, oldKek, newKek }) {
|
|
@@ -64,14 +63,14 @@ async function rotateEntries({ service, entries, oldKek, newKek }) {
|
|
|
64
63
|
* @returns {boolean}
|
|
65
64
|
*/
|
|
66
65
|
function isRetryableConflict(err, attempt, maxRetries) {
|
|
67
|
-
return err instanceof CasError && err.code ===
|
|
66
|
+
return err instanceof CasError && err.code === ErrorCodes.VAULT_CONFLICT && attempt < maxRetries - 1;
|
|
68
67
|
}
|
|
69
68
|
|
|
70
69
|
/**
|
|
71
70
|
* Builds updated vault metadata with new KDF params.
|
|
72
71
|
*
|
|
73
72
|
* @param {Object} metadata - Existing vault metadata.
|
|
74
|
-
* @param {
|
|
73
|
+
* @param {Uint8Array} newSalt - New KDF salt.
|
|
75
74
|
* @param {Object} newParams - New KDF parameters.
|
|
76
75
|
* @returns {Object} Updated metadata.
|
|
77
76
|
*/
|
|
@@ -85,6 +84,20 @@ function buildRotatedMetadata(metadata, newSalt, newParams) {
|
|
|
85
84
|
};
|
|
86
85
|
}
|
|
87
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Reads vault metadata without requiring privacy-index decryption.
|
|
89
|
+
*
|
|
90
|
+
* @param {import('./VaultService.js').default} vault - VaultService instance.
|
|
91
|
+
* @returns {Promise<Object>} Encrypted vault metadata.
|
|
92
|
+
*/
|
|
93
|
+
async function readEncryptedVaultMetadata(vault) {
|
|
94
|
+
const metadata = await vault.getVaultMetadata();
|
|
95
|
+
if (!metadata?.encryption) {
|
|
96
|
+
throw new CasError('Vault is not encrypted — nothing to rotate', ErrorCodes.VAULT_METADATA_INVALID);
|
|
97
|
+
}
|
|
98
|
+
return metadata;
|
|
99
|
+
}
|
|
100
|
+
|
|
88
101
|
/**
|
|
89
102
|
* Rotates the vault-level passphrase. Re-wraps every envelope-encrypted
|
|
90
103
|
* entry's DEK with a new KEK derived from `newPassphrase`. Entries using
|
|
@@ -108,15 +121,18 @@ export default async function rotateVaultPassphrase(
|
|
|
108
121
|
{ oldPassphrase, newPassphrase, kdfOptions, maxRetries = DEFAULT_MAX_RETRIES, retryBaseMs = DEFAULT_RETRY_BASE_MS },
|
|
109
122
|
) {
|
|
110
123
|
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
throw new CasError('Vault is not encrypted — nothing to rotate', 'VAULT_METADATA_INVALID');
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const { kdf } = state.metadata.encryption;
|
|
124
|
+
const metadata = await readEncryptedVaultMetadata(vault);
|
|
125
|
+
const { kdf } = metadata.encryption;
|
|
117
126
|
const oldKek = await deriveKekFromKdf(service, oldPassphrase, kdf);
|
|
127
|
+
await vault.verifyVaultKey({ encryptionKey: oldKek });
|
|
128
|
+
const state = await vault.readState({ encryptionKey: oldKek });
|
|
129
|
+
const nextKdfOptions = prepareKdfOptions(
|
|
130
|
+
{ ...kdfOptions, algorithm: kdfOptions?.algorithm || kdf.algorithm },
|
|
131
|
+
{ source: 'vault-rotation' },
|
|
132
|
+
);
|
|
118
133
|
const { key: newKek, salt: newSalt, params: newParams } = await service.deriveKey({
|
|
119
|
-
passphrase: newPassphrase,
|
|
134
|
+
passphrase: newPassphrase,
|
|
135
|
+
...nextKdfOptions,
|
|
120
136
|
});
|
|
121
137
|
|
|
122
138
|
const result = await rotateEntries({ service, entries: state.entries, oldKek, newKek });
|
|
@@ -128,6 +144,7 @@ export default async function rotateVaultPassphrase(
|
|
|
128
144
|
metadata: newMetadata,
|
|
129
145
|
parentCommitOid: state.parentCommitOid,
|
|
130
146
|
message: `vault: rotate passphrase (${result.rotatedSlugs.length} rotated, ${result.skippedSlugs.length} skipped)`,
|
|
147
|
+
encryptionKey: newKek,
|
|
131
148
|
});
|
|
132
149
|
return { commitOid, rotatedSlugs: result.rotatedSlugs, skippedSlugs: result.skippedSlugs };
|
|
133
150
|
} catch (err) {
|
|
@@ -139,5 +156,5 @@ export default async function rotateVaultPassphrase(
|
|
|
139
156
|
}
|
|
140
157
|
}
|
|
141
158
|
/* c8 ignore next 2 */
|
|
142
|
-
throw new CasError('Vault CAS retries exhausted',
|
|
159
|
+
throw new CasError('Vault CAS retries exhausted', ErrorCodes.VAULT_CONFLICT);
|
|
143
160
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { writeUint32BE } from '../bytes/ByteLayout.js';
|
|
2
|
+
import { utf8Encode } from '../encoding/utf8.js';
|
|
3
|
+
import createCasError from '../errors/createCasError.js';
|
|
4
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
5
|
+
|
|
6
|
+
function assertFrameIndex(frameIndex) {
|
|
7
|
+
if (!Number.isInteger(frameIndex) || frameIndex < 0 || frameIndex > 0xffffffff) {
|
|
8
|
+
throw createCasError(
|
|
9
|
+
`Framed AAD frame index must be an unsigned 32-bit integer; received ${frameIndex}`,
|
|
10
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
11
|
+
{ frameIndex },
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Builds AAD for whole encryption: UTF-8 bytes of the slug.
|
|
18
|
+
* @param {string} slug
|
|
19
|
+
* @returns {Uint8Array}
|
|
20
|
+
*/
|
|
21
|
+
export function buildWholeAad(slug) {
|
|
22
|
+
return utf8Encode(slug);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Builds AAD for framed encryption: UTF-8 slug + NUL + 4-byte BE frame index.
|
|
27
|
+
* @param {string} slug
|
|
28
|
+
* @param {number} frameIndex
|
|
29
|
+
* @returns {Uint8Array}
|
|
30
|
+
*/
|
|
31
|
+
export function buildFramedAad(slug, frameIndex) {
|
|
32
|
+
assertFrameIndex(frameIndex);
|
|
33
|
+
const slugBytes = utf8Encode(slug);
|
|
34
|
+
const bytes = new Uint8Array(slugBytes.length + 5);
|
|
35
|
+
bytes.set(slugBytes, 0);
|
|
36
|
+
bytes[slugBytes.length] = 0;
|
|
37
|
+
writeUint32BE(bytes, slugBytes.length + 1, frameIndex);
|
|
38
|
+
return bytes;
|
|
39
|
+
}
|