Skip to content

Commit 573ec7e

Browse files
authored
Merge branch 'develop' into misc-cleanup
2 parents cceffa6 + f94ca94 commit 573ec7e

File tree

13 files changed

+799
-25
lines changed

13 files changed

+799
-25
lines changed

configuration/esapi/ESAPI.properties

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,10 @@ Validator.AcceptLenientDates=false
535535
#
536536
#Validator.HtmlValidationAction=clean
537537
Validator.HtmlValidationAction=throw
538+
539+
# With the fix for #310 to enable loading antisamy-esapi.xml from the classpath
540+
# also an enhancement was made to be able to use a different filename for the configuration.
541+
# You don't have to configure the filename here, but in that case the code will keep looking for antisamy-esapi.xml.
542+
# This is the default behaviour of ESAPI.
543+
#
544+
#Validator.HtmlValidationConfigurationFile=antisamy-esapi.xml

documentation/esapi4java-core-2.2.1.0-release-notes.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Issue # GitHub Issue Title
4848
530 Log Bridge Tests
4949
536 Various fixes
5050
538 Addressing log4j 1.x CVE-2019-17571
51+
552 Rewrite implementation of some ESAPI classes to remove Java 8 dependencies
52+
5153

5254
-----------------------------------------------------------------------------
5355

@@ -95,7 +97,7 @@ Developer Total Total Number
9597
(GitHub ID) commits of Files Changed
9698
=====================================================
9799
jeremiahjstacey 11 68
98-
kwwall 15 26
100+
kwwall 16 26
99101
wiitek 3 6
100102
xeno6696 8 9
101103
Michael-Ziluck 2 3
@@ -104,8 +106,10 @@ sempf 1 1
104106

105107
-----------------------------------------------------------------------------
106108

107-
53 Closed PRs since 2.2.0.0 release
108-
===================================
109+
110+
53 Closed PRs since 2.2.0.0 release (those rejected not listed)
111+
===============================================================
112+
109113
504 New scripts to suppress noise for 'mvn test'
110114
510 Resolve #509 - Properly throw exception when HTML fails
111115
513 Close issue #512 by updating to 1.9.4 of Commons Beans Util.\
@@ -126,7 +130,7 @@ Project co-leaders
126130

127131
Special shout-outs to:
128132
Jeremiah Stacey (jeremiahjstacey) -- All around ESAPI support and JUnit test case developer extraordinaire
129-
Dave Wichers (davewichers) - for Maven Central / Sonatype help
133+
Dave Wichers (davewichers) - for pom.xml improvements
130134
Bill Sempf -- for these release notes. Awesome job, Bill. I owe you a brew.
131135

132136
Thanks you all for your time and effort to ESAPI and making it a better project. And if I've missed any, my apologies; let me know and I will correct it.

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

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

