-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,15 +180,58 @@ public void setReadListener(ReadListener readListener) { | |
public void shouldSanitizeAttackFromBody() throws Exception { | ||
when(httpRequest.getContentType()).thenReturn("application/JSON"); | ||
when(sanitizerFilter.getAttributesExcluded()).thenReturn(List.of("email", "password")); | ||
final String attName = "test"; | ||
final String saneValue = "Hello <b>World</b>"; | ||
final String attValue = saneValue | ||
// Classic XSS attack in value | ||
final String attName1 = "test1"; | ||
final String saneValue1 = "Hello <b>World</b>"; | ||
final String attValue1 = saneValue1 | ||
+ "<style>@keyframes slidein {}</style><xss style=\"animation-duration:1s;animation-name:slidein;animation-iteration-count:2\" onwebkitanimationiteration=\"alert(1)\"></xss>"; | ||
// Another XSS attack in value | ||
final String attName2 = "test2"; | ||
final String saneValue2 = "<div>test</div>"; | ||
final String attValue2 = "<div onclick=\"alert('test')\">test</div><script>alert('test')</script>"; | ||
// XSS attack in name | ||
final String saneAttName3 = "test3"; | ||
final String attName3 = saneAttName3 + "<script>alert('test')</script>"; | ||
final String attValue3 = "value3"; | ||
// XSS attack as exploited in IE (as seen on | ||
// https://github.com/OWASP/java-html-sanitizer/blob/master/docs/html-validation.md#valid-according-to-policy) | ||
final String attName4 = "test4"; | ||
final String saneValue4 = "v4"; | ||
final String attValue4 = saneValue4 + "<!--if[true]> <script>alert(1337)</script> -->"; | ||
// XSS attack as exploited in foreign content context (as seen on | ||
// https://github.com/OWASP/java-html-sanitizer/blob/master/docs/html-validation.md#valid-according-to-policy) | ||
final String attName5 = "test5"; | ||
final String saneValue5 = "v5"; | ||
final String attValue5 = saneValue5 + "<![CDATA[ <!-- ]]><script>alert(1337)</script><!-- -->"; | ||
// Don't break my heart (as seen on | ||
// https://github.com/OWASP/java-html-sanitizer/blob/master/docs/html-validation.md#dont-break-my-heart) | ||
final String attName6 = "test6"; | ||
final String saneValue6 = "I <3 Poniez!"; | ||
// but escaped chars are unescaped anyway... | ||
final String attName7 = "test7"; | ||
final String saneValue7 = "You <3 Poniez 2!"; | ||
final String attValue7 = "You <3 Poniez 2!"; | ||
// XSS attack escaped in value | ||
final String attName8 = "test8"; | ||
final String saneValue8 = "value8"; | ||
final String attValue8 = saneValue8 + "<script>alert('test')</script>"; | ||
|
||
final String body = String.format("{%n" + | ||
" \"key1\": \"value1\",%n" + | ||
" \"%s\": \"%s\",%n" + | ||
" \"%s\": \"%s\",%n" + | ||
" \"%s\": \"%s\",%n" + | ||
" \"%s\": \"%s\",%n" + | ||
" \"%s\": \"%s\",%n" + | ||
" \"%s\": \"%s\",%n" + | ||
" \"%s\": \"%s\",%n" + | ||
" \"%s\": \"%s\",%n" + | ||
" \"email\": \"[email protected]\"%n" + | ||
"}", attName, JSonUtil.escape(attValue)); | ||
"}", | ||
attName1, JSonUtil.escape(attValue1), attName2, JSonUtil.escape(attValue2), | ||
attName3, JSonUtil.escape(attValue3), attName4, JSonUtil.escape(attValue4), | ||
attName5, JSonUtil.escape(attValue5), attName6, JSonUtil.escape(saneValue6), | ||
attName7, JSonUtil.escape(attValue7), attName8, JSonUtil.escape(attValue8)); | ||
var is = new ByteArrayInputStream(body.getBytes()); | ||
|
||
when(httpRequest.getInputStream()).thenReturn(new ServletInputStream() { | ||
|
@@ -225,10 +268,19 @@ public void setReadListener(ReadListener readListener) { | |
var stringBody = IOUtils.toString(inputStream, r.getCharacterEncoding()); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
var json = mapper.readTree(stringBody); | ||
// check normal values | ||
assertThat(json.get("key1").asText()).isEqualTo("value1"); | ||
assertThat(json.get("email").asText()).isEqualTo("[email protected]"); | ||
var hackValue = json.get(attName).asText(); | ||
assertThat(hackValue).isEqualTo(saneValue); | ||
// check sanitized values | ||
assertThat(json.get(attName1).asText()).isEqualTo(saneValue1); | ||
assertThat(json.get(attName2).asText()).isEqualTo(saneValue2); | ||
assertThat(json.get(attName3)).isNull(); | ||
assertThat(json.get(saneAttName3).asText()).isEqualTo(attValue3); | ||
assertThat(json.get(attName4).asText()).isEqualTo(saneValue4); | ||
assertThat(json.get(attName5).asText()).isEqualTo(saneValue5); | ||
assertThat(json.get(attName6).asText()).isEqualTo(saneValue6); | ||
assertThat(json.get(attName7).asText()).isEqualTo(saneValue7); | ||
assertThat(json.get(attName8).asText()).isEqualTo(saneValue8); | ||
} catch (IOException e) { | ||
throw new AssertionError(e); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters