Repository navigation
[SEC-A04] Revisión cripto: passwords y JWT_SECRET #185
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0f40acf
OM-SEC-A04: feat: revisión cripto - bcrypt con coste y JWT_SECRET seguro
Benjakr04 1976822
OM-SEC-A04: feat: revisión cripto - bcrypt con coste y JWT_SECRET seguro
Benjakr04 35ce5ea
OM-SEC-A04: arregle los tests
Benjakr04 236a070
OM-SEC-A04: solucionando plagueos del conejito malo
Benjakr04 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
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,60 @@ | ||
| import bcrypt from 'bcrypt' | ||
|
|
||
| /** | ||
| * Coste de bcrypt adaptado al ambiente: | ||
| * - Desarrollo: 10 (rápido para testing) | ||
| * - Producción: 12 (más seguro, ~100ms por hash) | ||
| * | ||
| * En 2025 se recomienda 12+, pero el hardware del servidor define el máximo | ||
| * antes de timeouts (típicamente 100-200ms). | ||
| */ | ||
| const BCRYPT_ROUNDS = process.env.NODE_ENV === 'production' ? 12 : 10 | ||
|
|
||
| /** | ||
| * Hash de contraseña con bcrypt. | ||
| * NUNCA devuelve la contraseña en claro. | ||
| * | ||
| * @param {string} plainPassword - Contraseña sin hashear | ||
| * @returns {Promise<string>} Hash bcrypt seguro | ||
| * @throws {Error} Si la contraseña es inválida o el hash falla | ||
| */ | ||
| export async function hashPassword(plainPassword) { | ||
| if (!plainPassword || typeof plainPassword !== 'string') { | ||
| throw new Error('La contraseña debe ser un string no vacío') | ||
| } | ||
|
|
||
| if (plainPassword.length < 6) { | ||
| throw new Error('La contraseña debe tener mínimo 6 caracteres') | ||
| } | ||
|
|
||
| try { | ||
| return await bcrypt.hash(plainPassword, BCRYPT_ROUNDS) | ||
| } catch (error) { | ||
| console.error('[PASSWORD_HASH_ERROR]', error.message) | ||
| throw new Error('Error al procesar la contraseña') | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Compara contraseña en claro con hash bcrypt. | ||
| * NUNCA loguea la contraseña. | ||
| * | ||
| * @param {string} plainPassword - Contraseña ingresada por el usuario | ||
| * @param {string} hashedPassword - Hash almacenado en BD | ||
| * @returns {Promise<boolean>} true si coincide, false si no | ||
| * @throws {Error} Si hay error técnico en bcrypt (hash corrupto, etc) | ||
| */ | ||
| export async function verifyPassword(plainPassword, hashedPassword) { | ||
| if (!plainPassword || !hashedPassword) { | ||
| return false | ||
| } | ||
|
|
||
| try { | ||
| return await bcrypt.compare(plainPassword, hashedPassword) | ||
| } catch (error) { | ||
| // Loguear el error técnico para debugging en desarrollo | ||
| // pero NO exponer detalles en respuesta al cliente | ||
| console.error('[PASSWORD_VERIFY_ERROR]', error.message) | ||
| throw new Error('Error al verificar contraseña') | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.