|
| 1 | +/** |
| 2 | + * @id java/visible-for-testing-abuse |
| 3 | + * @name Accessing any method, field or class annotated with `@VisibleForTesting` from production code is discouraged |
| 4 | + * @description Accessing any method, field or class 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 maintainability |
| 11 | + * readability |
| 12 | + */ |
| 13 | + |
| 14 | +import java |
| 15 | + |
| 16 | +/** |
| 17 | + * A `Callable` is within some `RefType` |
| 18 | + */ |
| 19 | +predicate isWithinType(Callable c, RefType t) { c.getDeclaringType() = t } |
| 20 | + |
| 21 | +/** |
| 22 | + * A `Callable` is within same package as the `RefType` |
| 23 | + */ |
| 24 | +predicate isWithinPackage(Callable c, RefType t) { |
| 25 | + c.getDeclaringType().getPackage() = t.getPackage() |
| 26 | +} |
| 27 | + |
| 28 | +predicate withinStaticContext(NestedClass c) { |
| 29 | + c.isStatic() or |
| 30 | + c.(AnonymousClass).getClassInstanceExpr().getEnclosingCallable().isStatic() // JLS 15.9.2 |
| 31 | +} |
| 32 | + |
| 33 | +RefType enclosingInstanceType(Class inner) { |
| 34 | + not withinStaticContext(inner) and |
| 35 | + result = inner.(NestedClass).getEnclosingType() |
| 36 | +} |
| 37 | + |
| 38 | +class OuterClass extends Class { |
| 39 | + OuterClass() { this = enclosingInstanceType+(_) } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * An innerclass is accessed outside of its outerclass |
| 44 | + * and also outside of its fellow inner parallel classes |
| 45 | + */ |
| 46 | +predicate isWithinDirectOuterClassOrSiblingInner( |
| 47 | + Callable classInstanceEnclosing, RefType typeBeingConstructed |
| 48 | +) { |
| 49 | + exists(NestedClass inner, OuterClass outer | |
| 50 | + outer = enclosingInstanceType(inner) and |
| 51 | + typeBeingConstructed = inner and |
| 52 | + // where the inner is called from the outer class |
| 53 | + classInstanceEnclosing.getDeclaringType() = outer |
| 54 | + ) |
| 55 | + or |
| 56 | + // and inner is called from the a parallel inner |
| 57 | + exists(NestedClass inner, OuterClass outer, NestedClass otherinner | |
| 58 | + typeBeingConstructed = inner and |
| 59 | + outer = enclosingInstanceType(otherinner) and |
| 60 | + outer = enclosingInstanceType(inner) and |
| 61 | + classInstanceEnclosing.getDeclaringType() = otherinner |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +from Annotatable annotated, Annotation annotation, Expr e |
| 66 | +where |
| 67 | + annotation.getType().hasName("VisibleForTesting") and |
| 68 | + annotated.getAnAnnotation() = annotation and |
| 69 | + ( |
| 70 | + // field access |
| 71 | + exists(FieldAccess v | |
| 72 | + v = e and |
| 73 | + v.getField() = annotated and |
| 74 | + // depending on the visiblity of the field, using the annotation to abuse the visibility may/may not be occurring |
| 75 | + ( |
| 76 | + // if its package protected report when its used outside its class bc it should have been private (class only permitted) |
| 77 | + v.getField().isPackageProtected() and |
| 78 | + not isWithinType(v.getEnclosingCallable(), v.getField().getDeclaringType()) |
| 79 | + or |
| 80 | + // if public or protected report when its used outside its package because package protected should have been enough (package only permitted) |
| 81 | + (v.getField().isPublic() or v.getField().isProtected()) and |
| 82 | + not isWithinPackage(v.getEnclosingCallable(), v.getField().getDeclaringType()) |
| 83 | + ) |
| 84 | + ) |
| 85 | + or |
| 86 | + // class instantiation |
| 87 | + exists(ClassInstanceExpr c | |
| 88 | + c = e and |
| 89 | + c.getConstructedType() = annotated and |
| 90 | + // depending on the visiblity of the class, using the annotation to abuse the visibility may/may not be occurring |
| 91 | + // if public report when its used outside its package because package protected should have been enough (package only permitted) |
| 92 | + ( |
| 93 | + c.getConstructedType().isPublic() and |
| 94 | + not isWithinPackage(c.getEnclosingCallable(), c.getConstructedType()) |
| 95 | + or |
| 96 | + // if its package protected report when its used outside its outer class bc it should have been private (outer class only permitted) |
| 97 | + c.getConstructedType().hasNoModifier() and |
| 98 | + // and the class is an innerclass, because otherwise recommending a lower accessibility makes no sense (only inner classes can be private) |
| 99 | + exists(enclosingInstanceType(c.getConstructedType())) and |
| 100 | + not isWithinDirectOuterClassOrSiblingInner(c.getEnclosingCallable(), c.getConstructedType()) |
| 101 | + ) |
| 102 | + ) |
| 103 | + or |
| 104 | + // method access |
| 105 | + exists(MethodCall c | |
| 106 | + c = e and |
| 107 | + c.getMethod() = annotated and |
| 108 | + // depending on the visiblity of the method, using the annotation to abuse the visibility may/may not be occurring |
| 109 | + ( |
| 110 | + // if its package protected report when its used outside its class bc it should have been private (class only permitted) |
| 111 | + c.getMethod().isPackageProtected() and |
| 112 | + not isWithinType(c.getEnclosingCallable(), c.getMethod().getDeclaringType()) |
| 113 | + or |
| 114 | + // if public or protected report when its used outside its package because package protected should have been enough (package only permitted) |
| 115 | + (c.getMethod().isPublic() or c.getMethod().isProtected()) and |
| 116 | + not isWithinPackage(c.getEnclosingCallable(), c.getMethod().getDeclaringType()) |
| 117 | + ) |
| 118 | + ) |
| 119 | + ) and |
| 120 | + // not in a test where use is appropriate |
| 121 | + not e.getEnclosingCallable() instanceof LikelyTestMethod and |
| 122 | + // also omit our own ql unit test where it is acceptable |
| 123 | + not e.getEnclosingCallable() |
| 124 | + .getFile() |
| 125 | + .getAbsolutePath() |
| 126 | + .matches("%java/ql/test/query-tests/%Test.java") |
| 127 | +select e, "Access of $@ annotated with VisibleForTesting found in production code.", annotated, |
| 128 | + "element" |
0 commit comments