url decode non-base64 text (WEB-37256)

This commit is contained in:
Dennis Ushakov
2019-03-29 19:25:57 +03:00
parent 599f9f2278
commit 03d2d48480
2 changed files with 10 additions and 7 deletions
@@ -10,10 +10,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.regex.Matcher;
@@ -247,9 +244,9 @@ public class URLUtil {
String content = matcher.group(4);
return ";base64".equalsIgnoreCase(matcher.group(3))
? Base64.getDecoder().decode(content)
: content.getBytes(StandardCharsets.UTF_8);
: URLDecoder.decode(content, CharsetToolkit.UTF8).getBytes(StandardCharsets.UTF_8);
}
catch (IllegalArgumentException e) {
catch (IllegalArgumentException | UnsupportedEncodingException e) {
return null;
}
}
@@ -112,13 +112,19 @@ public class UrlUtilTest {
}
@Test
public void testDataUri() {
public void testDataUriBase64() {
byte[] test = "test".getBytes(CharsetToolkit.UTF8_CHARSET);
assertThat(URLUtil.getBytesFromDataUri("data:text/plain;charset=utf-8;base64,dGVzdA==")).isEqualTo(test);
// https://youtrack.jetbrains.com/issue/WEB-14581#comment=27-1014790
assertThat(URLUtil.getBytesFromDataUri("data:text/plain;charset:utf-8;base64,dGVzdA==")).isEqualTo(test);
}
@Test
public void testDataUri() {
byte[] test = "Hello world!".getBytes(CharsetToolkit.UTF8_CHARSET);
assertThat(URLUtil.getBytesFromDataUri("data:text/plain;charset=utf-8,Hello%20world!")).isEqualTo(test);
}
private static void doUrlTest(@NotNull final String line, @Nullable final String expectedUrl) {
final Matcher matcher = URLUtil.URL_PATTERN.matcher(line);
boolean found = matcher.find();