Fix a bug where + before % is ignored

Achievement unlocked : fix a 10-year-old trivial bug

Bug: 1866121
Test: new CTS in Ib7ee866f65baf99b46a31e2115355a42a829421e
Change-Id: Ic91660d974dce21f2affdcacaeffe9accf8451ac
Merged-In: Ibe73a33264569b08cac4c00b4dda838f1e5551fc
Merged-In: Id4b1e456f7ce4e7aae84abb96fa55a8fe8f5caa5
This commit is contained in:
Chalard Jean
2019-04-19 14:58:51 +09:00
parent 0cab8dc0db
commit d55f2f3de7

View File

@@ -22,6 +22,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
@@ -837,15 +839,11 @@ public class UrlQuerySanitizer {
* @param string the escaped string
* @return the unescaped string.
*/
private static final Pattern plusOrPercent = Pattern.compile("[+%]");
public String unescape(String string) {
// Early exit if no escaped characters.
int firstEscape = string.indexOf('%');
if ( firstEscape < 0) {
firstEscape = string.indexOf('+');
if (firstEscape < 0) {
return string;
}
}
final Matcher matcher = plusOrPercent.matcher(string);
if (!matcher.find()) return string;
final int firstEscape = matcher.start();
int length = string.length();
@@ -855,8 +853,7 @@ public class UrlQuerySanitizer {
char c = string.charAt(i);
if (c == '+') {
c = ' ';
}
else if ( c == '%' && i + 2 < length) {
} else if (c == '%' && i + 2 < length) {
char c1 = string.charAt(i + 1);
char c2 = string.charAt(i + 2);
if (isHexDigit(c1) && isHexDigit(c2)) {