|
46 | 46 | import org.slf4j.LoggerFactory; |
47 | 47 |
|
48 | 48 | import javax.servlet.ServletConfig; |
49 | | - |
50 | 49 | import java.io.IOException; |
51 | 50 | import java.io.InputStream; |
52 | | -import java.net.MalformedURLException; |
53 | 51 | import java.security.*; |
54 | 52 | import java.time.Duration; |
55 | 53 | import java.util.*; |
@@ -113,8 +111,6 @@ public class PropertiesConfigurationProvider implements ConfigurationProvider { |
113 | 111 |
|
114 | 112 | private String javascriptTemplateCode; |
115 | 113 |
|
116 | | - private String javascriptSourceFile; |
117 | | - |
118 | 114 | private boolean javascriptDomainStrict; |
119 | 115 |
|
120 | 116 | private String javascriptCacheControl; |
@@ -288,11 +284,6 @@ public void initializeJavaScriptConfiguration() { |
288 | 284 | this.javascriptInitParamsIfNeeded(); |
289 | 285 | } |
290 | 286 |
|
291 | | - @Override |
292 | | - public String getJavascriptSourceFile() { |
293 | | - return this.javascriptSourceFile; |
294 | | - } |
295 | | - |
296 | 287 | @Override |
297 | 288 | public boolean isJavascriptDomainStrict() { |
298 | 289 | return this.javascriptDomainStrict; |
@@ -550,44 +541,45 @@ private void javascriptInitParamsIfNeeded() { |
550 | 541 | this.javascriptRefererMatchProtocol = getProperty(JavaScriptConfigParameters.REFERER_MATCH_PROTOCOL, servletConfig); |
551 | 542 | this.javascriptRefererMatchDomain = getProperty(JavaScriptConfigParameters.REFERER_MATCH_DOMAIN, servletConfig); |
552 | 543 | this.javascriptUnprotectedExtensions = getProperty(JavaScriptConfigParameters.UNPROTECTED_EXTENSIONS, servletConfig); |
553 | | - this.javascriptSourceFile = getProperty(JavaScriptConfigParameters.SOURCE_FILE, servletConfig); |
554 | 544 | this.javascriptXrequestedWith = getProperty(JavaScriptConfigParameters.X_REQUESTED_WITH, servletConfig); |
555 | 545 |
|
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); |
575 | 548 |
|
576 | 549 | this.javascriptParamsInitialized = true; |
577 | 550 | } |
578 | 551 | } |
579 | 552 | } |
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 | + } |
586 | 575 | } |
587 | | - |
588 | | - return inputStream; |
589 | | - } |
590 | 576 |
|
| 577 | + if (StringUtils.isBlank(result)) { |
| 578 | + throw new IllegalStateException("Error while trying to retrieve the JavaScript source code!"); |
| 579 | + } |
| 580 | + |
| 581 | + return result; |
| 582 | + } |
591 | 583 |
|
592 | 584 | private <T> T getProperty(final JsConfigParameter<T> jsConfigParameter, final ServletConfig servletConfig) { |
593 | 585 | return jsConfigParameter.getProperty(servletConfig, this.propertiesCache); |
|
0 commit comments