Skip to content

Commit 5db31cf

Browse files
xeno6696kwwall
andauthored
Some of this appears to have been already covered in PR #663 (#670)
* Signed key history for MATT SEIL begins here. * Signed key history for MATT SEIL begins here. Fixed email typo. * Revert "Signed key history for MATT SEIL begins here. Fixed email typo." This reverts commit 87c4c4e. * created file on main. * Deleted foo.txt * Bump release to new patch version #. * #661 Added ability to generate OSGi metadata with the command 'mvn org.apache.felix:maven-bundle-plugin:manifest'. * Updated to AntiSamy 1.6.5. * Revert "#661 Added ability to generate OSGi metadata with the command 'mvn org.apache.felix:maven-bundle-plugin:manifest'." This reverts commit 9fa2a53. * #656 --> Parameterized cookie name length and value to correspond with the HTTP maxes defined in esapi.properties. * Adjusted regex to allow for zero-length matches. * Added per review comments for PR #663 * #656 Finished sweep looking for headername, headervalue, and header value sizes as well as the 'Cookie' versions of those statements. Added unit tests. * #663 Fixed a missed unit test. * Antisamy 1.6.6, Antisamy regression test for analysis 1. A handful of new regression tests for other purposes in validation and encoder tests. * Attempting to fix classfile differences with antisamy dependencies. * Fixed typo on exclusion. * Added xerces exclusion to antisamy in the pom.xml * Added test cases 2 & 3. * Added test cases 2 & 3. @ignore on test case 3 from AntiSamy as the DOS is still present. * Forced my version to match Wichers. * Added a pair of unit tests for canoncialization to prove out an issue opened up on github. One of which however reminded me that we need a codec to account for UTF-8 encoding/decoding. Co-authored-by: Matt Seil <xeno6696[at]gmail.com> Co-authored-by: kwwall <[email protected]>
1 parent 086b3e8 commit 5db31cf

File tree

6 files changed

+103
-8
lines changed

6 files changed

+103
-8
lines changed

