Merge branch '6.1.x'
This commit is contained in:
commit
398e5528a1
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -18,6 +18,7 @@ package org.springframework.web.reactive.function.server;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
|
import java.net.URLDecoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
@ -30,6 +31,7 @@ import org.springframework.http.server.PathContainer;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ResourceUtils;
|
import org.springframework.util.ResourceUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.util.UriUtils;
|
||||||
import org.springframework.web.util.pattern.PathPattern;
|
import org.springframework.web.util.pattern.PathPattern;
|
||||||
import org.springframework.web.util.pattern.PathPatternParser;
|
import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
|
|
||||||
|
@ -63,12 +65,16 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||||
|
|
||||||
pathContainer = this.pattern.extractPathWithinPattern(pathContainer);
|
pathContainer = this.pattern.extractPathWithinPattern(pathContainer);
|
||||||
String path = processPath(pathContainer.value());
|
String path = processPath(pathContainer.value());
|
||||||
if (path.contains("%")) {
|
if (!StringUtils.hasText(path) || isInvalidPath(path)) {
|
||||||
path = StringUtils.uriDecode(path, StandardCharsets.UTF_8);
|
|
||||||
}
|
|
||||||
if (!StringUtils.hasLength(path) || isInvalidPath(path)) {
|
|
||||||
return Mono.empty();
|
return Mono.empty();
|
||||||
}
|
}
|
||||||
|
if (isInvalidEncodedInputPath(path)) {
|
||||||
|
return Mono.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(this.location instanceof UrlResource)) {
|
||||||
|
path = UriUtils.decode(path, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Resource resource = this.location.createRelative(path);
|
Resource resource = this.location.createRelative(path);
|
||||||
|
@ -84,7 +90,47 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String processPath(String path) {
|
/**
|
||||||
|
* Process the given resource path.
|
||||||
|
* <p>The default implementation replaces:
|
||||||
|
* <ul>
|
||||||
|
* <li>Backslash with forward slash.
|
||||||
|
* <li>Duplicate occurrences of slash with a single slash.
|
||||||
|
* <li>Any combination of leading slash and control characters (00-1F and 7F)
|
||||||
|
* with a single "/" or "". For example {@code " / // foo/bar"}
|
||||||
|
* becomes {@code "/foo/bar"}.
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
protected String processPath(String path) {
|
||||||
|
path = StringUtils.replace(path, "\\", "/");
|
||||||
|
path = cleanDuplicateSlashes(path);
|
||||||
|
return cleanLeadingSlash(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String cleanDuplicateSlashes(String path) {
|
||||||
|
StringBuilder sb = null;
|
||||||
|
char prev = 0;
|
||||||
|
for (int i = 0; i < path.length(); i++) {
|
||||||
|
char curr = path.charAt(i);
|
||||||
|
try {
|
||||||
|
if (curr == '/' && prev == '/') {
|
||||||
|
if (sb == null) {
|
||||||
|
sb = new StringBuilder(path.substring(0, i));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (sb != null) {
|
||||||
|
sb.append(path.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
prev = curr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (sb != null ? sb.toString() : path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String cleanLeadingSlash(String path) {
|
||||||
boolean slash = false;
|
boolean slash = false;
|
||||||
for (int i = 0; i < path.length(); i++) {
|
for (int i = 0; i < path.length(); i++) {
|
||||||
if (path.charAt(i) == '/') {
|
if (path.charAt(i) == '/') {
|
||||||
|
@ -94,8 +140,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||||
if (i == 0 || (i == 1 && slash)) {
|
if (i == 0 || (i == 1 && slash)) {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
path = slash ? "/" + path.substring(i) : path.substring(i);
|
return (slash ? "/" + path.substring(i) : path.substring(i));
|
||||||
return path;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (slash ? "/" : "");
|
return (slash ? "/" : "");
|
||||||
|
@ -117,6 +162,31 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the given path contains invalid escape sequences.
|
||||||
|
* @param path the path to validate
|
||||||
|
* @return {@code true} if the path is invalid, {@code false} otherwise
|
||||||
|
*/
|
||||||
|
private boolean isInvalidEncodedInputPath(String path) {
|
||||||
|
if (path.contains("%")) {
|
||||||
|
try {
|
||||||
|
// Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars
|
||||||
|
String decodedPath = URLDecoder.decode(path, StandardCharsets.UTF_8);
|
||||||
|
if (isInvalidPath(decodedPath)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
decodedPath = processPath(decodedPath);
|
||||||
|
if (isInvalidPath(decodedPath)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException ex) {
|
||||||
|
// May not be possible to decode...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isResourceUnderLocation(Resource resource) throws IOException {
|
private boolean isResourceUnderLocation(Resource resource) throws IOException {
|
||||||
if (resource.getClass() != this.location.getClass()) {
|
if (resource.getClass() != this.location.getClass()) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -142,15 +212,24 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
|
locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
|
||||||
if (!resourcePath.startsWith(locationPath)) {
|
return (resourcePath.startsWith(locationPath) && !isInvalidEncodedInputPath(resourcePath));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (resourcePath.contains("%") && StringUtils.uriDecode(resourcePath, StandardCharsets.UTF_8).contains("../")) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isInvalidEncodedResourcePath(String resourcePath) {
|
||||||
|
if (resourcePath.contains("%")) {
|
||||||
|
// Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars...
|
||||||
|
try {
|
||||||
|
String decodedPath = URLDecoder.decode(resourcePath, StandardCharsets.UTF_8);
|
||||||
|
if (decodedPath.contains("../") || decodedPath.contains("..\\")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException ex) {
|
||||||
|
// May not be possible to decode...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.web.servlet.function;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
|
import java.net.URLDecoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
@ -29,6 +30,8 @@ import org.springframework.http.server.PathContainer;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ResourceUtils;
|
import org.springframework.util.ResourceUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.context.support.ServletContextResource;
|
||||||
|
import org.springframework.web.util.UriUtils;
|
||||||
import org.springframework.web.util.pattern.PathPattern;
|
import org.springframework.web.util.pattern.PathPattern;
|
||||||
import org.springframework.web.util.pattern.PathPatternParser;
|
import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
|
|
||||||
|
@ -36,6 +39,7 @@ import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
* Lookup function used by {@link RouterFunctions#resources(String, Resource)}.
|
* Lookup function used by {@link RouterFunctions#resources(String, Resource)}.
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
* @since 5.2
|
* @since 5.2
|
||||||
*/
|
*/
|
||||||
class PathResourceLookupFunction implements Function<ServerRequest, Optional<Resource>> {
|
class PathResourceLookupFunction implements Function<ServerRequest, Optional<Resource>> {
|
||||||
|
@ -62,12 +66,16 @@ class PathResourceLookupFunction implements Function<ServerRequest, Optional<Res
|
||||||
|
|
||||||
pathContainer = this.pattern.extractPathWithinPattern(pathContainer);
|
pathContainer = this.pattern.extractPathWithinPattern(pathContainer);
|
||||||
String path = processPath(pathContainer.value());
|
String path = processPath(pathContainer.value());
|
||||||
if (path.contains("%")) {
|
if (!StringUtils.hasText(path) || isInvalidPath(path)) {
|
||||||
path = StringUtils.uriDecode(path, StandardCharsets.UTF_8);
|
|
||||||
}
|
|
||||||
if (!StringUtils.hasLength(path) || isInvalidPath(path)) {
|
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
if (isInvalidEncodedInputPath(path)) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(this.location instanceof UrlResource)) {
|
||||||
|
path = UriUtils.decode(path, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Resource resource = this.location.createRelative(path);
|
Resource resource = this.location.createRelative(path);
|
||||||
|
@ -83,7 +91,47 @@ class PathResourceLookupFunction implements Function<ServerRequest, Optional<Res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String processPath(String path) {
|
/**
|
||||||
|
* Process the given resource path.
|
||||||
|
* <p>The default implementation replaces:
|
||||||
|
* <ul>
|
||||||
|
* <li>Backslash with forward slash.
|
||||||
|
* <li>Duplicate occurrences of slash with a single slash.
|
||||||
|
* <li>Any combination of leading slash and control characters (00-1F and 7F)
|
||||||
|
* with a single "/" or "". For example {@code " / // foo/bar"}
|
||||||
|
* becomes {@code "/foo/bar"}.
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
protected String processPath(String path) {
|
||||||
|
path = StringUtils.replace(path, "\\", "/");
|
||||||
|
path = cleanDuplicateSlashes(path);
|
||||||
|
return cleanLeadingSlash(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String cleanDuplicateSlashes(String path) {
|
||||||
|
StringBuilder sb = null;
|
||||||
|
char prev = 0;
|
||||||
|
for (int i = 0; i < path.length(); i++) {
|
||||||
|
char curr = path.charAt(i);
|
||||||
|
try {
|
||||||
|
if ((curr == '/') && (prev == '/')) {
|
||||||
|
if (sb == null) {
|
||||||
|
sb = new StringBuilder(path.substring(0, i));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (sb != null) {
|
||||||
|
sb.append(path.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
prev = curr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb != null ? sb.toString() : path;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String cleanLeadingSlash(String path) {
|
||||||
boolean slash = false;
|
boolean slash = false;
|
||||||
for (int i = 0; i < path.length(); i++) {
|
for (int i = 0; i < path.length(); i++) {
|
||||||
if (path.charAt(i) == '/') {
|
if (path.charAt(i) == '/') {
|
||||||
|
@ -93,8 +141,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Optional<Res
|
||||||
if (i == 0 || (i == 1 && slash)) {
|
if (i == 0 || (i == 1 && slash)) {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
path = slash ? "/" + path.substring(i) : path.substring(i);
|
return (slash ? "/" + path.substring(i) : path.substring(i));
|
||||||
return path;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (slash ? "/" : "");
|
return (slash ? "/" : "");
|
||||||
|
@ -113,6 +160,26 @@ class PathResourceLookupFunction implements Function<ServerRequest, Optional<Res
|
||||||
return path.contains("..") && StringUtils.cleanPath(path).contains("../");
|
return path.contains("..") && StringUtils.cleanPath(path).contains("../");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isInvalidEncodedInputPath(String path) {
|
||||||
|
if (path.contains("%")) {
|
||||||
|
try {
|
||||||
|
// Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars
|
||||||
|
String decodedPath = URLDecoder.decode(path, StandardCharsets.UTF_8);
|
||||||
|
if (isInvalidPath(decodedPath)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
decodedPath = processPath(decodedPath);
|
||||||
|
if (isInvalidPath(decodedPath)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException ex) {
|
||||||
|
// May not be possible to decode...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isResourceUnderLocation(Resource resource) throws IOException {
|
private boolean isResourceUnderLocation(Resource resource) throws IOException {
|
||||||
if (resource.getClass() != this.location.getClass()) {
|
if (resource.getClass() != this.location.getClass()) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -129,6 +196,10 @@ class PathResourceLookupFunction implements Function<ServerRequest, Optional<Res
|
||||||
resourcePath = classPathResource.getPath();
|
resourcePath = classPathResource.getPath();
|
||||||
locationPath = StringUtils.cleanPath(((ClassPathResource) this.location).getPath());
|
locationPath = StringUtils.cleanPath(((ClassPathResource) this.location).getPath());
|
||||||
}
|
}
|
||||||
|
else if (resource instanceof ServletContextResource servletContextResource) {
|
||||||
|
resourcePath = servletContextResource.getPath();
|
||||||
|
locationPath = StringUtils.cleanPath(((ServletContextResource) this.location).getPath());
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
resourcePath = resource.getURL().getPath();
|
resourcePath = resource.getURL().getPath();
|
||||||
locationPath = StringUtils.cleanPath(this.location.getURL().getPath());
|
locationPath = StringUtils.cleanPath(this.location.getURL().getPath());
|
||||||
|
@ -138,13 +209,24 @@ class PathResourceLookupFunction implements Function<ServerRequest, Optional<Res
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
|
locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
|
||||||
if (!resourcePath.startsWith(locationPath)) {
|
return (resourcePath.startsWith(locationPath) && !isInvalidEncodedResourcePath(resourcePath));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return !resourcePath.contains("%") ||
|
|
||||||
!StringUtils.uriDecode(resourcePath, StandardCharsets.UTF_8).contains("../");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isInvalidEncodedResourcePath(String resourcePath) {
|
||||||
|
if (resourcePath.contains("%")) {
|
||||||
|
// Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars...
|
||||||
|
try {
|
||||||
|
String decodedPath = URLDecoder.decode(resourcePath, StandardCharsets.UTF_8);
|
||||||
|
if (decodedPath.contains("../") || decodedPath.contains("..\\")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException ex) {
|
||||||
|
// May not be possible to decode...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
Loading…
Reference in New Issue