-
-
Notifications
You must be signed in to change notification settings - Fork 27
feat(crypto-fips): add migration recipe for crypto.fips #177
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
39f92b0
feat(crypto-fips): add migration recipe for transitioning from crypto…
max-programming 0b7a664
Use one-liner arrow function
max-programming 17c3bc9
chore(vscode): revert settings json file to default
max-programming 078fecd
refactor(crypto-fips): streamline crypto base path retrieval and impr…
max-programming 7cd8777
refactor(crypto-fips): use generators for crypto base path collection
max-programming 866e1fc
refactor(crypto-fips): enhance code formatting and improve readability
max-programming 7200be7
refactor(crypto-fips): rename functions and variables for clarity in …
max-programming b4d9c0c
refactor(crypto-fips): implement transformation for crypto.fips to ge…
max-programming fe9ec48
refactor(crypto-fips): update README examplesto have import statement
max-programming 8c3bd84
refactor(crypto-fips): use existing utility function and reformat cod…
max-programming 91a80be
refactor(crypto-fips): added missing jsdoc for a functions
max-programming 92a829e
refactor(crypto-fips): add an extra line
max-programming 515da7d
docs(crypto-fips): fix description
max-programming a475efa
Merge branch 'main' into crypto-fips
max-programming 1e2f114
Merge remote-tracking branch 'upstream/main' into crypto-fips
max-programming 07856fb
chore: update @codemod.com/jssg-types dependency to version 1.0.9 in …
max-programming 9ce214e
feat(crypto-fips): enhance transform function to handle crypto.fips b…
max-programming 034d8ed
Merge branch 'main' into crypto-fips
max-programming a09c7a2
fix lockfile
max-programming 206c72e
Added many-to-many support to the updateBinding utility function
max-programming 021b88a
Handle many-to-many "aliased" imports in update-binding util
max-programming f7e10f7
refactor: use the updateBinding utility function
max-programming d1977a8
chore: follow convention for test case names
max-programming 2fb1f50
fix: correct test case name
max-programming 91c8019
refactor: improve binding handling in update-binding utility
max-programming 596670c
fix: failing test cases
max-programming bef68ce
Merge branch 'main' into crypto-fips
max-programming 2131f72
Change all arrows to ligature arrow
max-programming 0caa4bd
Change name from crypto-fips to crypto-fips-to-getFips
max-programming de3d804
make biome the default formatter for TypeScript and JavaScript
max-programming ff4b850
Use getNodeImportCalls
max-programming 24e7c1d
Added a testcase for dynamic imports
max-programming e9de77a
docs: dynamic imports in jsdoc
max-programming f5c847f
fix: remove redundant escape regex function
max-programming 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
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 |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| { | ||
| "editor.formatOnSave": true, | ||
| "javascript.updateImportsOnFileMove.enabled": "always", | ||
| "typescript.updateImportsOnFileMove.enabled": "always", | ||
| "editor.formatOnPaste": true, | ||
| "editor.wordWrap": "wordWrapColumn", | ||
| "editor.wordWrapColumn": 100, | ||
| "[markdown]": { | ||
| "editor.wordWrap": "off" | ||
| } | ||
| "editor.formatOnSave": true, | ||
| "javascript.updateImportsOnFileMove.enabled": "always", | ||
| "typescript.updateImportsOnFileMove.enabled": "always", | ||
| "editor.formatOnPaste": true, | ||
| "editor.wordWrap": "wordWrapColumn", | ||
| "editor.wordWrapColumn": 100, | ||
| "[javascript][typescript][json]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "[markdown]": { | ||
| "editor.wordWrap": "off" | ||
| }, | ||
| "[typescript]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| } | ||
| } |
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,33 @@ | ||
| # `crypto.fips` DEP0093 | ||
|
|
||
| This recipe provides a guide for migrating from the deprecated `crypto.fips` to `crypto.getFips()` and `crypto.setFips()`. | ||
max-programming marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| See [DEP0093](https://nodejs.org/api/deprecations.html#DEP0093). | ||
|
|
||
| ## Examples | ||
|
|
||
| **Before:** | ||
|
|
||
| ```js | ||
| // Using crypto.fips | ||
| crypto.fips; | ||
|
|
||
| // Using crypto.fips = true | ||
| crypto.fips = true; | ||
|
|
||
| // Using crypto.fips = false | ||
| crypto.fips = false; | ||
| ``` | ||
|
|
||
| **After:** | ||
|
|
||
| ```js | ||
| // Using crypto.getFips() | ||
| crypto.getFips(); | ||
|
|
||
| // Using crypto.setFips(true) | ||
| crypto.setFips(true); | ||
|
|
||
| // Using crypto.setFips(false) | ||
| crypto.setFips(false); | ||
| ``` | ||
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,21 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/crypto-fips" | ||
max-programming marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| version: 1.0.0 | ||
| description: Handle DEP0093 via transforming `crypto.fips` to `crypto.getFips()` and `crypto.setFips()` | ||
| author: Usman S. | ||
| license: MIT | ||
| workflow: workflow.yaml | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - transformation | ||
| - migration | ||
|
|
||
| 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,24 @@ | ||
| { | ||
| "name": "@nodejs/crypto-fips", | ||
| "version": "1.0.0", | ||
| "description": "Handle DEP0093 via transforming `crypto.fips` to `crypto.getFips()` and `crypto.setFips()`", | ||
| "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-fips", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "Usman S.", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/crypto-fips/README.md", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.0.3" | ||
| }, | ||
| "dependencies": { | ||
| "@nodejs/codemod-utils": "*" | ||
| } | ||
| } |
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,95 @@ | ||
| import { getNodeImportStatements } from '@nodejs/codemod-utils/ast-grep/import-statement'; | ||
max-programming marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import { getNodeRequireCalls } from '@nodejs/codemod-utils/ast-grep/require-call'; | ||
| import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path'; | ||
| import type { SgRoot, Edit, SgNode } from '@codemod.com/jssg-types/main'; | ||
|
|
||
| function escapeRegExp(input: string): string { | ||
| return input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
max-programming marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Transform function that converts deprecated crypto.fips calls | ||
| * to the new crypto.getFips() and crypto.setFips() syntax. | ||
| * | ||
| * Handles: | ||
| * 1. crypto.fips -> crypto.getFips() | ||
| * 2. crypto.fips = true -> crypto.setFips(true) | ||
| * 3. crypto.fips = false -> crypto.setFips(false) | ||
| */ | ||
| export default function transform(root: SgRoot): string | null { | ||
| const rootNode = root.root(); | ||
| let hasChanges = false; | ||
| const edits: Edit[] = []; | ||
|
|
||
| const cryptoBases = new Set<string>(); | ||
| setCryptoBases(getNodeRequireCalls(root, 'crypto'), cryptoBases); | ||
| setCryptoBases(getNodeImportStatements(root, 'crypto'), cryptoBases); | ||
|
|
||
| const assignmentResult = replaceAssignments(rootNode, cryptoBases); | ||
| edits.push(...assignmentResult.edits); | ||
| hasChanges = assignmentResult.hasChanges; | ||
|
|
||
| const readResult = replaceReads(rootNode, cryptoBases); | ||
| edits.push(...readResult.edits); | ||
| hasChanges = readResult.hasChanges; | ||
|
|
||
| if (!hasChanges) return null; | ||
| return rootNode.commitEdits(edits); | ||
max-programming marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| function setCryptoBases(statements: SgNode[], cryptoBases: Set<string>) { | ||
| for (const stmt of statements) { | ||
| const resolvedPath = resolveBindingPath(stmt, '$.fips'); | ||
| if (!resolvedPath || !resolvedPath.includes('.')) continue; | ||
| cryptoBases.add(resolvedPath.slice(0, resolvedPath.lastIndexOf('.'))); | ||
| } | ||
| } | ||
|
|
||
| function replaceAssignments(rootNode: SgNode, cryptoBases: Set<string>) { | ||
max-programming marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const edits: Edit[] = []; | ||
| let hasChanges = false; | ||
|
|
||
| for (const base of cryptoBases) { | ||
| const assignments = rootNode.findAll({ | ||
| rule: { | ||
| pattern: `${base}.fips = $VALUE`, | ||
| }, | ||
| }); | ||
|
|
||
| for (const assign of assignments) { | ||
| const valueText = assign.getMatch('VALUE')?.text() ?? ''; | ||
| const basePropRegex = new RegExp( | ||
| `\\b${escapeRegExp(base)}\\.fips\\b`, | ||
| 'g', | ||
| ); | ||
| const transformedValue = valueText.replace( | ||
| basePropRegex, | ||
| `${base}.getFips()`, | ||
| ); | ||
| edits.push(assign.replace(`${base}.setFips(${transformedValue})`)); | ||
| hasChanges = true; | ||
| } | ||
| } | ||
|
|
||
| return { edits, hasChanges }; | ||
| } | ||
|
|
||
| function replaceReads(rootNode: SgNode, cryptoBases: Set<string>) { | ||
max-programming marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const edits: Edit[] = []; | ||
| let hasChanges = false; | ||
|
|
||
| for (const base of cryptoBases) { | ||
| const reads = rootNode.findAll({ | ||
| rule: { | ||
| pattern: `${base}.fips`, | ||
| }, | ||
| }); | ||
|
|
||
| for (const read of reads) { | ||
| edits.push(read.replace(`${base}.getFips()`)); | ||
| hasChanges = true; | ||
| } | ||
| } | ||
|
|
||
| return { edits, hasChanges }; | ||
| } | ||
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,5 @@ | ||
| const crypto = require("node:crypto"); | ||
|
|
||
| if (crypto.getFips()) { | ||
| console.log("FIPS mode is enabled"); | ||
| } |
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,3 @@ | ||
| const crypto = require("node:crypto"); | ||
|
|
||
| crypto.setFips(true); |
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,6 @@ | ||
| const crypto = require("node:crypto"); | ||
|
|
||
| if (process.env.ENABLE_FIPS === "true") { | ||
| crypto.setFips(true); | ||
| } | ||
| console.log("FIPS enabled:", crypto.getFips()); |
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,4 @@ | ||
| import crypto from "node:crypto"; | ||
|
|
||
| const fipsStatus = crypto.getFips(); | ||
| crypto.setFips(!fipsStatus); |
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,4 @@ | ||
| const nodeCrypto = require("node:crypto"); | ||
|
|
||
| const currentFips = nodeCrypto.getFips(); | ||
| nodeCrypto.setFips(!currentFips); |
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,4 @@ | ||
| const crypto = require("node:crypto"); | ||
|
|
||
| console.log("FIPS enabled:", crypto.getFips()); | ||
| crypto.setFips(crypto.getFips() || process.env.FORCE_FIPS); |
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,5 @@ | ||
| const crypto = require("node:crypto"); | ||
|
|
||
| if (crypto.fips) { | ||
| console.log("FIPS mode is enabled"); | ||
| } |
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,3 @@ | ||
| const crypto = require("node:crypto"); | ||
|
|
||
| crypto.fips = true; |
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,6 @@ | ||
| const crypto = require("node:crypto"); | ||
|
|
||
| if (process.env.ENABLE_FIPS === "true") { | ||
| crypto.fips = true; | ||
| } | ||
| console.log("FIPS enabled:", crypto.fips); |
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,4 @@ | ||
| import crypto from "node:crypto"; | ||
|
|
||
| const fipsStatus = crypto.fips; | ||
| crypto.fips = !fipsStatus; |
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,4 @@ | ||
| const nodeCrypto = require("node:crypto"); | ||
|
|
||
| const currentFips = nodeCrypto.fips; | ||
| nodeCrypto.fips = !currentFips; |
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,4 @@ | ||
| const crypto = require("node:crypto"); | ||
|
|
||
| console.log("FIPS enabled:", crypto.fips); | ||
| crypto.fips = crypto.fips || process.env.FORCE_FIPS; |
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,23 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "allowImportingTsExtensions": true, | ||
| "allowJs": true, | ||
| "alwaysStrict": true, | ||
| "baseUrl": "./", | ||
| "declaration": true, | ||
| "declarationMap": true, | ||
| "emitDeclarationOnly": true, | ||
| "lib": ["ESNext", "DOM"], | ||
| "module": "NodeNext", | ||
| "moduleResolution": "NodeNext", | ||
| "noImplicitThis": true, | ||
| "removeComments": true, | ||
| "strict": true, | ||
| "stripInternal": true, | ||
| "target": "esnext" | ||
| }, | ||
| "include": ["./"], | ||
| "exclude": [ | ||
| "tests/**" | ||
| ] | ||
| } |
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 @@ | ||
| # yaml-language-server: $schema=https://raw.githubusercontent.com/codemod-com/codemod/refs/heads/main/schemas/workflow.json | ||
|
|
||
| version: "1" | ||
|
|
||
| nodes: | ||
| - id: apply-transforms | ||
| name: Apply AST Transformations | ||
| type: automatic | ||
| steps: | ||
| - name: Handle DEP0093 via transforming `crypto.fips` to `crypto.getFips()`, `crypto.setFips()` to `crypto.setFips(true)` and `crypto.setFips(false)`. | ||
| js-ast-grep: | ||
| js_file: src/workflow.ts | ||
| base_path: . | ||
| include: | ||
| - "**/*.js" | ||
| - "**/*.jsx" | ||
| - "**/*.mjs" | ||
| - "**/*.cjs" | ||
| - "**/*.cts" | ||
| - "**/*.mts" | ||
| - "**/*.ts" | ||
| - "**/*.tsx" | ||
| exclude: | ||
| - "**/node_modules/**" | ||
| language: typescript |
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.
Uh oh!
There was an error while loading. Please reload this page.