-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Daniel Beck <daniel-beck@users.noreply.github.com>
- Loading branch information
1 parent
f5d9842
commit f1056bd
Showing
5 changed files
with
459 additions
and
2 deletions.
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
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,81 @@ | ||
package hudson.model; | ||
|
||
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; | ||
import com.gargoylesoftware.htmlunit.HttpMethod; | ||
import com.gargoylesoftware.htmlunit.WebRequest; | ||
import hudson.ExtensionList; | ||
import hudson.diagnosis.OldDataMonitor; | ||
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 org.jvnet.hudson.test.MockAuthorizationStrategy; | ||
|
||
import java.util.Map; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
|
||
public class ComputerSEC1923Test { | ||
|
||
private static final String CONFIGURATOR = "configure_user"; | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Before | ||
public void setupSecurity() { | ||
j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); | ||
MockAuthorizationStrategy mas = new MockAuthorizationStrategy(); | ||
mas.grant(Computer.CONFIGURE, Computer.EXTENDED_READ, Jenkins.READ) | ||
.everywhere() | ||
.to(CONFIGURATOR); | ||
j.jenkins.setAuthorizationStrategy(mas); | ||
} | ||
|
||
@Issue("SECURITY-1923") | ||
@Test | ||
public void configDotXmlWithValidXmlAndBadField() throws Exception { | ||
Computer computer = j.createSlave().toComputer(); | ||
|
||
JenkinsRule.WebClient wc = j.createWebClient(); | ||
wc.login(CONFIGURATOR); | ||
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_FIELD_USER_XML); | ||
|
||
try { | ||
wc.getPage(req); | ||
fail("Should have returned failure."); | ||
} catch (FailingHttpStatusCodeException e) { | ||
// This really shouldn't return 500, but that's what it does now. | ||
assertThat(e.getStatusCode(), equalTo(500)); | ||
} | ||
|
||
OldDataMonitor odm = ExtensionList.lookupSingleton(OldDataMonitor.class); | ||
Map<Saveable, OldDataMonitor.VersionRange> data = odm.getData(); | ||
|
||
assertThat(data.size(), equalTo(0)); | ||
|
||
odm.doDiscard(null, null); | ||
|
||
User.AllUsers.scanAll(); | ||
boolean createUser = false; | ||
User badUser = User.getById("foo", createUser); | ||
|
||
assertNull("Should not have created user.", badUser); | ||
} | ||
|
||
private static final String VALID_XML_BAD_FIELD_USER_XML = | ||
"<hudson.model.User>\n" + | ||
" <id>foo</id>\n" + | ||
" <fullName>Foo User</fullName>\n" + | ||
" <badField/>\n" + | ||
"</hudson.model.User>\n"; | ||
|
||
|
||
} |
80 changes: 80 additions & 0 deletions
80
test/src/test/java/hudson/model/FreeStyleProjectSEC1923Test.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,80 @@ | ||
package hudson.model; | ||
|
||
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; | ||
import com.gargoylesoftware.htmlunit.HttpMethod; | ||
import com.gargoylesoftware.htmlunit.WebRequest; | ||
import hudson.ExtensionList; | ||
import hudson.diagnosis.OldDataMonitor; | ||
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 org.jvnet.hudson.test.MockAuthorizationStrategy; | ||
|
||
import java.util.Map; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
|
||
public class FreeStyleProjectSEC1923Test { | ||
|
||
private static final String CONFIGURATOR = "configure_user"; | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Before | ||
public void setupSecurity() { | ||
j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); | ||
MockAuthorizationStrategy mas = new MockAuthorizationStrategy(); | ||
mas.grant(Item.CONFIGURE, Item.READ, Jenkins.READ) | ||
.everywhere() | ||
.to(CONFIGURATOR); | ||
j.jenkins.setAuthorizationStrategy(mas); | ||
} | ||
|
||
@Issue("SECURITY-1923") | ||
@Test | ||
public void configDotXmlWithValidXmlAndBadField() throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject(); | ||
|
||
JenkinsRule.WebClient wc = j.createWebClient(); | ||
wc.login(CONFIGURATOR); | ||
WebRequest req = new WebRequest(wc.createCrumbedUrl(String.format("%s/config.xml", project.getUrl())), HttpMethod.POST); | ||
req.setAdditionalHeader("Content-Type", "application/xml"); | ||
req.setRequestBody(VALID_XML_BAD_FIELD_USER_XML); | ||
|
||
try { | ||
wc.getPage(req); | ||
fail("Should have returned failure."); | ||
} catch (FailingHttpStatusCodeException e) { | ||
// This really shouldn't return 500, but that's what it does now. | ||
assertThat(e.getStatusCode(), equalTo(500)); | ||
} | ||
|
||
OldDataMonitor odm = ExtensionList.lookupSingleton(OldDataMonitor.class); | ||
Map<Saveable, OldDataMonitor.VersionRange> data = odm.getData(); | ||
|
||
assertThat(data.size(), equalTo(0)); | ||
|
||
odm.doDiscard(null, null); | ||
|
||
User.AllUsers.scanAll(); | ||
boolean createUser = false; | ||
User badUser = User.getById("foo", createUser); | ||
|
||
assertNull("Should not have created user.", badUser); | ||
} | ||
|
||
private static final String VALID_XML_BAD_FIELD_USER_XML = | ||
"<hudson.model.User>\n" + | ||
" <id>foo</id>\n" + | ||
" <fullName>Foo User</fullName>\n" + | ||
" <badField/>\n" + | ||
"</hudson.model.User>\n"; | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
test/src/test/java/hudson/model/ItemGroupMixInSEC1923Test.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,81 @@ | ||
package hudson.model; | ||
|
||
|
||
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; | ||
import com.gargoylesoftware.htmlunit.HttpMethod; | ||
import com.gargoylesoftware.htmlunit.WebRequest; | ||
import hudson.ExtensionList; | ||
import hudson.diagnosis.OldDataMonitor; | ||
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 org.jvnet.hudson.test.MockAuthorizationStrategy; | ||
|
||
import java.net.URL; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
|
||
public class ItemGroupMixInSEC1923Test { | ||
|
||
private static final String CREATOR = "create_user"; | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Before | ||
public void setupSecurity() { | ||
j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); | ||
MockAuthorizationStrategy mas = new MockAuthorizationStrategy(); | ||
mas.grant(Item.CREATE, Item.CONFIGURE, Item.READ, Jenkins.READ) | ||
.everywhere() | ||
.to(CREATOR); | ||
j.jenkins.setAuthorizationStrategy(mas); | ||
} | ||
|
||
@Issue("SECURITY-1923") | ||
@Test | ||
public void doCreateItemWithValidXmlAndBadField() throws Exception { | ||
JenkinsRule.WebClient wc = j.createWebClient(); | ||
wc.login(CREATOR); | ||
WebRequest req = new WebRequest(wc.createCrumbedUrl("createItem?name=testProject"), HttpMethod.POST); | ||
req.setAdditionalHeader("Content-Type", "application/xml"); | ||
req.setRequestBody(VALID_XML_BAD_FIELD_USER_XML); | ||
|
||
try { | ||
wc.getPage(req); | ||
fail("Should have returned failure."); | ||
} catch (FailingHttpStatusCodeException e) { | ||
// This really shouldn't return 500, but that's what it does now. | ||
assertThat(e.getStatusCode(), equalTo(500)); | ||
} | ||
|
||
OldDataMonitor odm = ExtensionList.lookupSingleton(OldDataMonitor.class); | ||
Map<Saveable, OldDataMonitor.VersionRange> data = odm.getData(); | ||
|
||
assertThat(data.size(), equalTo(0)); | ||
|
||
odm.doDiscard(null, null); | ||
|
||
User.AllUsers.scanAll(); | ||
boolean createUser = false; | ||
User badUser = User.getById("foo", createUser); | ||
|
||
assertNull("Should not have created user.", badUser); | ||
} | ||
|
||
private static final String VALID_XML_BAD_FIELD_USER_XML = | ||
"<hudson.model.User>\n" + | ||
" <id>foo</id>\n" + | ||
" <fullName>Foo User</fullName>\n" + | ||
" <badField/>\n" + | ||
"</hudson.model.User>\n"; | ||
} |
Oops, something went wrong.