-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Java: Added new query java/visible-for-testing-abuse
#20178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Fixed
Show fixed
Hide fixed
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Fixed
Show fixed
Hide fixed
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Fixed
Show fixed
Hide fixed
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Fixed
Show fixed
Hide fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
b2ba0d7
to
8708130
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat new query.
I have added some comments, will continue the review tomorrow.
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
…sibleForTestingAbuse alerts
…VisibleForTestingAbuse.ql Co-authored-by: Copilot <[email protected]>
…VisibleForTestingAbuse.ql Co-authored-by: Michael Nebel <[email protected]>
bb37c26
to
4705ad2
Compare
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql
Outdated
Show resolved
Hide resolved
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also generalise this to support lambdas (to mirror the above).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
…t tests from the `java/visible-for-testing-abuse` query.
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.