Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address virtual host style addressing bugs for short subdomains provided in endpoint_url #2941

Merged
merged 3 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-EndpointProvider-38157.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "EndpointProvider",
"description": "Fixed bug in virtual addressing for S3 Buckets `#2938 <https://github.com/boto/botocore/issues/2938>`__"
}
10 changes: 3 additions & 7 deletions botocore/endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,9 @@ def aws_is_virtual_hostable_s3_bucket(self, value, allow_subdomains):
):
return False

if allow_subdomains is True:
return all(
self.aws_is_virtual_hostable_s3_bucket(label, False)
for label in value.split(".")
)

return self.is_valid_host_label(value, allow_subdomains=False)
return self.is_valid_host_label(
value, allow_subdomains=allow_subdomains
)


# maintains backwards compatibility as `Library` was misspelled
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3487,6 +3487,24 @@ def _addressing_for_presigned_url_test_cases():
signature_version="s3",
expected_url="https://s3.us-west-1.amazonaws.com/foo.bar.biz/key",
)
# Bucket names that contain dots and subcomponents that are less than
# 3 characters should still use virtual host style addressing if
# configured by the customer and they provide their own ``endpoint_url``
# that is insecure. https://github.com/boto/botocore/issues/2938
yield dict(
bucket="foo.b.biz",
key="key",
s3_config={"addressing_style": "virtual"},
customer_provided_endpoint="http://s3.us-west-2.amazonaws.com",
expected_url="http://foo.b.biz.s3.us-west-2.amazonaws.com/key",
)
yield dict(
bucket="foo.b.biz",
key="key",
s3_config={"addressing_style": "virtual"},
customer_provided_endpoint="https://s3.us-west-2.amazonaws.com",
expected_url="https://s3.us-west-2.amazonaws.com/foo.b.biz/key",
)


@pytest.mark.parametrize(
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,23 @@ def test_endpoint_reevaluates_result(endpoint_provider, monkeypatch):
for region in regions:
endpoint_provider.resolve_endpoint(Region=region)
assert mock_evaluate.call_count == 2


@pytest.mark.parametrize(
"bucket, expected_value",
[
("mybucket", True),
("ab", False),
("a.b", True),
("my.great.bucket.aws.com", True),
("mY.GREAT.bucket.aws.com", False),
("192.168.1.1", False),
],
)
def test_aws_is_virtual_hostable_s3_bucket_allow_subdomains(
rule_lib, bucket, expected_value
):
assert (
rule_lib.aws_is_virtual_hostable_s3_bucket(bucket, True)
== expected_value
)