Skip to content

Commit

Permalink
Add empty value checks for delete_object
Browse files Browse the repository at this point in the history
Add checks for nil and empty string in delete_object.
Calling the function with empty values results in an S3 operation
that would try to delete the bucket itself.
  • Loading branch information
j-asser committed Sep 6, 2024
1 parent 2a0d691 commit d579a53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/ex_aws/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,24 @@ defmodule ExAws.S3 do
@spec delete_object(bucket :: binary, object :: binary) :: ExAws.Operation.S3.t()
@spec delete_object(bucket :: binary, object :: binary, opts :: delete_object_opts) ::
ExAws.Operation.S3.t()
@spec delete_object(bucket :: binary, object :: nil) :: no_return
@request_headers [
:x_amz_mfa,
:x_amz_request_payer,
:x_amz_bypass_governance_retention,
:x_amz_expected_bucket_owner
]
def delete_object(bucket, object, opts \\ []) do
def delete_object(bucket, object, opts \\ [])

def delete_object(_bucket, nil = _object, _opts) do
raise "object must not be nil"
end

def delete_object(_bucket, "" = _object, _opts) do
raise "object must not be empty string"
end

def delete_object(bucket, object, opts) do
opts = opts |> Map.new()

params =
Expand Down
12 changes: 12 additions & 0 deletions test/lib/s3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,18 @@ defmodule ExAws.S3Test do
assert expected == S3.delete_object("bucket", "object")
end

test "#delete_object object must not be nil" do
assert_raise RuntimeError, "object must not be nil", fn ->
S3.delete_object("bucket", nil)
end
end

test "#delete_object object must not be empty string" do
assert_raise RuntimeError, "object must not be empty string", fn ->
S3.delete_object("bucket", "")
end
end

test "#delete_object version_id option" do
expected = %Operation.S3{
body: "",
Expand Down

0 comments on commit d579a53

Please sign in to comment.