-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a128337
commit b19b34d
Showing
6 changed files
with
235 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
test/src/test/java/hudson/cli/CreateNodeCommandSEC2021Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2013 Red Hat, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package hudson.cli; | ||
|
||
import hudson.model.Computer; | ||
import hudson.model.Messages; | ||
import jenkins.model.Jenkins; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import static hudson.cli.CLICommandInvoker.Matcher.failedWith; | ||
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
//TODO to be merged into base test after security release | ||
public class CreateNodeCommandSEC2021Test { | ||
|
||
private CLICommandInvoker command; | ||
|
||
@Rule public final JenkinsRule j = new JenkinsRule(); | ||
|
||
@Before public void setUp() { | ||
|
||
command = new CLICommandInvoker(j, new CreateNodeCommand()); | ||
} | ||
|
||
@Test | ||
@Issue("SECURITY-2021") | ||
public void createNodeShouldFailIfNodeIsNotGood() throws Exception { | ||
int nodeListSizeBefore = j.jenkins.getNodes().size(); | ||
|
||
final CLICommandInvoker.Result result = command | ||
.authorizedTo(Computer.CREATE, Jenkins.READ) | ||
.withStdin(CreateNodeCommandSEC2021Test.class.getResourceAsStream("node_sec2021.xml")) | ||
.invoke() | ||
; | ||
|
||
assertThat(result.stderr(), containsString(Messages.Hudson_UnsafeChar('/'))); | ||
assertThat(result, hasNoStandardOutput()); | ||
assertThat(result, failedWith(1)); | ||
|
||
// ensure not side effects | ||
assertEquals(nodeListSizeBefore, j.jenkins.getNodes().size()); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
test/src/test/java/hudson/cli/UpdateNodeCommandSEC2021Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2013 Red Hat, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package hudson.cli; | ||
|
||
import hudson.model.Computer; | ||
import hudson.model.Messages; | ||
import hudson.model.Slave; | ||
import jenkins.model.Jenkins; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import static hudson.cli.CLICommandInvoker.Matcher.failedWith; | ||
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
//TODO to be merged into base test after security release | ||
public class UpdateNodeCommandSEC2021Test { | ||
|
||
private CLICommandInvoker command; | ||
|
||
@Rule public final JenkinsRule j = new JenkinsRule(); | ||
|
||
@Before public void setUp() { | ||
|
||
command = new CLICommandInvoker(j, new UpdateNodeCommand()); | ||
} | ||
|
||
@Test | ||
@Issue("SECURITY-2021") | ||
public void updateNodeShouldFailForDotDot() throws Exception { | ||
String okName = "MyNode"; | ||
Slave node = j.createSlave(okName, null, null); | ||
// currently <dummy>, but doing so will be a bit more future-proof | ||
String defaultDescription = node.getNodeDescription(); | ||
|
||
final CLICommandInvoker.Result result = command | ||
.authorizedTo(Computer.CONFIGURE, Jenkins.READ) | ||
.withStdin(UpdateNodeCommandSEC2021Test.class.getResourceAsStream("node_sec2021.xml")) | ||
.invokeWithArgs(okName) | ||
; | ||
|
||
assertThat(result.stderr(), containsString(Messages.Hudson_UnsafeChar('/'))); | ||
assertThat(result, hasNoStandardOutput()); | ||
assertThat(result, failedWith(1)); | ||
|
||
assertEquals(okName, node.getNodeName()); | ||
// ensure the other data were not saved | ||
assertEquals(defaultDescription, node.getNodeDescription()); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
test/src/test/java/hudson/model/ComputerConfigDotXmlSEC2021Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package hudson.model; | ||
|
||
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; | ||
import com.gargoylesoftware.htmlunit.HttpMethod; | ||
import com.gargoylesoftware.htmlunit.WebRequest; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.junit.Assert.fail; | ||
|
||
public class ComputerConfigDotXmlSEC2021Test { | ||
|
||
@Rule public final JenkinsRule rule = new JenkinsRule(); | ||
|
||
@Issue("SECURITY-2021") | ||
@Test | ||
public void nodeNameReferencesParentDir() throws Exception { | ||
Computer computer = rule.createSlave("anything", null).toComputer(); | ||
|
||
JenkinsRule.WebClient wc = rule.createWebClient(); | ||
WebRequest req = new WebRequest(wc.createCrumbedUrl(String.format("%s/config.xml", computer.getUrl())), HttpMethod.POST); | ||
req.setAdditionalHeader("Content-Type", "application/xml"); | ||
req.setRequestBody(VALID_XML_BAD_NAME_XML); | ||
|
||
try { | ||
wc.getPage(req); | ||
fail("Should have returned failure."); | ||
} catch (FailingHttpStatusCodeException e) { | ||
assertThat(e.getStatusCode(), equalTo(400)); | ||
} | ||
File configDotXml = new File(rule.jenkins.getRootDir(), "config.xml"); | ||
String configDotXmlContents = new String(Files.readAllBytes(configDotXml.toPath()), StandardCharsets.UTF_8); | ||
|
||
assertThat(configDotXmlContents, not(containsString("<name>../</name>"))); | ||
} | ||
|
||
private static final String VALID_XML_BAD_NAME_XML = | ||
"<slave>\n" + | ||
" <name>../</name>\n" + | ||
"</slave>"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<slave> | ||
<name>../</name> | ||
<description>Payload from XML</description> | ||
<remoteFS>/user/hudson/workspace</remoteFS> | ||
<numExecutors>42</numExecutors> | ||
<mode>NORMAL</mode> | ||
<retentionStrategy class="hudson.slaves.RetentionStrategy$1"> | ||
<DESCRIPTOR> | ||
<outer-class reference="../.."/> | ||
</DESCRIPTOR> | ||
</retentionStrategy> | ||
<launcher class="hudson.slaves.JNLPLauncher"/> | ||
<label></label> | ||
<nodeProperties/> | ||
<userId>SYSTEM</userId> | ||
</slave> |