fix: add RFC 8707 resource validation to OAuth client (#2069)
This commit is contained in:
parent
78ddff022b
commit
67458948ae
2 changed files with 102 additions and 18 deletions
|
|
@ -267,6 +267,15 @@ class OAuthClientProvider(httpx.Auth):
|
|||
)
|
||||
self._initialized = False
|
||||
|
||||
async def _validate_resource_match(self, prm: ProtectedResourceMetadata) -> None:
|
||||
"""Validate that PRM resource matches the server URL per RFC 8707."""
|
||||
prm_resource = str(prm.resource) if prm.resource else None
|
||||
if not prm_resource:
|
||||
return # pragma: no cover
|
||||
default_resource = resource_url_from_server_url(self.context.server_url)
|
||||
if not check_resource_allowed(requested_resource=default_resource, configured_resource=prm_resource):
|
||||
raise OAuthFlowError(f"Protected resource {prm_resource} does not match expected {default_resource}")
|
||||
|
||||
async def _handle_protected_resource_response(self, response: httpx.Response) -> bool:
|
||||
"""
|
||||
Handle protected resource metadata discovery response.
|
||||
|
|
@ -520,6 +529,8 @@ class OAuthClientProvider(httpx.Auth):
|
|||
|
||||
prm = await handle_protected_resource_response(discovery_response)
|
||||
if prm:
|
||||
# Validate PRM resource matches server URL (RFC 8707)
|
||||
await self._validate_resource_match(prm)
|
||||
self.context.protected_resource_metadata = prm
|
||||
|
||||
# todo: try all authorization_servers to find the OASM
|
||||
|
|
|
|||
|
|
@ -51,22 +51,17 @@ def check_resource_allowed(requested_resource: str, configured_resource: str) ->
|
|||
if requested.scheme.lower() != configured.scheme.lower() or requested.netloc.lower() != configured.netloc.lower():
|
||||
return False
|
||||
|
||||
# Handle cases like requested=/foo and configured=/foo/
|
||||
# Normalize trailing slashes before comparison so that
|
||||
# "/foo" and "/foo/" are treated as equivalent.
|
||||
requested_path = requested.path
|
||||
configured_path = configured.path
|
||||
|
||||
# If requested path is shorter, it cannot be a child
|
||||
if len(requested_path) < len(configured_path):
|
||||
return False
|
||||
|
||||
# Check if the requested path starts with the configured path
|
||||
# Ensure both paths end with / for proper comparison
|
||||
# This ensures that paths like "/api123" don't incorrectly match "/api"
|
||||
if not requested_path.endswith("/"):
|
||||
requested_path += "/"
|
||||
if not configured_path.endswith("/"):
|
||||
configured_path += "/"
|
||||
|
||||
# Check hierarchical match: requested must start with configured path.
|
||||
# The trailing-slash normalization ensures "/api123/" won't match "/api/".
|
||||
return requested_path.startswith(configured_path)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue