Security: Fix CWE-328 (Weak Hash Algorithm) vulnerability in src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02476.java:51#680
Open
appsecai-app[bot] wants to merge 1 commit intomainfrom
Conversation
…erability Replace insecure MD5 algorithm with SHA-256 in BenchmarkTest02476.java:51. MD5 is cryptographically broken and susceptible to collision attacks. SHA-256 is NIST-approved and provides no known practical collision attacks. Fixes 1 CWE-328 vulnerability. No behavioral regression.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What we found
69c734ebsrc/main/java/org/owasp/benchmark/testcode/BenchmarkTest02476.java:51Description: Line 51 explicitly instantiates
MessageDigestwith theMD5algorithm string. MD5 is cryptographically broken and susceptible to collision attacks, making it unsuitable for any security-relevant hashing operation.Why this matters
Cryptographic weakness: MD5 is not collision-resistant. Two distinct inputs can be crafted to produce the same digest, invalidating any integrity or authenticity guarantee the hash was meant to provide.
Attack surface in this codebase: User-controlled input flows from the HTTP request parameter
BenchmarkTest02476(line 43-46) through thedoSomething()method (line 48) into the MD5 digest operation at line 66. The hash result is written topasswordFile.txt(line 72), indicating a password-storage context. Without salting, key-stretching, or HMAC protection, the raw MD5 hash is trivially reversible via rainbow tables or brute-force attacks.Risk if not fixed: An attacker with read access to
passwordFile.txtcan recover the original input values through offline cracking, compromising any security guarantee the hashing operation was intended to provide.Why we're changing it
MD5 has been cryptographically broken since 2004 (Wang et al. collision attack). NIST SP 800-107 and FIPS PUB 180-4 recommend SHA-256 or stronger for general-purpose hashing. SHA-256 provides a 256-bit digest with no known practical collision attacks and is a drop-in replacement for the
MessageDigestAPI.The fix replaces
MessageDigest.getInstance("MD5")withMessageDigest.getInstance("SHA-256")at line 51. All downstream operations (md.update(input),md.digest(), base64 encoding, file write) remain functionally equivalent with no behavioral regression.How we confirmed
"MD5"string passed toMessageDigest.getInstance()BenchmarkTest02476(line 43-46) flows throughdoSomething()(line 48) toinputParam(line 53-65) and intomd.update(input)(line 66)passwordFile.txt(line 72) confirms security-sensitive hashing contextVulnerability Flow Diagram
%%{init: {'theme':'base','themeVariables':{'fontFamily':'ui-sans-serif, Inter, system-ui, sans-serif','primaryColor':'#EDE9FE','primaryTextColor':'#1A1A2E','primaryBorderColor':'#7C3AED','lineColor':'#5B21B6','secondaryColor':'#FEF3C7','tertiaryColor':'#DCFCE7'}}}%% flowchart TD A["HTTP Request Parameter<br/>BenchmarkTest02476"] --> B["doSomething() Method<br/>Line 48"] B --> C["inputParam Assignment<br/>Line 53-65"] C --> D["MessageDigest.getInstance<br/>MD5 - Line 51"] D --> E["md.update inputParam<br/>Line 66"] E --> F["md.digest Computation<br/>Line 68"] F --> G["Write to passwordFile.txt<br/>Line 72"] G --> H["❌ Collision Vulnerability<br/>Offline Cracking Risk"] I["✅ Fixed - SHA-256<br/>Line 51"] -.-> J["No Known Collisions<br/>NIST Approved"] J -.-> K["Same API Contract<br/>No Regression"] style D fill:#FFE5E5,stroke:#F65A5A style H fill:#FEF3C7,stroke:#F59E0B style I fill:#DCFCE7,stroke:#16A34A style J fill:#DCFCE7,stroke:#16A34AVulnerable flow: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02476.java:51
Weak Hash Algorithm
%%{init: {'theme':'base','themeVariables':{'fontFamily':'ui-sans-serif, Inter, system-ui, sans-serif','primaryColor':'#EDE9FE','primaryTextColor':'#1A1A2E','primaryBorderColor':'#7C3AED','lineColor':'#5B21B6','secondaryColor':'#FEF3C7','tertiaryColor':'#DCFCE7'}}}%% flowchart TD subgraph Vulnerable["❌ Vulnerable Flow"] direction LR A1["Project"] --> A2["Reversible one-way hash used"] A2 --> A3["💥 Password Recovery"] end Vulnerable ~~~ Fixed subgraph Fixed["✅ Fixed Flow"] direction LR B1["Project"] --> B2["Use bcrypt/argon2 for passwords"] B3["🛡️ Attack Blocked"] B2 --> B3 end style A2 fill:#FFE5E5,color:#000 style A3 fill:#ffa94d,color:#000 style B2 fill:#74c0fc,color:#000 style B3 fill:#DCFCE7,color:#000How we fixed it
Change: Line 51 in
BenchmarkTest02476.javaWhy this works: SHA-256 is standardized in FIPS PUB 180-4 and recommended by NIST SP 800-107 for general-purpose hashing. It provides a 256-bit digest with no known practical collision attacks. The
MessageDigestAPI contract is identical for both algorithms, so all downstream operations remain functionally equivalent.Vulnerabilities Addressed
CWE-328
Use Of Md5
How we validated it
MessageDigest.getInstance("SHA-256")is available in all supported Java versions (Java 8+)md.update(),md.digest(), and base64 encoding remains unchangedHow to verify
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02476.javaMessageDigest md = MessageDigest.getInstance("SHA-256");mvn test -Dtest=BenchmarkTest02476passwordFile.txtis still generated with valid SHA-256 digestsRunnable Verification Script (click to expand)
Save this script and run with
bash verify_fix.sh:Before you merge
Learn more
This fix was generated by AppSecAI. Please review before merging.