|
| 1 | +package com.cloudbees.jenkins.plugins.bitbucket; |
| 2 | + |
| 3 | +import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint; |
| 4 | +import com.cloudbees.plugins.credentials.CredentialsScope; |
| 5 | +import com.cloudbees.plugins.credentials.SystemCredentialsProvider; |
| 6 | +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; |
| 7 | +import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; |
| 8 | +import com.gargoylesoftware.htmlunit.Page; |
| 9 | +import hudson.model.Item; |
| 10 | +import hudson.model.User; |
| 11 | +import hudson.security.ACL; |
| 12 | +import hudson.security.ACLContext; |
| 13 | +import hudson.util.ListBoxModel; |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.HttpURLConnection; |
| 16 | +import jenkins.model.Jenkins; |
| 17 | +import org.hamcrest.CoreMatchers; |
| 18 | +import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.jvnet.hudson.test.Issue; |
| 23 | +import org.jvnet.hudson.test.JenkinsRule; |
| 24 | +import org.jvnet.hudson.test.MockAuthorizationStrategy; |
| 25 | + |
| 26 | +import static org.hamcrest.CoreMatchers.not; |
| 27 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | +import static org.hamcrest.Matchers.containsString; |
| 29 | +import static org.hamcrest.Matchers.empty; |
| 30 | +import static org.hamcrest.Matchers.hasSize; |
| 31 | +import static org.hamcrest.Matchers.is; |
| 32 | +import static org.junit.Assert.fail; |
| 33 | + |
| 34 | +public class Security2033Test { |
| 35 | + |
| 36 | + private static final String PROJECT_NAME = "p"; |
| 37 | + private static final String NOT_AUTHORIZED_USER = "userNoPermission"; |
| 38 | + private static final String SERVER_URL = "server.url"; |
| 39 | + |
| 40 | + @Rule |
| 41 | + public JenkinsRule j = new JenkinsRule(); |
| 42 | + |
| 43 | + private WorkflowMultiBranchProject pr; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setup() throws Exception { |
| 47 | + pr = j.jenkins.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME); |
| 48 | + setUpAuthorization(); |
| 49 | + initCredentials(); |
| 50 | + } |
| 51 | + |
| 52 | + @Issue("SECURITY-2033") |
| 53 | + @Test |
| 54 | + public void doFillCredentialsIdItemsSCMSourceWhenUserWithoutCredentialsViewPermissionThenListNotPopulated() { |
| 55 | + BitbucketSCMSource.DescriptorImpl descriptor = (BitbucketSCMSource.DescriptorImpl) Jenkins.get().getDescriptorOrDie(BitbucketSCMSource.class); |
| 56 | + try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NOT_AUTHORIZED_USER))) { |
| 57 | + ListBoxModel actual = descriptor.doFillCredentialsIdItems(pr, SERVER_URL); |
| 58 | + ListBoxModel expected = new ListBoxModel(new ListBoxModel.Option("- none -", "")); |
| 59 | + assertListBoxModel(actual, expected); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Issue("SECURITY-2033") |
| 64 | + @Test |
| 65 | + public void doFillCredentialsIdItemsSCMNavigatorWhenUserWithoutCredentialsViewPermissionThenListNotPopulated() { |
| 66 | + BitbucketSCMNavigator.DescriptorImpl descriptor = (BitbucketSCMNavigator.DescriptorImpl) Jenkins.get().getDescriptorOrDie(BitbucketSCMNavigator.class); |
| 67 | + try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NOT_AUTHORIZED_USER))) { |
| 68 | + ListBoxModel actual = descriptor.doFillCredentialsIdItems(pr, SERVER_URL); |
| 69 | + ListBoxModel expected = new ListBoxModel(new ListBoxModel.Option("- none -", "")); |
| 70 | + assertListBoxModel(actual, expected); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Issue("SECURITY-2033") |
| 75 | + @Test |
| 76 | + public void doCheckCredentialsIdSCMNavigatorWhenUserWithoutCredentialsViewPermissionThenReturnForbiddenStatus() { |
| 77 | + BitbucketSCMNavigator.DescriptorImpl descriptor = (BitbucketSCMNavigator.DescriptorImpl) Jenkins.get().getDescriptorOrDie(BitbucketSCMNavigator.class); |
| 78 | + try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NOT_AUTHORIZED_USER))) { |
| 79 | + descriptor.doCheckCredentialsId(pr, SERVER_URL, "nonEmpty"); |
| 80 | + fail("Should fail with AccessDeniedException2"); |
| 81 | + } catch (Exception accessDeniedException2) { |
| 82 | + assertThat(accessDeniedException2.getMessage(), is(NOT_AUTHORIZED_USER + " is missing the Credentials/View permission")); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Issue("SECURITY-2033") |
| 87 | + @Test |
| 88 | + public void doCheckCredentialsIdSCMSourceWhenUserWithoutCredentialsViewPermissionThenReturnForbiddenStatus() { |
| 89 | + BitbucketSCMSource.DescriptorImpl descriptor = (BitbucketSCMSource.DescriptorImpl) Jenkins.get().getDescriptorOrDie(BitbucketSCMSource.class); |
| 90 | + try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NOT_AUTHORIZED_USER))) { |
| 91 | + descriptor.doCheckCredentialsId(pr, SERVER_URL, "nonEmpty"); |
| 92 | + fail("Should fail with AccessDeniedException2 but not"); |
| 93 | + } catch (Exception accessDeniedException2) { |
| 94 | + assertThat(accessDeniedException2.getMessage(), is(NOT_AUTHORIZED_USER + " is missing the Credentials/View permission")); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Issue("SECURITY-2033") |
| 99 | + @Test |
| 100 | + public void doFillServerUrlItemsSCMNavigatorWhenUserWithoutPermissionThenReturnEmptyList() { |
| 101 | + BitbucketSCMNavigator.DescriptorImpl descriptor = (BitbucketSCMNavigator.DescriptorImpl) Jenkins.get().getDescriptorOrDie(BitbucketSCMNavigator.class); |
| 102 | + try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NOT_AUTHORIZED_USER))) { |
| 103 | + ListBoxModel actual = descriptor.doFillServerUrlItems(pr); |
| 104 | + assertThat(actual, is(empty())); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Issue("SECURITY-2033") |
| 109 | + @Test |
| 110 | + public void doFillServerUrlItemsSCMSourceWhenUserWithoutPermissionThenReturnEmptyList() { |
| 111 | + BitbucketSCMSource.DescriptorImpl descriptor = (BitbucketSCMSource.DescriptorImpl) Jenkins.get().getDescriptorOrDie(BitbucketSCMSource.class); |
| 112 | + try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NOT_AUTHORIZED_USER))) { |
| 113 | + ListBoxModel actual = descriptor.doFillServerUrlItems(pr); |
| 114 | + assertThat(actual, is(empty())); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 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 | + |
| 129 | + @Issue("SECURITY-2033") |
| 130 | + @Test |
| 131 | + public void doShowStatsWhenUserWithoutAdminPermissionThenReturnForbiddenStatus() { |
| 132 | + BitbucketCloudEndpoint.DescriptorImpl descriptor = (BitbucketCloudEndpoint.DescriptorImpl) Jenkins.get().getDescriptorOrDie(BitbucketCloudEndpoint.class); |
| 133 | + try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NOT_AUTHORIZED_USER))) { |
| 134 | + descriptor.doShowStats(); |
| 135 | + fail("Should fail with AccessDeniedException2"); |
| 136 | + } catch (Exception accessDeniedException2) { |
| 137 | + assertThat(accessDeniedException2.getMessage(), is(NOT_AUTHORIZED_USER + " is missing the Overall/Administer permission")); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Issue("SECURITY-2033") |
| 142 | + @Test |
| 143 | + public void doClearWhenUserWithoutAdminPermissionThenReturnForbiddenStatus() { |
| 144 | + BitbucketCloudEndpoint.DescriptorImpl descriptor = (BitbucketCloudEndpoint.DescriptorImpl) Jenkins.get().getDescriptorOrDie(BitbucketCloudEndpoint.class); |
| 145 | + try (ACLContext aclContext = ACL.as(User.getOrCreateByIdOrFullName(NOT_AUTHORIZED_USER))) { |
| 146 | + descriptor.doClear(); |
| 147 | + fail("Should fail with AccessDeniedException2"); |
| 148 | + } catch (Exception accessDeniedException2) { |
| 149 | + assertThat(accessDeniedException2.getMessage(), is(NOT_AUTHORIZED_USER + " is missing the Overall/Administer permission")); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Issue("SECURITY-2033") |
| 154 | + @Test |
| 155 | + public void doClearWhenInvokedUsingGetMethodThenResourceNotFound() throws Exception { |
| 156 | + JenkinsRule.WebClient webClient = j .createWebClient().withThrowExceptionOnFailingStatusCode(false); |
| 157 | + webClient.login(NOT_AUTHORIZED_USER); |
| 158 | + Page page = webClient.goTo("job/" + PROJECT_NAME +"/descriptorByName/com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint/clear"); |
| 159 | + |
| 160 | + assertThat(page.getWebResponse().getStatusCode(), is(HttpURLConnection.HTTP_NOT_FOUND)); |
| 161 | + assertThat(page.getWebResponse().getContentAsString(), containsString("Stapler processed this HTTP request as follows, but couldn't find the resource to consume the request")); |
| 162 | + } |
| 163 | + |
| 164 | + private void initCredentials() throws IOException { |
| 165 | + StandardUsernamePasswordCredentials key = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "id", "desc", "username", "pass"); |
| 166 | + SystemCredentialsProvider.getInstance().getCredentials().add(key); |
| 167 | + |
| 168 | + SystemCredentialsProvider.getInstance().save(); |
| 169 | + } |
| 170 | + |
| 171 | + private void setUpAuthorization() { |
| 172 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 173 | + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy() |
| 174 | + .grant(Jenkins.READ, Item.READ).everywhere().to(NOT_AUTHORIZED_USER)); |
| 175 | + } |
| 176 | + |
| 177 | + private static void assertListBoxModel(ListBoxModel actual, ListBoxModel expected) { |
| 178 | + assertThat(actual, CoreMatchers.is(not(empty()))); |
| 179 | + assertThat(actual, hasSize(expected.size())); |
| 180 | + assertThat(actual.get(0).name, CoreMatchers.is(expected.get(0).name)); |
| 181 | + assertThat(actual.get(0).value, CoreMatchers.is(expected.get(0).value)); |
| 182 | + } |
| 183 | +} |
0 commit comments