Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ex_aws/auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ defmodule ExAws.Auth do
end

defp signature(http_method, url, query, headers, body, service, datetime, config) do
path = url |> Url.get_path(service) |> Url.uri_encode()
path = url |> Url.get_path(service)
request = build_canonical_request(http_method, path, query, headers, body)
string_to_sign = string_to_sign(request, service, datetime, config)
Signatures.generate_signature_v4(service, config, datetime, string_to_sign)
Expand Down
6 changes: 6 additions & 0 deletions lib/ex_aws/operation/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule ExAws.Operation.S3 do
url =
operation
|> add_resource_to_params()
|> encode_path_query_fragment_into_path()
|> ExAws.Request.Url.build(config)

hashed_payload = ExAws.Auth.Utils.hash_sha256(body)
Expand All @@ -42,6 +43,11 @@ defmodule ExAws.Operation.S3 do
|> operation.parser.()
end

def encode_path_query_fragment_into_path(%ExAws.Operation.S3{path: path} = operation) do
new_path = ExAws.Request.Url.uri_encode(path)
%ExAws.Operation.S3{operation | path: new_path}
end

def stream!(%{stream_builder: fun}, config), do: fun.(config)

defp put_content_length_header(headers, "", :get), do: headers
Expand Down
3 changes: 1 addition & 2 deletions lib/ex_aws/request/url.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule ExAws.Request.Url do
|> convert_port_to_integer
|> (&struct(URI, &1)).()
|> URI.to_string()
|> String.trim_trailing("?")
|> String.replace_suffix("?", "")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed to support text.txt? object, since if the object name ends with the question mark, the URI.to_string will add another question mark and the String.trim_trailing/2 will remove both.

end

defp query(operation) do
Expand Down Expand Up @@ -52,7 +52,6 @@ defmodule ExAws.Request.Url do
url
|> get_path(service)
|> String.replace_prefix("/", "")
|> uri_encode()

query =
case String.split(url, "?", parts: 2) do
Expand Down
2 changes: 1 addition & 1 deletion test/ex_aws/auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ defmodule ExAws.AuthTest do
"&X-Amz-Date=20130524T000000Z" <>
"&X-Amz-Expires=86400" <>
"&X-Amz-SignedHeaders=host" <>
"&X-Amz-Signature=d1892eeaf3110a6c1a805d8ad7a0c825a72a4255c7f48908922be55a7c4ae753"
"&X-Amz-Signature=a515d441eea0607e063a287d32c9070d5d473bc5e47f4e8d61701e0a1aec84bc"

assert {:ok, expected} == actual
end
Expand Down
107 changes: 107 additions & 0 deletions test/ex_aws/operation/s3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule ExAws.Operation.S3Test do

alias Elixir.ExAws.Operation.ExAws.Operation.S3

import Mox

def s3_operation(bucket \\ "my-bucket-1") do
%ExAws.Operation.S3{
body: "",
Expand Down Expand Up @@ -80,4 +82,109 @@ defmodule ExAws.Operation.S3Test do
{processed_operation, _processed_config} = S3.add_bucket_to_path(operation, config)
assert processed_operation.path == "/folder/"
end

test "S3 object encoding with query parameter seperator (?)" do
config = %{
http_client: ExAws.Request.HttpMock,
json_codec: JSX,
access_key_id: "AKIAIOSFODNN7EXAMPLE",
secret_access_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
region: "us-east-1",
retries: [
max_attempts: 5,
base_backoff_in_ms: 1,
max_backoff_in_ms: 20
],
normalize_path: false,
scheme: "https://",
host: "s3.amazonaws.com",
virtual_host: true,
port: 443
}
Comment on lines +87 to +103
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was put together just to check if the test is valid. This should be improved and moved to a separate test.


headers = %{
"x-amz-bucket-region" => "us-east-1",
"x-amz-content-sha256" => ExAws.Auth.Utils.hash_sha256(""),
"content-length" => byte_size("")
}

expect(
ExAws.Request.HttpMock,
:request,
fn _method, url, _body, _headers, _opts ->
assert url == "https://examplebucket.s3.amazonaws.com/test%20hello%20%233.txt%3Facl%3D21"
{:ok, %{status_code: 200}}
end
)

operation = %ExAws.Operation.S3{
s3_operation()
| path: "test hello #3.txt?acl=21",
headers: headers,
bucket: "examplebucket"
}

assert {:ok, %{status_code: 200}} == S3.perform(operation, config)
end

test "S3 object encoding with query parameter seperator (?) and version_id" do
config = %{
http_client: ExAws.Request.HttpMock,
json_codec: JSX,
access_key_id: "AKIAIOSFODNN7EXAMPLE",
secret_access_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
region: "us-east-1",
retries: [
max_attempts: 5,
base_backoff_in_ms: 1,
max_backoff_in_ms: 20
],
normalize_path: false,
scheme: "https://",
host: "s3.amazonaws.com",
virtual_host: true,
port: 443
}

headers = %{
"x-amz-bucket-region" => "us-east-1",
"x-amz-content-sha256" => ExAws.Auth.Utils.hash_sha256(""),
"content-length" => byte_size("")
}

expect(
ExAws.Request.HttpMock,
:request,
fn _method, url, _body, _headers, _opts ->
assert url ==
"https://examplebucket.s3.amazonaws.com/test%20hello%20%233.txt%3Facl%3D21?version-id=v1"

{:ok, %{status_code: 200}}
end
)

operation = %ExAws.Operation.S3{
s3_operation()
| path: "test hello #3.txt?acl=21",
headers: headers,
bucket: "examplebucket",
params: %{"version-id" => "v1"}
}

assert {:ok, %{status_code: 200}} == S3.perform(operation, config)
end

describe "encode_path_query_fragment_into_path" do
test "fetch object ending in question mark (?)" do
%{path: "object.txt%3F"} =
%ExAws.Operation.S3{path: "object.txt?"}
|> S3.encode_path_query_fragment_into_path()
end

test "fetch object with name contains query parameters" do
%{path: "object.txt%3Fid%3D3"} =
%ExAws.Operation.S3{path: "object.txt?id=3"}
|> S3.encode_path_query_fragment_into_path()
end
end
end
4 changes: 4 additions & 0 deletions test/ex_aws/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ defmodule ExAws.RequestTest do
}}
end

# URL encoding is done under ExAws.Operation.S3
@tag :skip
test "handles encoding S3 URLs with params", context do
http_method = :get
url = "https://examplebucket.s3.amazonaws.com/test hello #3.txt?acl=21"
Expand Down Expand Up @@ -109,6 +111,8 @@ defmodule ExAws.RequestTest do
}}
end

# URL encoding is done under ExAws.Operation.S3
@tag :skip
test "handles encoding S3 URLs without params", context do
http_method = :get
url = "https://examplebucket.s3.amazonaws.com/up//double//test hello+#3.txt"
Expand Down