|
| 1 | +/** |
| 2 | + * @id java/visible-for-testing-abuse |
| 3 | + * @name Use of VisibleForTesting in production code |
| 4 | + * @description Accessing methods, fields or classes annotated with `@VisibleForTesting` from |
| 5 | + * production code goes against the intention of the annotation and may indicate |
| 6 | + * programmer error. |
| 7 | + * @kind problem |
| 8 | + * @precision high |
| 9 | + * @problem.severity warning |
| 10 | + * @tags quality |
| 11 | + * maintainability |
| 12 | + * readability |
| 13 | + */ |
| 14 | + |
| 15 | +import java |
| 16 | + |
| 17 | +/** |
| 18 | + * Holds if a `Callable` is within the same type hierarchy as `RefType` |
| 19 | + * (including through lambdas, inner classes, and outer classes) |
| 20 | + */ |
| 21 | +predicate isWithinType(Callable c, RefType t) { |
| 22 | + // Either the callable is in the target type, or they share a common enclosing type |
| 23 | + c.getDeclaringType().getEnclosingType*() = t.getEnclosingType*() |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Holds if `e` is within the same package as `t` |
| 28 | + */ |
| 29 | +predicate isWithinPackage(Expr e, RefType t) { |
| 30 | + e.getCompilationUnit().getPackage() = t.getPackage() |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Holds if a callable or any of its enclosing callables is annotated with @VisibleForTesting |
| 35 | + */ |
| 36 | +predicate isWithinVisibleForTestingContext(Callable c) { |
| 37 | + c.getAnAnnotation().getType().hasName("VisibleForTesting") |
| 38 | + or |
| 39 | + isWithinVisibleForTestingContext(c.getEnclosingCallable()) |
| 40 | +} |
| 41 | + |
| 42 | +from Annotatable annotated, Expr e |
| 43 | +where |
| 44 | + annotated.getAnAnnotation().getType().hasName("VisibleForTesting") and |
| 45 | + ( |
| 46 | + // field access |
| 47 | + e = |
| 48 | + any(FieldAccess v | |
| 49 | + v.getField() = annotated and |
| 50 | + // depending on the visibility of the field, using the annotation to abuse the visibility may/may not be occurring |
| 51 | + ( |
| 52 | + // if its package protected report when its used outside its class because it should have been private (class only permitted) |
| 53 | + v.getField().isPackageProtected() and |
| 54 | + not isWithinType(v.getEnclosingCallable(), v.getField().getDeclaringType()) |
| 55 | + or |
| 56 | + // if public or protected report when its used outside its package because package protected should have been enough (package only permitted) |
| 57 | + (v.getField().isPublic() or v.getField().isProtected()) and |
| 58 | + not isWithinPackage(v, v.getField().getDeclaringType()) |
| 59 | + ) |
| 60 | + ) |
| 61 | + or |
| 62 | + // method access |
| 63 | + e = |
| 64 | + any(MethodCall c | |
| 65 | + c.getMethod() = annotated and |
| 66 | + // depending on the visibility of the method, using the annotation to abuse the visibility may/may not be occurring |
| 67 | + ( |
| 68 | + // if its package protected report when its used outside its class because it should have been private (class only permitted) |
| 69 | + c.getMethod().isPackageProtected() and |
| 70 | + not isWithinType(c.getEnclosingCallable(), c.getMethod().getDeclaringType()) |
| 71 | + or |
| 72 | + // if public or protected report when its used outside its package because package protected should have been enough (package only permitted) |
| 73 | + (c.getMethod().isPublic() or c.getMethod().isProtected()) and |
| 74 | + not isWithinPackage(c, c.getMethod().getDeclaringType()) |
| 75 | + ) |
| 76 | + ) |
| 77 | + or |
| 78 | + // Class instantiation - report if used outside appropriate scope |
| 79 | + e = |
| 80 | + any(ClassInstanceExpr c | |
| 81 | + c.getConstructedType() = annotated and |
| 82 | + ( |
| 83 | + c.getConstructedType().isPublic() and not isWithinPackage(c, c.getConstructedType()) |
| 84 | + or |
| 85 | + c.getConstructedType().hasNoModifier() and |
| 86 | + c.getConstructedType() instanceof NestedClass and |
| 87 | + not isWithinType(c.getEnclosingCallable(), c.getConstructedType()) |
| 88 | + ) |
| 89 | + ) |
| 90 | + ) and |
| 91 | + // not in a test where use is appropriate |
| 92 | + not e.getEnclosingCallable() instanceof LikelyTestMethod and |
| 93 | + // not when the accessing method or any enclosing method is @VisibleForTesting (test-to-test communication) |
| 94 | + not isWithinVisibleForTestingContext(e.getEnclosingCallable()) and |
| 95 | + // not when used in annotation contexts |
| 96 | + not e.getParent*() instanceof Annotation |
| 97 | +select e, "Access of $@ annotated with VisibleForTesting found in production code.", annotated, |
| 98 | + "element" |
0 commit comments