Improve URI/query strings sanitization

This commit is contained in:
Сергей Цыпанов
2020-11-02 12:49:00 +02:00
committed by Juergen Hoeller
parent b077e4cd85
commit 0015fd6734
6 changed files with 47 additions and 30 deletions

View File

@@ -1023,15 +1023,21 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
if (this.path.length() == 0) {
return null;
}
String path = this.path.toString();
while (true) {
int index = path.indexOf("//");
if (index == -1) {
break;
String sanitized = getSanitizedPath(this.path);
return new HierarchicalUriComponents.FullPathComponent(sanitized);
}
private static String getSanitizedPath(final StringBuilder path) {
int index = path.indexOf("//");
if (index >= 0) {
StringBuilder sanitized = new StringBuilder(path);
while (index != -1) {
sanitized.deleteCharAt(index);
index = sanitized.indexOf("//", index);
}
path = path.substring(0, index) + path.substring(index + 1);
return sanitized.toString();
}
return new HierarchicalUriComponents.FullPathComponent(path);
return path.toString();
}
public void removeTrailingSlash() {

View File

@@ -401,18 +401,17 @@ public class UrlPathHelper {
* <li>replace all "//" by "/"</li>
* </ul>
*/
private String getSanitizedPath(final String path) {
String sanitized = path;
while (true) {
int index = sanitized.indexOf("//");
if (index < 0) {
break;
}
else {
sanitized = sanitized.substring(0, index) + sanitized.substring(index + 1);
private static String getSanitizedPath(final String path) {
int index = path.indexOf("//");
if (index >= 0) {
StringBuilder sanitized = new StringBuilder(path);
while (index != -1) {
sanitized.deleteCharAt(index);
index = sanitized.indexOf("//", index);
}
return sanitized.toString();
}
return sanitized;
return path;
}
/**
@@ -612,15 +611,20 @@ public class UrlPathHelper {
removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
}
private String removeSemicolonContentInternal(String requestUri) {
private static String removeSemicolonContentInternal(String requestUri) {
int semicolonIndex = requestUri.indexOf(';');
if (semicolonIndex == -1) {
return requestUri;
}
StringBuilder sb = new StringBuilder(requestUri);
while (semicolonIndex != -1) {
int slashIndex = requestUri.indexOf('/', semicolonIndex);
String start = requestUri.substring(0, semicolonIndex);
requestUri = (slashIndex != -1) ? start + requestUri.substring(slashIndex) : start;
semicolonIndex = requestUri.indexOf(';', semicolonIndex);
if (slashIndex >= 0) {
sb.delete(semicolonIndex, slashIndex);
}
semicolonIndex = sb.indexOf(";", semicolonIndex);
}
return requestUri;
return sb.toString();
}
private String removeJsessionid(String requestUri) {