Use Charset variants of URLEncoder and URLDecoder methods

This commit is contained in:
Christoph Dreis
2021-10-13 14:38:58 +02:00
committed by Juergen Hoeller
parent 2fba0bc272
commit 5c972fcc54
11 changed files with 27 additions and 46 deletions

View File

@@ -17,7 +17,6 @@
package org.springframework.web.servlet.resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@@ -295,14 +294,14 @@ public class PathResourceResolver extends AbstractResourceResolver {
if (resourcePath.contains("%")) {
// Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars...
try {
String decodedPath = URLDecoder.decode(resourcePath, "UTF-8");
String decodedPath = URLDecoder.decode(resourcePath, StandardCharsets.UTF_8);
if (decodedPath.contains("../") || decodedPath.contains("..\\")) {
logger.warn("Resolved resource path contains encoded \"../\" or \"..\\\": " + resourcePath);
return true;
}
}
catch (IllegalArgumentException | UnsupportedEncodingException ex) {
// May not be possible to decode... | Should never happen...
catch (IllegalArgumentException ex) {
// May not be possible to decode...
}
}
return false;

View File

@@ -17,9 +17,9 @@
package org.springframework.web.servlet.resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -694,7 +694,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
if (path.contains("%")) {
try {
// Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars
String decodedPath = URLDecoder.decode(path, "UTF-8");
String decodedPath = URLDecoder.decode(path, StandardCharsets.UTF_8);
if (isInvalidPath(decodedPath)) {
return true;
}
@@ -703,8 +703,8 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
return true;
}
}
catch (IllegalArgumentException | UnsupportedEncodingException ex) {
// May not be possible to decode... | Should never happen...
catch (IllegalArgumentException ex) {
// May not be possible to decode...
}
}
return false;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.web.servlet.support;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -291,7 +292,7 @@ public class FlashMapManagerTests {
@Test // SPR-12569
public void flashAttributesWithQueryParamsWithSpace() throws Exception {
String encodedValue = URLEncoder.encode("1 2", "UTF-8");
String encodedValue = URLEncoder.encode("1 2", StandardCharsets.UTF_8);
FlashMap flashMap = new FlashMap();
flashMap.put("key", "value");