Skip to content

Commit 317a1f9

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-36459
1 parent de6601f commit 317a1f9

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

@@ -306,11 +307,26 @@ protected void loadScripts(ScriptEngine engine) {
306307

307308
@Nullable
308309
protected Resource getResource(String location) {
309-
if (this.resourceLoaderPaths != null) {
310+
String normalizedLocation = ResourceHandlerUtils.normalizeInputPath(location);
311+
if (this.resourceLoaderPaths != null && !ResourceHandlerUtils.shouldIgnoreInputPath(normalizedLocation)) {
312+
ApplicationContext context = obtainApplicationContext();
310313
for (String path : this.resourceLoaderPaths) {
311-
Resource resource = obtainApplicationContext().getResource(path + location);
312-
if (resource.exists()) {
313-
return resource;
314+
Resource resource = context.getResource(path + normalizedLocation);
315+
try {
316+
if (resource.exists() && ResourceHandlerUtils.isResourceUnderLocation(context.getResource(path), resource)) {
317+
return resource;
318+
}
319+
}
320+
catch (IOException ex) {
321+
if (logger.isDebugEnabled()) {
322+
String error = "Skip location [" + normalizedLocation + "] due to error";
323+
if (logger.isTraceEnabled()) {
324+
logger.trace(error, ex);
325+
}
326+
else {
327+
logger.debug(error + ": " + ex.getMessage());
328+
}
329+
}
314330
}
315331
}
316332
}

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

@@ -351,11 +352,26 @@ protected void loadScripts(ScriptEngine engine) {
351352

352353
@Nullable
353354
protected Resource getResource(String location) {
354-
if (this.resourceLoaderPaths != null) {
355+
String normalizedLocation = ResourceHandlerUtils.normalizeInputPath(location);
356+
if (this.resourceLoaderPaths != null && !ResourceHandlerUtils.shouldIgnoreInputPath(normalizedLocation)) {
357+
ApplicationContext context = obtainApplicationContext();
355358
for (String path : this.resourceLoaderPaths) {
356-
Resource resource = obtainApplicationContext().getResource(path + location);
357-
if (resource.exists()) {
358-
return resource;
359+
Resource resource = context.getResource(path + normalizedLocation);
360+
try {
361+
if (resource.exists() && ResourceHandlerUtils.isResourceUnderLocation(context.getResource(path), resource)) {
362+
return resource;
363+
}
364+
}
365+
catch (IOException ex) {
366+
if (logger.isDebugEnabled()) {
367+
String error = "Skip location [" + normalizedLocation + "] due to error";
368+
if (logger.isTraceEnabled()) {
369+
logger.trace(error, ex);
370+
}
371+
else {
372+
logger.debug(error + ": " + ex.getMessage());
373+
}
374+
}
359375
}
360376
}
361377
}

0 commit comments

Comments
 (0)