src/main/java/org/owasp/esapi/reference/AbstractAuthenticator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ protected User getUserFromSession() {
114114
*/
115115
protected DefaultUser getUserFromRememberToken() {
116116
try {
117-
String token = ESAPI.httpUtilities().getCookie(ESAPI.currentRequest(), HTTPUtilities.REMEMBER_TOKEN_COOKIE_NAME);
117+
HTTPUtilities utils =ESAPI.httpUtilities();
118+
String token = utils.getCookie(ESAPI.currentRequest(), HTTPUtilities.REMEMBER_TOKEN_COOKIE_NAME);
118119
if (token == null) return null;
119120

120121
// See Google Issue 144 regarding first URLDecode the token and THEN unsealing.

src/main/java/org/owasp/esapi/reference/DefaultHTTPUtilities.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,12 @@ public void addHeader(String name, String value) {
235235
* {@inheritDoc}
236236
*/
237237
public void addHeader(HttpServletResponse response, String name, String value) {
238+
SecurityConfiguration sc = ESAPI.securityConfiguration();
238239
try {
239240
String strippedName = StringUtilities.replaceLinearWhiteSpace(name);
240241
String strippedValue = StringUtilities.replaceLinearWhiteSpace(value);
241-
String safeName = ESAPI.validator().getValidInput("addHeader", strippedName, "HTTPHeaderName", 20, false);
242-
String safeValue = ESAPI.validator().getValidInput("addHeader", strippedValue, "HTTPHeaderValue", 500, false);
242+
String safeName = ESAPI.validator().getValidInput("addHeader", strippedName, "HTTPHeaderName", sc.getIntProp("HttpUtilities.MaxHeaderNameSize"), false);
243+
String safeValue = ESAPI.validator().getValidInput("addHeader", strippedValue, "HTTPHeaderValue", sc.getIntProp("HttpUtilities.MaxHeaderValueSize"), false);
243244
response.addHeader(safeName, safeValue);
244245
} catch (ValidationException e) {
245246
logger.warning(Logger.SECURITY_FAILURE, "Attempt to add invalid header denied", e);
@@ -464,9 +465,10 @@ public void encryptStateInCookie( Map<String,String> cleartext ) throws Encrypti
464465
*/
465466
public String getCookie( HttpServletRequest request, String name ) throws ValidationException {
466467
Cookie c = getFirstCookie( request, name );
468+
SecurityConfiguration sc = ESAPI.securityConfiguration();
467469
if ( c == null ) return null;
468470
String value = c.getValue();
469-
return ESAPI.validator().getValidInput("HTTP cookie value: " + value, value, "HTTPCookieValue", 1000, false);
471+
return ESAPI.validator().getValidInput("HTTP cookie value: " + value, value, "HTTPCookieValue", sc.getIntProp("HttpUtilities.MaxHeaderValueSize"), false);
470472
}
471473

472474
/**
@@ -656,8 +658,9 @@ private Cookie getFirstCookie(HttpServletRequest request, String name) {
656658
* {@inheritDoc}
657659
*/
658660
public String getHeader( HttpServletRequest request, String name ) throws ValidationException {
661+
SecurityConfiguration sc = ESAPI.securityConfiguration();
659662
String value = request.getHeader(name);
660-
return ESAPI.validator().getValidInput("HTTP header value: " + value, value, "HTTPHeaderValue", 150, false);
663+
return ESAPI.validator().getValidInput("HTTP header value: " + value, value, "HTTPHeaderValue", sc.getIntProp("HttpUtilities.MaxHeaderValueSize"), false);
661664
}
662665

663666

src/test/java/org/owasp/esapi/reference/EncoderTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ public void testCanonicalize() throws EncodingException {
212212
assertEquals( "<", instance.canonicalize("&lT;"));
213213
assertEquals( "<", instance.canonicalize("&Lt;"));
214214
assertEquals( "<", instance.canonicalize("&LT;"));
215+
assertEquals( "&", instance.canonicalize("&amp"));
216+
assertEquals( "〈", instance.canonicalize("&lang"));
215217

216218
assertEquals( "<script>alert(\"hello\");</script>", instance.canonicalize("%3Cscript%3Ealert%28%22hello%22%29%3B%3C%2Fscript%3E") );
217219
assertEquals( "<script>alert(\"hello\");</script>", instance.canonicalize("%3Cscript&#x3E;alert%28%22hello&#34%29%3B%3C%2Fscript%3E", false) );
@@ -912,11 +914,28 @@ public void testHtmlEncodeStrSurrogatePair()
912914

913915
public void testHtmlDecodeHexEntititesSurrogatePair()
914916
{
915-
HTMLEntityCodec htmlCodec = new HTMLEntityCodec();
917+
HTMLEntityCodec htmlCodec = new HTMLEntityCodec();
916918
String expected = new String (new int[]{0x2f804}, 0, 1);
917919
assertEquals( expected, htmlCodec.decode("&#194564;") );
918920
assertEquals( expected, htmlCodec.decode("&#x2f804;") );
919921
}
920922

923+
public void testUnicodeCanonicalize() {
924+
Encoder e = ESAPI.encoder();
925+
String input = "测试";
926+
String expected = "测试";
927+
String output = e.canonicalize(input);
928+
assertEquals(expected, output);
929+
}
930+
931+
public void testUnicodeCanonicalizePercentEncoding() {
932+
//TODO: We need to find a way to specify the encoding type for percent encoding.
933+
//I believe by default we're doing Latin-1 and we really should be doing UTF-8
934+
Encoder e = ESAPI.encoder();
935+
String input = "%E6%B5%8B%E8%AF%95";
936+
String expected = "测试";
937+
String output = e.canonicalize(input);
938+
assertNotSame(expected, output);
939+
}
921940
}
922941

src/test/java/org/owasp/esapi/reference/HTTPUtilitiesTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.owasp.esapi.http.MockHttpServletResponse;
4646
import org.owasp.esapi.http.MockHttpSession;
4747
import org.owasp.esapi.util.FileTestUtils;
48+
import org.owasp.esapi.util.TestUtils;
4849

4950
import junit.framework.Test;
5051
import junit.framework.TestCase;
@@ -372,6 +373,27 @@ public void testSetCookie() {
372373
instance.addCookie( response, new Cookie( "test3", "tes<t3" ) );
373374
assertTrue(response.getHeaderNames().size() == 2);
374375
}
376+
377+
/**
378+
* Test of setCookie method, of class org.owasp.esapi.HTTPUtilities.
379+
* Validation failures should prevent cookies being added.
380+
*/
381+
public void testSetCookieExceedingMaxValueAndName() {
382+
HTTPUtilities instance = ESAPI.httpUtilities();
383+
MockHttpServletResponse response = new MockHttpServletResponse();
384+
assertTrue(response.getHeaderNames().isEmpty());
385+
//request.addParameter(TestUtils.generateStringOfLength(32), "pass");
386+
instance.addCookie( response, new Cookie( TestUtils.generateStringOfLength(32), "pass" ) );
387+
assertTrue(response.getHeaderNames().size() == 1);
388+
389+
instance.addCookie( response, new Cookie( "pass", TestUtils.generateStringOfLength(32) ) );
390+
assertTrue(response.getHeaderNames().size() == 2);
391+
instance.addCookie( response, new Cookie( TestUtils.generateStringOfLength(5000), "fail" ) );
392+
assertTrue(response.getHeaderNames().size() == 2);
393+
instance.addCookie( response, new Cookie( "fail", TestUtils.generateStringOfLength(5001) ) );
394+
assertTrue(response.getHeaderNames().size() == 2);
395+
}
396+
375397

376398
/**
377399
*

src/test/java/org/owasp/esapi/reference/ValidatorTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,15 +1040,13 @@ public void testHeaderLengthChecks(){
10401040

10411041
@Test
10421042
public void testGetHeaderNames() {
1043-
//testing Validator.HTTPHeaderName
10441043
MockHttpServletRequest request = new MockHttpServletRequest();
10451044
SecurityWrapperRequest safeRequest = new SecurityWrapperRequest(request);
10461045
request.addHeader("d-49653-p", "pass");
10471046
request.addHeader("<img ", "fail");
10481047
// Note: Max length in ESAPI.properties as per
10491048
// Validator.HTTPHeaderName regex is 256, but upper
10501049
// bound is configurable by the property HttpUtilities.MaxHeaderNameSize
1051-
SecurityConfiguration sc = ESAPI.securityConfiguration();
10521050
request.addHeader(TestUtils.generateStringOfLength(255), "pass");
10531051
request.addHeader(TestUtils.generateStringOfLength(257), "fail");
10541052
assertEquals(2, Collections.list(safeRequest.getHeaderNames()).size());
@@ -1130,5 +1128,13 @@ public void testavaloqLooseSafeString(){
11301128
boolean isValid = v.isValidInput("RegexString", "&quot;test&quot;", "avaloqLooseSafeString", 2147483647, true, true);
11311129
assertFalse(isValid);
11321130
}
1131+
1132+
@Test
1133+
public void testStandardHeader() {
1134+
Validator v = ESAPI.validator();
1135+
boolean expected = false;
1136+
boolean result = v.isValidInput("HTTPHeaderValue ", "[email protected]", "HTTPHeaderValue", 2147483647, true, true);
1137+
assertEquals(expected, result);
1138+
}
11331139
}
11341140

src/test/java/org/owasp/esapi/reference/validation/HTMLValidationRuleCleanTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import org.owasp.esapi.ValidationErrorList;
2323
import org.owasp.esapi.ValidationRule;
2424
import org.owasp.esapi.Validator;
25+
import org.owasp.esapi.errors.IntrusionException;
2526
import org.owasp.esapi.errors.ValidationException;
2627
import org.owasp.esapi.filters.SecurityWrapperRequest;
2728
import org.owasp.esapi.reference.validation.HTMLValidationRule;
2829

2930
import org.junit.Test;
3031
import org.junit.Before;
32+
import org.junit.Ignore;
3133
import org.junit.After;
3234
import org.junit.Rule;
3335
import org.junit.rules.ExpectedException;
@@ -153,4 +155,46 @@ public void testIsValidSafeHTML() {
153155
assertTrue(errors.size() == 0);
154156

155157
}
158+
159+
@Test
160+
public void testAntiSamyRegressionCDATAWithJavascriptURL() throws Exception {
161+
Validator instance = ESAPI.validator();
162+
ValidationErrorList errors = new ValidationErrorList();
163+
String input = "<style/>b<![cdata[</style><a href=javascript:alert(1)>test";
164+
assertTrue(instance.isValidSafeHTML("test7", input, 100, false, errors));
165+
String expected = "b&lt;/style&gt;&lt;a href=javascript:alert(1)&gt;test";
166+
String output = instance.getValidSafeHTML("javascript Link", input, 250, false);
167+
assertEquals(expected, output);
168+
assertTrue(errors.size() == 0);
169+
170+
}
171+
172+
@Test
173+
public void testScriptTagAfterStyleClosing() throws Exception {
174+
Validator instance = ESAPI.validator();
175+
ValidationErrorList errors = new ValidationErrorList();
176+
String input = "<select<style/>W<xmp<script>alert(1)</script>";
177+
assertTrue(instance.isValidSafeHTML("test7", input, 100, false, errors));
178+
String expected = "W&lt;script&gt;alert(1)&lt;/script&gt;";
179+
String output = instance.getValidSafeHTML("escaping style tag attack", input, 250, false);
180+
assertEquals(expected, output);
181+
assertTrue(errors.size() == 0);
182+
183+
}
184+
185+
@Test
186+
@Ignore
187+
public void testNekoDOSWithAnHTMLComment() throws Exception {
188+
/**
189+
* FIXME: This unit test needs to pass before the next ESAPI release.
190+
*/
191+
Validator instance = ESAPI.validator();
192+
ValidationErrorList errors = new ValidationErrorList();
193+
String input = "<!--><?a/";
194+
assertTrue(instance.isValidSafeHTML("test7", input, 100, false, errors));
195+
String expected = "&#x3C;!--&#x3E;&#x3C;?a/";
196+
String output = instance.getValidSafeHTML("escaping style tag attack", input, 250, false);
197+
assertEquals(expected, output);
198+
assertTrue(errors.size() == 0);
199+
}
156200
}

0 commit comments

Comments
 (0)