Skip to content

Commit b4bd4e3

Browse files
committed
Merge pull request #1 from karansanwal/master
Fix Issue # 195 : BaseValidationRule.assertValid(String context, String ...
2 parents d9c4fbe + 95baa9a commit b4bd4e3

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

Diff for: src/main/java/org/owasp/esapi/reference/DefaultHTTPUtilities.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ public void sendForward( String location ) throws AccessControlException,Servle
811811
public void sendRedirect(HttpServletResponse response, String location) throws AccessControlException, IOException {
812812
if (!ESAPI.validator().isValidRedirectLocation("Redirect", location, false)) {
813813
logger.fatal(Logger.SECURITY_FAILURE, "Bad redirect location: " + location);
814-
throw new IOException("Redirect failed");
814+
throw new AccessControlException("Redirect failed");
815815
}
816816
response.sendRedirect(location);
817817
}

Diff for: src/main/java/org/owasp/esapi/reference/validation/BaseValidationRule.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public final void setEncoder( Encoder encoder ) {
8989
* {@inheritDoc}
9090
*/
9191
public void assertValid( String context, String input ) throws ValidationException {
92-
getValid( context, input, null );
92+
getValid( context, input );
9393
}
9494

9595
/**
@@ -100,7 +100,11 @@ public Object getValid( String context, String input, ValidationErrorList errorL
100100
try {
101101
valid = getValid( context, input );
102102
} catch (ValidationException e) {
103-
errorList.addError(context, e);
103+
if( errorList == null) {
104+
throw e;
105+
} else {
106+
errorList.addError(context, e);
107+
}
104108
}
105109
return valid;
106110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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) 2007 - 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+
* @author Ben Sleek <a href="http://www.spartasystems.com">Sparta Systems</a>
14+
* @created 2015
15+
*/
16+
package org.owasp.esapi.reference.validation;
17+
18+
import junit.framework.Test;
19+
import junit.framework.TestCase;
20+
import junit.framework.TestSuite;
21+
22+
import org.owasp.esapi.Encoder;
23+
import org.owasp.esapi.errors.ValidationException;
24+
25+
public class BaseValidationRuleTest extends TestCase {
26+
27+
/**
28+
* Instantiates a new base validation rule test.
29+
*
30+
* @param testName
31+
* the test name
32+
*/
33+
public BaseValidationRuleTest(String testName) {
34+
super(testName);
35+
}
36+
37+
/**
38+
* {@inheritDoc}
39+
*
40+
* @throws Exception
41+
*/
42+
protected void setUp() throws Exception {
43+
// none
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*
49+
* @throws Exception
50+
*/
51+
protected void tearDown() throws Exception {
52+
// none
53+
}
54+
55+
/**
56+
* Suite.
57+
*
58+
* @return the test
59+
*/
60+
public static Test suite() {
61+
TestSuite suite = new TestSuite(BaseValidationRuleTest.class);
62+
return suite;
63+
}
64+
65+
/**
66+
* Verifies assertValid throws ValidationException on invalid input
67+
* Validates fix for Google issue #195
68+
*
69+
* @throws ValidationException
70+
*/
71+
public void testAssertValid() throws ValidationException {
72+
SampleValidationRule rule = new SampleValidationRule("UnitTest");
73+
try {
74+
rule.assertValid("testcontext", "badinput");
75+
fail();
76+
} catch (ValidationException e) {
77+
// success
78+
}
79+
}
80+
81+
public class SampleValidationRule extends BaseValidationRule {
82+
83+
public SampleValidationRule(String typeName, Encoder encoder) {
84+
super(typeName, encoder);
85+
}
86+
87+
public SampleValidationRule(String typeName) {
88+
super(typeName);
89+
}
90+
91+
@Override
92+
protected Object sanitize(String context, String input) {
93+
return null;
94+
}
95+
96+
public Object getValid(String context, String input) throws ValidationException {
97+
throw new ValidationException("Demonstration Exception", "Demonstration Exception");
98+
}
99+
100+
}
101+
}

0 commit comments

Comments
 (0)