643-
644643
/**
645644
* Sets the ESAPI resource directory.
646645
*

src/main/java/org/owasp/esapi/logging/appender/ClientInfoSupplier.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
package org.owasp.esapi.logging.appender;
1717

18-
import java.util.function.Supplier;
18+
// Uncomment and use once ESAPI supports Java 8 as the minimal baseline.
19+
// import java.util.function.Supplier;
1920

2021
import javax.servlet.http.HttpServletRequest;
2122
import javax.servlet.http.HttpSession;
@@ -27,7 +28,8 @@
2728
* Supplier which can provide a String representing the client-side connection
2829
* information.
2930
*/
30-
public class ClientInfoSupplier implements Supplier<String> {
31+
public class ClientInfoSupplier // implements Supplier<String>
32+
{
3133
/** Default Last Host string if the Authenticated user is null.*/
3234
private static final String DEFAULT_LAST_HOST = "#UNKNOWN_HOST#";
3335
/** Session Attribute containing the ESAPI Session id. */
@@ -47,7 +49,7 @@ public class ClientInfoSupplier implements Supplier<String> {
4749
/** Whether to log the user info from this instance. */
4850
private boolean logClientInfo = true;
4951

50-
@Override
52+
// @Override -- Uncomment when we switch to Java 8 as minimal baseline.
5153
public String get() {
5254
String clientInfo = "";
5355

src/main/java/org/owasp/esapi/logging/appender/EventTypeLogSupplier.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
package org.owasp.esapi.logging.appender;
1717

18-
import java.util.function.Supplier;
18+
// Uncomment and use once ESAPI supports Java 8 as the minimal baseline.
19+
// import java.util.function.Supplier;
1920

2021
import org.owasp.esapi.Logger;
2122
import org.owasp.esapi.Logger.EventType;
@@ -25,7 +26,8 @@
2526
* an EventType for logging
2627
*
2728
*/
28-
public class EventTypeLogSupplier implements Supplier<String> {
29+
public class EventTypeLogSupplier // implements Supplier<String>
30+
{
2931
/** EventType reference to supply log representation of. */
3032
private final EventType eventType;
3133

@@ -38,7 +40,7 @@ public EventTypeLogSupplier(EventType evtyp) {
3840
this.eventType = evtyp == null ? Logger.EVENT_UNSPECIFIED : evtyp;
3941
}
4042

41-
@Override
43+
// @Override -- Uncomment when we switch to Java 8 as minimal baseline.
4244
public String get() {
4345
return eventType.toString();
4446
}

src/main/java/org/owasp/esapi/logging/appender/ServerInfoSupplier.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
package org.owasp.esapi.logging.appender;
1717

18-
import java.util.function.Supplier;
18+
// Uncomment and use once ESAPI supports Java 8 as the minimal baseline.
19+
// import java.util.function.Supplier;
1920

2021
import javax.servlet.http.HttpServletRequest;
2122

@@ -25,7 +26,8 @@
2526
* Supplier which can provide a String representing the server-side connection
2627
* information.
2728
*/
28-
public class ServerInfoSupplier implements Supplier<String> {
29+
public class ServerInfoSupplier // implements Supplier<String>
30+
{
2931
/** Whether to log the server connection info. */
3032
private boolean logServerIP = true;
3133
/** Whether to log the application name. */
@@ -45,7 +47,7 @@ public ServerInfoSupplier(String logName) {
4547
this.logName = logName;
4648
}
4749

48-
@Override
50+
// @Override -- Uncomment when we switch to Java 8 as minimal baseline.
4951
public String get() {
5052
// log server, port, app name, module name -- server:80/app/module
5153
StringBuilder appInfo = new StringBuilder();

src/main/java/org/owasp/esapi/logging/appender/UserInfoSupplier.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
package org.owasp.esapi.logging.appender;
1717

18-
import java.util.function.Supplier;
18+
// Uncomment and use once ESAPI supports Java 8 as the minimal baseline.
19+
// import java.util.function.Supplier;
1920

2021
import org.owasp.esapi.ESAPI;
2122
import org.owasp.esapi.User;
@@ -24,14 +25,15 @@
2425
* Supplier which can provide a String representing the client-side connection
2526
* information.
2627
*/
27-
public class UserInfoSupplier implements Supplier<String> {
28+
public class UserInfoSupplier // implements Supplier<String>
29+
{
2830
/** Default UserName string if the Authenticated user is null.*/
2931
private static final String DEFAULT_USERNAME = "#ANONYMOUS#";
3032

3133
/** Whether to log the user info from this instance. */
3234
private boolean logUserInfo = true;
3335

34-
@Override
36+
// @Override -- Uncomment when we switch to Java 8 as minimal baseline.
3537
public String get() {
3638
// log user information - username:session@ipaddr
3739
User user = ESAPI.authenticator().getCurrentUser();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public static SecurityConfiguration getInstance() {
159159
public static final String VALIDATION_PROPERTIES_MULTIVALUED = "Validator.ConfigurationFile.MultiValued";
160160
public static final String ACCEPT_LENIENT_DATES = "Validator.AcceptLenientDates";
161161
public static final String VALIDATOR_HTML_VALIDATION_ACTION = "Validator.HtmlValidationAction";
162+
public static final String VALIDATOR_HTML_VALIDATION_CONFIGURATION_FILE = "Validator.HtmlValidationConfigurationFile";
162163

163164
/**
164165
* Special {@code System} property that, if set to {@code true}, will

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

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.owasp.validator.html.Policy;
3131
import org.owasp.validator.html.PolicyException;
3232
import org.owasp.validator.html.ScanException;
33+
import org.owasp.esapi.reference.DefaultSecurityConfiguration;
3334

3435

3536
/**
@@ -46,22 +47,113 @@ public class HTMLValidationRule extends StringValidationRule {
4647
/** OWASP AntiSamy markup verification policy */
4748
private static Policy antiSamyPolicy = null;
4849
private static final Logger LOGGER = ESAPI.getLogger( "HTMLValidationRule" );
50+
private static final String ANTISAMYPOLICY_FILENAME = "antisamy-esapi.xml";
51+
52+
/**
53+
* Used to load antisamy-esapi.xml from a variety of different classpath locations.
54+
* The classpath locations are the same classpath locations as used to load esapi.properties.
55+
* See DefaultSecurityConfiguration.DefaultSearchPath.
56+
*
57+
* @param fileName The resource file filename.
58+
*/
59+
private static InputStream getResourceStreamFromClasspath(String fileName) {
60+
InputStream resourceStream = null;
61+
62+
ClassLoader[] loaders = new ClassLoader[] {
63+
Thread.currentThread().getContextClassLoader(),
64+
ClassLoader.getSystemClassLoader(),
65+
ESAPI.securityConfiguration().getClass().getClassLoader()
66+
/* can't use just getClass.getClassLoader() in a static context, so using the DefaultSecurityConfiguration class. */
67+
};
68+
69+
String[] classLoaderNames = {
70+
"current thread context class loader",
71+
"system class loader",
72+
"class loader for DefaultSecurityConfiguration class"
73+
};
74+
75+
int i = 0;
76+
for (ClassLoader loader : loaders) {
77+
// try root
78+
String currentClasspathSearchLocation = "/ (root)";
79+
resourceStream = loader.getResourceAsStream(DefaultSecurityConfiguration.DefaultSearchPath.ROOT.value() + fileName);
80+
81+
// try resourceDirectory folder
82+
if (resourceStream == null){
83+
currentClasspathSearchLocation = DefaultSecurityConfiguration.DefaultSearchPath.RESOURCE_DIRECTORY.value();
84+
resourceStream = loader.getResourceAsStream(currentClasspathSearchLocation + fileName);
85+
}
86+
87+
// try .esapi folder. Look here first for backward compatibility.
88+
if (resourceStream == null){
89+
currentClasspathSearchLocation = DefaultSecurityConfiguration.DefaultSearchPath.DOT_ESAPI.value();
90+
resourceStream = loader.getResourceAsStream(currentClasspathSearchLocation + fileName);
91+
}
92+
93+
// try esapi folder (new directory)
94+
if (resourceStream == null){
95+
currentClasspathSearchLocation = DefaultSecurityConfiguration.DefaultSearchPath.ESAPI.value();
96+
resourceStream = loader.getResourceAsStream(currentClasspathSearchLocation + fileName);
97+
}
98+
99+
// try resources folder
100+
if (resourceStream == null){
101+
currentClasspathSearchLocation = DefaultSecurityConfiguration.DefaultSearchPath.RESOURCES.value();
102+
resourceStream = loader.getResourceAsStream(currentClasspathSearchLocation + fileName);
103+
}
104+
105+
// try src/main/resources folder
106+
if (resourceStream == null){
107+
currentClasspathSearchLocation = DefaultSecurityConfiguration.DefaultSearchPath.SRC_MAIN_RESOURCES.value();
108+
resourceStream = loader.getResourceAsStream(currentClasspathSearchLocation + fileName);
109+
}
110+
111+
if (resourceStream != null) {
112+
LOGGER.info(Logger.EVENT_FAILURE, "SUCCESSFULLY LOADED " + fileName + " via the CLASSPATH from '" +
113+
currentClasspathSearchLocation + "' using " + classLoaderNames[i] + "!");
114+
break; // Outta here since we've found and loaded it.
115+
}
116+
117+
i++;
118+
}
119+
120+
return resourceStream;
121+
}
49122

50123
static {
51124
InputStream resourceStream = null;
125+
String antisamyPolicyFilename = null;
126+
127+
try {
128+
antisamyPolicyFilename = ESAPI.securityConfiguration().getStringProp(
129+
// Future: This will be moved to a new PropNames class
130+
org.owasp.esapi.reference.DefaultSecurityConfiguration.VALIDATOR_HTML_VALIDATION_CONFIGURATION_FILE );
131+
} catch (ConfigurationException cex) {
132+
133+
LOGGER.info(Logger.EVENT_FAILURE, "ESAPI property " +
134+
org.owasp.esapi.reference.DefaultSecurityConfiguration.VALIDATOR_HTML_VALIDATION_CONFIGURATION_FILE +
135+
" not set, using default value: " + ANTISAMYPOLICY_FILENAME);
136+
antisamyPolicyFilename = ANTISAMYPOLICY_FILENAME;
137+
}
52138
try {
53-
resourceStream = ESAPI.securityConfiguration().getResourceStream("antisamy-esapi.xml");
139+
resourceStream = ESAPI.securityConfiguration().getResourceStream(antisamyPolicyFilename);
54140
} catch (IOException e) {
55-
throw new ConfigurationException("Couldn't find antisamy-esapi.xml", e);
56-
}
141+
142+
LOGGER.info(Logger.EVENT_FAILURE, "Loading " + antisamyPolicyFilename + " from classpaths");
143+
144+
resourceStream = getResourceStreamFromClasspath(antisamyPolicyFilename);
145+
}
57146
if (resourceStream != null) {
58147
try {
59148
antiSamyPolicy = Policy.getInstance(resourceStream);
60149
} catch (PolicyException e) {
61-
throw new ConfigurationException("Couldn't parse antisamy policy", e);
62-
}
63-
}
150+
throw new ConfigurationException("Couldn't parse " + antisamyPolicyFilename, e);
151+
}
64152
}
153+
else {
154+
throw new ConfigurationException("Couldn't find " + antisamyPolicyFilename);
155+
}
156+
}
65157

66158
public HTMLValidationRule( String typeName ) {
67159
super( typeName );

0 commit comments

Comments
 (0)