feat(server): deriveSecurity.encryptionKeySet and storage secret plumbing (gate, encrypt, unmask, mask, redact)

This commit is contained in:
jubnl
2026-08-20 00:50:27 +02:00
parent a6fb82833f
commit d9f6151f53
7 changed files with 309 additions and 2 deletions
+10 -1
View File
@@ -13,6 +13,7 @@ import {
deriveBackup,
deriveNet,
derivePaths,
deriveSecurity,
deriveAll,
} from '../../../src/app-config/derive';
@@ -231,6 +232,14 @@ describe('derivePaths', () => {
});
});
describe('deriveSecurity', () => {
it('encryptionKeySet only for a non-blank explicit ENCRYPTION_KEY', () => {
expect(deriveSecurity({}).encryptionKeySet).toBe(false);
expect(deriveSecurity({ ENCRYPTION_KEY: ' ' }).encryptionKeySet).toBe(false);
expect(deriveSecurity({ ENCRYPTION_KEY: 'k' }).encryptionKeySet).toBe(true);
});
});
describe('deriveAll', () => {
it('assembles every namespace', () => {
const env = deriveAll({ PORT: '4000', DEMO_MODE: 'true' });
@@ -238,7 +247,7 @@ describe('deriveAll', () => {
expect(env.demo.enabled).toBe(true);
for (const ns of [
'app', 'http', 'session', 'demo', 'adminBootstrap', 'oidc', 'smtp', 'mcp',
'plugins', 'webauthn', 'integrations', 'backup', 'db', 'paths', 'net',
'plugins', 'webauthn', 'integrations', 'backup', 'db', 'paths', 'security', 'net',
] as const) {
expect(env[ns]).toBeDefined();
}
@@ -0,0 +1,125 @@
import { describe, it, expect, vi } from 'vitest';
vi.mock('../../../../src/config', () => ({ ENCRYPTION_KEY: 'storage-secrets-test-key' }));
import { MASKED_SETTING_VALUE, type StorageConfig } from '@trek/shared';
import {
decrypt_api_key,
encrypt_api_key,
} from '../../../../src/nest/common/crypto/apiKeyCrypto';
import {
assertNoMaskSentinels,
encryptionGateError,
encryptStorageSecrets,
listPlaintextSecrets,
maskBackendOptions,
redactStorageSecrets,
unmaskStorageConfig,
} from '../../../../src/nest/storage/storage-secrets';
const S3_OPTIONS = {
endpoint: 'http://127.0.0.1:9000',
bucket: 'trek',
accessKeyId: 'ak',
secretAccessKey: 'sk-plain',
region: 'us-east-1',
keyPrefix: '',
retries: 1,
timeoutMs: 30000,
};
function s3Config(secretAccessKey: string): StorageConfig {
return {
backends: [{ name: 'off-box', type: 's3', options: { ...S3_OPTIONS, secretAccessKey } }],
categories: { backups: 'off-box' },
};
}
const LOCAL_ONLY: StorageConfig = {
backends: [{ name: 'nas', type: 'local', options: { root: '/mnt/nas' } }],
categories: {},
};
describe('listPlaintextSecrets', () => {
it('finds plaintext secrets, skips encrypted ones and secretless types', () => {
expect(listPlaintextSecrets(s3Config('sk-plain'))).toEqual([{ backend: 'off-box', field: 'secretAccessKey' }]);
expect(listPlaintextSecrets(s3Config(encrypt_api_key('sk')))).toEqual([]);
expect(listPlaintextSecrets(LOCAL_ONLY)).toEqual([]);
});
});
describe('encryptionGateError', () => {
it('names ENCRYPTION_KEY, the backend, and the field', () => {
const message = encryptionGateError({ backend: 'off-box', field: 'secretAccessKey' });
expect(message).toContain('ENCRYPTION_KEY');
expect(message).toContain("'off-box'");
expect(message).toContain("'secretAccessKey'");
expect(message).toContain('fallback');
});
});
describe('encryptStorageSecrets', () => {
it('encrypts plaintext secrets and round-trips through decrypt', () => {
const out = encryptStorageSecrets(s3Config('sk-plain'));
const stored = (out.backends[0]!.options as Record<string, unknown>).secretAccessKey as string;
expect(stored.startsWith('enc:v1:')).toBe(true);
expect(decrypt_api_key(stored)).toBe('sk-plain');
});
it('is idempotent — an enc:v1: value passes through byte-for-byte', () => {
const cipher = encrypt_api_key('sk');
const out = encryptStorageSecrets(s3Config(cipher));
expect((out.backends[0]!.options as Record<string, unknown>).secretAccessKey).toBe(cipher);
});
it('does not mutate its input', () => {
const input = s3Config('sk-plain');
void encryptStorageSecrets(input);
expect((input.backends[0]!.options as Record<string, unknown>).secretAccessKey).toBe('sk-plain');
});
});
describe('unmaskStorageConfig', () => {
it('replaces a mask echo with the stored ciphertext byte-for-byte', () => {
const cipher = encrypt_api_key('sk');
const stored = [{ name: 'off-box', type: 's3', options: { ...S3_OPTIONS, secretAccessKey: cipher } }];
const out = unmaskStorageConfig(s3Config(MASKED_SETTING_VALUE), stored);
expect((out.backends[0]!.options as Record<string, unknown>).secretAccessKey).toBe(cipher);
});
it('throws for a mask with no stored counterpart (rename or new backend)', () => {
expect(() => unmaskStorageConfig(s3Config(MASKED_SETTING_VALUE), [])).toThrow(
"re-enter the secret 'secretAccessKey' for 'off-box'",
);
const renamed = [{ name: 'old-name', type: 's3', options: { ...S3_OPTIONS, secretAccessKey: encrypt_api_key('sk') } }];
expect(() => unmaskStorageConfig(s3Config(MASKED_SETTING_VALUE), renamed)).toThrow(
"re-enter the secret 'secretAccessKey' for 'off-box'",
);
});
it('leaves non-mask values and secretless backends untouched, and tolerates a garbage stored row', () => {
expect(unmaskStorageConfig(s3Config('sk-new'), 'not-an-array')).toEqual(s3Config('sk-new'));
expect(unmaskStorageConfig(LOCAL_ONLY, undefined)).toEqual(LOCAL_ONLY);
});
});
describe('assertNoMaskSentinels', () => {
it('throws when a seed-style config carries the mask sentinel', () => {
expect(() => assertNoMaskSentinels(s3Config(MASKED_SETTING_VALUE))).toThrow('mask');
expect(() => assertNoMaskSentinels(s3Config('sk'))).not.toThrow();
});
});
describe('maskBackendOptions / redactStorageSecrets', () => {
it('masks exactly the secret fields on GET-shaped options', () => {
const masked = maskBackendOptions('s3', { ...S3_OPTIONS });
expect(masked.secretAccessKey).toBe(MASKED_SETTING_VALUE);
expect(masked.accessKeyId).toBe('ak'); // never over-masked
expect(maskBackendOptions('local', { root: '/x' })).toEqual({ root: '/x' });
});
it('redacts secrets to *** for audit details', () => {
const out = redactStorageSecrets(s3Config('sk-plain'));
expect((out.backends[0]!.options as Record<string, unknown>).secretAccessKey).toBe('***');
expect((out.backends[0]!.options as Record<string, unknown>).accessKeyId).toBe('ak');
});
});
@@ -7,7 +7,12 @@ vi.mock('../../../src/config', () => ({
updateJwtSecret: () => {},
}));
import { encrypt_api_key, decrypt_api_key, maybe_encrypt_api_key } from '../../../src/nest/common/crypto/apiKeyCrypto';
import {
encrypt_api_key,
decrypt_api_key,
maybe_encrypt_api_key,
is_encrypted_api_key,
} from '../../../src/nest/common/crypto/apiKeyCrypto';
describe('apiKeyCrypto', () => {
const PLAINTEXT_KEY = 'my-secret-api-key-12345';
@@ -76,4 +81,15 @@ describe('apiKeyCrypto', () => {
expect(result).toBe(encrypted);
});
});
describe('is_encrypted_api_key', () => {
it('is true exactly for enc:v1: envelopes', () => {
expect(is_encrypted_api_key(encrypt_api_key('s'))).toBe(true);
expect(is_encrypted_api_key('enc:v1:AAAA')).toBe(true);
expect(is_encrypted_api_key('plaintext')).toBe(false);
expect(is_encrypted_api_key('')).toBe(false);
expect(is_encrypted_api_key(null)).toBe(false);
expect(is_encrypted_api_key(42)).toBe(false);
});
});
});