Skip to content

Commit c46331f

Browse files
author
Kad DEMBELE
committed
Merge pull request #1 from ESAPI/master
Merge of ESAPI/esapi-java-legacy into demkada/esapi-java-legacy
2 parents 948592a + b4bd4e3 commit c46331f

6 files changed

Lines changed: 153 additions & 9 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
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
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,9 @@ private void log(Level level, EventType type, String message, Throwable throwabl
447447
}
448448

449449
// log the message
450-
log(level, "[" + typeInfo + getUserInfo() + " -> " + appInfo + "] " + clean, throwable);
450+
// Fix for https://code.google.com/p/owasp-esapi-java/issues/detail?id=268
451+
// need to pass callerFQCN so the log is not generated as if it were always generated from this wrapper class
452+
log(Log4JLogger.class.getName(), level, "[" + typeInfo + getUserInfo() + " -> " + appInfo + "] " + clean, throwable);
451453
}
452454

453455
/**

src/main/java/org/owasp/esapi/reference/validation/BaseValidationRule.java

Lines changed: 6 additions & 2 deletions
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
}

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@
1515
*/
1616
package org.owasp.esapi.reference;
1717

18-
import java.io.IOException;
19-
import java.util.Arrays;
20-
2118
import junit.framework.Test;
2219
import junit.framework.TestCase;
2320
import junit.framework.TestSuite;
24-
21+
import org.apache.log4j.Appender;
22+
import org.apache.log4j.Layout;
23+
import org.apache.log4j.WriterAppender;
24+
import org.apache.log4j.spi.LoggingEvent;
2525
import org.owasp.esapi.ESAPI;
2626
import org.owasp.esapi.Logger;
2727
import org.owasp.esapi.errors.AuthenticationException;
2828
import org.owasp.esapi.errors.ValidationException;
2929
import org.owasp.esapi.http.MockHttpServletRequest;
3030
import org.owasp.esapi.http.MockHttpServletResponse;
3131

32+
import java.io.IOException;
33+
import java.io.StringWriter;
34+
import java.util.Arrays;
35+
3236
/**
3337
* The Class LoggerTest.
3438
*
@@ -460,4 +464,37 @@ public void testAlways() {
460464
}
461465
}
462466

467+
/**
468+
* Validation for issue: https://code.google.com/p/owasp-esapi-java/issues/detail?id=268
469+
* Line number must be the line of the caller and not of the wrapper.
470+
*/
471+
public void testLine() {
472+
final String message = "testing only";
473+
StringWriter sw = new StringWriter();
474+
Layout layout = new Layout() {
475+
@Override
476+
public String format(LoggingEvent event) {
477+
assertEquals("the calling class is this test class", Log4JLoggerTest.class.getName(),event.getLocationInformation().getClassName());
478+
return message;
479+
}
480+
481+
@Override
482+
public boolean ignoresThrowable() {
483+
return false;
484+
}
485+
486+
@Override
487+
public void activateOptions() {
488+
489+
}
490+
};
491+
Appender appender = new WriterAppender(layout, sw);
492+
log4JLogger.addAppender(appender);
493+
try {
494+
log4JLogger.fatal("testLine");
495+
assertEquals("message not generated as expected", message, sw.toString());
496+
} finally {
497+
log4JLogger.removeAppender(appender);
498+
}
499+
}
463500
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public void testIsInvalidFilename() {
323323
public void testIsValidDate() {
324324
System.out.println("isValidDate");
325325
Validator instance = ESAPI.validator();
326-
DateFormat format = SimpleDateFormat.getDateInstance();
326+
DateFormat format = SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM, Locale.US);
327327
assertTrue(instance.isValidDate("datetest1", "September 11, 2001", format, true));
328328
assertFalse(instance.isValidDate("datetest2", null, format, false));
329329
assertFalse(instance.isValidDate("datetest3", "", format, false));
Lines changed: 101 additions & 0 deletions
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)