Security: Fix CWE-328 (Weak Hash Algorithm) vulnerability in src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02311.java:63#658
Open
appsecai-app[bot] wants to merge 1 commit intomainfrom
Conversation
Replace MD5 with SHA-256 for cryptographic hash computation. MD5 is broken and unsuitable for security purposes; SHA-256 is NIST-approved and collision-resistant. No API changes required. Fixes: 1 CWE-328 vulnerability
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/BenchmarkTest02311.java:63Description: MD5 hash algorithm is instantiated via
MessageDigest.getInstance("MD5")at line 63. MD5 is cryptographically broken and unsuitable for further use. Collision attacks can be computed in seconds on commodity hardware, making it unsuitable for any integrity or security purpose.Why this matters
Risk if not fixed: MD5 collisions are practical and well-documented. An attacker can forge hash values or precompute rainbow tables to reverse hashes. In this codebase, user-controlled HTTP request parameters flow directly into the MD5 hash computation and the result is written to
passwordFile.txt, indicating a security-sensitive context (credential or integrity verification). Without collision resistance, the stored hashes provide no meaningful security guarantee.Risk level: Medium — Should be addressed in regular security maintenance. This becomes Critical if the hash is used for password storage without salt or key-stretching (which is the case here).
Why we're changing it
Root cause: MD5 is explicitly instantiated and used to hash user-supplied data with no salt, iteration, or key-stretching applied. The hash is stored in a file named
passwordFile.txt, confirming a security-sensitive use case.Triage reasoning:
java.security.MessageDigest.getInstance("MD5")— explicit MD5 instantiation with no ambiguityparamvariablebar = doSomething(request, param)— trivial substring transformation preserves attacker controlinputParamderived frombar, fed directly tomd.update(input)md.digest()result written topasswordFile.txt— security-sensitive storage contextThis is a confirmed vulnerability: MD5 of user-supplied data stored in a password file with no salt or key-stretching constitutes a credential storage weakness. Offline dictionary and rainbow table attacks against the stored hashes are trivially feasible.
How we confirmed
Manual verification steps
BenchmarkTest02311.javaMessageDigest.getInstance("MD5")is presentdoSomething()→md.update()→ file writepasswordFile.txt(lines 81–90)MessageDigest.getInstance("SHA-256")is in placeupdate(),digest(), base64 encoding, file write) remain unchangedVulnerability 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"] --> B["doSomething() extracts param"] B --> C{"Vulnerable: MD5 Hash"} C -->|"MessageDigest.getInstance('MD5')"|D["md.update input + md.digest"] D --> E["❌ MD5 hash stored to passwordFile.txt"] E --> F["❌ Collision attacks feasible"] E --> G["❌ Rainbow table attacks feasible"] H["HTTP Request Parameter"] --> I["doSomething() extracts param"] I --> J{"Fixed: SHA-256 Hash"} J -->|"MessageDigest.getInstance('SHA-256')"|K["md.update input + md.digest"] K --> L["✅ SHA-256 hash stored to passwordFile.txt"] L --> M["✅ Collision resistant"] L --> N["✅ NIST-approved algorithm"] style C fill:#FFE5E5,stroke:#F65A5A style E fill:#FEF3C7,stroke:#F59E0B style F fill:#FEF3C7,stroke:#F59E0B style G fill:#FEF3C7,stroke:#F59E0B style J fill:#DCFCE7,stroke:#16A34A style L fill:#DCFCE7,stroke:#16A34A style M fill:#DCFCE7,stroke:#16A34A style N fill:#DCFCE7,stroke:#16A34AVulnerable flow: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02311.java:63
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 - MD5 Hash"] direction LR A1["HTTP Request Param"] --> A2["doSomething() extracts param"] A2 --> A3["MessageDigest.getInstance MD5"] A3 --> A4["md.update input + md.digest"] A4 --> A5["💥 MD5 hash stored to file"] A5 --> A6["💥 Collision Attack Possible"] end Vulnerable ~~~ Fixed subgraph Fixed["✅ Fixed Flow - SHA-256 Hash"] direction LR B1["HTTP Request Param"] --> B2["doSomething() extracts param"] B2 --> B3["MessageDigest.getInstance SHA-256"] B3 --> B4["md.update input + md.digest"] B4 --> B5["🛡️ SHA-256 hash stored to file"] B5 --> B6["🛡️ Collision Resistant"] end style A3 fill:#FFE5E5,color:#1A1A2E style A5 fill:#ffa94d,color:#000 style A6 fill:#ffa94d,color:#000 style B3 fill:#74c0fc,color:#000 style B5 fill:#DCFCE7,color:#000 style B6 fill:#DCFCE7,color:#000How we fixed it
Fix Summary
The
MessageDigest.getInstance("MD5")call at line 63 is replaced withMessageDigest.getInstance("SHA-256").MD5 is cryptographically broken: collision attacks can be computed in seconds on commodity hardware, making it unsuitable for any integrity or security purpose (CWE-328, NIST SP 800-131A Rev. 2 deprecation). SHA-256 is a NIST-approved, collision-resistant algorithm that slots directly into the same
MessageDigestAPI —update(),digest(), base64 encoding, and file-write logic are entirely unchanged. The output size changes from 16 bytes to 32 bytes, but the downstream code only encodes and stores the raw bytes, so no further changes are needed.Code change:
Vulnerabilities Addressed
CWE-328
Use Of Md5
How we validated it
MessageDigestAPI for SHA-256 is identical to MD5:update(),digest(), and encoding operations work without modificationHow to verify
Manual verification
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02311.javaMessageDigest md = MessageDigest.getInstance("SHA-256");mvn clean compilemvn testRunnable Verification Script (click to expand)
Save this script and run with
bash verify_fix.sh:Before you merge
passwordFile.txtcan handle 32-byte hashes (verify if applicable)Learn more
This fix was generated by AppSecAI. Please review before merging.