feat(core): Add encryptWithKey and decryptWithKey to Cipher service#28608
Open
guillaumejacquart wants to merge 1 commit intomasterfrom
Open
feat(core): Add encryptWithKey and decryptWithKey to Cipher service#28608guillaumejacquart wants to merge 1 commit intomasterfrom
guillaumejacquart wants to merge 1 commit intomasterfrom
Conversation
Adds explicit-key methods to the Cipher service so callers can pass pre-resolved key material and an algorithm. Legacy encrypt/decrypt now delegate to the new methods to keep a single CBC implementation. GCM branch is stubbed and throws until the algorithm lands. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
There was a problem hiding this comment.
No issues found across 2 files
Architecture diagram
sequenceDiagram
participant Caller as Caller (e.g. KeyManager)
participant Cipher as Cipher Service
participant IS as InstanceSettings
participant Crypto as Node.js Crypto
Note over Caller, Crypto: Encryption Flow
alt Legacy Path: encrypt(data, customKey?)
Caller->>Cipher: encrypt(data, optionalKey)
opt No customKey provided
Cipher->>IS: Get encryptionKey (global)
IS-->>Cipher: globalKey
end
Cipher->>Cipher: CHANGED: delegate to encryptWithKey()
else NEW: encryptWithKey(data, key, algorithm)
Caller->>Cipher: encryptWithKey(data, key, algorithm)
end
alt NEW: algorithm == 'aes-256-gcm'
Cipher-->>Caller: Throw "GCM not yet implemented"
else algorithm == 'aes-256-cbc'
Cipher->>Crypto: randomBytes(8) (salt)
Cipher->>Cipher: getKeyAndIv(salt, key)
Cipher->>Crypto: createCipheriv('aes-256-cbc', derivedKey, iv)
Cipher-->>Caller: Base64 string (Header + Salt + Ciphertext)
end
Note over Caller, Crypto: Decryption Flow
alt Legacy Path: decrypt(data, customKey?)
Caller->>Cipher: decrypt(data, optionalKey)
opt No customKey provided
Cipher->>IS: Get encryptionKey (global)
IS-->>Cipher: globalKey
end
Cipher->>Cipher: CHANGED: delegate to decryptWithKey()
else NEW: decryptWithKey(data, key, algorithm)
Caller->>Cipher: decryptWithKey(data, key, algorithm)
end
alt NEW: algorithm == 'aes-256-gcm'
Cipher-->>Caller: Throw "GCM not yet implemented"
else algorithm == 'aes-256-cbc'
Cipher->>Cipher: Extract salt from Base64 input
Cipher->>Cipher: getKeyAndIv(salt, key)
Cipher->>Crypto: createDecipheriv('aes-256-cbc', derivedKey, iv)
Cipher-->>Caller: Plaintext string
end
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
encryptWithKey(data, key, algorithm)anddecryptWithKey(data, key, algorithm)to theCipherservice inpackages/core. These let callers pass pre-resolved key material and an algorithm explicitly, instead of relying on the hardcodedInstanceSettingsencryption key — unblocking call sites that resolve keys per row viaKeyManagerService.The legacy
encrypt/decryptmethods are preserved and now delegate to the new ones, so there's a single CBC implementation and no regression for existing callers. Theaes-256-gcmbranch is stubbed and throws until the GCM implementation lands in a later ticket.How to test:
pnpm --filter n8n-core test cipher— 12 tests pass, including roundtrip, random-IV, wrong-key failure, GCM throws on both directions, and cross-compat between legacyencryptand newdecryptWithKey.Related Linear tickets, Github issues, and Community forum posts
https://linear.app/n8n/issue/IAM-492
Review / Merge checklist
Backport to Beta,Backport to Stable, orBackport to v1(if the PR is an urgent fix that needs to be backported)🤖 PR Summary generated by AI