@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,61 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SCHEME_CONVERGENT,
|
|
3
|
+
SCHEME_FRAMED,
|
|
4
|
+
SCHEME_WHOLE,
|
|
5
|
+
} from '../encryption/schemes.js';
|
|
6
|
+
import createCasError from '../errors/createCasError.js';
|
|
7
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
8
|
+
|
|
9
|
+
const ENCRYPTED_STRATEGY_BY_SCHEME = Object.freeze({
|
|
10
|
+
[SCHEME_CONVERGENT]: 'convergent',
|
|
11
|
+
[SCHEME_FRAMED]: 'framed',
|
|
12
|
+
[SCHEME_WHOLE]: 'whole',
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Selects the store strategy entity for the current request.
|
|
17
|
+
*/
|
|
18
|
+
export default class StoreStrategy {
|
|
19
|
+
/**
|
|
20
|
+
* @param {Object} options
|
|
21
|
+
* @param {{ key?: Uint8Array }} options.keyInfo
|
|
22
|
+
* @param {{ scheme?: string }} [options.encryptionConfig]
|
|
23
|
+
* @param {{ strategy?: string }} options.chunker
|
|
24
|
+
* @param {{ log: Function }} options.observability
|
|
25
|
+
* @param {{ plain: object, convergent: object, framed: object, whole: object }} options.strategies
|
|
26
|
+
* @returns {object}
|
|
27
|
+
*/
|
|
28
|
+
static for({ keyInfo, encryptionConfig, chunker, observability, strategies }) {
|
|
29
|
+
if (!keyInfo.key) {
|
|
30
|
+
return strategies.plain;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const strategyName = StoreStrategy.#resolveEncryptedStrategyName(encryptionConfig?.scheme);
|
|
34
|
+
if (strategyName !== 'convergent') {
|
|
35
|
+
StoreStrategy.#warnEncryptedCdc({ chunker, observability });
|
|
36
|
+
}
|
|
37
|
+
return strategies[strategyName];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static #resolveEncryptedStrategyName(scheme) {
|
|
41
|
+
if (Object.hasOwn(ENCRYPTED_STRATEGY_BY_SCHEME, scheme)) {
|
|
42
|
+
return ENCRYPTED_STRATEGY_BY_SCHEME[scheme];
|
|
43
|
+
}
|
|
44
|
+
throw createCasError(
|
|
45
|
+
`Encrypted store requires a current encryption scheme; received ${scheme ?? 'none'}`,
|
|
46
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
47
|
+
{ scheme },
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static #warnEncryptedCdc({ chunker, observability }) {
|
|
52
|
+
if (chunker.strategy !== 'cdc') {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
observability.log(
|
|
56
|
+
'warn',
|
|
57
|
+
'CDC deduplication is ineffective with encryption — ciphertext is pseudorandom',
|
|
58
|
+
{ strategy: 'cdc' },
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { SCHEME_WHOLE } from '../encryption/schemes.js';
|
|
2
|
+
import { buildWholeAad } from './Aad.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Stores whole-object encrypted content through the CryptoPort stream API.
|
|
6
|
+
*/
|
|
7
|
+
export default class StoreWhole {
|
|
8
|
+
#chunks;
|
|
9
|
+
#crypto;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {Object} options
|
|
13
|
+
* @param {import('../services/ChunkRepository.js').default} options.chunks
|
|
14
|
+
* @param {import('../../ports/CryptoPort.js').default} options.crypto
|
|
15
|
+
*/
|
|
16
|
+
constructor({ chunks, crypto }) {
|
|
17
|
+
this.#chunks = chunks;
|
|
18
|
+
this.#crypto = crypto;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {{ processedSource: AsyncIterable<Uint8Array>, manifestData: { slug: string, chunks: Array, size: number, encryption?: object }, keyInfo: { key: Uint8Array, encExtra: object } }} options
|
|
23
|
+
*/
|
|
24
|
+
async execute({ processedSource, manifestData, keyInfo }) {
|
|
25
|
+
const aad = buildWholeAad(manifestData.slug);
|
|
26
|
+
const { encrypt, finalize } = this.#crypto.createEncryptionStream(keyInfo.key, aad);
|
|
27
|
+
await this.#chunks.chunkAndStore(encrypt(processedSource), manifestData);
|
|
28
|
+
manifestData.encryption = {
|
|
29
|
+
...finalize(),
|
|
30
|
+
scheme: SCHEME_WHOLE,
|
|
31
|
+
...keyInfo.encExtra,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import {
|
|
3
|
+
SCHEME_CONVERGENT,
|
|
4
|
+
SCHEME_FRAMED,
|
|
5
|
+
SCHEME_WHOLE,
|
|
6
|
+
} from '../encryption/schemes.js';
|
|
7
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Immutable validated encryption metadata for a manifest.
|
|
11
|
+
*/
|
|
12
|
+
export default class EncryptionMetadata {
|
|
13
|
+
/**
|
|
14
|
+
* @param {object} fields
|
|
15
|
+
*/
|
|
16
|
+
constructor(fields) {
|
|
17
|
+
Object.assign(this, fields);
|
|
18
|
+
Object.freeze(this);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {{ slug?: string, encryption?: object }} manifest
|
|
23
|
+
* @returns {EncryptionMetadata|undefined}
|
|
24
|
+
*/
|
|
25
|
+
static fromManifest(manifest) {
|
|
26
|
+
const meta = manifest.encryption;
|
|
27
|
+
if (!meta) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
EncryptionMetadata.#validateCommon(manifest, meta);
|
|
31
|
+
|
|
32
|
+
if (meta.scheme === SCHEME_WHOLE) {
|
|
33
|
+
return EncryptionMetadata.#whole(manifest, meta);
|
|
34
|
+
}
|
|
35
|
+
if (meta.scheme === SCHEME_FRAMED) {
|
|
36
|
+
return EncryptionMetadata.#framed(manifest, meta);
|
|
37
|
+
}
|
|
38
|
+
if (meta.scheme === SCHEME_CONVERGENT) {
|
|
39
|
+
return new EncryptionMetadata({ ...meta, scheme: SCHEME_CONVERGENT });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
throw createCasError(
|
|
43
|
+
`Encrypted manifest uses unknown scheme: ${meta.scheme}`,
|
|
44
|
+
ErrorCodes.INTEGRITY_ERROR,
|
|
45
|
+
{ slug: manifest.slug, reason: 'manifest-encryption-scheme', scheme: meta.scheme },
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static #validateCommon(manifest, meta) {
|
|
50
|
+
if (meta.encrypted !== true) {
|
|
51
|
+
throw createCasError(
|
|
52
|
+
'Encrypted manifest metadata was downgraded or is invalid',
|
|
53
|
+
ErrorCodes.INTEGRITY_ERROR,
|
|
54
|
+
{ slug: manifest.slug, reason: 'manifest-encryption-downgrade' },
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
if (meta.algorithm !== 'aes-256-gcm') {
|
|
58
|
+
throw createCasError(
|
|
59
|
+
`Encrypted manifest uses unexpected algorithm: ${meta.algorithm}`,
|
|
60
|
+
ErrorCodes.INTEGRITY_ERROR,
|
|
61
|
+
{ slug: manifest.slug, reason: 'manifest-encryption-algorithm', algorithm: meta.algorithm },
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static #whole(manifest, meta) {
|
|
67
|
+
if (typeof meta.nonce !== 'string' || meta.nonce.length === 0 || typeof meta.tag !== 'string' || meta.tag.length === 0) {
|
|
68
|
+
throw createCasError(
|
|
69
|
+
'Whole encrypted manifest is missing nonce/tag metadata',
|
|
70
|
+
ErrorCodes.INTEGRITY_ERROR,
|
|
71
|
+
{ slug: manifest.slug, reason: 'manifest-encryption-meta' },
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return new EncryptionMetadata({ ...meta, scheme: SCHEME_WHOLE });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static #framed(manifest, meta) {
|
|
78
|
+
if (!Number.isInteger(meta.frameBytes) || meta.frameBytes < 1) {
|
|
79
|
+
throw createCasError(
|
|
80
|
+
'Framed encrypted manifest is missing a valid frameBytes value',
|
|
81
|
+
ErrorCodes.INTEGRITY_ERROR,
|
|
82
|
+
{ slug: manifest.slug, reason: 'manifest-encryption-frame-bytes', frameBytes: meta.frameBytes },
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return new EncryptionMetadata({ ...meta, scheme: SCHEME_FRAMED, frameBytes: meta.frameBytes });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -21,17 +21,47 @@ export interface RecipientEntry {
|
|
|
21
21
|
keyVersion?: number;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
export type EncryptionScheme = "whole" | "framed" | "convergent";
|
|
25
|
+
|
|
26
|
+
export interface WholeEncryptionMeta {
|
|
27
|
+
scheme: "whole";
|
|
28
|
+
algorithm: "aes-256-gcm";
|
|
27
29
|
nonce: string;
|
|
28
30
|
tag: string;
|
|
29
|
-
|
|
31
|
+
frameBytes?: never;
|
|
32
|
+
encrypted: true;
|
|
33
|
+
kdf?: KdfParams;
|
|
34
|
+
recipients?: RecipientEntry[];
|
|
35
|
+
keyVersion?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface FramedEncryptionMeta {
|
|
39
|
+
scheme: "framed";
|
|
40
|
+
algorithm: "aes-256-gcm";
|
|
41
|
+
nonce?: never;
|
|
42
|
+
tag?: never;
|
|
43
|
+
frameBytes: number;
|
|
44
|
+
encrypted: true;
|
|
30
45
|
kdf?: KdfParams;
|
|
31
46
|
recipients?: RecipientEntry[];
|
|
32
47
|
keyVersion?: number;
|
|
33
48
|
}
|
|
34
49
|
|
|
50
|
+
export interface ConvergentEncryptionMeta {
|
|
51
|
+
scheme: "convergent";
|
|
52
|
+
algorithm: "aes-256-gcm";
|
|
53
|
+
nonce?: never;
|
|
54
|
+
tag?: never;
|
|
55
|
+
frameBytes?: never;
|
|
56
|
+
encrypted: true;
|
|
57
|
+
kdf?: KdfParams;
|
|
58
|
+
recipients?: RecipientEntry[];
|
|
59
|
+
keyVersion?: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** AES-256-GCM encryption metadata attached to an encrypted manifest. */
|
|
63
|
+
export type EncryptionMeta = WholeEncryptionMeta | FramedEncryptionMeta | ConvergentEncryptionMeta;
|
|
64
|
+
|
|
35
65
|
/** Compression metadata. */
|
|
36
66
|
export interface CompressionMeta {
|
|
37
67
|
algorithm: "gzip";
|
|
@@ -47,13 +77,16 @@ export interface SubManifestRef {
|
|
|
47
77
|
/** Raw manifest data accepted by the {@link Manifest} constructor. */
|
|
48
78
|
export interface ManifestData {
|
|
49
79
|
version?: number;
|
|
80
|
+
formatVersion?: string;
|
|
50
81
|
slug: string;
|
|
51
82
|
filename: string;
|
|
52
83
|
size: number;
|
|
53
84
|
chunks: Array<{ index: number; size: number; digest: string; blob: string }>;
|
|
54
85
|
encryption?: EncryptionMeta;
|
|
55
86
|
compression?: CompressionMeta;
|
|
87
|
+
chunking?: { strategy: "fixed"; params: { chunkSize: number } } | { strategy: "cdc"; params: { target: number; min: number; max: number; normalized?: boolean } };
|
|
56
88
|
subManifests?: SubManifestRef[];
|
|
89
|
+
manifestHash?: string;
|
|
57
90
|
}
|
|
58
91
|
|
|
59
92
|
/**
|
|
@@ -61,13 +94,16 @@ export interface ManifestData {
|
|
|
61
94
|
*/
|
|
62
95
|
export default class Manifest {
|
|
63
96
|
readonly version: number;
|
|
97
|
+
readonly formatVersion?: string;
|
|
64
98
|
readonly slug: string;
|
|
65
99
|
readonly filename: string;
|
|
66
100
|
readonly size: number;
|
|
67
101
|
readonly chunks: readonly Chunk[];
|
|
68
102
|
readonly encryption?: EncryptionMeta;
|
|
69
103
|
readonly compression?: CompressionMeta;
|
|
104
|
+
readonly chunking?: ManifestData["chunking"];
|
|
70
105
|
readonly subManifests?: readonly SubManifestRef[];
|
|
106
|
+
readonly manifestHash?: string;
|
|
71
107
|
|
|
72
108
|
constructor(data: ManifestData);
|
|
73
109
|
|
|
@@ -16,25 +16,28 @@ export default class Manifest {
|
|
|
16
16
|
* @param {string} data.filename - Original filename.
|
|
17
17
|
* @param {number} data.size - Total size in bytes.
|
|
18
18
|
* @param {Array<{ index: number, size: number, digest: string, blob: string }>} data.chunks - Chunk metadata.
|
|
19
|
-
* @param {{ algorithm: string, nonce
|
|
19
|
+
* @param {{ algorithm: string, nonce?: string, tag?: string, frameBytes?: number, encrypted: boolean }} [data.encryption] - Encryption metadata.
|
|
20
20
|
* @throws {Error} If data fails schema validation.
|
|
21
21
|
*/
|
|
22
22
|
constructor(data) {
|
|
23
23
|
try {
|
|
24
|
-
ManifestSchema.parse(data);
|
|
25
|
-
this.version =
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
31
|
-
|
|
24
|
+
const parsed = ManifestSchema.parse(data);
|
|
25
|
+
this.version = parsed.version;
|
|
26
|
+
this.formatVersion = parsed.formatVersion;
|
|
27
|
+
this.slug = parsed.slug;
|
|
28
|
+
this.filename = parsed.filename;
|
|
29
|
+
this.size = parsed.size;
|
|
30
|
+
this.chunks = parsed.chunks.map((c) => new Chunk(c));
|
|
31
|
+
this.encryption = parsed.encryption
|
|
32
|
+
? { ...parsed.encryption, recipients: parsed.encryption.recipients?.map((r) => ({ ...r })) }
|
|
32
33
|
: undefined;
|
|
33
|
-
this.compression =
|
|
34
|
-
this.chunking =
|
|
35
|
-
? { strategy:
|
|
34
|
+
this.compression = parsed.compression ? { ...parsed.compression } : undefined;
|
|
35
|
+
this.chunking = parsed.chunking
|
|
36
|
+
? { strategy: parsed.chunking.strategy, params: { ...parsed.chunking.params } }
|
|
36
37
|
: undefined;
|
|
37
|
-
this.subManifests =
|
|
38
|
+
this.subManifests = parsed.subManifests ? parsed.subManifests.map((s) => ({ ...s })) : undefined;
|
|
39
|
+
this.manifestHash = parsed.manifestHash;
|
|
40
|
+
Manifest.#deepFreeze(this);
|
|
38
41
|
Object.freeze(this);
|
|
39
42
|
} catch (error) {
|
|
40
43
|
if (error instanceof ZodError) {
|
|
@@ -45,20 +48,58 @@ export default class Manifest {
|
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
/**
|
|
48
|
-
*
|
|
51
|
+
* Deep-freezes nested mutable objects on a Manifest instance.
|
|
52
|
+
* Called before the top-level `Object.freeze(this)`.
|
|
53
|
+
* @param {Manifest} manifest
|
|
54
|
+
*/
|
|
55
|
+
static #deepFreeze(manifest) {
|
|
56
|
+
if (manifest.encryption) {
|
|
57
|
+
if (manifest.encryption.kdf) { Object.freeze(manifest.encryption.kdf); }
|
|
58
|
+
if (manifest.encryption.recipients) {
|
|
59
|
+
manifest.encryption.recipients.forEach((r) => Object.freeze(r));
|
|
60
|
+
Object.freeze(manifest.encryption.recipients);
|
|
61
|
+
}
|
|
62
|
+
Object.freeze(manifest.encryption);
|
|
63
|
+
}
|
|
64
|
+
if (manifest.compression) { Object.freeze(manifest.compression); }
|
|
65
|
+
if (manifest.chunking) {
|
|
66
|
+
if (manifest.chunking.params) { Object.freeze(manifest.chunking.params); }
|
|
67
|
+
Object.freeze(manifest.chunking);
|
|
68
|
+
}
|
|
69
|
+
if (manifest.subManifests) {
|
|
70
|
+
manifest.subManifests.forEach((s) => Object.freeze(s));
|
|
71
|
+
Object.freeze(manifest.subManifests);
|
|
72
|
+
}
|
|
73
|
+
if (manifest.chunks) { Object.freeze(manifest.chunks); }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Serializes the manifest to a mutable plain object suitable for JSON/CBOR encoding.
|
|
78
|
+
* Returns deep copies of nested objects so callers can freely mutate the result.
|
|
49
79
|
* @returns {{ slug: string, filename: string, size: number, chunks: Array, encryption?: Object }}
|
|
50
80
|
*/
|
|
51
81
|
toJSON() {
|
|
52
|
-
|
|
82
|
+
const obj = {
|
|
53
83
|
version: this.version,
|
|
84
|
+
formatVersion: this.formatVersion,
|
|
54
85
|
slug: this.slug,
|
|
55
86
|
filename: this.filename,
|
|
56
87
|
size: this.size,
|
|
57
|
-
chunks: this.chunks,
|
|
58
|
-
encryption: this.encryption
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
88
|
+
chunks: this.chunks.map((c) => ({ ...c })),
|
|
89
|
+
encryption: this.encryption
|
|
90
|
+
? { ...this.encryption, recipients: this.encryption.recipients?.map((r) => ({ ...r })) }
|
|
91
|
+
: undefined,
|
|
92
|
+
compression: this.compression ? { ...this.compression } : undefined,
|
|
93
|
+
chunking: this.chunking
|
|
94
|
+
? { ...this.chunking, params: { ...this.chunking.params } }
|
|
95
|
+
: undefined,
|
|
96
|
+
subManifests: this.subManifests ? this.subManifests.map((s) => ({ ...s })) : undefined,
|
|
97
|
+
manifestHash: this.manifestHash,
|
|
62
98
|
};
|
|
99
|
+
// Remove undefined values for CBOR codec compatibility
|
|
100
|
+
for (const key of Object.keys(obj)) {
|
|
101
|
+
if (obj[key] === undefined) { delete obj[key]; }
|
|
102
|
+
}
|
|
103
|
+
return obj;
|
|
63
104
|
}
|
|
64
105
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
|
|
4
|
+
const OID_PATTERN = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/i;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Immutable Git object identifier value object.
|
|
8
|
+
*/
|
|
9
|
+
export default class Oid {
|
|
10
|
+
#value;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} value
|
|
14
|
+
*/
|
|
15
|
+
constructor(value) {
|
|
16
|
+
if (typeof value !== 'string' || !OID_PATTERN.test(value)) {
|
|
17
|
+
throw createCasError('Git OID must be a 40- or 64-character hexadecimal string', ErrorCodes.INVALID_OID, { oid: value });
|
|
18
|
+
}
|
|
19
|
+
this.#value = value.toLowerCase();
|
|
20
|
+
Object.freeze(this);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {string|Oid} value
|
|
25
|
+
* @returns {Oid}
|
|
26
|
+
*/
|
|
27
|
+
static from(value) {
|
|
28
|
+
return value instanceof Oid ? value : new Oid(value);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {unknown} value
|
|
33
|
+
* @returns {boolean}
|
|
34
|
+
*/
|
|
35
|
+
static isValid(value) {
|
|
36
|
+
return typeof value === 'string' && OID_PATTERN.test(value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @returns {string}
|
|
41
|
+
*/
|
|
42
|
+
toString() {
|
|
43
|
+
return this.#value;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import { utf8ByteLength } from '../encoding/utf8.js';
|
|
3
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Immutable domain value object for user-facing CAS/vault slugs.
|
|
7
|
+
*/
|
|
8
|
+
export default class Slug {
|
|
9
|
+
/** @type {string} */
|
|
10
|
+
#value;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} value
|
|
14
|
+
*/
|
|
15
|
+
constructor(value) {
|
|
16
|
+
Slug.validate(value);
|
|
17
|
+
this.#value = value;
|
|
18
|
+
Object.freeze(this);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {string | Slug} value
|
|
23
|
+
* @returns {Slug}
|
|
24
|
+
*/
|
|
25
|
+
static from(value) {
|
|
26
|
+
return value instanceof Slug ? value : new Slug(value);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Validates a slug string.
|
|
31
|
+
*
|
|
32
|
+
* @param {unknown} slug
|
|
33
|
+
* @throws {CasError} INVALID_SLUG when the slug is not valid.
|
|
34
|
+
*/
|
|
35
|
+
static validate(slug) {
|
|
36
|
+
if (typeof slug !== 'string' || slug.length === 0) {
|
|
37
|
+
throw new CasError('Slug must be a non-empty string', ErrorCodes.INVALID_SLUG, { slug });
|
|
38
|
+
}
|
|
39
|
+
if (slug.startsWith('/') || slug.endsWith('/')) {
|
|
40
|
+
throw new CasError('Slug must not start or end with "/"', ErrorCodes.INVALID_SLUG, { slug });
|
|
41
|
+
}
|
|
42
|
+
if (utf8ByteLength(slug) > 1024) {
|
|
43
|
+
throw new CasError('Slug exceeds 1024 bytes total', ErrorCodes.INVALID_SLUG, { slug });
|
|
44
|
+
}
|
|
45
|
+
for (const segment of slug.split('/')) {
|
|
46
|
+
Slug.#validateSegment(segment, slug);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {string} segment
|
|
52
|
+
* @param {string} slug
|
|
53
|
+
*/
|
|
54
|
+
static #validateSegment(segment, slug) {
|
|
55
|
+
if (segment.length === 0) {
|
|
56
|
+
throw new CasError('Slug contains empty segment', ErrorCodes.INVALID_SLUG, { slug });
|
|
57
|
+
}
|
|
58
|
+
if (segment === '.' || segment === '..') {
|
|
59
|
+
throw new CasError('Slug contains "." or ".." segment', ErrorCodes.INVALID_SLUG, { slug });
|
|
60
|
+
}
|
|
61
|
+
if (utf8ByteLength(segment) > 255) {
|
|
62
|
+
throw new CasError('Slug segment exceeds 255 bytes', ErrorCodes.INVALID_SLUG, { slug });
|
|
63
|
+
}
|
|
64
|
+
if (Slug.hasControlChars(segment)) {
|
|
65
|
+
throw new CasError('Slug contains control characters', ErrorCodes.INVALID_SLUG, { slug });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {string} value
|
|
71
|
+
* @returns {boolean}
|
|
72
|
+
*/
|
|
73
|
+
static hasControlChars(value) {
|
|
74
|
+
for (let index = 0; index < value.length; index++) {
|
|
75
|
+
const code = value.charCodeAt(index);
|
|
76
|
+
if (code <= 0x1f || code === 0x7f) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Percent-encodes a slug for use as one Git tree entry name.
|
|
85
|
+
*
|
|
86
|
+
* @param {string | Slug} slug
|
|
87
|
+
* @returns {string}
|
|
88
|
+
*/
|
|
89
|
+
static encode(slug) {
|
|
90
|
+
const value = Slug.from(slug).toString();
|
|
91
|
+
if (Slug.hasControlChars(value)) {
|
|
92
|
+
throw new CasError(
|
|
93
|
+
'Slug contains control characters — refusing to encode for mktree',
|
|
94
|
+
ErrorCodes.INVALID_SLUG,
|
|
95
|
+
{ slug: value },
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return value.replaceAll('%', '%25').replaceAll('/', '%2F');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Decodes a percent-encoded Git tree entry name back to a slug.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} treePath
|
|
105
|
+
* @returns {string}
|
|
106
|
+
*/
|
|
107
|
+
static decode(treePath) {
|
|
108
|
+
return treePath.replaceAll('%2F', '/').replaceAll('%25', '%');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @returns {string}
|
|
113
|
+
*/
|
|
114
|
+
toString() {
|
|
115
|
+
return this.#value;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @returns {string}
|
|
120
|
+
*/
|
|
121
|
+
toTreePath() {
|
|
122
|
+
return Slug.encode(this);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import {
|
|
3
|
+
SCHEME_CONVERGENT,
|
|
4
|
+
SCHEME_FRAMED,
|
|
5
|
+
SCHEME_WHOLE,
|
|
6
|
+
} from '../encryption/schemes.js';
|
|
7
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_FRAMED_FRAME_BYTES = 64 * 1024;
|
|
10
|
+
export const MAX_FRAMED_FRAME_BYTES = 64 * 1024 * 1024;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Immutable store-time encryption strategy configuration.
|
|
14
|
+
*/
|
|
15
|
+
export default class StoreEncryptionConfig {
|
|
16
|
+
/**
|
|
17
|
+
* @param {{ scheme: 'whole'|'framed'|'convergent', frameBytes?: number }} fields
|
|
18
|
+
*/
|
|
19
|
+
constructor(fields) {
|
|
20
|
+
this.scheme = fields.scheme;
|
|
21
|
+
if (fields.frameBytes !== undefined) {
|
|
22
|
+
this.frameBytes = fields.frameBytes;
|
|
23
|
+
}
|
|
24
|
+
Object.freeze(this);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {{ encryption?: { scheme?: string, frameBytes?: number, convergent?: boolean }, hasEncryptionKey: boolean, chunker: { strategy?: string }, observability: { log: Function } }} options
|
|
29
|
+
* @returns {StoreEncryptionConfig|undefined}
|
|
30
|
+
*/
|
|
31
|
+
static resolve({ encryption, hasEncryptionKey, chunker, observability }) {
|
|
32
|
+
const scheme = encryption?.scheme;
|
|
33
|
+
const frameBytes = encryption?.frameBytes;
|
|
34
|
+
StoreEncryptionConfig.#assertPrereqs({ hasEncryptionKey, scheme, frameBytes });
|
|
35
|
+
|
|
36
|
+
if (!hasEncryptionKey) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
if (scheme === SCHEME_CONVERGENT) {
|
|
40
|
+
return new StoreEncryptionConfig({ scheme: SCHEME_CONVERGENT });
|
|
41
|
+
}
|
|
42
|
+
if (scheme === SCHEME_WHOLE) {
|
|
43
|
+
return new StoreEncryptionConfig({ scheme: SCHEME_WHOLE });
|
|
44
|
+
}
|
|
45
|
+
if (scheme === SCHEME_FRAMED) {
|
|
46
|
+
return StoreEncryptionConfig.resolveFramed(frameBytes);
|
|
47
|
+
}
|
|
48
|
+
if (!scheme) {
|
|
49
|
+
return StoreEncryptionConfig.#resolveAuto({ encryption, frameBytes, chunker, observability });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
throw createCasError(`Unsupported encryption scheme: ${scheme}`, ErrorCodes.INVALID_OPTIONS, { scheme });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {number|undefined} frameBytes
|
|
57
|
+
* @returns {StoreEncryptionConfig}
|
|
58
|
+
*/
|
|
59
|
+
static resolveFramed(frameBytes) {
|
|
60
|
+
const normalizedFrameBytes = frameBytes ?? DEFAULT_FRAMED_FRAME_BYTES;
|
|
61
|
+
if (!Number.isInteger(normalizedFrameBytes) || normalizedFrameBytes < 1) {
|
|
62
|
+
throw createCasError(
|
|
63
|
+
'encryption.frameBytes must be a positive integer',
|
|
64
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
65
|
+
{ frameBytes: normalizedFrameBytes },
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
if (normalizedFrameBytes > MAX_FRAMED_FRAME_BYTES) {
|
|
69
|
+
throw createCasError(
|
|
70
|
+
`encryption.frameBytes must not exceed ${MAX_FRAMED_FRAME_BYTES} bytes (64 MiB), got ${normalizedFrameBytes}`,
|
|
71
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
72
|
+
{ frameBytes: normalizedFrameBytes, max: MAX_FRAMED_FRAME_BYTES },
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return new StoreEncryptionConfig({ scheme: SCHEME_FRAMED, frameBytes: normalizedFrameBytes });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static #resolveAuto({ encryption, frameBytes, chunker, observability }) {
|
|
79
|
+
const convergentExplicit = encryption?.convergent;
|
|
80
|
+
if (convergentExplicit === true || (convergentExplicit !== false && chunker.strategy === 'cdc')) {
|
|
81
|
+
if (convergentExplicit !== true && chunker.strategy === 'cdc') {
|
|
82
|
+
observability.log(
|
|
83
|
+
'warn',
|
|
84
|
+
'CDC encrypted store auto-selected deterministic convergent encryption to preserve deduplication',
|
|
85
|
+
{
|
|
86
|
+
strategy: 'cdc',
|
|
87
|
+
selectedScheme: SCHEME_CONVERGENT,
|
|
88
|
+
deterministic: true,
|
|
89
|
+
optOut: 'Use encryption.scheme "framed" or "whole", or encryption.convergent false.',
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return new StoreEncryptionConfig({ scheme: SCHEME_CONVERGENT });
|
|
94
|
+
}
|
|
95
|
+
return StoreEncryptionConfig.resolveFramed(frameBytes);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static #assertPrereqs({ hasEncryptionKey, scheme, frameBytes }) {
|
|
99
|
+
if (!hasEncryptionKey && (scheme || frameBytes !== undefined)) {
|
|
100
|
+
throw createCasError(
|
|
101
|
+
'encryption options require encryptionKey, passphrase, or recipients',
|
|
102
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
103
|
+
{ scheme, frameBytes },
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (frameBytes !== undefined && scheme === SCHEME_WHOLE) {
|
|
107
|
+
throw createCasError(
|
|
108
|
+
`encryption.frameBytes is not supported for ${scheme} stores`,
|
|
109
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
110
|
+
{ scheme, frameBytes },
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|