-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: - changed PolicyRefactor for OwaspXssValidator.java - Moved files to different packages (refactor)
- Loading branch information
1 parent
85554b8
commit 1a42801
Showing
12 changed files
with
121 additions
and
88 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/org/scada_lts/web/beans/validation/xss/OwaspXssValidator.java
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.scada_lts.web.beans.validation.xss; | ||
|
||
import org.owasp.html.HtmlPolicyBuilder; | ||
import org.owasp.html.Sanitizers; | ||
import org.scada_lts.web.beans.validation.ScadaValidator; | ||
import org.owasp.html.PolicyFactory; | ||
|
||
public class OwaspXssValidator implements ScadaValidator<String> { | ||
|
||
private final PolicyFactory policyFactory; | ||
|
||
public OwaspXssValidator() { | ||
PolicyFactory basePolicy = Sanitizers.FORMATTING | ||
.and(Sanitizers.LINKS) | ||
.and(Sanitizers.STYLES) | ||
.and(Sanitizers.IMAGES); | ||
|
||
this.policyFactory = new HtmlPolicyBuilder() | ||
.allowCommonInlineFormattingElements() | ||
.allowCommonBlockElements() | ||
.allowStandardUrlProtocols() | ||
.allowStyling() | ||
.toFactory() | ||
.and(basePolicy); | ||
} | ||
|
||
@Override | ||
public void validate(String input) throws XssValidatorException { | ||
System.out.println("input: " + input); | ||
String sanitized = policyFactory.sanitize(input); | ||
System.out.println("Sanitized: " + sanitized); | ||
if (!sanitized.equals(input)) { | ||
throw new XssValidatorException("Potential XSS attack detected"); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../web/security/XssConstraintValidator.java → ...alidation/xss/XssConstraintValidator.java
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
2 changes: 1 addition & 1 deletion
2
src/org/scada_lts/web/security/XssValid.java → ...ts/web/beans/validation/xss/XssValid.java
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
2 changes: 1 addition & 1 deletion
2
...s/web/security/XssValidatorException.java → ...validation/xss/XssValidatorException.java
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 was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
test/org/scada_lts/web/security/OwaspXssValidatorExceptionTest.java
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.scada_lts.web.security; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.scada_lts.web.beans.validation.xss.OwaspXssValidator; | ||
import org.scada_lts.web.beans.validation.xss.XssValidatorException; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
@RunWith(Parameterized.class) | ||
public class OwaspXssValidatorExceptionTest { | ||
|
||
@Parameterized.Parameters | ||
public static Collection<Object[]> testData() { | ||
return Arrays.asList(new Object[][]{ | ||
{null}, | ||
{"<script>alert(1)</script>"}, | ||
{"<a href=\"javascript:alert(1)\">Link</a>"}, | ||
{"<div onclick=\"alert(1)\">Click me</div>"}, | ||
{"body { background-image: url('\"><img src=x onerror=alert(document.location)>'); }"}, | ||
{"div { content: \"<script>alert('XSS')</script>\"; }"}, | ||
{"h1 { font-family: \"<img src=x onerror=alert('XSS')>\"; }"}, | ||
{"@import url(\"javascript:alert('XSS')\");"}, | ||
{"div { /* comment: <img src=x onerror=alert('XSS')> */ }"}, | ||
{"span { content: '\"><script>alert(1)</script>'; }"}, | ||
{"h2 { color: expression(alert('XSS')); }"}, | ||
{"\"><img src=x onerror=alert(document.location)>"}, | ||
{"<img src='x' onerror='alert(1)'>"}, | ||
{"<input type=\"text\" value=\"\" onfocus=\"alert(1)\">"}, | ||
{"<iframe src=\"javascript:alert(1)\"></iframe>"}, | ||
{"<form action=\"javascript:alert(1)\"><input type=\"submit\"></form>"}, | ||
{"<object data=\"javascript:alert(1)\"></object>"}, | ||
{"<embed src=\"javascript:alert(1)\">"}, | ||
{"<base href=\"javascript:alert(1)//\">"}, | ||
{"<svg onload=\"alert(1)\">"}, | ||
{"<svg><script>alert(1)</script></svg>"}, | ||
{"<math><a xlink:href=\"javascript:alert(1)\">XSS</a></math>"}, | ||
{"<img src=x onerror=\"alert(String.fromCharCode(88,83,83))\">"}, | ||
{"<b onmouseover=\"alert(1)\">XSS</b>"}, | ||
{"<video><source onerror=\"javascript:alert(1)\"></video>"}, | ||
{"<details open ontoggle=\"alert(1)\">"}, | ||
{"<input onfocus=\"alert('XSS')\">"}, | ||
{"<div style=\"width: expression(alert('XSS'))\">"}, | ||
{"<meta http-equiv=\"refresh\" content=\"0;url=javascript:alert(1)\">"}, | ||
{"<link rel=\"stylesheet\" href=\"javascript:alert(1)\">"}, | ||
{"<textarea onfocus=\"alert(1)\"></textarea>"}, | ||
{"<a href=\"//example.com\" onclick=\"alert(1)\">Link</a>"}, | ||
{"<button onclick=\"alert(1)\">Click me</button>"}, | ||
{"<div style=\"background-image: url(javascript:alert(1))\">XSS</div>"}, | ||
{"<audio src=\"javascript:alert(1)\"></audio>"}, | ||
{"<marquee onstart=\"alert(1)\">XSS</marquee>"}, | ||
{"<keygen autofocus onfocus=\"alert(1)\">"}, | ||
{"<command onclick=\"alert(1)\">Click me</command>"} | ||
}); | ||
} | ||
|
||
private final String input; | ||
private final OwaspXssValidator owaspXssValidator = new OwaspXssValidator(); | ||
|
||
public OwaspXssValidatorExceptionTest(String input) { | ||
this.input = input; | ||
} | ||
|
||
@Test(expected = XssValidatorException.class) | ||
public void testValidateHttpBodyException() throws XssValidatorException { | ||
owaspXssValidator.validate(input); | ||
} | ||
} |
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
40 changes: 0 additions & 40 deletions
40
test/org/scada_lts/web/security/XssUtilsValidateHttpBodyExceptionTest.java
This file was deleted.
Oops, something went wrong.