-
Notifications
You must be signed in to change notification settings - Fork 206
Onboarding #655
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
Onboarding #655
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| // Inspired from: https://github.com/nextauthjs/next-auth/blob/c4ad77b86762b7fd2e6362d8bf26c5953846774a/packages/next-auth/src/core/lib/utils.ts#L16 | ||
| export function hashCode(code: number) { | ||
| return createHash("sha256") | ||
| .update(`${code}${process.env.AUTH_SECRET}`) |
Check failure
Code scanning / CodeQL
Use of password hash with insufficient computational effort High
a call to generateUniquePasscode
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 3 months ago
To fix the problem, we should replace the use of crypto.createHash('sha256') with a proper password hashing function, such as bcrypt. This means importing the bcrypt library, and using bcrypt.hashSync (or its async variant if desired) to hash the passcode along with a unique salt.
The hashCode function in apps/web/lib/utils.ts should be changed to use bcrypt.hashSync, using a salt value that is sufficiently high (e.g., 10 rounds). Since only the static six-digit passcode (plus the secret) is currently hashed, the resulting code will now use bcrypt's built-in salting and iteration functionality. Any usage of hashCode (called on line 33 in apps/web/app/api/auth/code/generate/route.ts) will now use the improved hashing scheme, but no further changes are needed to the rest of this code.
We need to:
- Import the
bcryptlibrary at the top ofapps/web/lib/utils.ts. - Update
hashCodeto usebcrypt.hashSyncinstead ofcrypto.createHash. - Optionally, update any relevant tests or usages, but none are present in the shown snippet.
-
Copy modified lines R2-R3 -
Copy modified lines R73-R75
| @@ -1,5 +1,6 @@ | ||
| import { UIConstants } from "@courselit/common-models"; | ||
| import { createHash, randomInt } from "crypto"; | ||
| import { randomInt } from "crypto"; | ||
| import bcrypt from "bcrypt"; | ||
|
|
||
| export const capitalize = (s: string) => { | ||
| if (typeof s !== "string") return ""; | ||
| @@ -69,7 +70,7 @@ | ||
|
|
||
| // Inspired from: https://github.com/nextauthjs/next-auth/blob/c4ad77b86762b7fd2e6362d8bf26c5953846774a/packages/next-auth/src/core/lib/utils.ts#L16 | ||
| export function hashCode(code: number) { | ||
| return createHash("sha256") | ||
| .update(`${code}${process.env.AUTH_SECRET}`) | ||
| .digest("hex"); | ||
| const saltRounds = 10; | ||
| const toHash = `${code}${process.env.AUTH_SECRET}`; | ||
| return bcrypt.hashSync(toHash, saltRounds); | ||
| } |
-
Copy modified lines R80-R81
| @@ -77,7 +77,8 @@ | ||
| "stripe": "^17.5.0", | ||
| "tailwind-merge": "^2.5.4", | ||
| "tailwindcss-animate": "^1.0.7", | ||
| "zod": "^3.24.1" | ||
| "zod": "^3.24.1", | ||
| "bcrypt": "^6.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@shelf/jest-mongodb": "^5.2.2", |
| Package | Version | Security advisories |
| bcrypt (npm) | 6.0.0 | None |
No description provided.