Skip to content

Commit 6ef0938

Browse files
committed
Defensive URL cleaning (preserving the original URL if possible)
Issue: SPR-17198
1 parent c55a907 commit 6ef0938

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

spring-core/src/main/java/org/springframework/core/io/UrlResource.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,20 @@ public UrlResource(String protocol, String location, @Nullable String fragment)
141141
* Determine a cleaned URL for the given original URL.
142142
* @param originalUrl the original URL
143143
* @param originalPath the original URL path
144-
* @return the cleaned URL
144+
* @return the cleaned URL (possibly the original URL as-is)
145145
* @see org.springframework.util.StringUtils#cleanPath
146146
*/
147147
private URL getCleanedUrl(URL originalUrl, String originalPath) {
148-
try {
149-
return new URL(StringUtils.cleanPath(originalPath));
150-
}
151-
catch (MalformedURLException ex) {
152-
// Cleaned URL path cannot be converted to URL
153-
// -> take original URL.
154-
return originalUrl;
148+
String cleanedPath = StringUtils.cleanPath(originalPath);
149+
if (!cleanedPath.equals(originalPath)) {
150+
try {
151+
return new URL(cleanedPath);
152+
}
153+
catch (MalformedURLException ex) {
154+
// Cleaned URL path cannot be converted to URL -> take original URL.
155+
}
155156
}
157+
return originalUrl;
156158
}
157159

158160
/**

0 commit comments

Comments
 (0)