Skip to content

Commit 67890ce

Browse files
committed
Avoid unnecessary copies
Signed-off-by: Sven Strickroth <email@cs-ware.de>
1 parent 91c5fdc commit 67890ce

File tree

9 files changed

+42
-34
lines changed

9 files changed

+42
-34
lines changed

src/main/java/org/owasp/html/CssSchema.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public static CssSchema withProperties(
151151
if (prop == null) { throw new IllegalArgumentException(propertyName); }
152152
propertiesBuilder.put(propertyName, prop);
153153
}
154-
return new CssSchema(Map.copyOf(propertiesBuilder));
154+
return new CssSchema(Collections.unmodifiableMap(propertiesBuilder));
155155
}
156156

157157
/**
@@ -161,20 +161,20 @@ public static CssSchema withProperties(
161161
*/
162162
public static CssSchema withProperties(
163163
Map<? extends String, ? extends Property> properties) {
164-
Map<String, Property> propertyMap =
164+
Map<String, Property> propertyMapBuilder =
165165
new HashMap<>();
166166
// check that all fnKeys are defined in properties.
167-
for (Map.Entry<String, Property> e : propertyMap.entrySet()) {
167+
for (Map.Entry<String, Property> e : propertyMapBuilder.entrySet()) {
168168
Property property = e.getValue();
169169
for (String fnKey : property.fnKeys.values()) {
170-
if (!propertyMap.containsKey(fnKey)) {
170+
if (!propertyMapBuilder.containsKey(fnKey)) {
171171
throw new IllegalArgumentException(
172172
"Property map is not self contained. " + e.getValue()
173173
+ " depends on undefined function key " + fnKey);
174174
}
175175
}
176176
}
177-
return new CssSchema(Map.copyOf(propertyMap));
177+
return new CssSchema(Collections.unmodifiableMap(propertyMapBuilder));
178178
}
179179

180180
/**
@@ -187,7 +187,7 @@ public static CssSchema withProperties(
187187
*/
188188
public static CssSchema union(CssSchema... cssSchemas) {
189189
if (cssSchemas.length == 1) { return cssSchemas[0]; }
190-
Map<String, Property> properties = new LinkedHashMap<>();
190+
Map<String, Property> propertyMapBuilder = new LinkedHashMap<>();
191191
for (CssSchema cssSchema : cssSchemas) {
192192
for (Map.Entry<String, Property> e : cssSchema.properties.entrySet()) {
193193
String name = e.getKey();
@@ -198,14 +198,14 @@ public static CssSchema union(CssSchema... cssSchemas) {
198198
if (Objects.isNull(newProp)) {
199199
throw new NullPointerException("An entry was returned with null value from cssSchema.properties");
200200
}
201-
Property oldProp = properties.put(name, newProp);
201+
Property oldProp = propertyMapBuilder.put(name, newProp);
202202
if (oldProp != null && !oldProp.equals(newProp)) {
203203
throw new IllegalArgumentException(
204204
"Duplicate irreconcilable definitions for " + name);
205205
}
206206
}
207207
}
208-
return new CssSchema(Map.copyOf(properties));
208+
return new CssSchema(Collections.unmodifiableMap(propertyMapBuilder));
209209
}
210210

211211
/**
@@ -846,15 +846,15 @@ Property forKey(String propertyName) {
846846
builder.put("z-index", bottom);
847847
builder.put("repeating-linear-gradient()", linearGradient$Fun);
848848
builder.put("repeating-radial-gradient()", radialGradient$Fun);
849-
DEFINITIONS = Map.copyOf(builder);
849+
DEFINITIONS = Collections.unmodifiableMap(builder);
850850
}
851851

852852
private static <T> Set<T> union(Set<T>... subsets) {
853853
Set<T> all = new HashSet<>();
854854
for (Set<T> subset : subsets) {
855855
all.addAll(subset);
856856
}
857-
return Set.copyOf(all);
857+
return Collections.unmodifiableSet(all);
858858
}
859859

860860
static final Set<String> DEFAULT_WHITELIST = Set.of(

src/main/java/org/owasp/html/ElementAndAttributePolicies.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
package org.owasp.html;
3030

31+
import java.util.Collections;
3132
import java.util.HashMap;
3233
import java.util.LinkedHashMap;
3334
import java.util.Map;
@@ -81,7 +82,7 @@ ElementAndAttributePolicies and(ElementAndAttributePolicies p) {
8182
return new ElementAndAttributePolicies(
8283
elementName,
8384
ElementPolicy.Util.join(elPolicy, p.elPolicy),
84-
Map.copyOf(joinedAttrPolicies),
85+
Collections.unmodifiableMap(joinedAttrPolicies),
8586
this.htmlTagSkipType.and(p.htmlTagSkipType));
8687
}
8788

src/main/java/org/owasp/html/ElementPolicy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
package org.owasp.html;
3030

3131
import java.util.ArrayList;
32+
import java.util.Collections;
3233
import java.util.List;
3334
import java.util.Optional;
3435
import java.util.Set;
@@ -133,7 +134,7 @@ final class JoinedElementPolicy implements ElementPolicy {
133134
JoinedElementPolicy(Iterable<? extends ElementPolicy> policies) {
134135
List<ElementPolicy> builder = new ArrayList<>();
135136
policies.forEach(builder::add);
136-
this.policies = List.copyOf(builder);
137+
this.policies = Collections.unmodifiableList(builder);
137138
}
138139

139140
public @Nullable String apply(String elementName, List<String> attrs) {

src/main/java/org/owasp/html/FilterUrlByProtocolAttributePolicy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
package org.owasp.html;
3030

31+
import java.util.Collections;
3132
import java.util.HashSet;
3233
import java.util.Set;
3334

@@ -66,7 +67,7 @@ public FilterUrlByProtocolAttributePolicy(
6667
Iterable<? extends String> protocols) {
6768
Set<String> builder = new HashSet<>();
6869
protocols.forEach(builder::add);
69-
this.protocols = Set.copyOf(builder);
70+
this.protocols = Collections.unmodifiableSet(builder);
7071
}
7172

7273
public @Nullable String apply(

src/main/java/org/owasp/html/HtmlElementTables.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.owasp.html;
22

33
import java.util.Arrays;
4+
import java.util.Collections;
45
import java.util.Comparator;
56
import java.util.HashMap;
67
import java.util.List;
@@ -418,7 +419,7 @@ public int getElementNameIndex(String canonName) {
418419
for (int i = 0, n = this.canonNames.size(); i < n; ++i) {
419420
builder.put(this.canonNames.get(i), i);
420421
}
421-
canonNameToIndex = Map.copyOf(builder);
422+
canonNameToIndex = Collections.unmodifiableMap(builder);
422423
this.customElementIndex = canonNames.indexOf(CUSTOM_ELEMENT_NAME);
423424
if (this.customElementIndex < 0) {
424425
throw new IllegalStateException("Negative element index");

src/main/java/org/owasp/html/HtmlEntities.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
package org.owasp.html;
3030

31+
import java.util.Collections;
3132
import java.util.HashMap;
3233
import java.util.Map;
3334

@@ -2290,7 +2291,7 @@ final class HtmlEntities {
22902291
}
22912292
}
22922293

2293-
final Map<String, String> entityNameToCodePointMap = Map.copyOf(builder);
2294+
final Map<String, String> entityNameToCodePointMap = Collections.unmodifiableMap(builder);
22942295

22952296
ENTITY_TRIE = new Trie<String>(entityNameToCodePointMap);
22962297
LONGEST_ENTITY_NAME = longestEntityName;

src/main/java/org/owasp/html/HtmlPolicyBuilder.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
package org.owasp.html;
3030

3131
import java.util.ArrayList;
32+
import java.util.Collections;
3233
import java.util.HashMap;
3334
import java.util.HashSet;
3435
import java.util.LinkedHashMap;
@@ -171,7 +172,7 @@ public class HtmlPolicyBuilder {
171172
for (String elementName : DEFAULT_SKIP_IF_EMPTY) {
172173
builder.put(elementName, HtmlTagSkipType.SKIP_BY_DEFAULT);
173174
}
174-
DEFAULT_SKIP_TAG_MAP_IF_EMPTY_ATTR = Map.copyOf(builder);
175+
DEFAULT_SKIP_TAG_MAP_IF_EMPTY_ATTR = Collections.unmodifiableMap(builder);
175176
}
176177

177178
/**
@@ -348,7 +349,7 @@ public AttributeBuilder allowAttributes(String... attributeNames) {
348349
for (String attributeName : attributeNames) {
349350
builder.add(HtmlLexer.canonicalAttributeName(attributeName));
350351
}
351-
return new AttributeBuilder(List.copyOf(builder));
352+
return new AttributeBuilder(Collections.unmodifiableList(builder));
352353
}
353354

354355
/**
@@ -648,7 +649,7 @@ AttributePolicy makeGuard(AttributeGuardIntermediates intermediates) {
648649
}
649650

650651
});
651-
ATTRIBUTE_GUARDS = Map.copyOf(builder);
652+
ATTRIBUTE_GUARDS = Collections.unmodifiableMap(builder);
652653
}
653654

654655
/**
@@ -687,17 +688,17 @@ public <CTX> HtmlSanitizer.Policy build(
687688
* each backed by a different output channel.
688689
*/
689690
public PolicyFactory toFactory() {
690-
Set<String> textContainerSet = new HashSet<>();
691+
Set<String> textContainerSetBuilder = new HashSet<>();
691692
for (Map.Entry<String, Boolean> textContainer
692693
: this.textContainers.entrySet()) {
693694
if (Boolean.TRUE.equals(textContainer.getValue())) {
694-
textContainerSet.add(textContainer.getKey());
695+
textContainerSetBuilder.add(textContainer.getKey());
695696
}
696697
}
697698
CompiledState compiled = compilePolicies();
698699

699700
return new PolicyFactory(
700-
compiled.compiledPolicies, Set.copyOf(textContainerSet),
701+
compiled.compiledPolicies, Collections.unmodifiableSet(textContainerSetBuilder),
701702
Map.copyOf(compiled.globalAttrPolicies),
702703
preprocessor, postprocessor);
703704
}
@@ -813,7 +814,7 @@ private CompiledState compilePolicies() {
813814
elAttrPolicies = Map.of();
814815
}
815816

816-
Map<String, AttributePolicy> attrs
817+
Map<String, AttributePolicy> attrsBuilder
817818
= new HashMap<>();
818819

819820
for (Map.Entry<String, AttributePolicy> ape : elAttrPolicies.entrySet()) {
@@ -823,7 +824,7 @@ private CompiledState compilePolicies() {
823824
if (globalAttrPolicies.containsKey(attributeName)) { continue; }
824825
AttributePolicy policy = ape.getValue();
825826
if (!AttributePolicy.REJECT_ALL_ATTRIBUTE_POLICY.equals(policy)) {
826-
attrs.put(attributeName, policy);
827+
attrsBuilder.put(attributeName, policy);
827828
}
828829
}
829830
for (Map.Entry<String, AttributePolicy> ape
@@ -832,21 +833,21 @@ private CompiledState compilePolicies() {
832833
AttributePolicy policy = AttributePolicy.Util.join(
833834
elAttrPolicies.get(attributeName), ape.getValue());
834835
if (!AttributePolicy.REJECT_ALL_ATTRIBUTE_POLICY.equals(policy)) {
835-
attrs.put(attributeName, policy);
836+
attrsBuilder.put(attributeName, policy);
836837
}
837838
}
838839

839840
policiesBuilder.put(
840841
elementName,
841842
new ElementAndAttributePolicies(
842843
elementName,
843-
elPolicy, Map.copyOf(attrs),
844+
elPolicy, Collections.unmodifiableMap(attrsBuilder),
844845
getHtmlTagSkipType(elementName)
845846
)
846847
);
847848
}
848849
compiledState = new CompiledState(
849-
globalAttrPolicies, Map.copyOf(policiesBuilder));
850+
globalAttrPolicies, Collections.unmodifiableMap(policiesBuilder));
850851
return compiledState;
851852
}
852853

@@ -984,7 +985,7 @@ public HtmlPolicyBuilder onElements(String... elementNames) {
984985
builder.add(HtmlLexer.canonicalElementName(elementName));
985986
}
986987
return HtmlPolicyBuilder.this.allowAttributesOnElements(
987-
policy, attributeNames, List.copyOf(builder));
988+
policy, attributeNames, Collections.unmodifiableList(builder));
988989
}
989990
}
990991

