Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404/** Covers encryption-algorithm recognition and the config-file encryption wire format. @module */
import { readFileSync, writeFileSync,} from 'node:fs';
import { join,} from 'node:path';
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { bytesToString, DecryptionFailedError, decryptConfigData, decryptWithSalt, encryptSerializedStore, InvalidAuthenticationTagError, isSupportedEncryptionAlgorithm, stringToBytes,} from '../dist/final/neutral/index.mjs';
import { createTempDirectory, decryptConfigFile, writeLegacyEncryptedConfigFile,} from './test-support.ts';
/** Runs a call expected to throw and returns the captured error so class and message text can be asserted. @param call - Call that must throw. @returns The captured thrown value. */function captureThrown(call: () => unknown,): Error { try { call(); } catch (error) { return error as Error; } throw new Error('expected the call to throw, but it returned',);}
await describe({ name: 'config file encryption', children: [ it({ name: 'recognizes each supported algorithm identifier', fn: async () => { expect(isSupportedEncryptionAlgorithm('aes-256-cbc',),).toBe(true,); expect(isSupportedEncryptionAlgorithm('aes-256-gcm',),).toBe(true,); expect(isSupportedEncryptionAlgorithm('aes-256-ctr',),).toBe(true,); }, },), it({ name: 'rejects other strings and non-string values as algorithm identifiers', fn: async () => { expect(isSupportedEncryptionAlgorithm('rot13',),).toBe(false,); expect(isSupportedEncryptionAlgorithm('AES-256-CBC',),).toBe(false,); expect(isSupportedEncryptionAlgorithm('',),).toBe(false,); expect(isSupportedEncryptionAlgorithm(42,),).toBe(false,); expect(isSupportedEncryptionAlgorithm(true,),).toBe(false,); expect(isSupportedEncryptionAlgorithm({},),).toBe(false,); expect(isSupportedEncryptionAlgorithm(null,),).toBe(false,); expect(isSupportedEncryptionAlgorithm(undefined,),).toBe(false,); }, },), it({ name: 'round-trips serialized store text under every supported algorithm', fn: async () => { /** Algorithms whose encryption must decrypt back to the exact input. */ const algorithms = [ 'aes-256-cbc', 'aes-256-gcm', 'aes-256-ctr', ] as const; for (const encryptionAlgorithm of algorithms) { /** Serialized store text protected under this algorithm. */ const encrypted = encryptSerializedStore({ serialized: '{"theme":"dark"}', encryptionKey: 'correct horse', encryptionAlgorithm, },); expect(decryptConfigData({ data: encrypted, encryptionKey: 'correct horse', encryptionAlgorithm, },),).toBe('{"theme":"dark"}',); } }, },), it({ name: 'frames the payload with a 16-byte initialization vector and a colon separator every algorithm reads back', fn: async () => { /** Disposable directory holding one config file per algorithm. */ const directory = createTempDirectory(); /** Algorithms whose on-disk framing must follow the shared layout. */ const algorithms = [ 'aes-256-cbc', 'aes-256-gcm', 'aes-256-ctr', ] as const; for (const encryptionAlgorithm of algorithms) { /** Serialized store text protected under this algorithm. */ const encrypted = encryptSerializedStore({ serialized: '{"theme":"dark"}', encryptionKey: 'correct horse', encryptionAlgorithm, },); // Code point of the `:` separator following the initialization vector. expect(encrypted[16],).toBe(58,); /** Config file path the store would have written these bytes to. */ const filePath = join( directory, 'config.json', ); writeFileSync( filePath, encrypted, ); /** Decrypted text re-read through the store's on-disk layout, which derives the key from the first 16 bytes as the initialization vector. */ const decrypted = decryptConfigFile({ filePath, encryptionKey: 'correct horse', encryptionAlgorithm, },); expect(decrypted,).toBe('{"theme":"dark"}',); } }, },), it({ name: 'appends a 16-byte authentication tag after the aes-256-gcm ciphertext', fn: async () => { /** Serialized store text protected under aes-256-gcm. */ const encrypted = encryptSerializedStore({ serialized: '{"theme":"dark"}', encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-gcm', },); // GCM ciphertext length equals plaintext length, so the trailing 16 bytes past the framing are exactly the tag. expect(encrypted.length,).toBe(17 + '{"theme":"dark"}'.length + 16,); }, },), it({ name: 'returns unframed data unchanged under aes-256-cbc', fn: async () => { /** Long plaintext whose byte 16 is not the separator code point. */ const longPlaintext = '{"theme":"dark","nested":{"a":1}}'; expect(decryptConfigData({ data: stringToBytes(longPlaintext,), encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-cbc', },),).toBe(longPlaintext,); /** Short plaintext with no byte 16 to hold a separator at all. */ const shortPlaintext = 'plain'; expect(decryptConfigData({ data: stringToBytes(shortPlaintext,), encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-cbc', },),).toBe(shortPlaintext,); }, },), it({ name: 'throws DecryptionFailedError for unframed aes-256-gcm data', fn: async () => { /** Error thrown because aes-256-gcm has no plaintext passthrough. */ const error = captureThrown((): unknown => decryptConfigData({ data: stringToBytes('{"theme":"dark","nested":{"a":1}}',), encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-gcm', },),); expect(error,).toBeInstanceOf(DecryptionFailedError,); expect(error.name,).toBe('DecryptionFailedError',); }, },), it({ name: 'throws DecryptionFailedError for unframed aes-256-ctr data', fn: async () => { /** Error thrown because aes-256-ctr has no plaintext passthrough. */ const error = captureThrown((): unknown => decryptConfigData({ data: stringToBytes('{"theme":"dark","nested":{"a":1}}',), encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-ctr', },),); expect(error,).toBeInstanceOf(DecryptionFailedError,); expect(error.name,).toBe('DecryptionFailedError',); }, },), it({ name: 'falls back to raw bytes as text when aes-256-cbc decryption fails under a wrong key', fn: async () => { /** Frozen ciphertext fixture written under `fixture-right-key`. Fixed bytes keep the wrong-key case deterministic: a random draw can otherwise decrypt with lucky padding roughly one run in 256 and break the fallback assertion. */ const encrypted = new Uint8Array([ 116, 30, 39, 133, 139, 153, 21, 164, 156, 14, 85, 251, 128, 136, 13, 204, 58, 178, 108, 117, 240, 251, 67, 86, 209, 100, 201, 75, 200, 245, 59, 98, 194, 219, 142, 0, 121, 171, 142, 24, 42, 42, 108, 61, 236, 36, 83, 255, 119, ],); /** Decryption attempt under a key that cannot open the framing. */ const fallback = decryptConfigData({ data: encrypted, encryptionKey: 'fixture-wrong-key', encryptionAlgorithm: 'aes-256-cbc', },); // Upstream mirrors this plaintext passthrough of the raw encrypted bytes as text. expect(fallback,).toBe(bytesToString(encrypted,),); expect(fallback === '{"theme":"dark"}',).toBe(false,); }, },), it({ name: 'falls back to raw bytes as text when aes-256-cbc framing carries an undecryptable payload', fn: async () => { /** Framed data whose 5-byte payload cannot be a block cipher output. */ const framed = stringToBytes('0123456789abcdef:short',); expect(decryptConfigData({ data: framed, encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-cbc', },),).toBe('0123456789abcdef:short',); }, },), it({ name: 'throws DecryptionFailedError when aes-256-gcm decryption fails under a wrong key', fn: async () => { /** Ciphertext written under the correct key. */ const encrypted = encryptSerializedStore({ serialized: '{"theme":"dark"}', encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-gcm', },); /** Error thrown because authenticated decryption rejects the wrong key. */ const error = captureThrown((): unknown => decryptConfigData({ data: encrypted, encryptionKey: 'wrong horse', encryptionAlgorithm: 'aes-256-gcm', },),); expect(error,).toBeInstanceOf(DecryptionFailedError,); expect(error.name,).toBe('DecryptionFailedError',); }, },), it({ name: 'returns silent garbage text when aes-256-ctr decryption runs under a wrong key', fn: async () => { /** Ciphertext written under the correct key. */ const encrypted = encryptSerializedStore({ serialized: '{"theme":"dark"}', encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-ctr', },); /** Decryption attempt under a wrong key. */ const garbage = decryptConfigData({ data: encrypted, encryptionKey: 'wrong horse', encryptionAlgorithm: 'aes-256-ctr', },); // Unauthenticated stream decryption cannot detect the wrong key, so it reports garbage instead of failing. expect(garbage === '{"theme":"dark"}',).toBe(false,); }, },), it({ name: 'throws InvalidAuthenticationTagError from decryptWithSalt for an aes-256-gcm payload shorter than its authentication tag', fn: async () => { /** Framed data whose 5-byte payload cannot hold a 16-byte authentication tag. */ const framed = stringToBytes('0123456789abcdef:short',); /** Error thrown where the authentication tag length is checked. */ const error = captureThrown((): unknown => decryptWithSalt({ data: framed, encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-gcm', salt: stringToBytes('0123456789abcdef',), },),); expect(error,).toBeInstanceOf(InvalidAuthenticationTagError,); expect(error.name,).toBe('InvalidAuthenticationTagError',); expect(error.message,).toBe('Invalid authentication tag length.',); }, },), it({ name: 'surfaces DecryptionFailedError from decryptConfigData for the same truncated aes-256-gcm payload', fn: async () => { /** Framed data whose 5-byte payload cannot hold a 16-byte authentication tag. */ const framed = stringToBytes('0123456789abcdef:short',); /** Error surfaced after both salt attempts fail and no passthrough applies. */ const error = captureThrown((): unknown => decryptConfigData({ data: framed, encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-gcm', },),); // Current behavior: decryptConfigData swallows the authentication-tag failure into its salt fallback. expect(error,).toBeInstanceOf(DecryptionFailedError,); }, },), it({ name: 'reads a legacy-salt encrypted file back with the same key', fn: async () => { /** Disposable directory holding the legacy-layout config file. */ const directory = createTempDirectory(); /** Config file path carrying the legacy salt layout. */ const filePath = join( directory, 'config.json', ); writeLegacyEncryptedConfigFile({ filePath, encryptionKey: 'correct horse', serialized: '{"theme":"dark"}', },); /** Raw legacy file bytes as written by older releases. */ const data = new Uint8Array(readFileSync(filePath,),); expect(decryptConfigData({ data, encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-cbc', },),).toBe('{"theme":"dark"}',); }, },), it({ name: 'round-trips an empty serialized payload under aes-256-gcm', fn: async () => { /** Encryption of the empty serialized store. */ const encrypted = encryptSerializedStore({ serialized: '', encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-gcm', },); expect(decryptConfigData({ data: encrypted, encryptionKey: 'correct horse', encryptionAlgorithm: 'aes-256-gcm', },),).toBe('',); }, },), ],},);