Fix URL encoding problem for HTTP signatures (#6637)

This commit is contained in:
Sebastien Rosset 2020-06-12 09:02:45 -07:00 committed by GitHub
parent a0f229302d
commit 3626bc4bf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 20 deletions

View File

@ -234,16 +234,12 @@ public class HttpSignatureAuth implements Authentication {
throw new ApiException("Signer cannot be null. Please call the method `setPrivateKey` to set it up correctly"); throw new ApiException("Signer cannot be null. Please call the method `setPrivateKey` to set it up correctly");
} }
// construct the path with the URL query string // construct the path with the URL-encoded path and query.
String path = uri.getPath(); // Calling getRawPath and getRawQuery ensures the path is URL-encoded as it will be serialized
// on the wire. The HTTP signature must use the encode URL as it is sent on the wire.
List<String> urlQueries = new ArrayList<String>(); String path = uri.getRawPath();
for (Pair queryParam : queryParams) { if (uri.getRawQuery() != "") {
urlQueries.add(queryParam.getName() + "=" + URLEncoder.encode(queryParam.getValue(), "utf8").replaceAll("\\+", "%20")); path += "?" + uri.getRawQuery();
}
if (!urlQueries.isEmpty()) {
path = path + "?" + String.join("&", urlQueries);
} }
headerParams.put("Authorization", signer.sign(method, path, headerParams).toString()); headerParams.put("Authorization", signer.sign(method, path, headerParams).toString());

View File

@ -245,16 +245,12 @@ public class HttpSignatureAuth implements Authentication {
throw new ApiException("Signer cannot be null. Please call the method `setPrivateKey` to set it up correctly"); throw new ApiException("Signer cannot be null. Please call the method `setPrivateKey` to set it up correctly");
} }
// construct the path with the URL query string // construct the path with the URL-encoded path and query.
String path = uri.getPath(); // Calling getRawPath and getRawQuery ensures the path is URL-encoded as it will be serialized
// on the wire. The HTTP signature must use the encode URL as it is sent on the wire.
List<String> urlQueries = new ArrayList<String>(); String path = uri.getRawPath();
for (Pair queryParam : queryParams) { if (uri.getRawQuery() != "") {
urlQueries.add(queryParam.getName() + "=" + URLEncoder.encode(queryParam.getValue(), "utf8").replaceAll("\\+", "%20")); path += "?" + uri.getRawQuery();
}
if (!urlQueries.isEmpty()) {
path = path + "?" + String.join("&", urlQueries);
} }
headerParams.put("Authorization", signer.sign(method, path, headerParams).toString()); headerParams.put("Authorization", signer.sign(method, path, headerParams).toString());