2929import java .io .File ;
3030import java .net .MalformedURLException ;
3131import java .net .URL ;
32+ import java .nio .file .Files ;
33+ import java .nio .file .Path ;
3234
35+ import jenkins .model .Jenkins ;
36+ import org .htmlunit .html .HtmlPage ;
3337import org .junit .Rule ;
3438import org .junit .Test ;
3539import org .junit .rules .TemporaryFolder ;
3640
41+ import static org .hamcrest .Matchers .containsString ;
42+ import static org .hamcrest .Matchers .emptyString ;
3743import static org .junit .Assert .*;
38- import org .jvnet .hudson .test .Issue ;
44+
45+ import org .jvnet .hudson .test .*;
3946
4047public class ClasspathEntryTest {
4148 @ Rule public TemporaryFolder rule = new TemporaryFolder ();
42-
49+ @ Rule public JenkinsRule jr = new JenkinsRule ();
50+
51+ @ Issue ("SECURITY-3447" )
52+ @ Test
53+ public void testDoCheckPath () throws Exception {
54+ jr .jenkins .setSecurityRealm (jr .createDummySecurityRealm ());
55+ jr .jenkins .setAuthorizationStrategy (new MockAuthorizationStrategy ().
56+ grant (Jenkins .ADMINISTER ).everywhere ().to ("admin" )
57+ .grant (Jenkins .READ ).everywhere ().to ("dev" ));
58+ Path path = Files .createTempDirectory ("temp dir" );
59+ try (JenkinsRule .WebClient webClient = jr .createWebClient ()) {
60+ webClient .login ("admin" );
61+ final HtmlPage adminPage = webClient .goTo ("descriptor/org.jenkinsci.plugins.scriptsecurity.scripts.ClasspathEntry/checkPath?value=" + path .toUri ());
62+ final String adminContent = adminPage .asXml ();
63+ assertThat (adminContent , containsString ("Class directories are not allowed as classpath entries." ));
64+ }
65+ try (JenkinsRule .WebClient devWebClient = jr .createWebClient ()) {
66+ devWebClient .login ("dev" );
67+ final HtmlPage devPage = devWebClient .goTo ("descriptor/org.jenkinsci.plugins.scriptsecurity.scripts.ClasspathEntry/checkPath?value=" + path .toUri ());
68+ final String devContent = devPage .asNormalizedText ();
69+ assertThat (devContent , emptyString ());
70+ }
71+ Files .deleteIfExists (path );
72+
73+ }
74+
75+ @ WithoutJenkins
4376 @ Test public void pathURLConversion () throws Exception {
4477 if (!Functions .isWindows ()) {
4578 assertRoundTrip ("/tmp/x.jar" , "file:/tmp/x.jar" );
@@ -54,6 +87,7 @@ private static void assertRoundTrip(String path, String url) throws Exception {
5487 assertEquals (url , ClasspathEntry .pathToURL (path ).toString ());
5588 }
5689
90+ @ WithoutJenkins
5791 @ Test public void classDirDetected () throws Exception {
5892 final File tmpDir = rule .newFolder ();
5993 assertTrue ("Existing directory must be detected" , ClasspathEntry .isClassDirectoryURL (tmpDir .toURI ().toURL ()));
@@ -67,6 +101,7 @@ private static void assertRoundTrip(String path, String url) throws Exception {
67101 assertFalse ("Generic URLs ending in / are not considered class directories" , ClasspathEntry .isClassDirectoryURL (new URL ("http://example.com/file" )));
68102 }
69103
104+ @ WithoutJenkins
70105 @ Issue ("JENKINS-37599" )
71106 @ Test public void pathToURL () throws Exception {
72107 ClasspathEntry ignore = new ClasspathEntry ("http://nowhere.net/" );
0 commit comments