Skip to content

fix: specify authTagLength in AES-GCM createDecipheriv calls#3881

Closed
NLmejiro wants to merge 1 commit intosimstudioai:mainfrom
NLmejiro:fix/gcm-auth-tag-length
Closed

fix: specify authTagLength in AES-GCM createDecipheriv calls#3881
NLmejiro wants to merge 1 commit intosimstudioai:mainfrom
NLmejiro:fix/gcm-auth-tag-length

Conversation

@NLmejiro
Copy link
Copy Markdown

@NLmejiro NLmejiro commented Apr 1, 2026

Summary

Specifies explicit authTagLength: 16 parameter in createDecipheriv calls using AES-256-GCM mode.

Vulnerability

CWE-310 (Cryptographic Issues) — missing authentication tag length specification in GCM mode decryption. Without an explicit expected tag length, an attacker may be able to spoof ciphertexts using a shorter-than-expected authentication tag.

Affected Files

  • apps/sim/lib/api-key/crypto.ts
  • apps/sim/lib/core/security/encryption.ts
  • packages/db/scripts/migrate-block-api-keys-to-byok.ts

Fix

```typescript
// Before
crypto.createDecipheriv('aes-256-gcm', key, iv)

// After
crypto.createDecipheriv('aes-256-gcm', key, iv, { authTagLength: 16 })
```

Fixes missing authTagLength parameter in createDecipheriv calls using
AES-256-GCM mode. Without explicit tag length specification, the
application may be tricked into accepting shorter authentication tags,
potentially allowing ciphertext spoofing.

CWE-310: Cryptographic Issues (gcm-no-tag-length)
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 1, 2026

@NLmejiro is attempting to deploy a commit to the Sim Team on Vercel.

A member of the Team first needs to authorize it.

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 1, 2026

PR Summary

Medium Risk
Touches cryptography used to decrypt stored secrets/API keys; while the change is small, it can break decryption if any ciphertexts were created with a non-16-byte auth tag or if runtime crypto behavior differs across environments.

Overview
AES-256-GCM decryption is tightened by passing { authTagLength: 16 } into createDecipheriv in the app API-key decryptor, the shared secret decryptor, and the BYOK migration script.

This standardizes the expected GCM auth tag size during decryption to reduce acceptance of malformed/short tags and align behavior across these call sites.

Written by Cursor Bugbot for commit 84fe778. This will update automatically on new commits. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 1, 2026

Greptile Summary

This PR hardens AES-256-GCM decryption across three files by explicitly passing { authTagLength: 16 } to every createDecipheriv call. Without this option, Node.js's setAuthTag silently accepts authentication tags as short as 4 bytes, which opens the door to GCM tag-truncation/forgery attacks (CWE-310). By fixing the expected length at 16 bytes (128 bits), any ciphertext presented with a shorter tag will now cause setAuthTag to throw, preventing spoofing.

Key points:

  • All three changed files are consistent: encryption (createCipheriv) already defaults to a 16-byte tag via getAuthTag(), so decryption enforcing exactly 16 bytes is the correct complement.
  • The fix is backward-compatible — all previously encrypted values carry a full 16-byte tag and will continue to decrypt without error.
  • The migration script (migrate-block-api-keys-to-byok.ts) carries a copy of the encryption helpers; it is correctly patched in lock-step with the app-level implementations.

Confidence Score: 5/5

This PR is safe to merge — changes are minimal, correct, and strictly additive to security without breaking backward compatibility.

All three changes are identical one-line additions of { authTagLength: 16 } to createDecipheriv calls. The fix is consistent with the encrypt side (which already produces 16-byte tags by default), introduces no new logic paths, and no P0/P1 issues were found.

No files require special attention — all changes are straightforward and correct.

Important Files Changed

Filename Overview
apps/sim/lib/api-key/crypto.ts Single-line security fix: adds explicit authTagLength: 16 to createDecipheriv in decryptApiKey, correctly preventing GCM tag-truncation attacks without affecting existing encrypted data.
apps/sim/lib/core/security/encryption.ts Single-line security fix: adds explicit authTagLength: 16 to createDecipheriv in decryptSecret, consistent with the encryptSecret counterpart which produces a 16-byte tag by default.
packages/db/scripts/migrate-block-api-keys-to-byok.ts Single-line security fix: mirrors the same authTagLength: 16 hardening in the self-contained migration script's local decryptSecret, keeping it in sync with the app-level implementation.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant Decryptor
    participant NodeCrypto as Node.js crypto

    Caller->>Decryptor: decryptApiKey/decryptSecret(encryptedValue)
    Decryptor->>Decryptor: parse iv, ciphertext, authTag from "iv:ciphertext:authTag"
    Decryptor->>NodeCrypto: createDecipheriv('aes-256-gcm', key, iv, { authTagLength: 16 })
    Note over NodeCrypto: authTagLength: 16 enforces tag must be exactly 16 bytes
    Decryptor->>NodeCrypto: decipher.setAuthTag(authTag)
    Note over NodeCrypto: Throws if authTag.length is not 16
    Decryptor->>NodeCrypto: decipher.update(encrypted) + decipher.final()
    Note over NodeCrypto: final() verifies GCM tag and throws if invalid
    NodeCrypto-->>Decryptor: plaintext
    Decryptor-->>Caller: decrypted plaintext
Loading

Reviews (1): Last reviewed commit: "fix: specify authTagLength in AES-GCM de..." | Re-trigger Greptile

waleedlatif1 added a commit that referenced this pull request Apr 1, 2026
…tency

Complements #3881 by adding explicit authTagLength: 16 to the encrypt
side as well, ensuring both cipher and decipher specify the tag length.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
waleedlatif1 added a commit that referenced this pull request Apr 1, 2026
… calls (#3883)

* fix: specify authTagLength in AES-GCM decipheriv calls

Fixes missing authTagLength parameter in createDecipheriv calls using
AES-256-GCM mode. Without explicit tag length specification, the
application may be tricked into accepting shorter authentication tags,
potentially allowing ciphertext spoofing.

CWE-310: Cryptographic Issues (gcm-no-tag-length)

* fix: specify authTagLength on createCipheriv calls for AES-GCM consistency

Complements #3881 by adding explicit authTagLength: 16 to the encrypt
side as well, ensuring both cipher and decipher specify the tag length.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: clean up crypto modules

- Fix error: any → error: unknown with proper type guard in encryption.ts
- Eliminate duplicate iv.toString('hex') calls in both encrypt functions
- Remove redundant string split in decryptApiKey (was splitting twice)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* new turborepo version

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Lakee Sivaraya <71339072+lakeesiv@users.noreply.github.com>
Co-authored-by: Vikhyath Mondreti <vikhyath@simstudio.ai>
Co-authored-by: Vikhyath Mondreti <vikhyathvikku@gmail.com>
Co-authored-by: Siddharth Ganesan <33737564+Sg312@users.noreply.github.com>
Co-authored-by: NLmejiro <kuroda.k1021@gmail.com>
@NLmejiro NLmejiro closed this by deleting the head repository Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant