|
| 1 | +import { createHash, randomBytes, createCipheriv } from 'crypto'; |
| 2 | +import { |
| 3 | + generateDEK, |
| 4 | + wrapDEK, |
| 5 | + unwrapDEK, |
| 6 | + encryptWithDEK, |
| 7 | + decryptWithDEK, |
| 8 | +} from './workspace-encryption.util'; |
| 9 | + |
| 10 | +// Helper: manually encrypt with DEFAULT key in OLD format (no prefix) |
| 11 | +function encryptLegacy(text: string): string { |
| 12 | + const key = createHash('sha256') |
| 13 | + .update('OASM_DEFAULT_ENCRYPTION_KEY') |
| 14 | + .digest(); |
| 15 | + const iv = randomBytes(16); |
| 16 | + const cipher = createCipheriv('aes-256-cbc', key, iv); |
| 17 | + const encrypted = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]); |
| 18 | + return iv.toString('hex') + ':' + encrypted.toString('hex'); |
| 19 | +} |
| 20 | + |
| 21 | +describe('generateDEK', () => { |
| 22 | + it('should generate a 32-byte buffer', () => { |
| 23 | + const dek = generateDEK(); |
| 24 | + expect(Buffer.isBuffer(dek)).toBe(true); |
| 25 | + expect(dek.length).toBe(32); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should generate unique keys each call', () => { |
| 29 | + const dek1 = generateDEK(); |
| 30 | + const dek2 = generateDEK(); |
| 31 | + expect(dek1.equals(dek2)).toBe(false); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +describe('wrapDEK / unwrapDEK', () => { |
| 36 | + it('should roundtrip: wrap then unwrap produces original DEK', () => { |
| 37 | + const dek = generateDEK(); |
| 38 | + const wrapped = wrapDEK(dek); |
| 39 | + expect(typeof wrapped).toBe('string'); |
| 40 | + expect(wrapped).toContain(':'); |
| 41 | + |
| 42 | + // Should have numeric index prefix |
| 43 | + const firstColon = wrapped.indexOf(':'); |
| 44 | + const indexStr = wrapped.substring(0, firstColon); |
| 45 | + expect(/^\d+$/.test(indexStr)).toBe(true); |
| 46 | + |
| 47 | + const unwrapped = unwrapDEK(wrapped); |
| 48 | + expect(Buffer.isBuffer(unwrapped)).toBe(true); |
| 49 | + expect(dek.equals(unwrapped)).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should fail unwrap on corrupted data', () => { |
| 53 | + expect(() => unwrapDEK('0:bad:data')).toThrow(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should fail unwrap on invalid key index', () => { |
| 57 | + expect(() => unwrapDEK('99:abc:def')).toThrow(/key index/); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('encryptWithDEK / decryptWithDEK', () => { |
| 62 | + const sampleText = 'Hello, World! Đây là dữ liệu cần mã hóa.'; |
| 63 | + |
| 64 | + it('should roundtrip with valid DEK', () => { |
| 65 | + const dek = generateDEK(); |
| 66 | + const encrypted = encryptWithDEK(sampleText, dek); |
| 67 | + expect(encrypted).not.toBe(sampleText); |
| 68 | + expect(encrypted).toContain(':'); |
| 69 | + |
| 70 | + const decrypted = decryptWithDEK(encrypted, dek); |
| 71 | + expect(decrypted).toBe(sampleText); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle empty string', () => { |
| 75 | + const dek = generateDEK(); |
| 76 | + const encrypted = encryptWithDEK('', dek); |
| 77 | + expect(decryptWithDEK(encrypted, dek)).toBe(''); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should handle UTF-8 characters', () => { |
| 81 | + const dek = generateDEK(); |
| 82 | + const text = 'こんにちは世界 🌍'; |
| 83 | + const encrypted = encryptWithDEK(text, dek); |
| 84 | + expect(decryptWithDEK(encrypted, dek)).toBe(text); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should fail decrypt with wrong DEK', () => { |
| 88 | + const dek1 = generateDEK(); |
| 89 | + const dek2 = generateDEK(); |
| 90 | + const encrypted = encryptWithDEK(sampleText, dek1); |
| 91 | + expect(() => decryptWithDEK(encrypted, dek2)).toThrow(); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe('backward compatibility — decryptWithDEK with legacy KEK data', () => { |
| 96 | + const sampleText = 'Hello, World! Đây là dữ liệu cần mã hóa.'; |
| 97 | + |
| 98 | + it('should decrypt legacy KEK data (no prefix) when DEK is null', () => { |
| 99 | + const legacyEncrypted = encryptLegacy(sampleText); |
| 100 | + // Legacy format: no numeric prefix |
| 101 | + expect(legacyEncrypted.split(':')[0]).not.toMatch(/^\d+$/); |
| 102 | + |
| 103 | + const result = decryptWithDEK(legacyEncrypted, null); |
| 104 | + expect(result).toBe(sampleText); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should fall back to KEK when DEK decryption fails', () => { |
| 108 | + const legacyEncrypted = encryptLegacy(sampleText); |
| 109 | + const dek = generateDEK(); |
| 110 | + const result = decryptWithDEK(legacyEncrypted, dek); |
| 111 | + expect(result).toBe(sampleText); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('end-to-end envelope encryption flow', () => { |
| 116 | + const sampleText = 'Full lifecycle test.'; |
| 117 | + |
| 118 | + it('should simulate the full workspace encryption lifecycle', () => { |
| 119 | + // 1. Generate DEK |
| 120 | + const dek = generateDEK(); |
| 121 | + // 2. Wrap DEK with KEK (what gets stored in workspace.dek) |
| 122 | + const wrappedDEK = wrapDEK(dek); |
| 123 | + // 3. Store wrappedDEK in DB (simulated) |
| 124 | + const storedWrappedDEK = wrappedDEK; |
| 125 | + // 4. Later: unwrap DEK from stored value |
| 126 | + const unwrappedDEK = unwrapDEK(storedWrappedDEK); |
| 127 | + expect(dek.equals(unwrappedDEK)).toBe(true); |
| 128 | + // 5. Encrypt data with DEK (no prefix — DEK is the key) |
| 129 | + const encrypted = encryptWithDEK(sampleText, unwrappedDEK); |
| 130 | + // 6. Decrypt data with DEK |
| 131 | + const decrypted = decryptWithDEK(encrypted, unwrappedDEK); |
| 132 | + expect(decrypted).toBe(sampleText); |
| 133 | + // 7. Different workspace DEK cannot decrypt |
| 134 | + const otherDEK = generateDEK(); |
| 135 | + expect(() => decryptWithDEK(encrypted, otherDEK)).toThrow(); |
| 136 | + }); |
| 137 | +}); |
0 commit comments