Skip to content

Commit b92d178

Browse files
authored
[JENKINS-73468] Fix mistaken doCheckServerUrl AccessDeniedException (#864)
[JENKINS-73468] View Configuration shows AccessDeniedException on BitbucketScmSource#doCheckServerUrl Fix mistaken doCheckServerUrl AccessDeniedException
1 parent 1f346df commit b92d178

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,8 +1341,11 @@ public FormValidation doCheckCredentialsId(@CheckForNull @AncestorInPath SCMSour
13411341

13421342
@SuppressWarnings("unused") // used By stapler
13431343
public static FormValidation doCheckServerUrl(@AncestorInPath SCMSourceOwner context, @QueryParameter String value) {
1344-
AccessControlled contextToCheck = context == null ? Jenkins.get() : context;
1345-
contextToCheck.checkPermission(Item.CONFIGURE);
1344+
if (context == null && !Jenkins.get().hasPermission(Jenkins.MANAGE)
1345+
|| context != null && !context.hasPermission(Item.EXTENDED_READ)) {
1346+
return FormValidation.error(
1347+
"Unauthorized to validate Server URL"); // not supposed to be seeing this form
1348+
}
13461349
if (BitbucketEndpointConfiguration.get().findEndpoint(value) == null) {
13471350
return FormValidation.error("Unregistered Server: " + value);
13481351
}

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/Security2033Test.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import hudson.model.User;
1010
import hudson.security.ACL;
1111
import hudson.security.ACLContext;
12+
import hudson.util.FormValidation;
1213
import hudson.util.ListBoxModel;
1314
import java.io.IOException;
1415
import java.net.HttpURLConnection;
@@ -35,6 +36,7 @@ public class Security2033Test {
3536

3637
private static final String PROJECT_NAME = "p";
3738
private static final String NOT_AUTHORIZED_USER = "userNoPermission";
39+
private static final String NO_ITEM_READ_USER = "userNoReadPermission";
3840
private static final String SERVER_URL = "server.url";
3941

4042
@Rule
@@ -95,6 +97,17 @@ public void doCheckCredentialsIdSCMSourceWhenUserWithoutCredentialsViewPermissio
9597
}
9698
}
9799

100+
@Issue("SECURITY-2033")
101+
@Test
102+
public void doCheckServerUrlWhenUserWithoutPermissionThenReturnForbiddenMessage() {
103+
((MockAuthorizationStrategy) j.jenkins.getAuthorizationStrategy())
104+
.grant(Jenkins.READ, Item.READ).everywhere().to(NOT_AUTHORIZED_USER);
105+
try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NO_ITEM_READ_USER))) {
106+
FormValidation formValidation = BitbucketSCMSource.DescriptorImpl.doCheckServerUrl(pr, SERVER_URL);
107+
assertThat(formValidation.getMessage(), is("Unauthorized to validate Server URL"));
108+
}
109+
}
110+
98111
@Issue("SECURITY-2033")
99112
@Test
100113
public void doFillServerUrlItemsSCMNavigatorWhenUserWithoutPermissionThenReturnEmptyList() {
@@ -115,17 +128,6 @@ public void doFillServerUrlItemsSCMSourceWhenUserWithoutPermissionThenReturnEmpt
115128
}
116129
}
117130

118-
@Issue("SECURITY-2033")
119-
@Test
120-
public void doCheckServerUrlWhenUserWithoutPermissionThenReturnForbiddenStatus() {
121-
try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NOT_AUTHORIZED_USER))) {
122-
BitbucketSCMSource.DescriptorImpl.doCheckServerUrl(pr, SERVER_URL);
123-
fail("Should fail with AccessDeniedException2");
124-
} catch (Exception accessDeniedException2) {
125-
assertThat(accessDeniedException2.getMessage(), is(NOT_AUTHORIZED_USER + " is missing the Job/Configure permission"));
126-
}
127-
}
128-
129131
@Issue("SECURITY-2033")
130132
@Test
131133
public void doShowStatsWhenUserWithoutAdminPermissionThenReturnForbiddenStatus() {

0 commit comments

Comments
 (0)