martin-g commented on a change in pull request #399: WICKET-6727: Configurable Content-Security-Policy URL: https://github.com/apache/wicket/pull/399#discussion_r368895838
########## File path: wicket-core/src/main/java/org/apache/wicket/csp/CSPDirective.java ########## @@ -0,0 +1,208 @@ +/* + * 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 java.net.URI; +import java.net.URISyntaxException; +import java.util.List; + +import org.apache.wicket.util.string.Strings; + +/** + * An enum holding the possible CSP Directives. Via the + * {@link #checkValueForDirective(CSPRenderable, List)}-method, new values are verified before being + * added to the list of values for a directive. + * + * @see <a href="https://www.w3.org/TR/CSP2/">https://www.w3.org/TR/CSP2</a> + * @see <a href= + * "https://developer.mozilla.org/en-US/docs/Web/Security/CSP">https://developer.mozilla.org/en-US/docs/Web/Security/CSP</a> + */ +public enum CSPDirective +{ + DEFAULT_SRC("default-src"), + SCRIPT_SRC("script-src"), + STYLE_SRC("style-src"), + IMG_SRC("img-src"), + CONNECT_SRC("connect-src"), + FONT_SRC("font-src"), + OBJECT_SRC("object-src"), + MANIFEST_SRC("manifest-src"), + MEDIA_SRC("media-src"), + CHILD_SRC("child-src"), + FRAME_ANCESTORS("frame-ancestors"), + /** + * This directive was deprecated in CSP 2, but no longer in 3. Wicket will automatically add a + * {@code frame-src} directive when {@code child-src} is added. + */ + FRAME_SRC("frame-src"), + SANDBOX("sandbox") + { + /** + * Only allow {@link CSPDirectiveSandboxValue} for the {@code 'sandbox'} directive and block + * conflicting options. + */ + @Override + public void checkValueForDirective(CSPRenderable value, + List<CSPRenderable> existingDirectiveValues) + { + if (!existingDirectiveValues.isEmpty()) + { + if (CSPDirectiveSandboxValue.EMPTY.equals(value)) + { + throw new IllegalArgumentException( + "A sandbox directive can't contain an empty string if it already contains other values "); + } + if (existingDirectiveValues.contains(CSPDirectiveSandboxValue.EMPTY)) + { + throw new IllegalArgumentException( + "A sandbox directive can't contain other values if it already contains an empty string"); + } + } + + if (!(value instanceof CSPDirectiveSandboxValue)) + { + throw new IllegalArgumentException( + "A sandbox directive can only contain values from CSPDirectiveSandboxValue or be empty"); Review comment: I would be helpful to print the problematic value, for easier understanding of the problem ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services