Fix issue-4539 (#4546)

* Protect double URI encoding

* removeExtraLine
This commit is contained in:
Akash Dwivedi 2017-07-19 09:38:29 -07:00 committed by Fangjin Yang
parent b64dbbb5b5
commit 0b85c60869
2 changed files with 31 additions and 3 deletions

View File

@ -23,9 +23,9 @@ import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Inject;
import io.druid.indexing.overlord.TaskMaster;
import io.druid.java.util.common.StringUtils;
import io.druid.server.http.RedirectInfo;
import java.net.URI;
import java.net.URL;
import java.util.Set;
@ -59,9 +59,15 @@ public class OverlordRedirectInfo implements RedirectInfo
final String leader = taskMaster.getCurrentLeader();
if (leader == null || leader.isEmpty()) {
return null;
} else {
return new URI(scheme, leader, requestURI, queryString, null).toURL();
}
String location = StringUtils.format("%s://%s%s", scheme, leader, requestURI);
if (queryString != null) {
location = StringUtils.format("%s?%s", location, queryString);
}
return new URL(location);
}
catch (Exception e) {
throw Throwables.propagate(e);

View File

@ -25,7 +25,9 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
public class OverlordRedirectInfoTest
{
@ -95,4 +97,24 @@ public class OverlordRedirectInfoTest
Assert.assertEquals("http://localhost/request?foo=bar&x=y", url.toString());
EasyMock.verify(taskMaster);
}
@Test
public void testGetRedirectURLWithEncodedCharacter() throws UnsupportedEncodingException
{
String host = "localhost";
String request = "/druid/indexer/v1/task/" + URLEncoder.encode(
"index_hadoop_datasource_2017-07-12T07:43:01.495Z",
"UTF-8"
) + "/status";
EasyMock.expect(taskMaster.getCurrentLeader()).andReturn(host).anyTimes();
EasyMock.replay(taskMaster);
URL url = redirectInfo.getRedirectURL("http", null, request);
Assert.assertEquals(
"http://localhost/druid/indexer/v1/task/index_hadoop_datasource_2017-07-12T07%3A43%3A01.495Z/status",
url.toString()
);
EasyMock.verify(taskMaster);
}
}