Skip to content

Commit 197d37e

Browse files
refactor: NPE in case of un-exploded WAR deployments #100
* Removed the "getJavascriptSourceFile()" method from the ConfigurationProvider interface. * Better error handling (narrower exception catching and explicit null check)
1 parent 006092b commit 197d37e

File tree

4 files changed

+31
-49
lines changed

4 files changed

+31
-49
lines changed

csrfguard/src/main/java/org/owasp/csrfguard/config/ConfigurationProvider.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,6 @@ public interface ConfigurationProvider {
160160
*/
161161
List<IAction> getActions();
162162

163-
/**
164-
* @return the overridden path to the configured CSRFGuard JavaScript logic, or <b>null</b> if the default is used
165-
*/
166-
String getJavascriptSourceFile();
167-
168163
/**
169164
* @return true if tokens should only be injected into links that have the same domain from which the HTML originates,
170165
* false if subdomains are also permitted

csrfguard/src/main/java/org/owasp/csrfguard/config/NullConfigurationProvider.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,6 @@ public List<IAction> getActions() {
153153
return Collections.emptyList();
154154
}
155155

156-
@Override
157-
public String getJavascriptSourceFile() {
158-
return null;
159-
}
160-
161156
@Override
162157
public boolean isJavascriptDomainStrict() {
163158
return false;

csrfguard/src/main/java/org/owasp/csrfguard/config/PropertiesConfigurationProvider.java

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@
4646
import org.slf4j.LoggerFactory;
4747

4848
import javax.servlet.ServletConfig;
49-
5049
import java.io.IOException;
5150
import java.io.InputStream;
52-
import java.net.MalformedURLException;
5351
import java.security.*;
5452
import java.time.Duration;
5553
import java.util.*;
@@ -113,8 +111,6 @@ public class PropertiesConfigurationProvider implements ConfigurationProvider {
113111

114112
private String javascriptTemplateCode;
115113

116-
private String javascriptSourceFile;
117-
118114
private boolean javascriptDomainStrict;
119115

120116
private String javascriptCacheControl;
@@ -288,11 +284,6 @@ public void initializeJavaScriptConfiguration() {
288284
this.javascriptInitParamsIfNeeded();
289285
}
290286

291-
@Override
292-
public String getJavascriptSourceFile() {
293-
return this.javascriptSourceFile;
294-
}
295-
296287
@Override
297288
public boolean isJavascriptDomainStrict() {
298289
return this.javascriptDomainStrict;
@@ -550,44 +541,45 @@ private void javascriptInitParamsIfNeeded() {
550541
this.javascriptRefererMatchProtocol = getProperty(JavaScriptConfigParameters.REFERER_MATCH_PROTOCOL, servletConfig);
551542
this.javascriptRefererMatchDomain = getProperty(JavaScriptConfigParameters.REFERER_MATCH_DOMAIN, servletConfig);
552543
this.javascriptUnprotectedExtensions = getProperty(JavaScriptConfigParameters.UNPROTECTED_EXTENSIONS, servletConfig);
553-
this.javascriptSourceFile = getProperty(JavaScriptConfigParameters.SOURCE_FILE, servletConfig);
554544
this.javascriptXrequestedWith = getProperty(JavaScriptConfigParameters.X_REQUESTED_WITH, servletConfig);
555545

556-
if (StringUtils.isBlank(this.javascriptSourceFile)) {
557-
this.javascriptTemplateCode = CsrfGuardUtils.readResourceFileContent("META-INF/csrfguard.js");
558-
} else if (this.javascriptSourceFile.startsWith("META-INF/")) {
559-
this.javascriptTemplateCode = CsrfGuardUtils.readResourceFileContent(this.javascriptSourceFile);
560-
} else if (this.javascriptSourceFile.startsWith("classpath:")) {
561-
final String location = this.javascriptSourceFile.substring("classpath:".length()).trim();
562-
this.javascriptTemplateCode = CsrfGuardUtils.readResourceFileContent(location);
563-
} else if (this.javascriptSourceFile.startsWith("file:")) {
564-
final String location = this.javascriptSourceFile.substring("file:".length()).trim();
565-
this.javascriptTemplateCode = CsrfGuardUtils.readFileContent(location);
566-
} else if (servletConfig.getServletContext().getRealPath(this.javascriptSourceFile) != null) {
567-
this.javascriptTemplateCode = CsrfGuardUtils.readFileContent(servletConfig.getServletContext().getRealPath(this.javascriptSourceFile));
568-
} else {
569-
try( final InputStream inputStream = getResourceStream(this.javascriptSourceFile, servletConfig)){
570-
this.javascriptTemplateCode = CsrfGuardUtils.readInputStreamContent(inputStream);
571-
} catch (final Exception e) {
572-
throw new IllegalStateException("getRealPath failed for file " + this.javascriptSourceFile);
573-
}
574-
}
546+
final String javascriptSourceFileLocation = getProperty(JavaScriptConfigParameters.SOURCE_FILE_LOCATION, servletConfig);
547+
this.javascriptTemplateCode = retrieveJavaScriptTemplateCode(servletConfig, javascriptSourceFileLocation);
575548

576549
this.javascriptParamsInitialized = true;
577550
}
578551
}
579552
}
580-
581-
private InputStream getResourceStream(final String resourcePath, final ServletConfig servletConfig) throws MalformedURLException {
582-
InputStream inputStream = null;
583-
584-
if(servletConfig.getServletContext().getResource("/" + this.javascriptSourceFile) != null) {
585-
inputStream = servletConfig.getServletContext().getResourceAsStream("/" + this.javascriptSourceFile);
553+
554+
private static String retrieveJavaScriptTemplateCode(ServletConfig servletConfig, String jsSourceFileLocation) {
555+
String result = null;
556+
557+
if (StringUtils.isBlank(jsSourceFileLocation)) {
558+
result = CsrfGuardUtils.readResourceFileContent("META-INF/csrfguard.js");
559+
} else if (jsSourceFileLocation.startsWith("META-INF/")) {
560+
result = CsrfGuardUtils.readResourceFileContent(jsSourceFileLocation);
561+
} else if (jsSourceFileLocation.startsWith("classpath:")) {
562+
final String location = jsSourceFileLocation.substring("classpath:".length()).trim();
563+
result = CsrfGuardUtils.readResourceFileContent(location);
564+
} else if (jsSourceFileLocation.startsWith("file:")) {
565+
final String location = jsSourceFileLocation.substring("file:".length()).trim();
566+
result = CsrfGuardUtils.readFileContent(location);
567+
} else {
568+
try (final InputStream inputStream = servletConfig.getServletContext().getResourceAsStream('/' + jsSourceFileLocation)) {
569+
if (inputStream != null) {
570+
result = CsrfGuardUtils.readInputStreamContent(inputStream);
571+
}
572+
} catch (final IOException e) {
573+
throw new IllegalStateException(String.format("Error while trying to close the '%s' resource.", jsSourceFileLocation));
574+
}
586575
}
587-
588-
return inputStream;
589-
}
590576

577+
if (StringUtils.isBlank(result)) {
578+
throw new IllegalStateException("Error while trying to retrieve the JavaScript source code!");
579+
}
580+
581+
return result;
582+
}
591583

592584
private <T> T getProperty(final JsConfigParameter<T> jsConfigParameter, final ServletConfig servletConfig) {
593585
return jsConfigParameter.getProperty(servletConfig, this.propertiesCache);

csrfguard/src/main/java/org/owasp/csrfguard/config/properties/javascript/JavaScriptConfigParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private JavaScriptConfigParameters() {}
4242
public static final StringJsConfigParameter CACHE_CONTROL = new StringJsConfigParameter("cache-control", "org.owasp.csrfguard.JavascriptServlet.cacheControl", "private, max-age=28800");
4343
public static final StringJsConfigParameter REFERER_PATTERN = new StringJsConfigParameter("referer-pattern", "org.owasp.csrfguard.JavascriptServlet.refererPattern", DEFAULT_REFERER_PATTERN);
4444
public static final StringJsConfigParameter UNPROTECTED_EXTENSIONS = new StringJsConfigParameter("unprotected-extensions", "org.owasp.csrfguard.JavascriptServlet.UnprotectedExtensions", StringUtils.EMPTY);
45-
public static final StringJsConfigParameter SOURCE_FILE = new StringJsConfigParameter("source-file", "org.owasp.csrfguard.JavascriptServlet.sourceFile", null);
45+
public static final StringJsConfigParameter SOURCE_FILE_LOCATION = new StringJsConfigParameter("source-file", "org.owasp.csrfguard.JavascriptServlet.sourceFile", null);
4646
public static final StringJsConfigParameter X_REQUESTED_WITH = new StringJsConfigParameter("x-requested-with", "org.owasp.csrfguard.JavascriptServlet.xRequestedWith", "OWASP CSRFGuard Project");
4747
public static final StringJsConfigParameter DYNAMIC_NODE_CREATION_EVENT_NAME = new StringJsConfigParameter("dynamic-node-creation-event", "org.owasp.csrfguard.JavascriptServlet.dynamicNodeCreationEventName", null);
4848

0 commit comments

Comments
 (0)