|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.reactive.function; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.UncheckedIOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.function.Function; |
| 24 | + |
| 25 | +import org.springframework.core.io.ClassPathResource; |
| 26 | +import org.springframework.core.io.Resource; |
| 27 | +import org.springframework.core.io.UrlResource; |
| 28 | +import org.springframework.util.AntPathMatcher; |
| 29 | +import org.springframework.util.PathMatcher; |
| 30 | +import org.springframework.util.ResourceUtils; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | +import org.springframework.web.util.UriUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * Lookup function used by {@link RouterFunctions#resources(String, Resource)}. |
| 36 | + * |
| 37 | + * @author Arjen Poutsma |
| 38 | + * @since 5.0 |
| 39 | + */ |
| 40 | +class PathResourceLookupFunction implements Function<ServerRequest, Optional<Resource>> { |
| 41 | + |
| 42 | + private static final PathMatcher PATH_MATCHER = new AntPathMatcher(); |
| 43 | + |
| 44 | + private final String pattern; |
| 45 | + |
| 46 | + private final Resource location; |
| 47 | + |
| 48 | + public PathResourceLookupFunction(String pattern, Resource location) { |
| 49 | + this.pattern = pattern; |
| 50 | + this.location = location; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Optional<Resource> apply(ServerRequest request) { |
| 55 | + String path = processPath(request.path()); |
| 56 | + if (path.contains("%")) { |
| 57 | + path = UriUtils.decode(path, StandardCharsets.UTF_8); |
| 58 | + } |
| 59 | + if (!StringUtils.hasLength(path) || isInvalidPath(path)) { |
| 60 | + return Optional.empty(); |
| 61 | + } |
| 62 | + if (!PATH_MATCHER.match(this.pattern, path)) { |
| 63 | + return Optional.empty(); |
| 64 | + } |
| 65 | + else { |
| 66 | + path = PATH_MATCHER.extractPathWithinPattern(this.pattern, path); |
| 67 | + } |
| 68 | + try { |
| 69 | + Resource resource = this.location.createRelative(path); |
| 70 | + if (resource.exists() && resource.isReadable() && isResourceUnderLocation(resource)) { |
| 71 | + return Optional.of(resource); |
| 72 | + } |
| 73 | + else { |
| 74 | + return Optional.empty(); |
| 75 | + } |
| 76 | + } |
| 77 | + catch (IOException ex) { |
| 78 | + throw new UncheckedIOException(ex); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static String processPath(String path) { |
| 83 | + boolean slash = false; |
| 84 | + for (int i = 0; i < path.length(); i++) { |
| 85 | + if (path.charAt(i) == '/') { |
| 86 | + slash = true; |
| 87 | + } |
| 88 | + else if (path.charAt(i) > ' ' && path.charAt(i) != 127) { |
| 89 | + if (i == 0 || (i == 1 && slash)) { |
| 90 | + return path; |
| 91 | + } |
| 92 | + path = slash ? "/" + path.substring(i) : path.substring(i); |
| 93 | + return path; |
| 94 | + } |
| 95 | + } |
| 96 | + return (slash ? "/" : ""); |
| 97 | + } |
| 98 | + |
| 99 | + private static boolean isInvalidPath(String path) { |
| 100 | + if (path.contains("WEB-INF") || path.contains("META-INF")) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + if (path.contains(":/")) { |
| 104 | + String relativePath = (path.charAt(0) == '/' ? path.substring(1) : path); |
| 105 | + if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + } |
| 109 | + if (path.contains("..")) { |
| 110 | + path = StringUtils.cleanPath(path); |
| 111 | + if (path.contains("../")) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + private boolean isResourceUnderLocation(Resource resource) throws |
| 119 | + IOException { |
| 120 | + if (resource.getClass() != this.location.getClass()) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + String resourcePath; |
| 125 | + String locationPath; |
| 126 | + |
| 127 | + if (resource instanceof UrlResource) { |
| 128 | + resourcePath = resource.getURL().toExternalForm(); |
| 129 | + locationPath = StringUtils.cleanPath(this.location.getURL().toString()); |
| 130 | + } |
| 131 | + else if (resource instanceof ClassPathResource) { |
| 132 | + resourcePath = ((ClassPathResource) resource).getPath(); |
| 133 | + locationPath = StringUtils.cleanPath(((ClassPathResource) this.location).getPath()); |
| 134 | + } |
| 135 | + else { |
| 136 | + resourcePath = resource.getURL().getPath(); |
| 137 | + locationPath = StringUtils.cleanPath(this.location.getURL().getPath()); |
| 138 | + } |
| 139 | + |
| 140 | + if (locationPath.equals(resourcePath)) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : |
| 144 | + locationPath + "/"); |
| 145 | + if (!resourcePath.startsWith(locationPath)) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + if (resourcePath.contains("%")) { |
| 150 | + if (UriUtils.decode(resourcePath, "UTF-8").contains("../")) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return true; |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | +} |
0 commit comments