Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Updated to include encrypting cookies, better protection client side
- Loading branch information
1 parent
ae134ea
commit 8eead6d
Showing
5 changed files
with
182 additions
and
83 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,57 @@ | ||
| const { randomBytes, createCipheriv, createDecipheriv } = require("crypto"); | ||
|
|
||
| const ALGORITHM = "aes-256-cbc"; | ||
|
|
||
| const encryptCookie = (cookie, _secret) => { | ||
| /** | ||
| * Encrypt a cookie using AES 256 bits | ||
| * @param {cookie} string the cookie we want to encrypt. Will be visible as plain string to client. | ||
| * @param {_secret} string the secret that will be stored server-side. Client will never see this. | ||
| */ | ||
| const iv = randomBytes(16); | ||
| const _cipher = createCipheriv(ALGORITHM, Buffer.from(_secret), iv); | ||
| const encrypted = [ | ||
| iv.toString("hex"), | ||
| ":", | ||
| _cipher.update(cookie, "utf8", "hex"), | ||
| _cipher.final("hex") | ||
| ]; | ||
| return encrypted.join(""); | ||
| }; | ||
|
|
||
| const decryptCookie = (cookie, _secret) => { | ||
| /** | ||
| * Decrypt a cookie using AES 256 bits | ||
| * @param {cookie} string the cookie we want to encrypt. Will be visible as plain string to client. | ||
| * @param {_secret} string the secret that will be stored server-side. Client will never see this. | ||
| */ | ||
| const _encryptedArray = cookie.split(":"); | ||
| if (_encryptedArray.length != 2) throw new Error("bad decrypt"); | ||
| const iv = new Buffer(_encryptedArray[0], "hex"); | ||
| const encrypted = new Buffer(_encryptedArray[1], "hex"); | ||
| const decipher = createDecipheriv(ALGORITHM, _secret, iv); | ||
| const decrypted = | ||
| decipher.update(encrypted, "hex", "utf8") + decipher.final("utf8"); | ||
| return decrypted; | ||
| }; | ||
|
|
||
| const verifyCsrf = (requestCsrf, cookieCsrf, _secret) => { | ||
| /** | ||
| * Verify a CSRF token | ||
| * @param {requestCsrf} string the CSRF coming from client side | ||
| * @param {cookieCsrf} string the CSRF as stored in the user's cookies | ||
| * @param {_secret} string the string used to encrypt the CSRF in the first place. | ||
| */ | ||
| try { | ||
| const decryptedCookie = decryptCookie(cookieCsrf, _secret); | ||
| return decryptedCookie === requestCsrf; | ||
| } catch (err) { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| module.exports = { | ||
| encryptCookie, | ||
| decryptCookie, | ||
| verifyCsrf | ||
| }; |
This file contains 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
This file contains 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.