Skip to content

Commit 11afde4

Browse files
balamuruganaharshavardhana
authored andcommitted
fix: encode query parameters properly. (#454)
As okhttp does not encode query parameters for requirements, this patch fixes it by having separate encoding function. Fixes #453
1 parent 6607f8c commit 11afde4

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/main/java/io/minio/MinioClient.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.minio;
1818

19+
import com.google.common.net.UrlEscapers;
20+
import com.google.common.escape.Escaper;
1921
import com.google.common.io.ByteStreams;
2022
import com.google.gson.Gson;
2123
import com.squareup.okhttp.HttpUrl;
@@ -126,6 +128,7 @@
126128
*/
127129
@SuppressWarnings({"SameParameterValue", "WeakerAccess"})
128130
public final class MinioClient {
131+
private static final Escaper QUERY_ESCAPER = UrlEscapers.urlPathSegmentEscaper();
129132
private static final Logger LOGGER = Logger.getLogger(MinioClient.class.getName());
130133
// maximum allowed object size is 5TiB
131134
private static final long MAX_OBJECT_SIZE = 5L * 1024 * 1024 * 1024 * 1024;
@@ -590,6 +593,29 @@ public void setTimeout(long connectTimeout, long writeTimeout, long readTimeout)
590593
httpClient.setReadTimeout(readTimeout, TimeUnit.MILLISECONDS);
591594
}
592595

596+
/**
597+
* Returns encoded query string for URL.
598+
*/
599+
private static String encodeQueryString(String str) {
600+
return QUERY_ESCAPER.escape(str)
601+
.replaceAll("\\!", "%21")
602+
.replaceAll("\\$", "%24")
603+
.replaceAll("\\&", "%26")
604+
.replaceAll("\\'", "%27")
605+
.replaceAll("\\(", "%28")
606+
.replaceAll("\\)", "%29")
607+
.replaceAll("\\*", "%2A")
608+
.replaceAll("\\+", "%2B")
609+
.replaceAll("\\,", "%2C")
610+
.replaceAll("\\\\", "%2F")
611+
.replaceAll("\\:", "%3A")
612+
.replaceAll("\\;", "%3B")
613+
.replaceAll("\\=", "%3D")
614+
.replaceAll("\\@", "%40")
615+
.replaceAll("\\[", "%5B")
616+
.replaceAll("\\]", "%5D");
617+
}
618+
593619
/**
594620
* Creates Request object for given request parameters.
595621
*
@@ -658,7 +684,8 @@ private Request createRequest(Method method, String bucketName, String objectNam
658684

659685
if (queryParamMap != null) {
660686
for (Map.Entry<String,String> entry : queryParamMap.entrySet()) {
661-
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
687+
urlBuilder.addEncodedQueryParameter(encodeQueryString(entry.getKey()),
688+
encodeQueryString(entry.getValue()));
662689
}
663690
}
664691

src/main/java/io/minio/Signer.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import com.squareup.okhttp.Request;
3131
import com.squareup.okhttp.HttpUrl;
3232
import com.squareup.okhttp.Headers;
33-
import com.google.common.net.UrlEscapers;
34-
import com.google.common.escape.Escaper;
3533
import com.google.common.base.Joiner;
3634
import com.google.common.io.BaseEncoding;
3735

@@ -42,7 +40,6 @@
4240
* Amazon AWS S3 signature V4 signer.
4341
*/
4442
class Signer {
45-
public static final Escaper QUERY_ESCAPER = UrlEscapers.urlPathSegmentEscaper();
4643
//
4744
// Excerpts from @lsegal - https://github.com/aws/aws-sdk-js/issues/659#issuecomment-120477258
4845
//
@@ -154,9 +151,9 @@ private void setCanonicalQueryString() {
154151
for (String queryParam : encodedQuery.split("&")) {
155152
String[] tokens = queryParam.split("=");
156153
if (tokens.length > 1) {
157-
signedQueryParams.put(QUERY_ESCAPER.escape(tokens[0]), QUERY_ESCAPER.escape(tokens[1]));
154+
signedQueryParams.put(tokens[0], tokens[1]);
158155
} else {
159-
signedQueryParams.put(QUERY_ESCAPER.escape(tokens[0]), "");
156+
signedQueryParams.put(tokens[0], "");
160157
}
161158
}
162159

0 commit comments

Comments
 (0)