|
| 1 | +/** |
| 2 | + * OWASP Enterprise Security API (ESAPI) |
| 3 | + * |
| 4 | + * This file is part of the Open Web Application Security Project (OWASP) |
| 5 | + * Enterprise Security API (ESAPI) project. For details, please see |
| 6 | + * <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>. |
| 7 | + * |
| 8 | + * Copyright (c) 2019 - The OWASP Foundation |
| 9 | + * |
| 10 | + * The ESAPI is published by OWASP under the BSD license. You should read and accept the |
| 11 | + * LICENSE before you use, modify, and/or redistribute this software. |
| 12 | + * |
| 13 | + |
| 14 | + * @since 2019 |
| 15 | + */ |
| 16 | +package org.owasp.esapi.reference.validation; |
| 17 | + |
| 18 | +import org.owasp.esapi.ESAPI; |
| 19 | +import org.owasp.esapi.SecurityConfiguration; |
| 20 | +import org.owasp.esapi.SecurityConfigurationWrapper; |
| 21 | +import org.owasp.esapi.ValidationErrorList; |
| 22 | +import org.owasp.esapi.ValidationRule; |
| 23 | +import org.owasp.esapi.Validator; |
| 24 | +import org.owasp.esapi.errors.ValidationException; |
| 25 | +import org.owasp.esapi.reference.validation.HTMLValidationRule; |
| 26 | + |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Rule; |
| 31 | +import org.junit.rules.ExpectedException; |
| 32 | +import static org.junit.Assert.*; |
| 33 | + |
| 34 | +/** |
| 35 | + * The Class HTMLValidationRuleThrowsTest. |
| 36 | + * |
| 37 | + * Based on original test cases, testGetValidSafeHTML() and |
| 38 | + * testIsValidSafeHTML() from ValidatorTest by |
| 39 | + * Mike Fauzy ([email protected]) and |
| 40 | + * Jeff Williams ([email protected]) |
| 41 | + * that were originally part of src/test/java/org/owasp/esapi/reference/ValidatorTest.java. |
| 42 | + * |
| 43 | + * This class tests the cases where the new ESAPI.property |
| 44 | + * Validator.HtmlValidationAction |
| 45 | + * is set to "throw", which causes certain calls to |
| 46 | + * ESAPI.validator().getValidSafeHTML() or ESAPI.validator().isValidSafeHTML() |
| 47 | + * to throw a ValidationException rather than simply logging a warning and returning |
| 48 | + * the cleansed (sanitizied) output when certain unsafe input is encountered. |
| 49 | + */ |
| 50 | +public class HTMLValidationRuleClasspathTest { |
| 51 | + private static class ConfOverride extends SecurityConfigurationWrapper { |
| 52 | + private String desiredReturnAction = "clean"; |
| 53 | + private String desiredReturnConfigurationFile = "antisamy-esapi.xml"; |
| 54 | + |
| 55 | + ConfOverride(SecurityConfiguration orig, String desiredReturnAction, String desiredReturnConfigurationFile) { |
| 56 | + super(orig); |
| 57 | + this.desiredReturnAction = desiredReturnAction; |
| 58 | + this.desiredReturnConfigurationFile = desiredReturnConfigurationFile; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getStringProp(String propName) { |
| 63 | + // Would it be better making this file a static import? |
| 64 | + if ( propName.equals( org.owasp.esapi.reference.DefaultSecurityConfiguration.VALIDATOR_HTML_VALIDATION_ACTION ) ) { |
| 65 | + return desiredReturnAction; |
| 66 | + } else if ( propName.equals( org.owasp.esapi.reference.DefaultSecurityConfiguration.VALIDATOR_HTML_VALIDATION_CONFIGURATION_FILE ) ) { |
| 67 | + return desiredReturnConfigurationFile; |
| 68 | + } else { |
| 69 | + return super.getStringProp( propName ); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Must be public! |
| 75 | + @Rule |
| 76 | + public ExpectedException thrownEx = ExpectedException.none(); |
| 77 | + |
| 78 | + @After |
| 79 | + public void tearDown() throws Exception { |
| 80 | + ESAPI.override(null); |
| 81 | + thrownEx = ExpectedException.none(); |
| 82 | + } |
| 83 | + |
| 84 | + @Before |
| 85 | + public void setUp() throws Exception { |
| 86 | + ESAPI.override( |
| 87 | + new ConfOverride( ESAPI.securityConfiguration(), "throw", "antisamy-esapi-CP.xml" ) |
| 88 | + ); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testGetValid() throws Exception { |
| 94 | + System.out.println("getValidCP"); |
| 95 | + Validator instance = ESAPI.validator(); |
| 96 | + HTMLValidationRule rule = new HTMLValidationRule("testCP"); |
| 97 | + ESAPI.validator().addRule(rule); |
| 98 | + |
| 99 | + thrownEx.expect(ValidationException.class); |
| 100 | + thrownEx.expectMessage("test: Invalid HTML input"); |
| 101 | + |
| 102 | + instance.getRule("testCP").getValid("test", "Test. <script>alert(document.cookie)</script>"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testGetValidSafeHTML() throws Exception { |
| 107 | + System.out.println("getValidSafeHTML"); |
| 108 | + Validator instance = ESAPI.validator(); |
| 109 | + |
| 110 | + HTMLValidationRule rule = new HTMLValidationRule("test"); |
| 111 | + ESAPI.validator().addRule(rule); |
| 112 | + |
| 113 | + String[] testInput = { |
| 114 | + // These first two don't cause AntiSamy to throw. |
| 115 | + // "Test. <a href=\"http://www.aspectsecurity.com\">Aspect Security</a>", |
| 116 | + // "Test. <<div on<script></script>load=alert()", |
| 117 | + "Test. <script>alert(document.cookie)</script>", |
| 118 | + "Test. <script>alert(document.cookie)</script>", |
| 119 | + "Test. <div style={xss:expression(xss)}>b</div>", |
| 120 | + "Test. <s%00cript>alert(document.cookie)</script>", |
| 121 | + "Test. <s\tcript>alert(document.cookie)</script>", |
| 122 | + "Test. <s\tcript>alert(document.cookie)</script>" |
| 123 | + }; |
| 124 | + |
| 125 | + int errors = 0; |
| 126 | + for( int i = 0; i < testInput.length; i++ ) { |
| 127 | + try { |
| 128 | + String result = instance.getValidSafeHTML("test", testInput[i], 100, false); |
| 129 | + errors++; |
| 130 | + System.out.println("testGetValidSafeHTML(): testInput '" + testInput[i] + "' failed to throw."); |
| 131 | + } |
| 132 | + catch( ValidationException vex ) { |
| 133 | + System.out.println("testGetValidSafeHTML(): testInput '" + testInput[i] + "' returned:"); |
| 134 | + System.out.println("\t" + i + ": logMsg =" + vex.getLogMessage()); |
| 135 | + assertEquals( vex.getUserMessage(), "test: Invalid HTML input"); |
| 136 | + } |
| 137 | + catch( Exception ex ) { |
| 138 | + errors++; |
| 139 | + System.out.println("testGetValidSafeHTML(): testInput '" + testInput[i] + |
| 140 | + "' threw wrong exception type: " + ex.getClass().getName() ); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if ( errors > 0 ) { |
| 145 | + fail("testGetValidSafeHTML() encountered " + errors + " failures."); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testIsValidSafeHTML() { |
| 151 | + System.out.println("isValidSafeHTML"); |
| 152 | + Validator instance = ESAPI.validator(); |
| 153 | + thrownEx = ExpectedException.none(); // Not expecting any exceptions here. |
| 154 | + |
| 155 | + assertTrue(instance.isValidSafeHTML("test", "<b>Jeff</b>", 100, false)); |
| 156 | + assertTrue(instance.isValidSafeHTML("test", "<a href=\"http://www.aspectsecurity.com\">Aspect Security</a>", 100, false)); |
| 157 | + assertFalse(instance.isValidSafeHTML("test", "Test. <script>alert(document.cookie)</script>", 100, false)); |
| 158 | + assertFalse(instance.isValidSafeHTML("test", "Test. <div style={xss:expression(xss)}>", 100, false)); |
| 159 | + assertFalse(instance.isValidSafeHTML("test", "Test. <s%00cript>alert(document.cookie)</script>", 100, false)); |
| 160 | + assertFalse(instance.isValidSafeHTML("test", "Test. <s\tcript>alert(document.cookie)</script>", 100, false)); |
| 161 | + assertFalse(instance.isValidSafeHTML("test", "Test. <s\r\n\0cript>alert(document.cookie)</script>", 100, false)); |
| 162 | + |
| 163 | + ValidationErrorList errors = new ValidationErrorList(); |
| 164 | + assertFalse(instance.isValidSafeHTML("test1", "Test. <script>alert(document.cookie)</script>", 100, false, errors)); |
| 165 | + assertFalse(instance.isValidSafeHTML("test2", "Test. <div style={xss:expression(xss)}>", 100, false, errors)); |
| 166 | + assertFalse(instance.isValidSafeHTML("test3", "Test. <s%00cript>alert(document.cookie)</script>", 100, false, errors)); |
| 167 | + assertFalse(instance.isValidSafeHTML("test4", "Test. <s\tcript>alert(document.cookie)</script>", 100, false, errors)); |
| 168 | + assertFalse(instance.isValidSafeHTML("test5", "Test. <s\r\n\0cript>alert(document.cookie)</script>", 100, false, errors)); |
| 169 | + assertTrue( errors.size() == 5 ); |
| 170 | + } |
| 171 | +} |
0 commit comments