diff --git a/parent/pom.xml b/parent/pom.xml index 968711c2..03a55fb4 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -179,17 +179,13 @@ application while protecting against XSS. bar - 6 + ${maven.compiler.source} org.apache.maven.plugins maven-compiler-plugin - 3.3 - - 9 - 9 - + 3.12.1 org.apache.maven.plugins diff --git a/src/main/java/org/owasp/html/CollectionsHelper.java b/src/main/java/org/owasp/html/CollectionsHelper.java new file mode 100644 index 00000000..bb8fa100 --- /dev/null +++ b/src/main/java/org/owasp/html/CollectionsHelper.java @@ -0,0 +1,29 @@ +package org.owasp.html; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Internal helper for common Collection creation/copy methods + */ +final class CollectionsHelper { + public static List copyToUnmodifiableList(Collection list) { + final ArrayList newList = new ArrayList<>(list.size()); + newList.addAll(list); + return Collections.unmodifiableList(newList); + } + + public static Set copyToUnmodifiableSet(Collection set) { + return Collections.unmodifiableSet(new HashSet(set)); + } + + public static Map copyToUnmodifiableMap(Map map) { + return Collections.unmodifiableMap(new HashMap<>(map)); + } +} diff --git a/src/main/java/org/owasp/html/CssSchema.java b/src/main/java/org/owasp/html/CssSchema.java index 342dd1ee..8ce2f661 100644 --- a/src/main/java/org/owasp/html/CssSchema.java +++ b/src/main/java/org/owasp/html/CssSchema.java @@ -69,8 +69,8 @@ public Property( int bits, Set literals, Map fnKeys) { this.bits = bits; - this.literals = Set.copyOf(literals); - this.fnKeys = Map.copyOf(fnKeys); + this.literals = CollectionsHelper.copyToUnmodifiableSet(literals); + this.fnKeys = CollectionsHelper.copyToUnmodifiableMap(fnKeys); } @Override diff --git a/src/main/java/org/owasp/html/ElementAndAttributePolicies.java b/src/main/java/org/owasp/html/ElementAndAttributePolicies.java index da4a0f2d..fb010440 100644 --- a/src/main/java/org/owasp/html/ElementAndAttributePolicies.java +++ b/src/main/java/org/owasp/html/ElementAndAttributePolicies.java @@ -54,7 +54,7 @@ final class ElementAndAttributePolicies { HtmlTagSkipType htmlTagSkipType) { this.elementName = elementName; this.elPolicy = elPolicy; - this.attrPolicies = Map.copyOf(attrPolicies); + this.attrPolicies = CollectionsHelper.copyToUnmodifiableMap(attrPolicies); this.htmlTagSkipType = htmlTagSkipType; } diff --git a/src/main/java/org/owasp/html/ElementAndAttributePolicyBasedSanitizerPolicy.java b/src/main/java/org/owasp/html/ElementAndAttributePolicyBasedSanitizerPolicy.java index 4f78e2c8..df1cfc84 100644 --- a/src/main/java/org/owasp/html/ElementAndAttributePolicyBasedSanitizerPolicy.java +++ b/src/main/java/org/owasp/html/ElementAndAttributePolicyBasedSanitizerPolicy.java @@ -63,8 +63,8 @@ class ElementAndAttributePolicyBasedSanitizerPolicy Map elAndAttrPolicies, Set allowedTextContainers) { this.out = out; - this.elAndAttrPolicies = Map.copyOf(elAndAttrPolicies); - this.allowedTextContainers = Set.copyOf(allowedTextContainers); + this.elAndAttrPolicies = CollectionsHelper.copyToUnmodifiableMap(elAndAttrPolicies); + this.allowedTextContainers = CollectionsHelper.copyToUnmodifiableSet(allowedTextContainers); } static final Set SKIPPABLE_ELEMENT_CONTENT diff --git a/src/main/java/org/owasp/html/HtmlElementTables.java b/src/main/java/org/owasp/html/HtmlElementTables.java index e230b870..09b0fbbb 100644 --- a/src/main/java/org/owasp/html/HtmlElementTables.java +++ b/src/main/java/org/owasp/html/HtmlElementTables.java @@ -401,7 +401,7 @@ public static final class HtmlElementNames { /** */ public HtmlElementNames(List canonNames) { - this.canonNames = List.copyOf(canonNames); + this.canonNames = CollectionsHelper.copyToUnmodifiableList(canonNames); } /** */ diff --git a/src/main/java/org/owasp/html/HtmlPolicyBuilder.java b/src/main/java/org/owasp/html/HtmlPolicyBuilder.java index 54c0305f..043e874d 100644 --- a/src/main/java/org/owasp/html/HtmlPolicyBuilder.java +++ b/src/main/java/org/owasp/html/HtmlPolicyBuilder.java @@ -698,7 +698,7 @@ public PolicyFactory toFactory() { return new PolicyFactory( compiled.compiledPolicies, Collections.unmodifiableSet(textContainerSetBuilder), - Map.copyOf(compiled.globalAttrPolicies), + CollectionsHelper.copyToUnmodifiableMap(compiled.globalAttrPolicies), preprocessor, postprocessor); } @@ -740,7 +740,7 @@ private CompiledState compilePolicies() { Map globalAttrPolicies = new LinkedHashMap<>(this.globalAttrPolicies); @SuppressWarnings("hiding") - Set allowedProtocols = Set.copyOf(this.allowedProtocols); + Set allowedProtocols = CollectionsHelper.copyToUnmodifiableSet(this.allowedProtocols); // Implement requireRelsOnLinks & skip... { @@ -874,7 +874,7 @@ public final class AttributeBuilder { private AttributePolicy policy = AttributePolicy.IDENTITY_ATTRIBUTE_POLICY; AttributeBuilder(List attributeNames) { - this.attributeNames = List.copyOf(attributeNames); + this.attributeNames = CollectionsHelper.copyToUnmodifiableList(attributeNames); } /** @@ -939,7 +939,7 @@ public AttributeBuilder matching( */ public AttributeBuilder matching( final boolean ignoreCase, Set allowedValues) { - final Set allowed = Set.copyOf(allowedValues); + final Set allowed = CollectionsHelper.copyToUnmodifiableSet(allowedValues); return matching(new AttributePolicy() { public @Nullable String apply( String elementName, String attributeName, String uncanonValue) { @@ -1007,13 +1007,13 @@ static RelsOnLinksPolicy create( RelsOnLinksPolicy( Set extra, Set skip) { - this.extra = Set.copyOf(extra); - this.skip = Set.copyOf(skip); + this.extra = CollectionsHelper.copyToUnmodifiableSet(extra); + this.skip = CollectionsHelper.copyToUnmodifiableSet(skip); Set targetOnly = new HashSet<>(); targetOnly.addAll(DEFAULT_RELS_ON_TARGETTED_LINKS); targetOnly.removeAll(extra); targetOnly.removeAll(skip); - this.whenTargetPresent = List.copyOf(targetOnly); + this.whenTargetPresent = CollectionsHelper.copyToUnmodifiableList(targetOnly); } private static int indexOfAttributeValue( diff --git a/src/test/java/org/owasp/html/Benchmark.java b/src/test/java/org/owasp/html/Benchmark.java index ea172b7d..b69c1af1 100644 --- a/src/test/java/org/owasp/html/Benchmark.java +++ b/src/test/java/org/owasp/html/Benchmark.java @@ -28,10 +28,10 @@ package org.owasp.html; -import java.io.File; import java.io.StringReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import java.util.ListIterator; @@ -58,7 +58,7 @@ public class Benchmark { * specifies a benchmark to run and unspecified ones are not run. */ public static void main(String[] args) throws Exception { - String html = new String(Files.readAllBytes(new File(args[0]).toPath()), StandardCharsets.UTF_8); + String html = new String(Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8); boolean timeLibhtmlparser = true; boolean timeSanitize = true;