-
Notifications
You must be signed in to change notification settings - Fork 369
Fixed issue #310 #541
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
Merged
Merged
Fixed issue #310 #541
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ba524e2
issue-#310-coding-done
HJW8472 459e82e
issue-#310-fixed-testclases-package
HJW8472 159e966
issue-ESAPI#310-tested-and-cleaned-code
HJW8472 4624c23
issue-ESAPI#310-removed-an-useless-import
HJW8472 de3d570
Merge pull request #1 from HJW8472/issue-#310
HJW8472 24ac423
issue-#310-fixed-review-comments
HJW8472 1413bbb
Merge pull request #2 from HJW8472/issue-#310
HJW8472 40a6322
issue-ESAPI#310-filename-configurable-and-added-testclass
HJW8472 dffb963
Merge pull request #3 from HJW8472/issue-#310
HJW8472 08e53af
Updated-ESAPI.properties-with-comment
HJW8472 d06e438
Merge pull request #4 from HJW8472/issue-#310
HJW8472 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
171 changes: 171 additions & 0 deletions
171
src/test/java/org/owasp/esapi/reference/validation/HTMLValidationRuleClasspathTest.java
This file contains hidden or 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,171 @@ | ||
/** | ||
* OWASP Enterprise Security API (ESAPI) | ||
* | ||
* This file is part of the Open Web Application Security Project (OWASP) | ||
* Enterprise Security API (ESAPI) project. For details, please see | ||
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>. | ||
* | ||
* Copyright (c) 2019 - The OWASP Foundation | ||
* | ||
* The ESAPI is published by OWASP under the BSD license. You should read and accept the | ||
* LICENSE before you use, modify, and/or redistribute this software. | ||
* | ||
* @author [email protected] | ||
* @since 2019 | ||
*/ | ||
package org.owasp.esapi.reference.validation; | ||
|
||
import org.owasp.esapi.ESAPI; | ||
import org.owasp.esapi.SecurityConfiguration; | ||
import org.owasp.esapi.SecurityConfigurationWrapper; | ||
import org.owasp.esapi.ValidationErrorList; | ||
import org.owasp.esapi.ValidationRule; | ||
import org.owasp.esapi.Validator; | ||
import org.owasp.esapi.errors.ValidationException; | ||
import org.owasp.esapi.reference.validation.HTMLValidationRule; | ||
|
||
import org.junit.Test; | ||
import org.junit.Before; | ||
import org.junit.After; | ||
import org.junit.Rule; | ||
import org.junit.rules.ExpectedException; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* The Class HTMLValidationRuleThrowsTest. | ||
* | ||
* Based on original test cases, testGetValidSafeHTML() and | ||
* testIsValidSafeHTML() from ValidatorTest by | ||
* Mike Fauzy ([email protected]) and | ||
* Jeff Williams ([email protected]) | ||
* that were originally part of src/test/java/org/owasp/esapi/reference/ValidatorTest.java. | ||
* | ||
* This class tests the cases where the new ESAPI.property | ||
* Validator.HtmlValidationAction | ||
* is set to "throw", which causes certain calls to | ||
* ESAPI.validator().getValidSafeHTML() or ESAPI.validator().isValidSafeHTML() | ||
* to throw a ValidationException rather than simply logging a warning and returning | ||
* the cleansed (sanitizied) output when certain unsafe input is encountered. | ||
*/ | ||
public class HTMLValidationRuleClasspathTest { | ||
private static class ConfOverride extends SecurityConfigurationWrapper { | ||
private String desiredReturnAction = "clean"; | ||
private String desiredReturnConfigurationFile = "antisamy-esapi.xml"; | ||
|
||
ConfOverride(SecurityConfiguration orig, String desiredReturnAction, String desiredReturnConfigurationFile) { | ||
super(orig); | ||
this.desiredReturnAction = desiredReturnAction; | ||
this.desiredReturnConfigurationFile = desiredReturnConfigurationFile; | ||
} | ||
|
||
@Override | ||
public String getStringProp(String propName) { | ||
// Would it be better making this file a static import? | ||
if ( propName.equals( org.owasp.esapi.reference.DefaultSecurityConfiguration.VALIDATOR_HTML_VALIDATION_ACTION ) ) { | ||
return desiredReturnAction; | ||
} else if ( propName.equals( org.owasp.esapi.reference.DefaultSecurityConfiguration.VALIDATOR_HTML_VALIDATION_CONFIGURATION_FILE ) ) { | ||
return desiredReturnConfigurationFile; | ||
} else { | ||
return super.getStringProp( propName ); | ||
} | ||
} | ||
} | ||
|
||
// Must be public! | ||
@Rule | ||
public ExpectedException thrownEx = ExpectedException.none(); | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
ESAPI.override(null); | ||
thrownEx = ExpectedException.none(); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
ESAPI.override( | ||
new ConfOverride( ESAPI.securityConfiguration(), "throw", "antisamy-esapi-CP.xml" ) | ||
); | ||
|
||
} | ||
|
||
@Test | ||
public void testGetValid() throws Exception { | ||
System.out.println("getValidCP"); | ||
Validator instance = ESAPI.validator(); | ||
HTMLValidationRule rule = new HTMLValidationRule("testCP"); | ||
ESAPI.validator().addRule(rule); | ||
|
||
thrownEx.expect(ValidationException.class); | ||
thrownEx.expectMessage("test: Invalid HTML input"); | ||
|
||
instance.getRule("testCP").getValid("test", "Test. <script>alert(document.cookie)</script>"); | ||
} | ||
|
||
@Test | ||
public void testGetValidSafeHTML() throws Exception { | ||
System.out.println("getValidSafeHTML"); | ||
Validator instance = ESAPI.validator(); | ||
|
||
HTMLValidationRule rule = new HTMLValidationRule("test"); | ||
ESAPI.validator().addRule(rule); | ||
|
||
String[] testInput = { | ||
// These first two don't cause AntiSamy to throw. | ||
// "Test. <a href=\"http://www.aspectsecurity.com\">Aspect Security</a>", | ||
// "Test. <<div on<script></script>load=alert()", | ||
"Test. <script>alert(document.cookie)</script>", | ||
"Test. <script>alert(document.cookie)</script>", | ||
"Test. <div style={xss:expression(xss)}>b</div>", | ||
"Test. <s%00cript>alert(document.cookie)</script>", | ||
"Test. <s\tcript>alert(document.cookie)</script>", | ||
"Test. <s\tcript>alert(document.cookie)</script>" | ||
}; | ||
|
||
int errors = 0; | ||
for( int i = 0; i < testInput.length; i++ ) { | ||
try { | ||
String result = instance.getValidSafeHTML("test", testInput[i], 100, false); | ||
errors++; | ||
System.out.println("testGetValidSafeHTML(): testInput '" + testInput[i] + "' failed to throw."); | ||
} | ||
catch( ValidationException vex ) { | ||
System.out.println("testGetValidSafeHTML(): testInput '" + testInput[i] + "' returned:"); | ||
System.out.println("\t" + i + ": logMsg =" + vex.getLogMessage()); | ||
assertEquals( vex.getUserMessage(), "test: Invalid HTML input"); | ||
} | ||
catch( Exception ex ) { | ||
errors++; | ||
System.out.println("testGetValidSafeHTML(): testInput '" + testInput[i] + | ||
"' threw wrong exception type: " + ex.getClass().getName() ); | ||
} | ||
} | ||
|
||
if ( errors > 0 ) { | ||
fail("testGetValidSafeHTML() encountered " + errors + " failures."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIsValidSafeHTML() { | ||
System.out.println("isValidSafeHTML"); | ||
Validator instance = ESAPI.validator(); | ||
thrownEx = ExpectedException.none(); // Not expecting any exceptions here. | ||
|
||
assertTrue(instance.isValidSafeHTML("test", "<b>Jeff</b>", 100, false)); | ||
assertTrue(instance.isValidSafeHTML("test", "<a href=\"http://www.aspectsecurity.com\">Aspect Security</a>", 100, false)); | ||
assertFalse(instance.isValidSafeHTML("test", "Test. <script>alert(document.cookie)</script>", 100, false)); | ||
assertFalse(instance.isValidSafeHTML("test", "Test. <div style={xss:expression(xss)}>", 100, false)); | ||
assertFalse(instance.isValidSafeHTML("test", "Test. <s%00cript>alert(document.cookie)</script>", 100, false)); | ||
assertFalse(instance.isValidSafeHTML("test", "Test. <s\tcript>alert(document.cookie)</script>", 100, false)); | ||
assertFalse(instance.isValidSafeHTML("test", "Test. <s\r\n\0cript>alert(document.cookie)</script>", 100, false)); | ||
|
||
ValidationErrorList errors = new ValidationErrorList(); | ||
assertFalse(instance.isValidSafeHTML("test1", "Test. <script>alert(document.cookie)</script>", 100, false, errors)); | ||
assertFalse(instance.isValidSafeHTML("test2", "Test. <div style={xss:expression(xss)}>", 100, false, errors)); | ||
assertFalse(instance.isValidSafeHTML("test3", "Test. <s%00cript>alert(document.cookie)</script>", 100, false, errors)); | ||
assertFalse(instance.isValidSafeHTML("test4", "Test. <s\tcript>alert(document.cookie)</script>", 100, false, errors)); | ||
assertFalse(instance.isValidSafeHTML("test5", "Test. <s\r\n\0cript>alert(document.cookie)</script>", 100, false, errors)); | ||
assertTrue( errors.size() == 5 ); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
* @author [email protected] | ||
* @since 2019 | ||
*/ | ||
package org.owasp.esapi.reference; | ||
package org.owasp.esapi.reference.validation; | ||
|
||
import org.owasp.esapi.ESAPI; | ||
import org.owasp.esapi.EncoderConstants; | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
* @author [email protected] | ||
* @since 2019 | ||
*/ | ||
package org.owasp.esapi.reference; | ||
package org.owasp.esapi.reference.validation; | ||
|
||
import org.owasp.esapi.ESAPI; | ||
import org.owasp.esapi.SecurityConfiguration; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.