-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[JENKINS-76263] Add Content-Security-Policy header
#11269
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
065c007
[JENKINS-76263] Add Content-Security-Policy header
daniel-beck 2a9c11b
Remove obsolete headers from everywhere
daniel-beck a95436d
Make this configurable, and more
daniel-beck 0acd2fc
Fix system property to set header name on UI
daniel-beck 72cae10
Address PR feedback, fix form validation related to RRURL
daniel-beck d9653d7
Add UI tests
daniel-beck a2a544b
Merge commit '8f1bf5be69203c8d45d9ec4acbb0a7ef35ab8830' into JENKINS-…
daniel-beck 4ed38f4
Various minor improvements and changes
daniel-beck ecd7424
Fix assertions
daniel-beck 1877ed6
Specify `base-uri 'none'` by default, add support for non-fetch direc…
daniel-beck 33f12b9
Minor adjustments
daniel-beck 9d5b567
Add test for ReportingAction
daniel-beck b890166
Fix Spotless
daniel-beck c68bb5e
Add test coverage for CspBuilder#NONE_DIRECTIVES
daniel-beck 4c8564f
Permission checks and require POST
daniel-beck 3f4f8ed
Change button label
daniel-beck b2b7927
Also set CSP for filters implementing custom responses
daniel-beck 698b9ab
Make loggers private
daniel-beck b8c9e77
Add test coverage for detecting changed CSP header
daniel-beck d571801
Add `AvatarContributor` utility class for use by plugins
daniel-beck 5f1418c
Fix misplaced log message
daniel-beck 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
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
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
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
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
48 changes: 48 additions & 0 deletions
48
core/src/main/java/jenkins/security/csp/BaseContributor.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,48 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2025, CloudBees, Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
|
|
||
| package jenkins.security.csp; | ||
|
|
||
| import static jenkins.security.csp.Directive.REPORT_SAMPLE; | ||
| import static jenkins.security.csp.Directive.SELF; | ||
|
|
||
| import hudson.Extension; | ||
| import org.kohsuke.accmod.Restricted; | ||
| import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
|
||
| /** | ||
| * Add a minimal set of CSP directives not expected to be removed later. | ||
| */ | ||
| @Extension(ordinal = Integer.MAX_VALUE) | ||
| @Restricted(NoExternalUse.class) | ||
| public final class BaseContributor implements Contributor { | ||
| @Override | ||
| public void apply(CspBuilder cspBuilder) { | ||
| cspBuilder | ||
| .initialize(FetchDirective.DEFAULT_SRC, SELF) | ||
| .initialize(FetchDirective.STYLE_SRC, SELF, REPORT_SAMPLE) | ||
| .initialize(FetchDirective.IMG_SRC, SELF) | ||
| .initialize(FetchDirective.SCRIPT_SRC, SELF, REPORT_SAMPLE); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
core/src/main/java/jenkins/security/csp/CompatibleContributor.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,53 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2025, CloudBees, Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
|
|
||
| package jenkins.security.csp; | ||
|
|
||
| import static jenkins.security.csp.Directive.DATA; | ||
| import static jenkins.security.csp.Directive.IMG_SRC; | ||
| import static jenkins.security.csp.Directive.SCRIPT_SRC; | ||
| import static jenkins.security.csp.Directive.STYLE_SRC; | ||
| import static jenkins.security.csp.Directive.UNSAFE_INLINE; | ||
|
|
||
| import hudson.Extension; | ||
| import org.kohsuke.accmod.Restricted; | ||
| import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
|
||
| /** | ||
| * Add the CSP directives currently required for Jenkins to work properly. | ||
| * The long-term goal is for these directives to become unnecessary. | ||
| * Any for which we determine with high confidence they can never be fixed | ||
| * should be moved into {@link jenkins.security.csp.BaseContributor} instead. | ||
| */ | ||
| @Extension(ordinal = Integer.MAX_VALUE / 2.0) | ||
| @Restricted(NoExternalUse.class) | ||
| public final class CompatibleContributor implements Contributor { | ||
| @Override | ||
| public void apply(CspBuilder cspBuilder) { | ||
| cspBuilder | ||
| .add(STYLE_SRC, UNSAFE_INLINE) // Infeasible for now given inline styles in SVGs | ||
| .add(IMG_SRC, DATA) // TODO https://github.com/jenkinsci/csp-plugin/issues/60 | ||
| .add(SCRIPT_SRC, "usage.jenkins.io"); // TODO https://issues.jenkins.io/browse/JENKINS-76268 | ||
| } | ||
| } |
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,50 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2025, CloudBees, Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
|
|
||
| package jenkins.security.csp; | ||
|
|
||
| import hudson.ExtensionList; | ||
| import hudson.ExtensionPoint; | ||
| import org.kohsuke.accmod.Restricted; | ||
| import org.kohsuke.accmod.restrictions.Beta; | ||
|
|
||
| /** | ||
| * Contribute to the Content-Security-Policy rules. | ||
| */ | ||
| @Restricted(Beta.class) | ||
| public interface Contributor extends ExtensionPoint { | ||
|
|
||
| /** | ||
| * Contribute to the builder's rules by adding to or | ||
| * removing from the provided {@link jenkins.security.csp.CspBuilder}. | ||
| * | ||
| * @param cspBuilder the builder | ||
| */ | ||
| default void apply(CspBuilder cspBuilder) { | ||
| } | ||
|
|
||
| static ExtensionList<Contributor> all() { | ||
| return ExtensionList.lookup(Contributor.class); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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.
nit: Otherwise the class seems very generic