src/main/java/org/owasp/html/PolicyFactory.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
package org.owasp.html;
3030

31+
import java.util.Collections;
3132
import java.util.HashMap;
3233
import java.util.HashSet;
3334
import java.util.Map;
@@ -173,10 +174,10 @@ public PolicyFactory and(PolicyFactory f) {
173174
} else if (f.textContainers.containsAll(this.textContainers)) {
174175
allTextContainers = f.textContainers;
175176
} else {
176-
Set<String> containers = new HashSet<>();
177-
this.textContainers.forEach(containers::add);
178-
f.textContainers.forEach(containers::add);
179-
allTextContainers = Set.copyOf(containers);
177+
Set<String> containerBuilder = new HashSet<>();
178+
this.textContainers.forEach(containerBuilder::add);
179+
f.textContainers.forEach(containerBuilder::add);
180+
allTextContainers = Collections.unmodifiableSet(containerBuilder);
180181
}
181182
Map<String, AttributePolicy> allGlobalAttrPolicies;
182183
if (f.globalAttrPolicies.isEmpty()) {
@@ -200,7 +201,7 @@ public PolicyFactory and(PolicyFactory f) {
200201
ab.put(attrName, e.getValue());
201202
}
202203
}
203-
allGlobalAttrPolicies = Map.copyOf(ab);
204+
allGlobalAttrPolicies = Collections.unmodifiableMap(ab);
204205
}
205206
HtmlStreamEventProcessor compositionOfPreprocessors
206207
= HtmlStreamEventProcessor.Processors.compose(
@@ -209,7 +210,7 @@ public PolicyFactory and(PolicyFactory f) {
209210
= HtmlStreamEventProcessor.Processors.compose(
210211
this.postprocessor, f.postprocessor);
211212
return new PolicyFactory(
212-
Map.copyOf(builder), allTextContainers, allGlobalAttrPolicies,
213+
Collections.unmodifiableMap(builder), allTextContainers, allGlobalAttrPolicies,
213214
compositionOfPreprocessors, compositionOfPostprocessors);
214215
}
215216
}

src/test/java/org/owasp/html/SanitizersTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.ArrayList;
3434
import java.util.Arrays;
3535
import java.util.BitSet;
36+
import java.util.Collections;
3637
import java.util.Iterator;
3738
import java.util.List;
3839
import java.util.NoSuchElementException;
@@ -527,7 +528,7 @@ private static class Permutations<T> implements Iterable<List<T>> {
527528
this.k = k;
528529
List<T> builder = new ArrayList<>();
529530
Arrays.stream(elements).forEach(builder::add);
530-
this.elements = List.copyOf(builder);
531+
this.elements = Collections.unmodifiableList(builder);
531532
}
532533

533534
public Iterator<List<T>> iterator() {

0 commit comments

Comments
 (0)