Skip to content
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

Unbreak CssSchema.withProperties(Map) #313

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/org/owasp/html/CssSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,16 @@ public static CssSchema withProperties(
Map<String, Property> propertyMap =
new HashMap<>();
// check that all fnKeys are defined in properties.
for (Map.Entry<String, Property> e : propertyMap.entrySet()) {
for (Map.Entry<? extends String, ? extends Property> e : properties.entrySet()) {
Property property = e.getValue();
for (String fnKey : property.fnKeys.values()) {
if (!propertyMap.containsKey(fnKey)) {
if (!properties.containsKey(fnKey)) {
throw new IllegalArgumentException(
"Property map is not self contained. " + e.getValue()
+ " depends on undefined function key " + fnKey);
}
}
propertyMap.put(e.getKey(), e.getValue());
}
return new CssSchema(Map.copyOf(propertyMap));
}
Expand Down
52 changes: 51 additions & 1 deletion src/test/java/org/owasp/html/HtmlPolicyBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
package org.owasp.html;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -250,7 +252,31 @@ public void testSpecificStyleFilterung() {
}

@Test
public void testUnionStyleFilterung() {
public void testCustomPropertyStyleFiltering() {
assertEquals(
Arrays.stream(new String[] {
"<h1>Header</h1>",
"<p>Paragraph 1</p>",
"<p>Click me out</p>",
"<p></p>",
"<p><b>Fancy</b> with <i><b>soupy</b></i><b> tags</b>.",
"</p><p style=\"text-align:center\">Stylish Para 1</p>",
"<p>Stylish Para 2</p>",
""}).collect(Collectors.joining("\n")),
apply(new HtmlPolicyBuilder()
.allowCommonInlineFormattingElements()
.allowCommonBlockElements()
.allowStyling(
CssSchema.withProperties(
Map.of("text-align",
new CssSchema.Property(0,
Set.of("center"),
Collections.emptyMap()))))
.allowStandardUrlProtocols()));
}

@Test
public void testUnionStyleFiltering() {
assertEquals(
Arrays.stream(new String[] {
"<h1>Header</h1>",
Expand All @@ -271,6 +297,30 @@ public void testUnionStyleFilterung() {
.allowStandardUrlProtocols()));
}

@Test
public void testCustomPropertyStyleFilteringDisallowed() {
assertEquals(
Arrays.stream(new String[] {
"<h1>Header</h1>",
"<p>Paragraph 1</p>",
"<p>Click me out</p>",
"<p></p>",
"<p><b>Fancy</b> with <i><b>soupy</b></i><b> tags</b>.",
"</p><p>Stylish Para 1</p>",
"<p>Stylish Para 2</p>",
""}).collect(Collectors.joining("\n")),
apply(new HtmlPolicyBuilder()
.allowCommonInlineFormattingElements()
.allowCommonBlockElements()
.allowStyling(
CssSchema.withProperties(
Map.of("text-align",
new CssSchema.Property(0,
Set.of("left", "right"),
Collections.emptyMap()))))
.allowStandardUrlProtocols()));
}

@Test
public static final void testElementTransforming() {
assertEquals(
Expand Down
Loading