Skip to content

Commit bf32018

Browse files
daniel-beckKevin-CB
authored andcommitted
Co-authored-by: Kevin-CB <kguerroudj@cloudbees.com>
1 parent dddb459 commit bf32018

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

core/src/main/java/hudson/model/ComputerSet.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ public synchronized void doCreateItem(StaplerRequest2 req, StaplerResponse2 rsp,
291291
}
292292
}
293293

294+
src.checkPermission(Computer.EXTENDED_READ);
295+
294296
// copy through XStream
295297
String xml = Jenkins.XSTREAM.toXML(src);
296298
Node result = (Node) Jenkins.XSTREAM.fromXML(xml);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package jenkins.security;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.containsString;
5+
import static org.junit.Assert.assertEquals;
6+
7+
import hudson.model.Computer;
8+
import hudson.slaves.DumbSlave;
9+
import java.net.URL;
10+
import jenkins.model.Jenkins;
11+
import org.htmlunit.HttpMethod;
12+
import org.htmlunit.WebRequest;
13+
import org.htmlunit.WebResponse;
14+
import org.junit.Rule;
15+
import org.junit.Test;
16+
import org.jvnet.hudson.test.Issue;
17+
import org.jvnet.hudson.test.JenkinsRule;
18+
import org.jvnet.hudson.test.MockAuthorizationStrategy;
19+
20+
public class Security3512Test {
21+
22+
@Rule
23+
public JenkinsRule j = new JenkinsRule();
24+
25+
@Test
26+
@Issue("SECURITY-3512")
27+
public void copyAgentTest() throws Exception {
28+
Computer.EXTENDED_READ.setEnabled(true);
29+
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
30+
MockAuthorizationStrategy mockAuthorizationStrategy = new MockAuthorizationStrategy();
31+
mockAuthorizationStrategy.grant(Jenkins.READ, Computer.CREATE, Computer.EXTENDED_READ).everywhere().to("alice");
32+
mockAuthorizationStrategy.grant(Jenkins.READ, Computer.CREATE).everywhere().to("bob");
33+
j.jenkins.setAuthorizationStrategy(mockAuthorizationStrategy);
34+
35+
DumbSlave agent = j.createOnlineSlave();
36+
37+
assertEquals(2, j.getInstance().getComputers().length);
38+
39+
String agentCopyURL = j.getURL() + "/computer/createItem?mode=copy&from=" + agent.getNodeName() + "&name=";
40+
41+
{ // with ExtendedRead permission you can copy a node
42+
try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false).login("alice")) {
43+
WebResponse rsp = wc.getPage(wc.addCrumb(new WebRequest(new URL(agentCopyURL + "aliceAgent"),
44+
HttpMethod.POST))).getWebResponse();
45+
46+
assertEquals(200, rsp.getStatusCode());
47+
assertEquals(3, j.getInstance().getComputers().length);
48+
}
49+
}
50+
51+
{ // without ExtendedRead permission you cannot copy a node
52+
try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false).login("bob")) {
53+
WebResponse rsp = wc.getPage(wc.addCrumb(new WebRequest(new URL(agentCopyURL + "bobAgent"),
54+
HttpMethod.POST))).getWebResponse();
55+
56+
assertEquals(403, rsp.getStatusCode());
57+
assertThat(rsp.getContentAsString(), containsString("bob is missing the Agent/ExtendedRead permission"));
58+
assertEquals(3, j.getInstance().getComputers().length);
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)