diff --git a/packages/smithy-core/.changes/next-release/smithy-core-bugfix-e440954e5bb743a993fe44072423df0c.json b/packages/smithy-core/.changes/next-release/smithy-core-bugfix-e440954e5bb743a993fe44072423df0c.json new file mode 100644 index 000000000..e4ffc7184 --- /dev/null +++ b/packages/smithy-core/.changes/next-release/smithy-core-bugfix-e440954e5bb743a993fe44072423df0c.json @@ -0,0 +1,4 @@ +{ + "type": "bugfix", + "description": "Reject endpoint URLs resolved from endpoint rules that contain userinfo or a fragment." +} \ No newline at end of file diff --git a/packages/smithy-core/src/smithy_core/endpoints/_engine.py b/packages/smithy-core/src/smithy_core/endpoints/_engine.py index 8def3752a..82b2d822b 100644 --- a/packages/smithy-core/src/smithy_core/endpoints/_engine.py +++ b/packages/smithy-core/src/smithy_core/endpoints/_engine.py @@ -443,9 +443,13 @@ def _is_literal(expr: Any) -> bool: def _to_uri(url: str) -> URI: """The URI of a rendered URL.""" try: - return parse_uri(url) + uri = parse_uri(url) except ExpectationNotMetError as e: raise EndpointResolutionError(f"Invalid endpoint URI: {url}") from e + # Userinfo or a fragment can move the host a request goes to. + if uri.username is not None or uri.password is not None or uri.fragment is not None: + raise EndpointResolutionError(f"Invalid endpoint URI: {url}") + return uri def _literal(value: Any) -> _Closure: diff --git a/packages/smithy-core/tests/unit/endpoints/test_engine.py b/packages/smithy-core/tests/unit/endpoints/test_engine.py index 3efa73b73..208ca79eb 100644 --- a/packages/smithy-core/tests/unit/endpoints/test_engine.py +++ b/packages/smithy-core/tests/unit/endpoints/test_engine.py @@ -231,6 +231,20 @@ def test_rendered_urls_must_be_uris() -> None: resolve(_one(endpoint((Tag.REF, "S"))), parameters={"S": "example.com"}) +@pytest.mark.parametrize( + "url", + [ + "https://user@127.0.0.1/", + "https://user:pass@example.com/", + "https://example.com/#fragment", + "https://aaaa@127.0.0.1:4443/#.example.com", + ], +) +def test_rendered_urls_must_not_have_userinfo_or_fragments(url: str) -> None: + with pytest.raises(EndpointResolutionError, match="Invalid endpoint URI"): + resolve(_one(endpoint((Tag.REF, "S"))), parameters={"S": url}) + + def test_auth_schemes_no_extension_maps_fail() -> None: properties = (Tag.MAP, ("authSchemes",), (Tag.LIST, (Tag.MAP, ("name",), "token"))) with pytest.raises(EndpointResolutionError, match="no extension provides: token"):