Skip to content

Fix building auth metadata paths #779

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 10 additions & 22 deletions src/mcp/server/auth/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,19 @@ def create_auth_routes(
return routes


def modify_url_path(url: AnyHttpUrl, path_mapper: Callable[[str], str]) -> AnyHttpUrl:
return AnyHttpUrl.build(
scheme=url.scheme,
username=url.username,
password=url.password,
host=url.host,
port=url.port,
path=path_mapper(url.path or ""),
query=url.query,
fragment=url.fragment,
)


def build_metadata(
issuer_url: AnyHttpUrl,
service_documentation_url: AnyHttpUrl | None,
client_registration_options: ClientRegistrationOptions,
revocation_options: RevocationOptions,
) -> OAuthMetadata:
authorization_url = modify_url_path(
issuer_url, lambda path: path.rstrip("/") + AUTHORIZATION_PATH.lstrip("/")
authorization_url = AnyHttpUrl(
str(issuer_url).rstrip("/") + AUTHORIZATION_PATH
Copy link
Member

Choose a reason for hiding this comment

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

There's no need to strip in any of those.

Suggested change
str(issuer_url).rstrip("/") + AUTHORIZATION_PATH
urljoin(str(issuer_url), AUTHORIZATION_PATH)

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, @Kludex!

I initially thought the same — that urljoin(str(issuer_url), "/authorize") would be enough. But it actually breaks in some cases where the base_url includes a path.

For example, when the base_url is https://example.com/auth/oidc/op/Customer/, urljoin returns https://example.com/authorize, which drops the intended path entirely. That’s because urljoin treats the /authorize as an absolute path and replaces everything after the domain.

To illustrate this, I added a test suite comparing both approaches:

import unittest
from urllib.parse import urljoin
from pydantic import AnyHttpUrl

class TestModifyUrlPath(unittest.TestCase):
    def test_append_authorize_to_urls_with_urljoin(self):
        """Test appending /authorize to various URL formats using urljoin"""
        test_cases = [
            ("https://example.com", "https://example.com/authorize"),
            ("https://example.com/", "https://example.com/authorize"),
            ("https://example.com/auth/oidc/op/Customer", "https://example.com/auth/oidc/op/Customer/authorize"),
            ("https://example.com/auth/oidc/op/Customer/", "https://example.com/auth/oidc/op/Customer/authorize"),
            ("http://localhost:8000", "http://localhost:8000/authorize"),
            ("http://localhost:8000/", "http://localhost:8000/authorize"),
        ]

        for base_url, expected in test_cases:
            any_http_url = AnyHttpUrl(base_url)
            with self.subTest(base_url=any_http_url):
                result = AnyHttpUrl(urljoin(str(any_http_url), "/authorize"))
                self.assertEqual(result, AnyHttpUrl(expected))

    def test_append_authorize_to_urls_with_rstrip(self):
        """Test appending /authorize to various URL formats using rstrip"""
        test_cases = [
            ("https://example.com", "https://example.com/authorize"),
            ("https://example.com/", "https://example.com/authorize"),
            ("https://example.com/auth/oidc/op/Customer", "https://example.com/auth/oidc/op/Customer/authorize"),
            ("https://example.com/auth/oidc/op/Customer/", "https://example.com/auth/oidc/op/Customer/authorize"),
            ("http://localhost:8000", "http://localhost:8000/authorize"),
            ("http://localhost:8000/", "http://localhost:8000/authorize"),
        ]

        for base_url, expected in test_cases:
            any_http_url = AnyHttpUrl(base_url)
            with self.subTest(base_url=any_http_url):
                result = AnyHttpUrl(str(any_http_url).rstrip("/") + "/authorize")
                self.assertEqual(result, AnyHttpUrl(expected))

if __name__ == "__main__":
    unittest.main()

So for consistency across all cases, I believe we need to keep the rstrip("/") approach.

Let me know what you think!

)
token_url = modify_url_path(
issuer_url, lambda path: path.rstrip("/") + TOKEN_PATH.lstrip("/")
token_url = AnyHttpUrl(
str(issuer_url).rstrip("/") + TOKEN_PATH
)

# Create metadata
metadata = OAuthMetadata(
issuer=issuer_url,
Expand All @@ -193,15 +181,15 @@ def build_metadata(

# Add registration endpoint if supported
if client_registration_options.enabled:
metadata.registration_endpoint = modify_url_path(
issuer_url, lambda path: path.rstrip("/") + REGISTRATION_PATH.lstrip("/")
metadata.registration_endpoint = AnyHttpUrl(
str(issuer_url).rstrip("/") + REGISTRATION_PATH
)

# Add revocation endpoint if supported
if revocation_options.enabled:
metadata.revocation_endpoint = modify_url_path(
issuer_url, lambda path: path.rstrip("/") + REVOCATION_PATH.lstrip("/")
metadata.revocation_endpoint = AnyHttpUrl(
str(issuer_url).rstrip("/") + REVOCATION_PATH
)
metadata.revocation_endpoint_auth_methods_supported = ["client_secret_post"]

return metadata
return metadata
Loading