Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(security): pin AES-256-GCM authTag length to 16 bytes on decrypt (#284)

@aaronjmars6b2d87d316e2
diff --git a/server/src/__tests__/lib/crypto.test.ts b/server/src/__tests__/lib/crypto.test.ts
index 14cc3e8a..121ddcfd 100644
--- a/server/src/__tests__/lib/crypto.test.ts
+++ b/server/src/__tests__/lib/crypto.test.ts
@@ -28,6 +28,14 @@ describe('Crypto', () => {
expect(() => decrypt(encrypted, iv, 'a'.repeat(32))).toThrow();
});
+ it('should reject truncated auth tags (< 16 bytes) to block forgery brute-force', () => {
+ const { encrypted, iv, authTag } = encrypt('test-key');
+ for (const truncatedBytes of [4, 8, 12, 13, 14, 15]) {
+ const truncated = authTag.slice(0, truncatedBytes * 2);
+ expect(() => decrypt(encrypted, iv, truncated)).toThrow();
+ }
+ });
+
describe('maskKey', () => {
it('should mask long keys', () => {
expect(maskKey('gsk_test1234567890abcdef')).toBe('gsk_...cdef');
diff --git a/server/src/lib/crypto.ts b/server/src/lib/crypto.ts
index 7635a355..bd78ccd1 100644
--- a/server/src/lib/crypto.ts
+++ b/server/src/lib/crypto.ts
@@ -98,9 +98,15 @@ export function encrypt(text: string): { encrypted: string; iv: string; authTag:
};
}
+// AUTH_TAG_BYTES pins the GCM tag length to 16 bytes. Without this option Node
+// will accept any tag of length 4–16 bytes (RFC 5116 §3.2), which lets anyone
+// who can rewrite a row in `api_keys` swap in a 4-byte tag and start brute-
+// forcing forgeries at 2^32 attempts. Pinning closes that truncation path.
+const AUTH_TAG_BYTES = 16;
+
export function decrypt(encrypted: string, iv: string, authTag: string): string {
const key = getEncryptionKey();
- const decipher = crypto.createDecipheriv(ALGORITHM, key, Buffer.from(iv, 'hex'));
+ const decipher = crypto.createDecipheriv(ALGORITHM, key, Buffer.from(iv, 'hex'), { authTagLength: AUTH_TAG_BYTES });
decipher.setAuthTag(Buffer.from(authTag, 'hex'));
let decrypted = decipher.update(encrypted, 'hex', 'utf8');