Skip to content

Commit

Permalink
put_aws_sigv4: Fix path encoding (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehodgkiss authored Jun 23, 2024
1 parent a7ce008 commit deae87c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/req/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ defmodule Req.Utils do
canonical_headers =
Enum.map_intersperse(canonical_headers, "\n", fn {name, value} -> [name, ":", value] end)

path = URI.encode(url.path || "/", &(&1 == ?/ or URI.char_unreserved?(&1)))

canonical_request = """
#{method}
#{url.path || "/"}
#{path}
#{url.query || ""}
#{canonical_headers}
Expand Down Expand Up @@ -137,6 +139,8 @@ defmodule Req.Utils do
&String.downcase(elem(&1, 0), :ascii)
)

path = URI.encode(url.path || "/", &(&1 == ?/ or URI.char_unreserved?(&1)))

true = url.query in [nil, ""]

method = method |> Atom.to_string() |> String.upcase()
Expand All @@ -146,7 +150,7 @@ defmodule Req.Utils do

canonical_request = """
#{method}
#{url.path || "/"}
#{path}
#{canonical_query_string}
#{canonical_headers}
Expand Down
8 changes: 4 additions & 4 deletions test/req/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Req.UtilsTest do
service: "s3",
datetime: ~U[2024-01-01 09:00:00Z],
method: :get,
url: "https://s3",
url: "https://s3/foo/:bar",
headers: [{"host", "s3"}],
body: ""
]
Expand Down Expand Up @@ -50,20 +50,20 @@ defmodule Req.UtilsTest do
service: "s3",
datetime: ~U[2024-01-01 09:00:00Z],
method: :get,
url: "https://s3"
url: "https://s3/foo/:bar"
]

url1 = to_string(Req.Utils.aws_sigv4_url(options))

url2 =
"""
https://s3?\
https://s3/foo/:bar?\
X-Amz-Algorithm=AWS4-HMAC-SHA256\
&X-Amz-Credential=dummy-access-key-id%2F20240101%2Fdummy-region%2Fs3%2Faws4_request\
&X-Amz-Date=20240101T090000Z\
&X-Amz-Expires=86400\
&X-Amz-SignedHeaders=host\
&X-Amz-Signature=684b112675beaf7f858dbf650cc12c5aa3d0eeb15fa4038ea809149f3c6476e3\
&X-Amz-Signature=7fd16f0749b0902acde5a3d8933315006f2993b279b995cad880165ff4be75ff\
"""

assert url1 == url2
Expand Down

0 comments on commit deae87c

Please sign in to comment.