rombert commented on code in PR #30: URL: https://github.com/apache/sling-org-apache-sling-xss/pull/30#discussion_r999362695
########## src/test/java/org/apache/sling/xss/impl/AntiSamyPolicyWithTestConfigTest.java: ########## @@ -0,0 +1,146 @@ +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ 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.sling.xss.impl; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import javax.xml.stream.XMLStreamException; + +import java.io.IOException; +import java.util.regex.Pattern; + +import org.apache.sling.xss.impl.xml.AntiSamyPolicy; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class AntiSamyPolicyWithTestConfigTest { + + public static final String POLICY_FILE = "./testConfig.xml"; Review Comment: Please set a self-explanatory name , e.g. `configWithFeatureFoo.xml` so it's immediately clear what it contains. ########## src/main/java/org/apache/sling/xss/impl/AntiSamyPolicyAdapter.java: ########## @@ -222,43 +224,28 @@ public boolean apply(String s) { private static Predicate<String> matchesPatternsOrLiterals(List<Pattern> patternList, boolean ignoreCase, List<String> literalList) { return new Predicate<String>() { public boolean apply(String s) { - // check if the string matches to the pattern - for (Pattern pattern : patternList) { - if (pattern.matcher(s).matches()) { - return true; - } - } - // if the pattern does not match it goes through the literals - for (String string : literalList) { - s = ignoreCase - ? s.toLowerCase() - : s; - if (string.equals(s)) { - return true; - } - } - // if it neither matches the patterns nor the literals it returns false - return false; + // check if the string matches to the pattern or one of the literal + s = ignoreCase ? s.toLowerCase() : s; + return matchesToPatterns(patternList).apply(s) || literalList.contains(s); } }; } - public AttributePolicy newDynamicAttributePolicy(final Pattern pattern) { + public AttributePolicy newDynamicAttributePolicy(final List<Pattern> patternList, final boolean ignoreCase, final List<String> literalList) { return new AttributePolicy() { @Override public @Nullable String apply(String elementName, String attributeName, String value) { - return pattern.matcher(value).matches() ? value : null; - } - }; - } + if (!literalList.isEmpty() && !patternList.isEmpty()) { + return matchesPatternsOrLiterals(patternList,ignoreCase, literalList).apply(value) ? value : null; - public AttributePolicy newDynamicAttributePolicy(boolean ignoreCase, String... allowedValues) { - final List<String> allowed = Arrays.asList(allowedValues); - return new AttributePolicy() { - @Override - public @Nullable String apply(String elementName, String attributeName, String uncanonValue) { - String value = ignoreCase ? uncanonValue.toLowerCase() : uncanonValue; - return allowed.contains(value) ? value : null; + } else if (!literalList.isEmpty()) { + value = ignoreCase ? value.toLowerCase() : value; Review Comment: `String.toLowerCase` without an explicit locale can be dangerous, see https://stackoverflow.com/questions/11063102/using-locales-with-javas-tolowercase-and-touppercase for some discussion. Please figure out whether we can run in the default locale or need to use something like english. ########## src/test/java/org/apache/sling/xss/impl/AntiSamyPolicyWithTestConfigTest.java: ########## @@ -0,0 +1,146 @@ +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ 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.sling.xss.impl; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import javax.xml.stream.XMLStreamException; + +import java.io.IOException; +import java.util.regex.Pattern; + +import org.apache.sling.xss.impl.xml.AntiSamyPolicy; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class AntiSamyPolicyWithTestConfigTest { Review Comment: Please set a self-explanatory name , e.g. `AntiSamyPolicyWithFeatureFooTest` so it's immediately clear what this test. ########## src/test/java/org/apache/sling/xss/impl/AntiSamyPolicyWithTestConfigTest.java: ########## @@ -0,0 +1,146 @@ +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ 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.sling.xss.impl; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import javax.xml.stream.XMLStreamException; + +import java.io.IOException; +import java.util.regex.Pattern; + +import org.apache.sling.xss.impl.xml.AntiSamyPolicy; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class AntiSamyPolicyWithTestConfigTest { + + public static final String POLICY_FILE = "./testConfig.xml"; + private static HtmlSanitizer antiSamy; + + @BeforeAll + public static void setup() throws InvalidConfigException, XMLStreamException, IOException { + antiSamy = new HtmlSanitizer(new AntiSamyPolicy(AntiSamyPolicyWithTestConfigTest.class.getClassLoader().getResourceAsStream(POLICY_FILE))); + } + + @ParameterizedTest + @MethodSource("dataForDynamicAttributes") + public void testDynamicAttributes(TestInput testInput) throws Exception { + testInput.skipComparingInputWithOutput = false; + testInput.runCheck(); + } + + @ParameterizedTest + @MethodSource("dataForGlobalAttributes") + public void testGlobalAttributes(TestInput testInput) throws Exception { + testInput.skipComparingInputWithOutput = false; + testInput.runCheck(); + } + + + static TestInput[] dataForGlobalAttributes() { + return new TestInput[]{ + new TestInput("<p draggable=\"wrong\">This is a paragraph.</p>", + "<p",false), + new TestInput("<p draggable=\"auto\">This is a paragraph.</p>", + "<p", true), + new TestInput("<p draggable=\"true\">This is a draggable paragraph.</p>", + "<p", true), + new TestInput("<p contenteditable=\"wrong\">This is a paragraph.</p>", + "<p",false), + new TestInput("<p contenteditable=\"true\">This is a paragraph.</p>", + "<p", true), + }; + } + + static TestInput[] dataForDynamicAttributes() { + return new TestInput[]{ + new TestInput("<p data-test=\"test-purpose\">This is a paragraph.</p>", + "<p data-test=\"test-purpose\">This is a paragraph.</p>", true), + new TestInput("<p data-test=\"test\">This is a paragraph.</p>", + "<p data-test=\"test\">This is a paragraph.</p>", true), + }; + } + + private static class TestInput { Review Comment: I think this TestInput class already exists in the code, right? Please extract it to a shared class so it can be reused instead of duplicated. ########## src/main/java/org/apache/sling/xss/impl/AntiSamyPolicyAdapter.java: ########## @@ -222,43 +224,28 @@ public boolean apply(String s) { private static Predicate<String> matchesPatternsOrLiterals(List<Pattern> patternList, boolean ignoreCase, List<String> literalList) { return new Predicate<String>() { public boolean apply(String s) { - // check if the string matches to the pattern - for (Pattern pattern : patternList) { - if (pattern.matcher(s).matches()) { - return true; - } - } - // if the pattern does not match it goes through the literals - for (String string : literalList) { - s = ignoreCase - ? s.toLowerCase() - : s; - if (string.equals(s)) { - return true; - } - } - // if it neither matches the patterns nor the literals it returns false - return false; + // check if the string matches to the pattern or one of the literal + s = ignoreCase ? s.toLowerCase() : s; Review Comment: `String.toLowerCase` without an explicit locale can be dangerous, see https://stackoverflow.com/questions/11063102/using-locales-with-javas-tolowercase-and-touppercase for some discussion. Please figure out whether we can run in the default locale or need to use something like english. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
