-
Notifications
You must be signed in to change notification settings - Fork 401
WICKET-7172: Extend CSP with support for script-src-attr, style-src-attr #1341
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
792a01f
4599c9d
1b637a2
e006b91
d3aa4af
738e917
766dbb3
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 |
|---|---|---|
|
|
@@ -35,7 +35,12 @@ public enum CSPDirective | |
| { | ||
| DEFAULT_SRC("default-src"), | ||
| SCRIPT_SRC("script-src"), | ||
| SCRIPT_SRC_ATTR("script-src-attr"), | ||
| SCRIPT_SRC_ELEM("script-src-elem"), | ||
| SRC("src"), | ||
| STYLE_SRC("style-src"), | ||
| STYLE_SRC_ATTR("style-src-attr"), | ||
| STYLE_SRC_ELEM("style-src-elem"), | ||
| IMG_SRC("img-src"), | ||
| CONNECT_SRC("connect-src"), | ||
| FONT_SRC("font-src"), | ||
|
|
@@ -121,7 +126,7 @@ public void checkValueForDirective(CSPRenderable value, | |
| } | ||
| }; | ||
|
|
||
| private String value; | ||
| private final String value; | ||
|
|
||
| CSPDirective(String value) | ||
| { | ||
|
|
@@ -135,7 +140,7 @@ public String getValue() | |
|
|
||
| /** | ||
| * Check if {@code value} can be added to the list of other values. By default, it checks for | ||
| * conflicts with wildcards and none and it checks if values are valid uris. | ||
| * conflicts with wildcards and none, and it checks if values are valid uris. | ||
| * | ||
| * @param value | ||
| * The value to add. | ||
|
|
@@ -147,6 +152,19 @@ public String getValue() | |
| public void checkValueForDirective(CSPRenderable value, | ||
| List<CSPRenderable> existingDirectiveValues) | ||
| { | ||
| if (this == SCRIPT_SRC_ATTR || this == STYLE_SRC_ATTR) | ||
| { | ||
| if (!existingDirectiveValues.isEmpty()) | ||
| { | ||
| throw new IllegalArgumentException("Directive " + this + " supports only one value"); | ||
|
Member
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. According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/script-src-attr#source-expression-list those could have multiple values, not just one.
Contributor
Author
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. Technically this is true, but in practice only 2 values are allowed: none and 'unsafe inline'. They contradict each other so shouldn't be used together.
Member
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. Let's hear some more opinions from other CSP users. My personal opinion is that we should follow the standards but I don't have experience in this area.
Contributor
Author
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. Actually you are correct, my implementation doesn't support all allowed values. These should be allowed: 'none' |
||
| } | ||
|
|
||
| if (value != CSPDirectiveSrcValue.NONE && value != CSPDirectiveSrcValue.UNSAFE_INLINE) | ||
martin-g marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| throw new IllegalArgumentException("Unsupported directive value: " + value + " for -src-attr directive"); | ||
| } | ||
| } | ||
|
|
||
| if (!existingDirectiveValues.isEmpty()) | ||
| { | ||
| if (CSPDirectiveSrcValue.WILDCARD.equals(value) | ||
|
|
@@ -185,11 +203,11 @@ public static CSPDirective fromValue(String value) | |
| { | ||
| return null; | ||
| } | ||
| for (int i = 0; i < values().length; i++) | ||
| for (CSPDirective directive : values()) | ||
| { | ||
| if (value.equals(values()[i].getValue())) | ||
| if (value.equals(directive.getValue())) | ||
| { | ||
| return values()[i]; | ||
| return directive; | ||
| } | ||
| } | ||
| return null; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.wicket.csp; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| class CSPDirectiveTest { | ||
|
|
||
| @Test | ||
| void scriptSrcAttrAndStyleSrcAttributesSupportValues() | ||
| { | ||
| CSPDirective.SCRIPT_SRC_ATTR.checkValueForDirective(CSPDirectiveSrcValue.NONE, List.of()); | ||
| CSPDirective.SCRIPT_SRC_ATTR.checkValueForDirective(CSPDirectiveSrcValue.UNSAFE_INLINE, List.of()); | ||
| CSPDirective.STYLE_SRC_ATTR.checkValueForDirective(CSPDirectiveSrcValue.NONE, List.of()); | ||
| CSPDirective.STYLE_SRC_ATTR.checkValueForDirective(CSPDirectiveSrcValue.UNSAFE_INLINE, List.of()); | ||
| } | ||
|
|
||
| @Test | ||
| void scriptSrcAttrAndStyleSrcAttributesOnlySupportOneValue() | ||
| { | ||
| assertThrows(IllegalArgumentException.class, () -> CSPDirective.SCRIPT_SRC_ATTR.checkValueForDirective(CSPDirectiveSrcValue.NONE, List.of(CSPDirectiveSrcValue.UNSAFE_INLINE))); | ||
| assertThrows(IllegalArgumentException.class, () -> CSPDirective.STYLE_SRC_ATTR.checkValueForDirective(CSPDirectiveSrcValue.UNSAFE_INLINE, List.of(CSPDirectiveSrcValue.UNSAFE_INLINE))); | ||
| } | ||
|
|
||
| @Test | ||
| void scriptSrcAttrAndStyleSrcAttributesOnlySupportNoneAndUnsafeInline() | ||
| { | ||
| assertThrows(IllegalArgumentException.class, () -> CSPDirective.SCRIPT_SRC_ATTR.checkValueForDirective(CSPDirectiveSrcValue.SELF, List.of())); | ||
| assertThrows(IllegalArgumentException.class, () -> CSPDirective.STYLE_SRC_ATTR.checkValueForDirective(CSPDirectiveSrcValue.WILDCARD, List.of())); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.