|
1 | 1 | // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
2 | 2 |
|
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
4 | | -import { mkdtempSync, rmSync, writeFileSync, existsSync } from 'node:fs'; |
| 4 | +import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync } from 'node:fs'; |
5 | 5 | import { tmpdir } from 'node:os'; |
6 | 6 | import { join } from 'node:path'; |
7 | | -import { randomBytes } from 'node:crypto'; |
| 7 | +import { createHash, createHmac, randomBytes } from 'node:crypto'; |
8 | 8 | import { |
9 | 9 | LocalCryptoProvider, |
10 | 10 | InMemoryCryptoProvider, |
| 11 | + KeyedDigestKeyUnavailableError, |
11 | 12 | } from './local-crypto-provider.js'; |
12 | 13 |
|
13 | 14 | const ctx = { namespace: 'mail', key: 'api_key' }; |
@@ -154,6 +155,117 @@ describe('LocalCryptoProvider — crypto semantics', () => { |
154 | 155 | }); |
155 | 156 | }); |
156 | 157 |
|
| 158 | +describe('LocalCryptoProvider — keyedDigest', () => { |
| 159 | + const KEYED_SHAPE = /^hmac-sha256:[0-9a-f]{64}$/; |
| 160 | + const input = 'sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a'; |
| 161 | + const sha256Hex = (s: string) => createHash('sha256').update(s, 'utf8').digest('hex'); |
| 162 | + |
| 163 | + let home: string; |
| 164 | + beforeEach(() => { |
| 165 | + home = mkdtempSync(join(tmpdir(), 'os-crypto-keyed-')); |
| 166 | + }); |
| 167 | + afterEach(() => { |
| 168 | + rmSync(home, { recursive: true, force: true }); |
| 169 | + }); |
| 170 | + |
| 171 | + it('is keyed: the output differs from the unkeyed SHA-256 of the same input', async () => { |
| 172 | + const p = new LocalCryptoProvider({ key: randomBytes(32) }); |
| 173 | + const out = await p.keyedDigest(input); |
| 174 | + expect(out).toMatch(KEYED_SHAPE); |
| 175 | + expect(out).toHaveLength(76); |
| 176 | + const hexBody = out.slice('hmac-sha256:'.length); |
| 177 | + expect(hexBody).not.toBe(sha256Hex(input)); |
| 178 | + expect(out).not.toBe(p.digest(input)); |
| 179 | + expect(out).not.toContain(input); |
| 180 | + }); |
| 181 | + |
| 182 | + it('is equal for equal input under one key — within an instance and across instances', async () => { |
| 183 | + const hex = randomBytes(32).toString('hex'); |
| 184 | + const env = { NODE_ENV: 'production', OS_SECRET_KEY: hex, OS_HOME: home }; |
| 185 | + const a = new LocalCryptoProvider({ env }); |
| 186 | + // A second instance resolving the same key stands in for another process or node. |
| 187 | + const b = new LocalCryptoProvider({ env }); |
| 188 | + expect(await a.keyedDigest(input)).toBe(await a.keyedDigest(input)); |
| 189 | + expect(await b.keyedDigest(input)).toBe(await a.keyedDigest(input)); |
| 190 | + // The same key spelled base64 is the same key. |
| 191 | + const c = new LocalCryptoProvider({ |
| 192 | + env: { NODE_ENV: 'production', OS_SECRET_KEY: Buffer.from(hex, 'hex').toString('base64'), OS_HOME: home }, |
| 193 | + }); |
| 194 | + expect(await c.keyedDigest(input)).toBe(await a.keyedDigest(input)); |
| 195 | + // Equal under one key is not constant under one key. |
| 196 | + expect(await a.keyedDigest(input + ' ')).not.toBe(await a.keyedDigest(input)); |
| 197 | + }); |
| 198 | + |
| 199 | + it('differs under a different key', async () => { |
| 200 | + const a = new LocalCryptoProvider({ key: randomBytes(32) }); |
| 201 | + const b = new LocalCryptoProvider({ key: randomBytes(32) }); |
| 202 | + expect(await a.keyedDigest(input)).not.toBe(await b.keyedDigest(input)); |
| 203 | + }); |
| 204 | + |
| 205 | + it('keys its MAC with a key derived from the data key, never with the data key itself', async () => { |
| 206 | + const key = randomBytes(32); |
| 207 | + const out = await new LocalCryptoProvider({ key }).keyedDigest(input); |
| 208 | + const underDataKey = createHmac('sha256', key).update(input, 'utf8').digest('hex'); |
| 209 | + expect(out).toMatch(KEYED_SHAPE); |
| 210 | + expect(out.slice('hmac-sha256:'.length)).not.toBe(underDataKey); |
| 211 | + }); |
| 212 | + |
| 213 | + it('answers a pinned vector, so builds on two nodes of one rolling deploy agree', async () => { |
| 214 | + // Vector computed outside this implementation (Python `hmac`): the MAC key is |
| 215 | + // HMAC-SHA-256(dataKey, label || 0x01), the output HMAC-SHA-256(macKey, input). |
| 216 | + const p = new LocalCryptoProvider({ key: Buffer.from(Array.from({ length: 32 }, (_, i) => i)) }); |
| 217 | + expect(await p.keyedDigest('hello')).toBe( |
| 218 | + 'hmac-sha256:de27f4140f1e5db4cdabc24d91dbf5eef67eeaaacd5fefa0aa491f283bdcded6', |
| 219 | + ); |
| 220 | + expect(await p.keyedDigest(input)).toBe( |
| 221 | + 'hmac-sha256:ac3e935526f452d6e230db8667e46422fdfe53800f70c13a70006576fae8a21b', |
| 222 | + ); |
| 223 | + }); |
| 224 | + |
| 225 | + it('is computed from whichever source resolved the data key', async () => { |
| 226 | + const key = randomBytes(32); |
| 227 | + const expected = await new LocalCryptoProvider({ key }).keyedDigest(input); |
| 228 | + |
| 229 | + const fromSecret = new LocalCryptoProvider({ |
| 230 | + env: { NODE_ENV: 'production', OS_SECRET_KEY: key.toString('hex'), OS_HOME: home }, |
| 231 | + }); |
| 232 | + expect(fromSecret.keySource).toBe('env:OS_SECRET_KEY'); |
| 233 | + expect(await fromSecret.keyedDigest(input)).toBe(expected); |
| 234 | + |
| 235 | + const fromDevKey = new LocalCryptoProvider({ |
| 236 | + env: { NODE_ENV: 'development', OS_DEV_CRYPTO_KEY: key.toString('hex'), HOME: home }, |
| 237 | + }); |
| 238 | + expect(fromDevKey.keySource).toBe('env:OS_DEV_CRYPTO_KEY'); |
| 239 | + expect(await fromDevKey.keyedDigest(input)).toBe(expected); |
| 240 | + |
| 241 | + mkdirSync(join(home, '.objectstack'), { recursive: true }); |
| 242 | + writeFileSync(join(home, '.objectstack', 'dev-crypto-key'), key.toString('base64'), { mode: 0o600 }); |
| 243 | + const fromFile = new LocalCryptoProvider({ env: { NODE_ENV: 'production', HOME: home } }); |
| 244 | + expect(fromFile.keySource).toBe('file'); |
| 245 | + expect(await fromFile.keyedDigest(input)).toBe(expected); |
| 246 | + |
| 247 | + const ephemeral = new LocalCryptoProvider({ env: { NODE_ENV: 'test', HOME: home } }); |
| 248 | + expect(ephemeral.keySource).toBe('ephemeral'); |
| 249 | + expect(await ephemeral.keyedDigest(input)).toMatch(KEYED_SHAPE); |
| 250 | + }); |
| 251 | + |
| 252 | + it('a provider without key material refuses — it never falls back to an unkeyed digest', async () => { |
| 253 | + // The explicit-key route is the one source that is not length-checked, so it |
| 254 | + // is where an instance without usable key material can exist at all. The |
| 255 | + // positive control takes the same route with a real key. |
| 256 | + const control = new LocalCryptoProvider({ key: randomBytes(32) }); |
| 257 | + expect(await control.keyedDigest(input)).toMatch(KEYED_SHAPE); |
| 258 | + |
| 259 | + for (const key of [Buffer.alloc(0), randomBytes(16)]) { |
| 260 | + const p = new LocalCryptoProvider({ key }); |
| 261 | + expect(p.keySource).toBe('explicit'); |
| 262 | + const refusal = p.keyedDigest(input); |
| 263 | + await expect(refusal).rejects.toBeInstanceOf(KeyedDigestKeyUnavailableError); |
| 264 | + await expect(refusal).rejects.toMatchObject({ keyLength: key.length }); |
| 265 | + } |
| 266 | + }); |
| 267 | +}); |
| 268 | + |
157 | 269 | describe('InMemoryCryptoProvider backward-compat alias', () => { |
158 | 270 | it('is the same class as LocalCryptoProvider', () => { |
159 | 271 | expect(InMemoryCryptoProvider).toBe(LocalCryptoProvider); |
|
0 commit comments