|
1 | 1 | package com.github.bgalek.security.svg;
|
2 | 2 |
|
3 | 3 | import org.junit.jupiter.params.ParameterizedTest;
|
| 4 | +import org.junit.jupiter.params.provider.Arguments; |
| 5 | +import org.junit.jupiter.params.provider.MethodSource; |
4 | 6 | import org.junit.jupiter.params.provider.ValueSource;
|
5 | 7 |
|
6 | 8 | import java.io.File;
|
7 | 9 | import java.io.IOException;
|
8 | 10 | import java.nio.file.Files;
|
| 11 | +import java.util.Collections; |
9 | 12 | import java.util.Objects;
|
| 13 | +import java.util.stream.Stream; |
10 | 14 |
|
11 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
12 | 16 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
15 | 19 | class SvgSecurityValidatorTest {
|
16 | 20 |
|
17 | 21 | @ParameterizedTest(name = "validate {0} svg")
|
18 |
| - @ValueSource(strings = {"hacked/with-onclick-attribute.svg", "hacked/with-script-tag.svg", "hacked/with-script-tag-in-styles.svg"}) |
19 |
| - void shouldDetectXssInFiles(String file) { |
| 22 | + @MethodSource("evilUseCases") |
| 23 | + void shouldDetectXssInFiles(String file, String expectedOffendingElements) { |
20 | 24 | ValidationResult detect = new SvgSecurityValidator().validate(loadFile(file));
|
21 |
| - assertEquals(1, detect.getOffendingElements().size()); |
22 | 25 | assertTrue(detect.hasViolations());
|
| 26 | + assertEquals(expectedOffendingElements, String.join(",", detect.getOffendingElements())); |
23 | 27 | }
|
24 | 28 |
|
25 | 29 | @ParameterizedTest(name = "validate {0} svg")
|
26 | 30 | @ValueSource(strings = {"original/valid.svg"})
|
27 | 31 | void shouldNotDetectAnythingInValidFiles(String file) {
|
28 | 32 | ValidationResult detect = new SvgSecurityValidator().validate(loadFile(file));
|
29 |
| - assertEquals(0, detect.getOffendingElements().size()); |
30 | 33 | assertFalse(detect.hasViolations());
|
| 34 | + assertEquals(Collections.emptySet(), detect.getOffendingElements()); |
31 | 35 | }
|
32 | 36 |
|
33 | 37 | @ParameterizedTest(name = "validate {0} svg")
|
34 | 38 | @ValueSource(strings = {"original/valid.svg"})
|
35 | 39 | void shouldNotDetectAnythingInValidFilesUsingBytes(String file) {
|
36 | 40 | ValidationResult detect = new SvgSecurityValidator().validate(loadFile(file).getBytes());
|
37 |
| - assertEquals(0, detect.getOffendingElements().size()); |
38 | 41 | assertFalse(detect.hasViolations());
|
| 42 | + assertEquals(Collections.emptySet(), detect.getOffendingElements()); |
39 | 43 | }
|
40 | 44 |
|
41 | 45 | @ParameterizedTest(name = "validate {0} svg")
|
42 | 46 | @ValueSource(strings = {"broken/broken.csv.svg"})
|
43 | 47 | void shouldThrowExceptionWhenInputIsNotValidXml(String file) {
|
44 | 48 | ValidationResult detect = new SvgSecurityValidator().validate(loadFile(file));
|
45 |
| - assertEquals(0, detect.getOffendingElements().size()); |
46 | 49 | assertFalse(detect.hasViolations());
|
| 50 | + assertEquals(Collections.emptySet(), detect.getOffendingElements()); |
47 | 51 | }
|
48 | 52 |
|
49 | 53 | @ParameterizedTest(name = "validate {0} svg")
|
50 | 54 | @ValueSource(strings = {"broken/broken.png.svg"})
|
51 | 55 | void shouldThrowExceptionWhenInputIsBinaryType(String file) {
|
52 | 56 | ValidationResult detect = new SvgSecurityValidator().validate(loadFile(file).getBytes());
|
53 |
| - assertEquals(0, detect.getOffendingElements().size()); |
54 | 57 | assertFalse(detect.hasViolations());
|
| 58 | + assertEquals(Collections.emptySet(), detect.getOffendingElements()); |
| 59 | + } |
| 60 | + |
| 61 | + private static Stream<Arguments> evilUseCases() { |
| 62 | + return Stream.of( |
| 63 | + Arguments.of("hacked/with-onclick-attribute.svg", "onclick"), |
| 64 | + Arguments.of("hacked/with-script-tag.svg", "script"), |
| 65 | + Arguments.of("hacked/with-script-tag-in-styles.svg", "script"), |
| 66 | + Arguments.of("hacked/with-css-url-syntax.svg", "style"), |
| 67 | + Arguments.of("hacked/with-xlink-injection.svg", "script") |
| 68 | + ); |
55 | 69 | }
|
56 | 70 |
|
57 | 71 | private String loadFile(String fileName) {
|
|
0 commit comments