-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Hide CSRF configuration UI when only DefaultCrumbIssuer is available #26057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
cbcfe18
230e4ea
25af895
8444d1c
25f33cc
1e48ce0
4e0662d
d46a7f7
364e291
d712d9b
163bfd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,31 @@ | ||
| package hudson.security.csrf.GlobalCrumbIssuerConfiguration | ||
|
|
||
| import hudson.security.csrf.CrumbIssuer | ||
| import hudson.security.csrf.DefaultCrumbIssuer | ||
|
|
||
| def f=namespace(lib.FormTagLib) | ||
| def f = namespace(lib.FormTagLib) | ||
| def all = CrumbIssuer.all() | ||
| def disableCsrf = | ||
| hudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION | ||
|
|
||
| if (!all.isEmpty()) { | ||
| def onlyDefaultIssuer = | ||
| all.size() == 1 && all[0].clazz == DefaultCrumbIssuer | ||
|
|
||
| def showCsrfConfig = !onlyDefaultIssuer || disableCsrf | ||
|
|
||
| if (showCsrfConfig) { | ||
| f.section(title: _("CSRF Protection")) { | ||
| if (hudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION) { | ||
| if (disableCsrf) { | ||
| f.entry { | ||
| p(raw(_('disabled'))) | ||
| p(_('unsupported')) | ||
| } | ||
| } else { | ||
| f.dropdownDescriptorSelector(title: _("Crumb Issuer"), descriptors: all, field: 'crumbIssuer') | ||
| f.dropdownDescriptorSelector( | ||
| title: _("Crumb Issuer"), | ||
| descriptors: all, | ||
| field: 'crumbIssuer' | ||
| ) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package hudson.security.csrf; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.containsString; | ||
|
|
||
| import jakarta.servlet.ServletRequest; | ||
| import org.htmlunit.html.HtmlPage; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.jvnet.hudson.test.JenkinsRule; | ||
| import org.jvnet.hudson.test.TestExtension; | ||
| import org.jvnet.hudson.test.junit.jupiter.WithJenkins; | ||
|
|
||
| @WithJenkins | ||
| class GlobalCrumbIssuerConfigurationTest { | ||
|
|
||
| private JenkinsRule j; | ||
|
|
||
| @BeforeEach | ||
| void setUp(JenkinsRule rule) { | ||
| j = rule; | ||
| } | ||
|
|
||
| @Test | ||
| void csrfSectionShownWhenNonDefaultIssuerConfigured() throws Exception { | ||
| // DefaultCrumbIssuer is default, but other CrumbIssuer descriptors exist in test environment | ||
| // so the CSRF section should be visible | ||
| j.jenkins.setCrumbIssuer(new DefaultCrumbIssuer(false)); | ||
|
|
||
| JenkinsRule.WebClient wc = j.createWebClient(); | ||
| HtmlPage page = wc.goTo("configureSecurity"); | ||
| String pageContent = page.asNormalizedText(); | ||
|
|
||
| // With multiple CrumbIssuer descriptors available (from test extensions), | ||
| // the CSRF Protection section should always be shown | ||
| assertThat("CSRF Protection section should be shown when multiple issuers are available", | ||
| pageContent, containsString("CSRF Protection")); | ||
| } | ||
|
|
||
| @Test | ||
| void csrfSectionShownWhenCsrfProtectionDisabled() throws Exception { | ||
| boolean original = GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION; | ||
| try { | ||
| GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION = true; | ||
|
|
||
| JenkinsRule.WebClient wc = j.createWebClient(); | ||
| HtmlPage page = wc.goTo("configureSecurity"); | ||
| String pageContent = page.asNormalizedText(); | ||
|
|
||
| assertThat("CSRF section should be shown when CSRF protection is disabled", | ||
| pageContent, containsString("CSRF Protection")); | ||
| } finally { | ||
| GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION = original; | ||
| } | ||
| } | ||
|
|
||
| @TestExtension | ||
|
||
| public static class DummyCrumbIssuer extends CrumbIssuer { | ||
|
|
||
| @Override | ||
| public String getCrumbRequestField() { | ||
| return "dummy"; | ||
| } | ||
|
|
||
| @Override | ||
| public String issueCrumb(ServletRequest request, String salt) { | ||
| return "dummy-crumb"; | ||
| } | ||
|
|
||
| public static class DescriptorImpl extends CrumbIssuerDescriptor<DummyCrumbIssuer> { | ||
|
|
||
| DescriptorImpl() { | ||
| super( | ||
| DummyCrumbIssuer.class.getName(), | ||
| "Dummy Crumb Issuer" | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Dummy Crumb Issuer"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should specifically assert the placeholder message, see other comment.