TL;DR
An attacker can exploit OAuth2/OpenID Connect systems where different API resources use the same scope names but grant access to distinct data or functionalities. This allows an attacker, after gaining consent for one resource, to potentially access information from another without explicit user permission.
Understanding the Problem
OAuth2 uses scopes to define what permissions an application has when accessing a user’s resources. If multiple API resources (e.g., profile data, email access, photos) use identical scope names like ‘read’, but each resource handles that scope differently, confusion can arise. A typical flow involves the user granting consent based on the scopes requested by an application. However, if the system doesn’t clearly differentiate between which resource a scope applies to, an attacker might be able to leverage consent granted for one resource to access data from another.
Solution Guide
- Identify Resources and Scopes:
- List all API resources in your OAuth2/OpenID Connect system.
- For each resource, document the scopes it defines and what access those scopes grant. Pay close attention to any overlapping scope names across different resources.
- Implement Scope Isolation: This is the core mitigation step. There are several approaches:
- Unique Scopes per Resource: The most robust solution is to use completely unique scope names for each resource. For example, instead of just ‘read’, use ‘profile_read’, ’email_read’, and ‘photos_read’.
- Namespaced Scopes: If you can’t avoid some overlap in functionality, namespace your scopes. For instance, ‘resource1.read’, ‘resource2.read’.
- Resource-Specific Consent Granularity: Ensure the consent screen clearly shows which resource each scope applies to. The user should understand exactly what data they are granting access to for *each* resource.
- Validate Scope Usage in API Resources:
- Within each API resource, rigorously validate that the scopes being used correspond to the intended functionality of that resource. Do not assume a ‘read’ scope automatically grants access to all data within the system.
- Implement fine-grained authorization checks based on the specific resource and requested scope.
- Review Authorization Server Configuration:
- Check your OAuth2/OpenID Connect authorization server configuration (e.g., Keycloak, Auth0, Okta) to ensure it supports clear scope differentiation.
- If using a custom authorization server, review the code responsible for handling scopes and consent to identify potential vulnerabilities.
- Example: Unique Scopes
Instead of:
Resource A: Scope 'read' - Access profile data Resource B: Scope 'read' - Access email contentUse:
Resource A: Scope 'profile_read' - Access profile data Resource B: Scope 'email_read' - Access email content - Example: Consent Screen Improvement
Ensure the consent screen displays something like:
- Application X wants access to:
- Resource A (Profile Data): profile_read – Read your profile information
- Resource B (Email Access): email_read – Read your email messages
- Application X wants access to:
- Regular Security Audits:
- Conduct regular security audits of your OAuth2/OpenID Connect implementation, including penetration testing and code reviews.
- Specifically focus on scope handling and authorization logic to identify potential vulnerabilities.

