Reflection switches off the access checks of a member
What it finds
field.setAccessible(true) switches off the access checks of the language for a private or protected member: the code can then read and change state that its class keeps to itself, break its invariants, and depend on internals that change without notice (CWE-284).
Reported is every setAccessible(true) and every trySetAccessible(); frameworks need them, so the rule is in the strict tier.
From the rule's source, accessibility_override.rs.
import java.lang.reflect.Field; class Peek { Object read(Field field, Object target) throws Exception { field.setAccessible(true);tx0 reports: the access checks of this member are switched off field.setAccessible(false); if (!field.trySetAccessible()) {tx0 reports: the access checks of this member are switched off return null; } return field.get(target); }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:accessibility-override" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:accessibility-override] reason="..."tx0 explain tx0/java:accessibility-override shows the same in the terminal. Found a false positive? Report it.