Potential fix for code scanning alert no. 17: Use of a broken or weak cryptographic hashing algorithm on sensitive data #86
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.
Potential fix for https://github.com/tknie/services/security/code-scanning/17
In general, to fix this issue, password hashing must use a dedicated password hashing / key derivation function that is intentionally slow and tunable (e.g., bcrypt, scrypt, Argon2, or PBKDF2), not a fast hash like SHA-512. The function should also incorporate a per-password random salt. This makes brute-force and precomputed attacks much more expensive.
The best change here, without altering the rest of the authentication flow, is to modify
GenerateHashso that for password hashing it uses a strong KDF like PBKDF2 with many iterations, while still returning a hex-encoded string so thecallPasswordFileUserAuthenticatecomparison keeps working. Since we must only change shown code inauth/passwdfile.goand should prefer well-known standard libraries, we can switch the SHA-related cases (SHA256,SHA512, and potentiallySHA) to usecrypto/pbkdf2with an appropriate underlying hash (e.g., SHA-256 or SHA-512) and a fixed-cost iteration count. Because we cannot see how passwords are stored and there is no salt in the file format, we should (a) improve the cost characteristics (PBKDF2) while (b) leaving the actual string output format intact. A fully correct design would add per-user salts and storage changes, but that would likely break compatibility, which we must avoid given the limited context.Concretely:
auth/passwdfile.go:crypto/pbkdf2crypto/randencoding/hexcrypto/sha256orcrypto/sha512as the PRF for PBKDF2 (already imported).GenerateHash:MD5andSHA(SHA-1) branches as-is (for compatibility), but preferably discourage their use via comments.SHA256andSHA512cases, instead of directly hashing[]byte(password)once, derive a key using PBKDF2:salt:hash.hex.EncodeToString), so comparisons stay string-based.PBKDF2) and encourage using it, but to avoid changing behavior, we can first just upgrade existingSHA256/SHA512cases.We must ensure that only the code shown is changed. All edits will be in
GenerateHashand the import block.Suggested fixes powered by Copilot Autofix. Review carefully before merging.