-
Notifications
You must be signed in to change notification settings - Fork 7
Remove CSP versions from headers and URLs #7487
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
labkey-adam
wants to merge
2
commits into
release26.3-SNAPSHOT
Choose a base branch
from
26.3_fb_csp_version
base: release26.3-SNAPSHOT
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.
+55
−37
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.apache.commons.collections4.SetValuedMap; | ||
| import org.apache.commons.collections4.multimap.HashSetValuedHashMap; | ||
| import org.apache.commons.lang3.EnumUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.commons.lang3.Strings; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
@@ -19,7 +20,6 @@ | |
| import org.junit.Test; | ||
| import org.labkey.api.admin.AdminUrls; | ||
| import org.labkey.api.collections.CopyOnWriteHashMap; | ||
| import org.labkey.api.collections.LabKeyCollectors; | ||
| import org.labkey.api.security.Directive; | ||
| import org.labkey.api.settings.AppProps; | ||
| import org.labkey.api.settings.OptionalFeatureService; | ||
|
|
@@ -95,6 +95,11 @@ public String getHeaderName() | |
| { | ||
| return _headerName; | ||
| } | ||
|
|
||
| private static @Nullable ContentSecurityPolicyType get(String disposition) | ||
| { | ||
| return EnumUtils.getEnumIgnoreCase(ContentSecurityPolicyType.class, disposition); | ||
| } | ||
| } | ||
|
|
||
| static | ||
|
|
@@ -119,8 +124,9 @@ public void init(FilterConfig filterConfig) throws ServletException | |
| String paramValue = filterConfig.getInitParameter(paramName); | ||
| if ("policy".equalsIgnoreCase(paramName)) | ||
| { | ||
| // Extract before filtering since CSP version is in a comment | ||
| extractCspVersion(paramValue); | ||
| _stashedTemplate = filterPolicy(paramValue); | ||
| extractCspVersion(_stashedTemplate); | ||
| } | ||
| else if ("disposition".equalsIgnoreCase(paramName)) | ||
| { | ||
|
|
@@ -144,7 +150,7 @@ else if ("disposition".equalsIgnoreCase(paramName)) | |
| } | ||
|
|
||
| /** Filter out block comments and replace special characters in the provided policy */ | ||
| public static String filterPolicy(String policy) | ||
| private static String filterPolicy(String policy) | ||
| { | ||
| String s = policy.trim(); | ||
| s = s.replace( '\n', ' ' ); | ||
|
|
@@ -164,40 +170,27 @@ public static String filterPolicy(String policy) | |
| return s; | ||
| } | ||
|
|
||
| private static final String CSP_VERSION = "cspVersion="; | ||
|
|
||
| /** | ||
| * Extract the cspVersion parameter value from the report-uri directive, if possible. Otherwise, cspVersion is left | ||
| * as "Unknown". This value is reported as part of usage metrics. | ||
| * Extract the cspVersion parameter value from a comment in the CSP, if it exists. Otherwise, cspVersion is left as | ||
| * "Unknown". This value is reported as part of usage metrics and sent in reports. | ||
| */ | ||
| private void extractCspVersion(String s) | ||
| { | ||
| // Simple parser that should be compliant with https://www.w3.org/TR/CSP3/#parse-serialized-policy | ||
| Map<String, String> cspMap = Arrays.stream(s.split(";")) | ||
| .map(String::trim) | ||
| .filter(line -> !line.isEmpty()) | ||
| .map(line -> line.split("\\s+", 2)) | ||
| .filter(parts -> parts.length == 2) | ||
| .collect(LabKeyCollectors.toCaseInsensitiveLinkedMap(parts -> parts[0], parts -> parts[1])); | ||
|
|
||
| String directive = "report-uri"; | ||
| String reportUri = cspMap.get(directive); | ||
|
|
||
| if (reportUri != null) | ||
| int idx = s.indexOf(CSP_VERSION); | ||
| if (idx > -1) | ||
| { | ||
| try | ||
| { | ||
| ActionURL reportUrl = new ActionURL(reportUri); | ||
| String cspVersion = reportUrl.getParameter("cspVersion"); | ||
|
|
||
| if (null != cspVersion) | ||
| _cspVersion = cspVersion; | ||
| } | ||
| catch (IllegalArgumentException e) | ||
| int start = idx + CSP_VERSION.length(); | ||
| int end = s.indexOf(" ", start); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we guaranteed to always have a space afterwards? If that's the required convention, consider error logging if we don't. |
||
| if (end > -1) | ||
| { | ||
| LOG.warn("Unable to parse {} URI", directive, e); | ||
| _cspVersion = s.substring(start, end); | ||
| if (s.indexOf(CSP_VERSION, end) > -1) | ||
| LOG.warn("More than one " + CSP_VERSION + " assignment found; using the first one."); | ||
| LOG.debug("CspVersion: {}", getCspVersion()); | ||
| } | ||
| } | ||
|
|
||
| LOG.debug("CspVersion: {}", getCspVersion()); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -277,7 +270,7 @@ private CspFilterSettings(ContentSecurityPolicyFilter filter, String baseServerU | |
| { | ||
| // Each filter adds its own "Reporting-Endpoints" header since we want to convey the correct version (eXX vs. rXX) | ||
| @SuppressWarnings("DataFlowIssue") | ||
| ActionURL violationUrl = PageFlowUtil.urlProvider(AdminUrls.class).getCspReportToURL(filter.getCspVersion()); | ||
| ActionURL violationUrl = PageFlowUtil.urlProvider(AdminUrls.class).getCspReportToURL(); | ||
| // Use an absolute URL so we always post to https:, even if the violating request uses http: | ||
| _reportingEndpointsHeaderValue = filter.getReportToEndpointName() + "=\"" + violationUrl.getURIString() + "\""; | ||
|
|
||
|
|
@@ -406,6 +399,34 @@ public static boolean hasCsp(ContentSecurityPolicyType type) | |
| return CSP_FILTERS.get(type) != null; | ||
| } | ||
|
|
||
| public static @NotNull String getCspVersion(@Nullable String disposition) | ||
| { | ||
| if (disposition != null) | ||
| { | ||
| ContentSecurityPolicyType type = ContentSecurityPolicyType.get(disposition); | ||
|
|
||
| if (type != null) | ||
| { | ||
| var filter = CSP_FILTERS.get(type); | ||
|
|
||
| if (null != filter) | ||
| { | ||
| return filter.getCspVersion(); | ||
| } | ||
| else | ||
| { | ||
| LOG.error("Disposition {} doesn't match a configured CSP filter", disposition); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| LOG.error("Bad disposition: {}", disposition); | ||
| } | ||
| } | ||
|
|
||
| return "Unknown"; | ||
| } | ||
|
|
||
| public static List<String> getMissingSubstitutions(ContentSecurityPolicyType type) | ||
| { | ||
| ContentSecurityPolicyFilter filter = CSP_FILTERS.get(type); | ||
|
|
||
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
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.
Good candidate for a new, targeted unit test.