Add Java heuristic insecure randomness query - #222
Conversation
Add a lower-noise CWE-338 query that reports weak randomness only when surrounding names suggest a security-sensitive value. Include query help, focused positive and negative tests, and bump the Java query pack for the new query. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f3ad7ef1-aadf-4de0-a149-74e15bea1bf8
There was a problem hiding this comment.
Pull request overview
Adds a new heuristic Java query to detect cryptographically weak randomness only in security-sensitive naming contexts, reducing noise compared to broad weak-PRNG checks. This integrates the query into the Community Packs layout with documentation, tests, and a pack version bump.
Changes:
- Added
security/CWE-338/HeuristicInsecureRandomness.qlimplementing a name-context heuristic on top ofRandomDataSource/InsecureRandomnessSource(excludingSecureRandom). - Added query help (
.qhelp) and an example Java snippet for documentation rendering. - Added focused CodeQL tests (QL ref, Java test file, expected results) and bumped
githubsecuritylab/codeql-java-queriesversion to0.8.0.
Show a summary per file
| File | Description |
|---|---|
| java/src/security/CWE-338/HeuristicInsecureRandomness.ql | New heuristic query detecting weak PRNG usage when surrounding identifiers imply security-sensitive output. |
| java/src/security/CWE-338/HeuristicInsecureRandomness.qhelp | Query help text with rationale, recommendations, and references. |
| java/src/security/CWE-338/HeuristicInsecureRandomness.java | Documentation/example snippet used by qhelp rendering. |
| java/test/security/CWE-338/HeuristicInsecureRandomness.qlref | Test harness reference pointing at the new query. |
| java/test/security/CWE-338/HeuristicInsecureRandomness.java | Positive/negative test cases covering method/variable/qualifier/Math.random() and SecureRandom exclusions. |
| java/test/security/CWE-338/HeuristicInsecureRandomness.expected | Expected findings verifying the intended matches. |
| java/src/qlpack.yml | Version bump of githubsecuritylab/codeql-java-queries from 0.7.5 to 0.8.0. |
Review details
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 7/7 changed files
- Comments generated: 0
- Review effort level: Lite
|
One or more custom setup steps configured for this repository failed during this Copilot code review run: Setup steps run before each review. If the review above is missing context, or no review was posted at all, the failing step above may be the cause. See the workflow run for failure details, fix your setup steps configuration, and re-request a review. Note You can configure setup steps for Copilot code review separately from Copilot cloud agent with a |
|
CI is currently blocked by repository-wide CodeQL CLI/package drift rather than this query. The failed job installs pinned CLI The repository already tracks the required CLI upgrade in #219. The prior upgrade workflow run failed on whitespace in its input, and #220 fixes that workflow validation. Once the repository-wide 2.26.3 dependency-refresh PR lands, this PR's checks should be rerun. I have not added a partial CLI/library bump here because CONTRIBUTING.md requires those pins and lockfiles to be updated together across all packs. |
Known issue with 2.26.3 packs being used with any other CLI version, synced this repo to use the latest cli and just reran your jobs! |
Summary
Adds a heuristic Java CWE-338 query that reports cryptographically weak randomness only when surrounding identifier names suggest a security-sensitive value, such as a token, secret, OTP, nonce, API key, session identifier, or authorization code.
This ports and adapts the query from dsp-testing/codeql-development-template#24 to the Community Packs layout and conventions.
Changes
githubsecuritylab/java/heuristic-insecure-randomnessunderjava/src/security/CWE-338RandomDataSourceandInsecureRandomnessSourceabstractions soSecureRandomremains excludedMath.random()matches, plusSecureRandomand non-sensitive negativesgithubsecuritylab/codeql-java-queriesfrom0.7.5to0.8.0The existing broader
WeakPRNG.qlquery is unchanged; this query provides a lower-noise, security-context heuristic.Real-project result
The query was executed against a CodeQL database for
hoanghuu1207/Electronic_Newsat commit90ffd7abfe84c56b2e39b07572ffb8f46c3af5c4and reported the weakRandom.nextInt(...)used bygenerateToken:https://github.com/hoanghuu1207/Electronic_News/blob/90ffd7abfe84c56b2e39b07572ffb8f46c3af5c4/src/helper/GenerateToken.java#L6-L14
Validation