Skip to content

Commit ddeb6ad

Browse files
committed
allowed custom styles, added test with user input, updated html sanitizer version - fixes #30
1 parent f260903 commit ddeb6ad

File tree

4 files changed

+560
-6
lines changed

4 files changed

+560
-6
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repositories {
1414
}
1515

1616
dependencies {
17-
implementation("com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer:20220608.1")
17+
implementation("com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer:1.1")
1818
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
1919
}
2020

src/main/java/com/github/bgalek/security/svg/SvgSecurityValidator.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ private Set<String> getOffendingElements(String xml) {
7171
if (JAVASCRIPT_PROTOCOL_IN_CSS_URL.matcher(xml).find()) return Collections.singleton("style");
7272
PolicyFactory policy = new HtmlPolicyBuilder()
7373
.allowElements(this.svgElements)
74-
.allowStyling(CssSchema.union(CssSchema.DEFAULT, CssSchema.withProperties(SVG_SPECIFIC_STYLES)))
7574
.allowAttributes(this.svgAttributes).globally()
7675
.allowUrlProtocols("https")
7776
.toFactory();
@@ -80,10 +79,6 @@ private Set<String> getOffendingElements(String xml) {
8079
return violations;
8180
}
8281

83-
private static final ImmutableMap<String, CssSchema.Property> SVG_SPECIFIC_STYLES = ImmutableMap.of(
84-
"enable-background", new CssSchema.Property(1, ImmutableSet.of(), ImmutableMap.of())
85-
);
86-
8782
private static HtmlChangeListener<Set<String>> violationsCollector() {
8883
return new ListHtmlChangeListener();
8984
}

src/test/java/com/github/bgalek/security/SvgSecurityValidatorTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.io.File;
1111
import java.io.IOException;
1212
import java.nio.file.Files;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
1315
import java.util.Collections;
1416
import java.util.List;
1517
import java.util.Objects;
@@ -67,6 +69,18 @@ void shouldNotFailWhenUserDefinedAttributesAreUsed() {
6769

6870
@Test
6971
void shouldNotFailWhenUserDefinedElementsAreUsed() {
72+
String testFile = loadFile("custom/custom3.svg");
73+
ValidationResult detect = SvgSecurityValidator.builder()
74+
.withAdditionalElements(Arrays.asList("horiz-adv-x", "missing-glyph", "font-face", "font"))
75+
.withAdditionalAttributes(Arrays.asList("horiz-adv-x", "font", "units-per-em"))
76+
.build()
77+
.validate(testFile);
78+
assertEquals(Collections.emptySet(), detect.getOffendingElements());
79+
assertFalse(detect.hasViolations());
80+
}
81+
82+
@Test
83+
void shouldNotFailWhenCustomStylesAreUsed() {
7084
String testFile = loadFile("custom/custom2.svg");
7185
List<String> strings = Collections.singletonList("cursor");
7286
ValidationResult detect = SvgSecurityValidator.builder()

0 commit comments

Comments
 (0)