-
-
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
Open
AmoghParmar
wants to merge
8
commits into
jenkinsci:master
Choose a base branch
from
AmoghParmar:fix-26012-hide-csrf-ui
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−4
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cbcfe18
Hide CSRF configuration UI when only DefaultCrumbIssuer is available
AmoghParmar 230e4ea
Add test for CSRF section visibility
AmoghParmar 25af895
Remove unused import in GlobalCrumbIssuerConfigurationTest
AmoghParmar 8444d1c
Add @TestExtension DummyCrumbIssuer to provide second CrumbIssuer des…
AmoghParmar 25f33cc
Fix CSRF configuration test assertions per review feedback
AmoghParmar 1e48ce0
Fix unused import in CSRF configuration test
AmoghParmar 4e0662d
Fix hidden test assertion to check DummyCrumbIssuer absence
AmoghParmar d46a7f7
Fix CSRF test: use TestCrumbIssuer, assert CSRF section hidden correctly
AmoghParmar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 16 additions & 4 deletions
20
core/src/main/resources/hudson/security/csrf/GlobalCrumbIssuerConfiguration/config.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | ||
| ) | ||
| } | ||
| } | ||
| } |
97 changes: 97 additions & 0 deletions
97
test/src/test/java/hudson/security/csrf/GlobalCrumbIssuerConfigurationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package hudson.security.csrf; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.not; | ||
|
|
||
| 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 csrfSectionHiddenWhenOnlyDefaultIssuerAvailable() throws Exception { | ||
| // No @TestExtension here, so only DefaultCrumbIssuer is registered | ||
| j.jenkins.setCrumbIssuer(new DefaultCrumbIssuer(false)); | ||
|
|
||
| JenkinsRule.WebClient wc = j.createWebClient(); | ||
| HtmlPage page = wc.goTo("configureSecurity"); | ||
| String pageContent = page.asNormalizedText(); | ||
|
|
||
| // When only DefaultCrumbIssuer exists, the CSRF section should be HIDDEN | ||
| assertThat(pageContent, not(containsString("Dummy Crumb Issuer"))); | ||
| } | ||
|
|
||
| @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(pageContent, containsString("Crumb Issuer")); | ||
| } | ||
|
|
||
| @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(pageContent, containsString("This configuration is unavailable because")); | ||
| } finally { | ||
| GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION = original; | ||
| } | ||
| } | ||
|
|
||
| @TestExtension("csrfSectionShownWhenNonDefaultIssuerConfigured") | ||
| 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"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.