|
4 | 4 | import static org.junit.Assert.assertTrue; |
5 | 5 | import static org.junit.Assert.fail; |
6 | 6 |
|
| 7 | +import java.net.URL; |
| 8 | +import java.util.Collections; |
| 9 | +import org.htmlunit.HttpMethod; |
| 10 | +import org.htmlunit.WebRequest; |
7 | 11 | import org.htmlunit.html.*; |
8 | 12 | //import org.htmlunit.javascript.host.URL; |
9 | 13 | import hudson.ExtensionList; |
|
18 | 22 | import java.util.List; |
19 | 23 |
|
20 | 24 | import org.apache.commons.fileupload.FileItem; |
| 25 | +import org.htmlunit.util.NameValuePair; |
21 | 26 | import org.jenkinsci.plugins.scriptler.ScriptlerManagementHelper; |
22 | 27 | import org.jenkinsci.plugins.scriptler.ScriptlerManagement; |
23 | 28 | import org.jenkinsci.plugins.scriptler.config.Parameter; |
| 29 | +import org.jenkinsci.plugins.scriptler.config.ScriptlerConfiguration; |
24 | 30 | import org.junit.*; |
25 | 31 | import org.jvnet.hudson.test.BuildWatcher; |
26 | 32 | import org.jvnet.hudson.test.Issue; |
@@ -127,4 +133,29 @@ public void testUnknownScript() throws Exception { |
127 | 133 | JenkinsRule.WebClient webClient = j.createWebClient(); |
128 | 134 | webClient.goTo("scriptler/runScript?id=unknown.groovy"); |
129 | 135 | } |
| 136 | + |
| 137 | + @Test |
| 138 | + @Issue("SECURITY-3205") |
| 139 | + public void fixFolderTraversalThroughDeleteScript() throws Exception { |
| 140 | + File configurationFile = ScriptlerConfiguration.getXmlFile().getFile(); |
| 141 | + String path = "../" + configurationFile.getName(); |
| 142 | + |
| 143 | + try (JenkinsRule.WebClient webClient = j.createWebClient()) { |
| 144 | + URL rootUrl = new URL(webClient.getContextPath() + "scriptler/removeScript"); |
| 145 | + WebRequest req = new WebRequest(rootUrl, HttpMethod.POST); |
| 146 | + req.setRequestParameters(Collections.singletonList(new NameValuePair("id", path))); |
| 147 | + webClient.addCrumb(req); |
| 148 | + webClient.getPage(req); |
| 149 | + fail(); |
| 150 | + } catch (FailingHttpStatusCodeException e) { |
| 151 | + if (e.getStatusCode() != 400) { |
| 152 | + // some other kind of error that we're not checking for |
| 153 | + throw e; |
| 154 | + } |
| 155 | + if (!configurationFile.exists()) { |
| 156 | + fail("The configuration file was deleted"); |
| 157 | + } |
| 158 | + assert(e.getResponse().getContentAsString().contains("Invalid file path received: " + path)); |
| 159 | + } |
| 160 | + } |
130 | 161 | } |
0 commit comments