Skip to content

Commit bdc94d3

Browse files
committed
[SECURITY-1527] Protect GerritServer.DescriptorImpl.testConnection
1 parent 8ee32b6 commit bdc94d3

File tree

3 files changed

+146
-22
lines changed

3 files changed

+146
-22
lines changed

Diff for: pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<java.level>8</java.level>
5858
<no-test-jar>false</no-test-jar>
5959
<findbugs.failOnError>false</findbugs.failOnError>
60-
<concurrency>1C</concurrency>
60+
<concurrency>0.5C</concurrency>
6161
<powermock.version>1.6.2</powermock.version>
6262
<checkstyle.version>2.13</checkstyle.version>
6363
<mockito.version>1.10.19</mockito.version>

Diff for: src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/GerritServer.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -711,27 +711,15 @@ public String getDisplayName() {
711711
* @return {@link FormValidation#ok() } if can be done,
712712
* {@link FormValidation#error(java.lang.String) } otherwise.
713713
*/
714+
@RequirePOST
714715
public FormValidation doTestConnection(
715716
@QueryParameter("gerritHostName") final String gerritHostName,
716717
@QueryParameter("gerritSshPort") final int gerritSshPort,
717718
@QueryParameter("gerritProxy") final String gerritProxy,
718719
@QueryParameter("gerritUserName") final String gerritUserName,
719720
@QueryParameter("gerritAuthKeyFile") final String gerritAuthKeyFile,
720721
@QueryParameter("gerritAuthKeyFilePassword") final String gerritAuthKeyFilePassword) {
721-
if (logger.isDebugEnabled()) {
722-
logger.debug("gerritHostName = {}\n"
723-
+ "gerritSshPort = {}\n"
724-
+ "gerritProxy = {}\n"
725-
+ "gerritUserName = {}\n"
726-
+ "gerritAuthKeyFile = {}\n"
727-
+ "gerritAuthKeyFilePassword = {}",
728-
new Object[]{gerritHostName,
729-
gerritSshPort,
730-
gerritProxy,
731-
gerritUserName,
732-
gerritAuthKeyFile,
733-
gerritAuthKeyFilePassword, });
734-
}
722+
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
735723

736724
File file = new File(gerritAuthKeyFile);
737725
String password = null;
@@ -790,11 +778,12 @@ public Integer call() throws Exception {
790778
* @param gerritHttpPassword the password
791779
* @return {@link FormValidation#ok()} if it works.
792780
*/
781+
@RequirePOST
793782
public FormValidation doTestRestConnection(
794783
@QueryParameter("gerritFrontEndUrl") final String gerritFrontEndUrl,
795784
@QueryParameter("gerritHttpUserName") final String gerritHttpUserName,
796785
@QueryParameter("gerritHttpPassword") final String gerritHttpPassword) {
797-
786+
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
798787
String password = Secret.fromString(gerritHttpPassword).getPlainText();
799788

800789
String restUrl = gerritFrontEndUrl;

Diff for: src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/LockedDownGerritEventTest.java

+141-6
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void testUserCanConfigureAJob() throws Exception {
206206
assertEquals("theOtherServer", serverName);
207207
}
208208

209-
// CS IGNORE MagicNumber FOR NEXT 32 LINES. REASON: test data.
209+
// CS IGNORE MagicNumber FOR NEXT 200 LINES. REASON: test data.
210210

211211
/**
212212
* Tests that only an admin can read server configuration and manipulate server state.
@@ -223,11 +223,7 @@ public void testOnlyAdminCanPerformServerConfigurationActions() throws Exception
223223
gerritServer.start();
224224

225225
Setup.lockDown(j);
226-
j.getInstance().setAuthorizationStrategy(
227-
new MockAuthorizationStrategy().grant(Item.READ, Item.DISCOVER).everywhere().toAuthenticated()
228-
.grant(Jenkins.READ, Item.DISCOVER).everywhere().toEveryone()
229-
.grant(Item.CONFIGURE).everywhere().to("bob")
230-
.grant(Jenkins.ADMINISTER).everywhere().to("alice"));
226+
grantsToAliceBobAndEveryone();
231227
j.jenkins.setCrumbIssuer(null); //Not really testing csrf right now
232228
JenkinsRule.WebClient webClient = j.createWebClient().login("alice", "alice");
233229
HtmlPage page = webClient.goTo("plugin/gerrit-trigger/servers/0/");
@@ -240,6 +236,145 @@ public void testOnlyAdminCanPerformServerConfigurationActions() throws Exception
240236
post(webClient, "plugin/gerrit-trigger/servers/0/wakeup", null, 403);
241237
}
242238

239+
/**
240+
* Tests that you can't do an http GET request to
241+
* ${@link GerritServer.DescriptorImpl#doTestConnection(String, int, String, String, String, String)}.
242+
*
243+
* @throws Exception if so
244+
*/
245+
@Test @Issue("SECURITY-1527")
246+
public void testGetTestConnectionNotWorking() throws Exception {
247+
GerritServer gerritServer = new GerritServer(PluginImpl.DEFAULT_SERVER_NAME);
248+
SshdServerMock.configureFor(sshd, gerritServer);
249+
PluginImpl.getInstance().addServer(gerritServer);
250+
gerritServer.getConfig().setNumberOfSendingWorkerThreads(NUMBEROFSENDERTHREADS);
251+
((Config)gerritServer.getConfig()).setGerritAuthKeyFile(sshKey.getPrivateKey());
252+
gerritServer.start();
253+
254+
Setup.lockDown(j);
255+
grantsToAliceBobAndEveryone();
256+
j.jenkins.setCrumbIssuer(null); //Not really testing csrf right now
257+
JenkinsRule.WebClient webClient = j.createWebClient(); //No login this time
258+
259+
webClient.assertFails("descriptorByName/"
260+
+ "com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer/"
261+
+ "testConnection?"
262+
+ "gerritHostName=foo&"
263+
+ "gerritSshPort=29418&"
264+
+ "gerritProxy=&"
265+
+ "gerritUserName=foo"
266+
+ "gerritAuthKeyFile=/tmp/foo"
267+
+ "gerritAuthKeyFilePassword=bar"
268+
+ "&foo=",
269+
405);
270+
271+
webClient.assertFails("descriptorByName/"
272+
+ "com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer/"
273+
+ "testRestConnection?"
274+
+ "gerritHttpUserName=foo&"
275+
+ "gerritHttpPassword=bar&"
276+
+ "gerritFrontEndUrl=http://localhost:8000/?foo=",
277+
405);
278+
279+
}
280+
281+
/**
282+
* Tests that you can do an http POST request to
283+
* ${@link GerritServer.DescriptorImpl#doTestConnection(String, int, String, String, String, String)}.
284+
*
285+
* @throws Exception if so
286+
*/
287+
@Test @Issue("SECURITY-1527")
288+
public void testPostTestConnectionNotWorking() throws Exception {
289+
GerritServer gerritServer = new GerritServer(PluginImpl.DEFAULT_SERVER_NAME);
290+
SshdServerMock.configureFor(sshd, gerritServer);
291+
PluginImpl.getInstance().addServer(gerritServer);
292+
gerritServer.getConfig().setNumberOfSendingWorkerThreads(NUMBEROFSENDERTHREADS);
293+
((Config)gerritServer.getConfig()).setGerritAuthKeyFile(sshKey.getPrivateKey());
294+
gerritServer.start();
295+
296+
Setup.lockDown(j);
297+
grantsToAliceBobAndEveryone();
298+
j.jenkins.setCrumbIssuer(null); //Not really testing csrf right now
299+
JenkinsRule.WebClient webClient = j.createWebClient(); //No login this time
300+
301+
post(webClient, "descriptorByName/"
302+
+ "com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer/"
303+
+ "testConnection?"
304+
+ "gerritHostName=foo&"
305+
+ "gerritSshPort=29418&"
306+
+ "gerritProxy=&"
307+
+ "gerritUserName=foo"
308+
+ "gerritAuthKeyFile=/tmp/foo"
309+
+ "gerritAuthKeyFilePassword=bar",
310+
null, 403);
311+
312+
post(webClient, "descriptorByName/"
313+
+ "com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer/"
314+
+ "testRestConnection?"
315+
+ "gerritHttpUserName=foo&"
316+
+ "gerritHttpPassword=bar&"
317+
+ "gerritFrontEndUrl=http://localhost:8000/?foo=",
318+
null, 403);
319+
320+
}
321+
322+
/**
323+
* Tests that you can do an http POST request to
324+
* ${@link GerritServer.DescriptorImpl#doTestConnection(String, int, String, String, String, String)}.
325+
*
326+
* @throws Exception if so
327+
*/
328+
@Test @Issue("SECURITY-1527")
329+
public void testPostTestConnectionWorking() throws Exception {
330+
GerritServer gerritServer = new GerritServer(PluginImpl.DEFAULT_SERVER_NAME);
331+
SshdServerMock.configureFor(sshd, gerritServer);
332+
PluginImpl.getInstance().addServer(gerritServer);
333+
gerritServer.getConfig().setNumberOfSendingWorkerThreads(NUMBEROFSENDERTHREADS);
334+
((Config)gerritServer.getConfig()).setGerritAuthKeyFile(sshKey.getPrivateKey());
335+
gerritServer.start();
336+
337+
Setup.lockDown(j);
338+
grantsToAliceBobAndEveryone();
339+
j.jenkins.setCrumbIssuer(null); //Not really testing csrf right now
340+
JenkinsRule.WebClient webClient = j.createWebClient().login("alice");
341+
342+
post(webClient, "descriptorByName/"
343+
+ "com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer/"
344+
+ "testConnection?"
345+
+ "gerritHostName=foo&"
346+
+ "gerritSshPort=29418&"
347+
+ "gerritProxy=&"
348+
+ "gerritUserName=foo&"
349+
+ "gerritAuthKeyFile=/tmp/foo&"
350+
+ "gerritAuthKeyFilePassword=bar",
351+
null, null);
352+
353+
post(webClient, "descriptorByName/"
354+
+ "com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer/"
355+
+ "testRestConnection?"
356+
+ "gerritHttpUserName=foo&"
357+
+ "gerritHttpPassword=bar&"
358+
+ "gerritFrontEndUrl=http://localhost:8000/",
359+
null, null);
360+
361+
}
362+
363+
/**
364+
* Grants some permissions.
365+
*
366+
* Read and discover to everyone and authenticated.
367+
* Configure everywhere to bob.
368+
* Administer everywhere to alice.
369+
*/
370+
private void grantsToAliceBobAndEveryone() {
371+
j.getInstance().setAuthorizationStrategy(
372+
new MockAuthorizationStrategy().grant(Item.READ, Item.DISCOVER).everywhere().toAuthenticated()
373+
.grant(Jenkins.READ, Item.DISCOVER).everywhere().toEveryone()
374+
.grant(Item.CONFIGURE).everywhere().to("bob")
375+
.grant(Jenkins.ADMINISTER).everywhere().to("alice"));
376+
}
377+
243378
/**
244379
* Performs an HTTP POST request to the relative url.
245380
*

0 commit comments

Comments
 (0)