forked from politics-rewired/Spoke
-
Notifications
You must be signed in to change notification settings - Fork 2
chore(crypto): use node v24 supported encryption format #103
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
ajohn25
wants to merge
3
commits into
main
Choose a base branch
from
chore-crypto-prep-24
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
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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| const crypto = require("crypto"); | ||
|
|
||
| // Defaults from config | ||
| const algorithm = "aes256"; | ||
| const inputEncoding = "utf8"; | ||
| const outputEncoding = "hex"; | ||
|
|
||
| // Legacy decryption using createDecipher (no IV) | ||
| function legacyDecrypt(encrypted, secret) { | ||
| const decipher = crypto.createDecipher(algorithm, secret); | ||
| let decrypted = decipher.update(encrypted, outputEncoding, inputEncoding); | ||
| decrypted += decipher.final(inputEncoding); | ||
| return decrypted; | ||
| } | ||
|
|
||
| // Legacy encryption using createCipher (no IV) | ||
| function legacyEncrypt(value, secret) { | ||
| const cipher = crypto.createCipher(algorithm, secret); | ||
| let encrypted = cipher.update(value, inputEncoding, outputEncoding); | ||
| encrypted += cipher.final(outputEncoding); | ||
| return encrypted; | ||
| } | ||
|
|
||
| // New encryption using createCipheriv (with IV) | ||
| function encryptWithIv(value, secret) { | ||
| const key = crypto.createHash("sha256").update(secret).digest(); | ||
| const iv = crypto.randomBytes(16); | ||
| const cipher = crypto.createCipheriv(algorithm, key, iv); | ||
|
|
||
| let encrypted = cipher.update(value, inputEncoding, outputEncoding); | ||
| encrypted += cipher.final(outputEncoding); | ||
| return `V2:${iv.toString(outputEncoding)}:${encrypted}`; | ||
| } | ||
|
|
||
| // New decryption using createDecipheriv (with IV) | ||
| function decryptWithIv(encrypted, secret) { | ||
| const key = crypto.createHash("sha256").update(secret).digest(); | ||
| const parts = encrypted.split(":"); | ||
| const iv = Buffer.from(parts[1], outputEncoding); | ||
| const encryptedData = parts[2]; | ||
|
|
||
| const decipher = crypto.createDecipheriv(algorithm, key, iv); | ||
| let decrypted = decipher.update(encryptedData, outputEncoding, inputEncoding); | ||
| decrypted += decipher.final(inputEncoding); | ||
| return decrypted; | ||
| } | ||
|
|
||
| exports.up = function up(knex) { | ||
| const sessionSecret = process.env.SESSION_SECRET; | ||
| if (!sessionSecret) { | ||
| throw new Error("SESSION_SECRET must be set to run this migration"); | ||
| } | ||
|
|
||
| return knex("messaging_service") | ||
| .select("messaging_service_sid", "encrypted_auth_token") | ||
| .whereNot("encrypted_auth_token", "") | ||
| .then((rows) => { | ||
| const updates = rows | ||
| .filter((row) => !row.encrypted_auth_token.startsWith("V2:")) | ||
| .map((row) => { | ||
| const decrypted = legacyDecrypt( | ||
| row.encrypted_auth_token, | ||
| sessionSecret | ||
| ); | ||
| const reEncrypted = encryptWithIv(decrypted, sessionSecret); | ||
| return knex("messaging_service") | ||
| .where("messaging_service_sid", row.messaging_service_sid) | ||
| .update({ encrypted_auth_token: reEncrypted }); | ||
| }); | ||
| return Promise.all(updates); | ||
| }); | ||
| }; | ||
|
|
||
| exports.down = function down(knex) { | ||
| const sessionSecret = process.env.SESSION_SECRET; | ||
| if (!sessionSecret) { | ||
| throw new Error("SESSION_SECRET must be set to run this migration"); | ||
| } | ||
|
|
||
| return knex("messaging_service") | ||
| .select("messaging_service_sid", "encrypted_auth_token") | ||
| .whereNot("encrypted_auth_token", "") | ||
| .then((rows) => { | ||
| const updates = rows | ||
| .filter((row) => row.encrypted_auth_token.startsWith("V2:")) | ||
| .map((row) => { | ||
| const decrypted = decryptWithIv( | ||
| row.encrypted_auth_token, | ||
| sessionSecret | ||
| ); | ||
| const reEncrypted = legacyEncrypt(decrypted, sessionSecret); | ||
| return knex("messaging_service") | ||
| .where("messaging_service_sid", row.messaging_service_sid) | ||
| .update({ encrypted_auth_token: reEncrypted }); | ||
| }); | ||
| return Promise.all(updates); | ||
| }); | ||
| }; | ||
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
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.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.