File tree Expand file tree Collapse file tree 4 files changed +29
-14
lines changed
spring-core/src/main/java/org/springframework/core/io
spring-webflux/src/main/java/org/springframework/web/reactive/function/server
spring-webmvc/src/main/java/org/springframework/web/servlet/function Expand file tree Collapse file tree 4 files changed +29
-14
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -88,7 +88,15 @@ else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
88
88
@ Override
89
89
public boolean isReadable () {
90
90
try {
91
- URL url = getURL ();
91
+ return checkReadable (getURL ());
92
+ }
93
+ catch (IOException ex ) {
94
+ return false ;
95
+ }
96
+ }
97
+
98
+ boolean checkReadable (URL url ) {
99
+ try {
92
100
if (ResourceUtils .isFileURL (url )) {
93
101
// Proceed with file system resolution
94
102
File file = getFile ();
Original file line number Diff line number Diff line change @@ -142,6 +142,18 @@ public boolean exists() {
142
142
return (resolveURL () != null );
143
143
}
144
144
145
+ /**
146
+ * This implementation checks for the resolution of a resource URL upfront,
147
+ * then proceeding with {@link AbstractFileResolvingResource}'s length check.
148
+ * @see java.lang.ClassLoader#getResource(String)
149
+ * @see java.lang.Class#getResource(String)
150
+ */
151
+ @ Override
152
+ public boolean isReadable () {
153
+ URL url = resolveURL ();
154
+ return (url != null && checkReadable (url ));
155
+ }
156
+
145
157
/**
146
158
* Resolves a URL for the underlying class path resource.
147
159
* @return the resolved URL, or {@code null} if not resolvable
Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -72,7 +72,7 @@ public Mono<Resource> apply(ServerRequest request) {
72
72
73
73
try {
74
74
Resource resource = this .location .createRelative (path );
75
- if (resource .exists () && resource . isReadable () && isResourceUnderLocation (resource )) {
75
+ if (resource .isReadable () && isResourceUnderLocation (resource )) {
76
76
return Mono .just (resource );
77
77
}
78
78
else {
Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@ public Optional<Resource> apply(ServerRequest request) {
71
71
72
72
try {
73
73
Resource resource = this .location .createRelative (path );
74
- if (resource .exists () && resource . isReadable () && isResourceUnderLocation (resource )) {
74
+ if (resource .isReadable () && isResourceUnderLocation (resource )) {
75
75
return Optional .of (resource );
76
76
}
77
77
else {
@@ -110,10 +110,7 @@ private boolean isInvalidPath(String path) {
110
110
return true ;
111
111
}
112
112
}
113
- if (path .contains (".." ) && StringUtils .cleanPath (path ).contains ("../" )) {
114
- return true ;
115
- }
116
- return false ;
113
+ return path .contains (".." ) && StringUtils .cleanPath (path ).contains ("../" );
117
114
}
118
115
119
116
private boolean isResourceUnderLocation (Resource resource ) throws IOException {
@@ -144,10 +141,8 @@ else if (resource instanceof ClassPathResource) {
144
141
if (!resourcePath .startsWith (locationPath )) {
145
142
return false ;
146
143
}
147
- if (resourcePath .contains ("%" ) && StringUtils .uriDecode (resourcePath , StandardCharsets .UTF_8 ).contains ("../" )) {
148
- return false ;
149
- }
150
- return true ;
144
+ return !resourcePath .contains ("%" ) ||
145
+ !StringUtils .uriDecode (resourcePath , StandardCharsets .UTF_8 ).contains ("../" );
151
146
}
152
147
153
148
You can’t perform that action at this time.
0 commit comments