3030import io .github .classgraph .ClassInfo ;
3131import io .github .classgraph .ScanResult ;
3232import jenkins .model .Jenkins ;
33- import jenkins .security .stapler .TypedFilter ;
3433import junit .framework .TestCase ;
3534import junit .framework .TestSuite ;
3635import org .apache .commons .lang3 .StringUtils ;
4847import java .util .ArrayList ;
4948import java .util .List ;
5049import java .util .Map ;
50+ import java .util .logging .Logger ;
5151
5252import static org .hamcrest .CoreMatchers .is ;
5353import static org .hamcrest .Matchers .empty ;
@@ -129,8 +129,10 @@ public void testPluginActive() {
129129 public void testStaplerDispatches () throws InvocationTargetException , IllegalAccessException , NoSuchMethodException {
130130 List <String > methodsFound = new ArrayList <>();
131131
132- Method isStaplerRoutableMethod = TypedFilter .class .getDeclaredMethod ("isRoutableMethod" , Method .class );
133- isStaplerRoutableMethod .setAccessible (true );
132+ Method isStaplerRoutableMethod = findIsRoutableMethod ();
133+ if (isStaplerRoutableMethod == null ) {
134+ return ;
135+ }
134136
135137 PluginWrapper thisPlugin = determineCurrentPlugin ();
136138 if (thisPlugin == null ) {
@@ -152,7 +154,18 @@ public void testStaplerDispatches() throws InvocationTargetException, IllegalAcc
152154 }
153155 }
154156 }
155- Assert .assertThat ("Web methods lack verb annotations like @RequirePOST, @GET, @POST, etc." , methodsFound , is (empty ()));
157+ Assert .assertThat ("There should be no web methods that lack HTTP verb annotations like @RequirePOST, @GET, @POST, etc. -- see https://jenkins.io/redirect/developer/csrf-protection" , methodsFound , is (empty ()));
158+ }
159+
160+ private Method findIsRoutableMethod () throws NoSuchMethodException {
161+ try {
162+ Method method = Class .forName ("jenkins.security.stapler.TypedFilter" ).getDeclaredMethod ("isRoutableMethod" , Method .class );
163+ method .setAccessible (true );
164+ return method ;
165+ } catch (ClassNotFoundException e ) {
166+ LOGGER .warning ("This test requires Jenkins 2.154, Jenkins LTS 2.138.4, or newer to run, use e.g. -Djenkins.version=2.138.4" );
167+ return null ;
168+ }
156169 }
157170
158171 private PluginWrapper determineCurrentPlugin () {
@@ -193,5 +206,7 @@ private boolean isStaplerDispatchable(Method m) {
193206 }
194207 return true ;
195208 }
209+
210+ private static Logger LOGGER = Logger .getLogger (OtherTests .class .getName ());
196211 }
197212}
0 commit comments