-
-
Notifications
You must be signed in to change notification settings - Fork 33
feat(DEP0106): migrate from crypto.createCipher & crypto.createDecipher
#265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AugustinMauroy
wants to merge
10
commits into
main
Choose a base branch
from
augustin/DEP-0106
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9652001
WIP
AugustinMauroy 95e8100
clean
AugustinMauroy b42f401
Merge branch 'main' into augustin/DEP-0106
AugustinMauroy 4b84f12
Update package-lock.json
AugustinMauroy 4fb3589
WIP
AugustinMauroy 1555cd6
use dedent
AugustinMauroy 4fb2e31
Update workflow.ts
AugustinMauroy 8db3dd2
simplify
AugustinMauroy f7e7ecc
Merge branch 'main' into augustin/DEP-0106
AugustinMauroy ae6040b
Update workflow.ts
AugustinMauroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # crypto-createcipheriv-migration | ||
|
|
||
| > Migrates deprecated `crypto.createCipher()` / `crypto.createDecipher()` usage to the supported `crypto.createCipheriv()` / `crypto.createDecipheriv()` APIs with explicit key derivation and IV handling. | ||
|
|
||
| ## Why? | ||
|
|
||
| Node.js removed `crypto.createCipher()` and `crypto.createDecipher()` in v22.0.0 (DEP0106). The legacy helpers derived keys with MD5 and no salt, and silently reused static IVs. This codemod replaces those calls with the modern, explicit APIs and scaffolds secure key derivation and IV management. | ||
|
|
||
| ## What it does | ||
|
|
||
| - Detects CommonJS and ESM imports of `crypto` (including destructured bindings). | ||
| - Replaces invocations of `createCipher()` / `createDecipher()` with `createCipheriv()` / `createDecipheriv()`. | ||
| - Inserts scaffolding that derives keys with `crypto.scryptSync()` and generates random salts and IVs. | ||
| - Reminds developers to persist salt + IV for decryption and to adjust key/IV lengths per algorithm. | ||
| - Updates destructured imports to include the new helpers (`createCipheriv`, `createDecipheriv`, `randomBytes`, `scryptSync`). | ||
|
|
||
| ## Example | ||
|
|
||
| ```diff | ||
| -const cipher = crypto.createCipher(algorithm, password); | ||
| +const cipher = (() => { | ||
| + const __dep0106Salt = crypto.randomBytes(16); | ||
| + const __dep0106Key = crypto.scryptSync(password, __dep0106Salt, 32); | ||
| + const __dep0106Iv = crypto.randomBytes(16); | ||
| + // DEP0106: Persist __dep0106Salt and __dep0106Iv alongside the ciphertext so it can be decrypted later. | ||
| + return crypto.createCipheriv(algorithm, __dep0106Key, __dep0106Iv); | ||
| +})(); | ||
| ``` | ||
|
|
||
| ## Caveats | ||
|
|
||
| - The codemod cannot guarantee algorithm-specific key/IV sizes. Review the generated `scryptSync` length and IV length defaults and adjust as needed. | ||
| - Decryption snippets include placeholders (`Buffer.alloc(16)`) that must be replaced with the salt and IV stored during encryption. | ||
| - If your project already wraps key derivation logic, you may prefer to adapt the generated scaffolding to call existing helpers. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/crypto-createcipheriv-migration" | ||
| version: 1.0.0 | ||
| description: Replace removed `crypto.createCipher()`/`createDecipher()` with `crypto.createCipheriv()`/`createDecipheriv()` and secure key derivation (DEP0106) | ||
| author: Augustin Mauroy | ||
| license: MIT | ||
| workflow: workflow.yaml | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - transformation | ||
| - migration | ||
| - crypto | ||
|
|
||
| registry: | ||
| access: public | ||
| visibility: public |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "@nodejs/crypto-createcipheriv-migration", | ||
| "version": "1.0.0", | ||
| "description": "Migrate deprecated crypto.createCipher()/createDecipher() (DEP0106) to crypto.createCipheriv()/createDecipheriv() with secure key derivation.", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "npx codemod jssg test -l typescript ./src/workflow.ts ./" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/nodejs/userland-migrations.git", | ||
| "directory": "recipes/crypto-createcipheriv-migration", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "Augustin Mauroy", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/crypto-createcipheriv-migration/README.md", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.0.9" | ||
| }, | ||
| "dependencies": { | ||
| "@nodejs/codemod-utils": "*", | ||
| "dedent": "^1.7.1" | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.