Java: Added new query java/visible-for-testing-abuse#20178
Java: Added new query java/visible-for-testing-abuse#20178Napalys merged 22 commits intogithub:mainfrom
java/visible-for-testing-abuse#20178Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a new CodeQL query java/visible-for-testing-abuse to detect misuse of the @VisibleForTesting annotation in production code. The query identifies cases where elements annotated with @VisibleForTesting are accessed from production code outside their intended scope, which violates the annotation's purpose of only increasing visibility for testing.
- Added the main query implementation with logic to detect inappropriate access patterns based on visibility modifiers
- Created comprehensive test cases covering various scenarios including cross-package access, lambda usage, and inner classes
- Integrated the query into the Java code quality query suites
Reviewed Changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql |
Main query implementation detecting misuse of @VisibleForTesting annotation |
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.md |
Documentation explaining the rule's purpose and implementation details |
java/ql/test/query-tests/VisibleForTestingAbuse/*.java |
Comprehensive test cases covering various usage scenarios |
java/ql/test/query-tests/VisibleForTestingAbuse/VisibleForTestingAbuse.qlref |
Test configuration referencing the query |
java/ql/test/query-tests/VisibleForTestingAbuse/VisibleForTestingAbuse.expected |
Expected test results |
java/ql/integration-tests/java/query-suite/*.expected |
Updated query suite integration test expectations |
b2ba0d7 to
8708130
Compare
michaelnebel
left a comment
There was a problem hiding this comment.
Neat new query.
I have added some comments, will continue the review tomorrow.
…sibleForTestingAbuse alerts
…VisibleForTestingAbuse.ql Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…VisibleForTestingAbuse.ql Co-authored-by: Michael Nebel <michaelnebel@github.com>
bb37c26 to
4705ad2
Compare
| // not in a test where use is appropriate | ||
| not e.getEnclosingCallable() instanceof LikelyTestMethod and | ||
| // not when the accessing method or any enclosing method is @VisibleForTesting (test-to-test communication) | ||
| not isWithinVisibleForTestingContext(e.getEnclosingCallable()) and |
There was a problem hiding this comment.
Maybe also generalise this to support lambdas (to mirror the above).
There was a problem hiding this comment.
Is it necessary, I can't seem to figure out which case would not be covered currently? As this is responsible for exclusion of such https://github.com/Napalys/codeql/blob/38f517ecfaaa3eb1d6b18d0969900aeaddfca420/java/ql/test/query-tests/VisibleForTestingAbuse/packageone/SourcePackage1.java#L16
There was a problem hiding this comment.
Oh - right! Then it is already covered!
…t tests from the `java/visible-for-testing-abuse` query.
michaelnebel
left a comment
There was a problem hiding this comment.
Great work!
Maybe run DCA once more.
owen-mc
left a comment
There was a problem hiding this comment.
Looks good. Just a few minor comments.
…VisibleForTestingAbuse.ql Co-authored-by: Owen Mansel-Chan <62447351+owen-mc@users.noreply.github.com>
…VisibleForTestingAbuse.md Co-authored-by: Owen Mansel-Chan <62447351+owen-mc@users.noreply.github.com>
|
(Don't forget to add full stops to the end of all the QLDocs.) |
Added new query
java/visible-for-testing-abuse.Initial MRVA 1000 run produced 5,108 results, after reducing false positives ended up with 2,611.
Autofix tends to remove
@VisibleForTesting, but it may requires deeper refactoring in which case it may not be accepted as a good fix.