Skip to content

Commit 739d5ba

Browse files
committed
Leverage ResourceHandlerUtils in ScriptTemplateView
This commit apply extra checks to ScriptTemplateView resource handling with ResourceHandlerUtils, consistently with what is done with static resource handling. Closes gh-36458
1 parent 19ab92e commit 739d5ba

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateView.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.util.FileCopyUtils;
4848
import org.springframework.util.ObjectUtils;
4949
import org.springframework.util.StringUtils;
50+
import org.springframework.web.reactive.resource.ResourceHandlerUtils;
5051
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
5152
import org.springframework.web.server.ServerWebExchange;
5253

@@ -292,11 +293,26 @@ protected void loadScripts(ScriptEngine engine) {
292293
}
293294

294295
protected @Nullable Resource getResource(String location) {
295-
if (this.resourceLoaderPaths != null) {
296+
String normalizedLocation = ResourceHandlerUtils.normalizeInputPath(location);
297+
if (this.resourceLoaderPaths != null && !ResourceHandlerUtils.shouldIgnoreInputPath(normalizedLocation)) {
298+
ApplicationContext context = obtainApplicationContext();
296299
for (String path : this.resourceLoaderPaths) {
297-
Resource resource = obtainApplicationContext().getResource(path + location);
298-
if (resource.exists()) {
299-
return resource;
300+
Resource resource = context.getResource(path + normalizedLocation);
301+
try {
302+
if (resource.exists() && ResourceHandlerUtils.isResourceUnderLocation(context.getResource(path), resource)) {
303+
return resource;
304+
}
305+
}
306+
catch (IOException ex) {
307+
if (logger.isDebugEnabled()) {
308+
String error = "Skip location [" + normalizedLocation + "] due to error";
309+
if (logger.isTraceEnabled()) {
310+
logger.trace(error, ex);
311+
}
312+
else {
313+
logger.debug(error + ": " + ex.getMessage());
314+
}
315+
}
300316
}
301317
}
302318
}

spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.util.FileCopyUtils;
5252
import org.springframework.util.ObjectUtils;
5353
import org.springframework.util.StringUtils;
54+
import org.springframework.web.servlet.resource.ResourceHandlerUtils;
5455
import org.springframework.web.servlet.support.RequestContextUtils;
5556
import org.springframework.web.servlet.view.AbstractUrlBasedView;
5657

@@ -336,11 +337,26 @@ protected void loadScripts(ScriptEngine engine) {
336337
}
337338

338339
protected @Nullable Resource getResource(String location) {
339-
if (this.resourceLoaderPaths != null) {
340+
String normalizedLocation = ResourceHandlerUtils.normalizeInputPath(location);
341+
if (this.resourceLoaderPaths != null && !ResourceHandlerUtils.shouldIgnoreInputPath(normalizedLocation)) {
342+
ApplicationContext context = obtainApplicationContext();
340343
for (String path : this.resourceLoaderPaths) {
341-
Resource resource = obtainApplicationContext().getResource(path + location);
342-
if (resource.exists()) {
343-
return resource;
344+
Resource resource = context.getResource(path + normalizedLocation);
345+
try {
346+
if (resource.exists() && ResourceHandlerUtils.isResourceUnderLocation(context.getResource(path), resource)) {
347+
return resource;
348+
}
349+
}
350+
catch (IOException ex) {
351+
if (logger.isDebugEnabled()) {
352+
String error = "Skip location [" + normalizedLocation + "] due to error";
353+
if (logger.isTraceEnabled()) {
354+
logger.trace(error, ex);
355+
}
356+
else {
357+
logger.debug(error + ": " + ex.getMessage());
358+
}
359+
}
344360
}
345361
}
346362
}

0 commit comments

Comments
 (0)