Skip to content

Commit d869303

Browse files
author
mzilu
committed
Resolve #509 - Properly throw exception when HTML fails
1 parent 197e2db commit d869303

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/**
22
* OWASP Enterprise Security API (ESAPI)
3-
*
3+
*
44
* This file is part of the Open Web Application Security Project (OWASP)
55
* Enterprise Security API (ESAPI) project. For details, please see
66
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
77
*
88
* Copyright (c) 2007 - The OWASP Foundation
9-
*
9+
*
1010
* The ESAPI is published by OWASP under the BSD license. You should read and accept the
1111
* LICENSE before you use, modify, and/or redistribute this software.
12-
*
12+
*
1313
* @author Jeff Williams <a href="http://www.aspectsecurity.com">Aspect Security</a>
1414
* @created 2007
1515
*/
@@ -35,18 +35,18 @@
3535
/**
3636
* A validator performs syntax and possibly semantic validation of a single
3737
* piece of data from an untrusted source.
38-
*
38+
*
3939
* @author Jeff Williams (jeff.williams .at. aspectsecurity.com) <a
4040
* href="http://www.aspectsecurity.com">Aspect Security</a>
4141
* @since June 1, 2007
4242
* @see org.owasp.esapi.Validator
4343
*/
4444
public class HTMLValidationRule extends StringValidationRule {
45-
45+
4646
/** OWASP AntiSamy markup verification policy */
4747
private static Policy antiSamyPolicy = null;
48-
private static final Logger LOGGER = ESAPI.getLogger( "HTMLValidationRule" );
49-
48+
private static final Logger LOGGER = ESAPI.getLogger( "HTMLValidationRule" );
49+
5050
static {
5151
InputStream resourceStream = null;
5252
try {
@@ -66,23 +66,23 @@ public class HTMLValidationRule extends StringValidationRule {
6666
public HTMLValidationRule( String typeName ) {
6767
super( typeName );
6868
}
69-
69+
7070
public HTMLValidationRule( String typeName, Encoder encoder ) {
7171
super( typeName, encoder );
7272
}
7373

7474
public HTMLValidationRule( String typeName, Encoder encoder, String whitelistPattern ) {
7575
super( typeName, encoder, whitelistPattern );
7676
}
77-
77+
7878
/**
7979
* {@inheritDoc}
8080
*/
8181
@Override
8282
public String getValid( String context, String input ) throws ValidationException {
8383
return invokeAntiSamy( context, input );
8484
}
85-
85+
8686
/**
8787
* {@inheritDoc}
8888
*/
@@ -105,20 +105,27 @@ private String invokeAntiSamy( String context, String input ) throws ValidationE
105105
}
106106
throw new ValidationException( context + " is required", "AntiSamy validation error: context=" + context + ", input=" + input, context );
107107
}
108-
108+
109109
String canonical = super.getValid( context, input );
110110

111111
try {
112112
AntiSamy as = new AntiSamy();
113113
CleanResults test = as.scan(canonical, antiSamyPolicy);
114-
114+
115115
List<String> errors = test.getErrorMessages();
116116
if ( !errors.isEmpty() ) {
117-
LOGGER.info( Logger.SECURITY_FAILURE, "Cleaned up invalid HTML input: " + errors );
117+
StringBuilder sb = new StringBuilder();
118+
for ( int i = 0; i < errors.size(); i++ ) {
119+
sb.append(errors.get(i));
120+
if ( i != errors.size() - 1 ) {
121+
sb.append(",");
122+
}
123+
}
124+
throw new ValidationException( context + ": Invalid HTML input", "Invalid HTML input does not follow rules in antisamy-esapi.xml: context=" + context + " errors=" + sb.toString());
118125
}
119-
126+
120127
return test.getCleanHTML().trim();
121-
128+
122129
} catch (ScanException e) {
123130
throw new ValidationException( context + ": Invalid HTML input", "Invalid HTML input: context=" + context + " error=" + e.getMessage(), e, context );
124131
} catch (PolicyException e) {

0 commit comments

Comments
 (0)