Skip to content

Commit de3d570

Browse files
authored
Merge pull request #1 from HJW8472/issue-#310
Issue ESAPI#310
2 parents 9b16ad0 + 4624c23 commit de3d570

File tree

5 files changed

+79
-8
lines changed

5 files changed

+79
-8
lines changed

src/main/java/org/owasp/esapi/SecurityConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,12 @@ public interface SecurityConfiguration extends EsapiPropertyLoader {
640640
*/
641641
InputStream getResourceStream( String filename ) throws IOException;
642642

643+
/**
644+
* Used to load antisamy-esapi.xml from a variety of different classpath locations.
645+
*
646+
* @param fileName The resource file filename.
647+
*/
648+
InputStream getResourceStreamFromClasspath( String fileName );
643649

644650
/**
645651
* Sets the ESAPI resource directory.

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,64 @@ public File getResourceFile(String filename) {
627627
return null;
628628
}
629629

630+
/**
631+
* Used to load antisamy-esapi.xml from a variety of different classpath locations.
632+
*
633+
* @param fileName The resource file filename.
634+
*/
635+
public InputStream getResourceStreamFromClasspath(String fileName) {
636+
InputStream resourceStream = null;
637+
638+
ClassLoader[] loaders = new ClassLoader[] {
639+
Thread.currentThread().getContextClassLoader(),
640+
ClassLoader.getSystemClassLoader(),
641+
getClass().getClassLoader()
642+
};
643+
644+
for (ClassLoader loader : loaders) {
645+
// try root
646+
String currentClasspathSearchLocation = "/ (root)";
647+
resourceStream = loader.getResourceAsStream(DefaultSearchPath.ROOT.value() + fileName);
648+
649+
// try resourceDirectory folder
650+
if (resourceStream == null){
651+
currentClasspathSearchLocation = resourceDirectory + "/";
652+
resourceStream = loader.getResourceAsStream(DefaultSearchPath.RESOURCE_DIRECTORY.value() + fileName);
653+
}
654+
655+
// try .esapi folder. Look here first for backward compatibility.
656+
if (resourceStream == null){
657+
currentClasspathSearchLocation = ".esapi/";
658+
resourceStream = loader.getResourceAsStream(DefaultSearchPath.DOT_ESAPI.value() + fileName);
659+
}
660+
661+
// try esapi folder (new directory)
662+
if (resourceStream == null){
663+
currentClasspathSearchLocation = "esapi/";
664+
resourceStream = loader.getResourceAsStream(DefaultSearchPath.ESAPI.value() + fileName);
665+
}
666+
667+
// try resources folder
668+
if (resourceStream == null){
669+
currentClasspathSearchLocation = "resources/";
670+
resourceStream = loader.getResourceAsStream(DefaultSearchPath.RESOURCES.value() + fileName);
671+
}
672+
673+
// try src/main/resources folder
674+
if (resourceStream == null){
675+
currentClasspathSearchLocation = "src/main/resources/";
676+
resourceStream = loader.getResourceAsStream(DefaultSearchPath.SRC_MAIN_RESOURCES.value() + fileName);
677+
}
678+
679+
if (resourceStream != null) {
680+
logSpecial("SUCCESSFULLY LOADED " + fileName + " via the CLASSPATH from '" + currentClasspathSearchLocation + "'!");
681+
break; // Outta here since we've found and loaded it.
682+
}
683+
}
684+
685+
return resourceStream;
686+
}
687+
630688
/**
631689
* Used to load ESAPI.properties from a variety of different classpath locations.
632690
*

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,29 @@ public class HTMLValidationRule extends StringValidationRule {
4646
/** OWASP AntiSamy markup verification policy */
4747
private static Policy antiSamyPolicy = null;
4848
private static final Logger LOGGER = ESAPI.getLogger( "HTMLValidationRule" );
49+
private static final String ANTISAMYPOLICY_FILENAME = "antisamy-esapi.xml";
4950

5051
static {
5152
InputStream resourceStream = null;
5253
try {
53-
resourceStream = ESAPI.securityConfiguration().getResourceStream("antisamy-esapi.xml");
54+
resourceStream = ESAPI.securityConfiguration().getResourceStream(ANTISAMYPOLICY_FILENAME);
5455
} catch (IOException e) {
55-
throw new ConfigurationException("Couldn't find antisamy-esapi.xml", e);
56-
}
56+
57+
LOGGER.info(Logger.EVENT_FAILURE, "Loading " + ANTISAMYPOLICY_FILENAME + " from classpaths");
58+
59+
resourceStream = ESAPI.securityConfiguration().getResourceStreamFromClasspath(ANTISAMYPOLICY_FILENAME);
60+
}
5761
if (resourceStream != null) {
5862
try {
5963
antiSamyPolicy = Policy.getInstance(resourceStream);
6064
} catch (PolicyException e) {
61-
throw new ConfigurationException("Couldn't parse antisamy policy", e);
62-
}
63-
}
65+
throw new ConfigurationException("Couldn't parse " + ANTISAMYPOLICY_FILENAME, e);
66+
}
6467
}
68+
else {
69+
throw new ConfigurationException("Couldn't find " + ANTISAMYPOLICY_FILENAME);
70+
}
71+
}
6572

6673
public HTMLValidationRule( String typeName ) {
6774
super( typeName );

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
* @since 2019
1515
*/
16-
package org.owasp.esapi.reference;
16+
package org.owasp.esapi.reference.validation;
1717

1818
import org.owasp.esapi.ESAPI;
1919
import org.owasp.esapi.EncoderConstants;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
* @since 2019
1515
*/
16-
package org.owasp.esapi.reference;
16+
package org.owasp.esapi.reference.validation;
1717

1818
import org.owasp.esapi.ESAPI;
1919
import org.owasp.esapi.SecurityConfiguration;

0 commit comments

Comments
 (0)