Web Authorization Protocol T. Lodderstedt
Internet-Draft yes.com
Intended status: Best Current Practice J. Bradley
Expires: January 9, 2020 Yubico
A. Labunets
Facebook
D. Fett
yes.com
July 8, 2019

OAuth 2.0 Security Best Current Practice
draft-ietf-oauth-security-topics-13

Abstract

This document describes best current security practice for OAuth 2.0. It updates and extends the OAuth 2.0 Security Threat Model to incorporate practical experiences gathered since OAuth 2.0 was published and covers new threats relevant due to the broader application of OAuth 2.0.

Status of This Memo

This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.

Internet-Drafts are working documents of the Internet Engineering Task Force (IETF). Note that other groups may also distribute working documents as Internet-Drafts. The list of current Internet-Drafts is at https://datatracker.ietf.org/drafts/current/.

Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as "work in progress."

This Internet-Draft will expire on January 9, 2020.

Copyright Notice

Copyright (c) 2019 IETF Trust and the persons identified as the document authors. All rights reserved.

This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (https://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License.


Table of Contents

1. Introduction

Since its publication in [RFC6749] and [RFC6750], OAuth 2.0 has gotten massive traction in the market and became the standard for API protection and, as the foundation of OpenID Connect [OpenID], identity providing. While OAuth was used in a variety of scenarios and different kinds of deployments, the following challenges could be observed:

OAuth initially assumed a static relationship between client, authorization server and resource servers. The URLs of AS and RS were known to the client at deployment time and built an anchor for the trust relationship among those parties. The validation whether the client talks to a legitimate server was based on TLS server authentication (see [RFC6819], Section 4.5.4). With the increasing adoption of OAuth, this simple model dissolved and, in several scenarios, was replaced by a dynamic establishment of the relationship between clients on one side and the authorization and resource servers of a particular deployment on the other side. This way the same client could be used to access services of different providers (in case of standard APIs, such as e-mail or OpenID Connect) or serves as a frontend to a particular tenant in a multi-tenancy. Extensions of OAuth, such as [RFC7591] and [RFC8414] were developed in order to support the usage of OAuth in dynamic scenarios. As a challenge to the community, such usage scenarios open up new attack angles, which are discussed in this document.

1.1. Structure

The remainder of the document is organized as follows: The next section updates the OAuth attacker model. Afterwards, the most important recommendations of the OAuth working group for every OAuth implementor are summarized. Subsequently, a detailed analysis of the threats and implementation issues which can be found in the wild today is given along with a discussion of potential countermeasures.

1.2. Conventions and Terminology

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all capitals, as shown here.

2. The Updated OAuth 2.0 Attacker Model

In [RFC6819], an attacker model was laid out that described the capabilities of attackers against which OAuth deployments must defend. In the following, this attacker model is updated to account for the potentially dynamic relationships involving multiple parties (as described above), to include new types of attackers, and to define the attacker model more clearly.

OAuth 2.0 MUST ensure that the authorization of the resource owner (RO) (with a user agent) at an authorization server (AS) and the subsequent usage of the access token at the resource server (RS) is protected at least against the following attackers:

These attackers conform to the attacker model that was used in formal analysis efforts for OAuth [arXiv.1601.01229]. Previous attacks on OAuth have shown that OAuth deployments SHOULD protect against an even stronger attacker model that is described as follows:

Note that in this attacker model, an attacker (see A1) can be a RO or act as one. For example, an attacker can use his own browser to replay tokens or authorization codes obtained by any of the attacks described above at the client or RS.

This document discusses the additional threats resulting from these attackers in detail and recommends suitable mitigations. Attacks in an even stronger attacker model are discussed, for example, in [arXiv.1901.11520].

This is a minimal attacker model. Implementers MUST take into account all possible attackers in the environment in which their OAuth implementations are expected to run.

3. Recommendations

This section describes the set of security mechanisms the OAuth working group recommends to OAuth implementers.

3.1. Protecting Redirect-Based Flows

Authorization servers MUST utilize exact matching of client redirect URIs against pre-registered URIs. This measure contributes to the prevention of leakage of authorization codes and access tokens (depending on the grant type). It also helps to detect mix-up attacks.

Clients SHOULD avoid forwarding the user’s browser to a URI obtained from a query parameter since such a function could be utilized to exfiltrate authorization codes and access tokens. If there is a strong need for this kind of redirects, clients are advised to implement appropriate countermeasures against open redirection, e.g., as described by OWASP [owasp].

Clients MUST prevent CSRF. One-time use CSRF tokens carried in the state parameter, which are securely bound to the user agent, SHOULD be used for that purpose. If PKCE [RFC7636] is used by the client and the authorization server supports PKCE, clients MAY opt to not use state for CSRF protection, as such protection is provided by PKCE. In this case, state MAY be used again for its original purpose, namely transporting data about the application state of the client (see Section 4.7.1).

In order to prevent mix-up attacks, clients MUST only process redirect responses of the OAuth authorization server they sent the respective request to and from the same user agent this authorization request was initiated with. Clients MUST memorize which authorization server they sent an authorization request to and bind this information to the user agent and ensure any sub-sequent messages are sent to the same authorization server. Clients SHOULD use AS-specific redirect URIs as a means to identify the AS a particular response came from.

Note: [I-D.bradley-oauth-jwt-encoded-state] gives advice on how to implement CSRF prevention and AS matching using signed JWTs in the state parameter.

AS which redirect a request that potentially contains user credentials MUST avoid forwarding these user credentials accidentally (see Section 4.10).

3.1.1. Authorization Code Grant

Clients utilizing the authorization grant type MUST use PKCE [RFC7636] in order to (with the help of the authorization server) detect and prevent attempts to inject (replay) authorization codes into the authorization response. The PKCE challenges must be transaction-specific and securely bound to the user agent in which the transaction was started and the respective client. OpenID Connect clients MAY use the nonce parameter of the OpenID Connect authentication request as specified in [OpenID] in conjunction with the corresponding ID Token claim for the same purpose.

Note: although PKCE so far was recommended as a mechanism to protect native apps, this advice applies to all kinds of OAuth clients, including web applications.

Clients SHOULD use PKCE code challenge methods that do not expose the PKCE verifier in the authorization request. (Otherwise, the attacker A4 can trivially break the security provided by PKCE.) Currently, S256 is the only such method.

AS MUST support PKCE [!@RFC7636].

AS SHOULD provide a way to detect their support for PKCE. To this end, they SHOULD either (a) publish, in their AS metadata ([!@RFC8418]), the element code_challenge_methods_supported containing the supported PKCE challenge methods (which can be used by the client to detect PKCE support) or (b) provide a deployment-specific way to ensure or determine PKCE support by the AS.

Authorization servers SHOULD furthermore consider the recommendations given in [RFC6819], Section 4.4.1.1, on authorization code replay prevention.

3.1.2. Implicit Grant

The implicit grant (response type "token") and other response types causing the authorization server to issue access tokens in the authorization response are vulnerable to access token leakage and access token replay as described in Section 4.1, Section 4.2, Section 4.3, and Section 4.6.

Moreover, no viable mechanism exists to cryptographically bind access tokens issued in the authorization response to a certain client as it is recommended in Section 3.2. This makes replay detection for such access tokens at resource servers impossible.

In order to avoid these issues, clients SHOULD NOT use the implicit grant (response type "token") or any other response type issuing access tokens in the authorization response, such as "token id_token" and "code token id_token", unless the issued access tokens are sender-constrained and access token injection in the authorization response is prevented.

A sender-constrained access token scopes the applicability of an access token to a certain sender. This sender is obliged to demonstrate knowledge of a certain secret as prerequisite for the acceptance of that token at the recipient (e.g., a resource server).

Clients SHOULD instead use the response type "code" (aka authorization code grant type) as specified in Section 3.1.1 or any other response type that causes the authorization server to issue access tokens in the token response. This allows the authorization server to detect replay attempts and generally reduces the attack surface since access tokens are not exposed in URLs. It also allows the authorization server to sender-constrain the issued tokens.

3.2. Token Replay Prevention

Authorization servers SHOULD use TLS-based methods for sender-constrained access tokens as described in Section 4.8.1.2, such as token binding [I-D.ietf-oauth-token-binding] or Mutual TLS for OAuth 2.0 [I-D.ietf-oauth-mtls] in order to prevent token replay. Refresh tokens MUST be sender-constrained or use refresh token rotation as described in Section 4.12.

It is recommended to use end-to-end TLS whenever possible. If TLS traffic needs to be terminated at an intermediary, refer to Section 4.11 for further security advice.

3.3. Access Token Privilege Restriction

The privileges associated with an access token SHOULD be restricted to the minimum required for the particular application or use case. This prevents clients from exceeding the privileges authorized by the resource owner. It also prevents users from exceeding their privileges authorized by the respective security policy. Privilege restrictions also limit the impact of token leakage although more effective counter-measures are described in Section 3.2.

In particular, access tokens SHOULD be restricted to certain resource servers, preferably to a single resource server. To put this into effect, the authorization server associates the access token with certain resource servers and every resource server is obliged to verify for every request, whether the access token sent with that request was meant to be used for that particular resource server. If not, the resource server MUST refuse to serve the respective request. Clients and authorization servers MAY utilize the parameters scope or resource as specified in [RFC6749] and [I-D.ietf-oauth-resource-indicators], respectively, to determine the resource server they want to access.

Additionally, access tokens SHOULD be restricted to certain resources and actions on resource servers or resources. To put this into effect, the authorization server associates the access token with the respective resource and actions and every resource server is obliged to verify for every request, whether the access token sent with that request was meant to be used for that particular action on the particular resource. If not, the resource server must refuse to serve the respective request. Clients and authorization servers MAY utilize the parameter scope as specified in [RFC6749] to determine those resources and/or actions.

3.4. Resource Owner Password Credentials Grant

The resource owner password credentials grant MUST NOT be used. This grant type insecurely exposes the credentials of the resource owner to the client. Even if the client is benign, this results in an increased attack surface (credentials can leak in more places than just the AS) and users are trained to enter their credentials in places other than the AS.

Furthermore, adapting the resource owner password credentials grant to two-factor authentication, authentication with cryptographic credentials, and authentication processes that require multiple steps can be hard or impossible (WebCrypto, WebAuthn).

3.5. Client Authentication

Authorization servers SHOULD use client authentication if possible.

It is RECOMMENDED to use asymmetric (public key based) methods for client authentication such as MTLS [I-D.draft-ietf-oauth-mtls] or private_key_jwt [OIDC]. When asymmetric methods for client authentication are used, authorization servers do not need to store sensitive symmetric keys, making these methods more robust against a number of attacks. Additionally, these methods enable non-repudation and work well with sender-constrained access tokens (without requiring additional keys to be distributed).

3.6. Other Recommendations

Authorization servers SHOULD NOT allow clients to influence their client_id or sub value or any other claim that might cause confusion with a genuine resource owner (see Section 4.13).

4. Attacks and Mitigations

This section gives a detailed description of attacks on OAuth implementations, along with potential countermeasures. This section complements and enhances the description given in [RFC6819].

4.1. Insufficient Redirect URI Validation

Some authorization servers allow clients to register redirect URI patterns instead of complete redirect URIs. In those cases, the authorization server, at runtime, matches the actual redirect URI parameter value at the authorization endpoint against this pattern. This approach allows clients to encode transaction state into additional redirect URI parameters or to register just a single pattern for multiple redirect URIs. As a downside, it turned out to be more complex to implement and error prone to manage than exact redirect URI matching. Several successful attacks, utilizing flaws in the pattern matching implementation or concrete configurations, have been observed in the wild. Insufficient validation of the redirect URI effectively breaks client identification or authentication (depending on grant and client type) and allows the attacker to obtain an authorization code or access token, either

4.1.1. Redirect URI Validation Attacks on Authorization Code Grant

For a public client using the grant type code, an attack would look as follows:

Let's assume the redirect URL pattern https://*.somesite.example/* had been registered for the client "s6BhdRkqt3". This pattern allows redirect URIs pointing to any host residing in the domain somesite.example. So if an attacker manages to establish a host or subdomain in somesite.example he can impersonate the legitimate client. Assume the attacker sets up the host evil.somesite.example.

The attack can then be conducted as follows:

First, the attacker needs to trick the user into opening a tampered URL in his browser, which launches a page under the attacker's control, say https://www.evil.example. (See Attacker A1.)

This URL initiates an authorization request with the client id of a legitimate client to the authorization endpoint. This is the example authorization request (line breaks are for display purposes only):

GET /authorize?response_type=code&client_id=s6BhdRkqt3&state=9ad67f13
     &redirect_uri=https%3A%2F%2Fevil.somesite.example%2Fcb HTTP/1.1
Host: server.somesite.example

Afterwards, the authorization server validates the redirect URI in order to identify the client. Since the pattern allows arbitrary host names in "somesite.example", the authorization request is processed under the legitimate client's identity. This includes the way the request for user consent is presented to the user. If auto-approval is allowed (which is not recommended for public clients according to [RFC6749]), the attack can be performed even easier.

If the user does not recognize the attack, the code is issued and immediately sent to the attacker's client.

Since the attacker impersonated a public client, it can exchange the code for tokens at the respective token endpoint.

Note: This attack will not work as easily for confidential clients, since the code exchange requires authentication with the legitimate client's secret. The attacker will need to impersonate or utilize the legitimate client to redeem the code (e.g., by performing a code injection attack). This kind of injections is covered in Section 4.5.

4.1.2. Redirect URI Validation Attacks on Implicit Grant

The attack described above works for the implicit grant as well. If the attacker is able to send the authorization response to a URI under his control, he will directly get access to the fragment carrying the access token.

Additionally, implicit clients can be subject to a further kind of attack. It utilizes the fact that user agents re-attach fragments to the destination URL of a redirect if the location header does not contain a fragment (see [RFC7231], Section 9.5). The attack described here combines this behavior with the client as an open redirector in order to get access to access tokens. This allows circumvention even of very narrow redirect URI patterns (but not strict URL matching!).

Assume the pattern for client "s6BhdRkqt3" is https://client.somesite.example/cb?*, i.e., any parameter is allowed for redirects to https://client.somesite.example/cb. Unfortunately, the client exposes an open redirector. This endpoint supports a parameter redirect_to which takes a target URL and will send the browser to this URL using an HTTP Location header redirect 303.

The attack can now be conducted as follows:

First, and as above, the attacker needs to trick the user into opening a tampered URL in his browser, which launches a page under the attacker's control, say https://www.evil.example.

Afterwards, the website initiates an authorization request, which is very similar to the one in the attack on the code flow. Different to above, it utilizes the open redirector by encoding redirect_to=https://client.evil.example into the redirect URI and it uses the response type "token" (line breaks are for display purposes only):

GET /authorize?response_type=token&state=9ad67f13
    &client_id=s6BhdRkqt3
    &redirect_uri=https%3A%2F%2Fclient.somesite.example
     %2Fcb%26redirect_to%253Dhttps%253A%252F
     %252Fclient.evil.example%252Fcb HTTP/1.1
Host: server.somesite.example

Now, since the redirect URI matches the registered pattern, the authorization server allows the request and sends the resulting access token with a 303 redirect (some response parameters are omitted for better readability)

HTTP/1.1 303 See Other
Location: https://client.somesite.example/cb?
          redirect_to%3Dhttps%3A%2F%2Fclient.evil.example%2Fcb
          #access_token=2YotnFZFEjr1zCsicMWpAA&...

At example.com, the request arrives at the open redirector. It will read the redirect parameter and will issue an HTTP 303 Location header redirect to the URL https://client.evil.example/cb.

HTTP/1.1 303 See Other
Location: https://client.evil.example/cb

Since the redirector at client.somesite.example does not include a fragment in the Location header, the user agent will re-attach the original fragment #access_token=2YotnFZFEjr1zCsicMWpAA&... to the URL and will navigate to the following URL:

https://client.evil.example/cb#access_token=2YotnFZFEjr1z...

The attacker's page at client.evil.example can now access the fragment and obtain the access token.

4.1.3. Proposed Countermeasures

The complexity of implementing and managing pattern matching correctly obviously causes security issues. This document therefore proposes to simplify the required logic and configuration by using exact redirect URI matching only. This means the authorization server must compare the two URIs using simple string comparison as defined in [RFC3986], Section 6.2.1.

Additional recommendations:

As an alternative to exact redirect URI matching, the AS could also authenticate clients, e.g., using [I-D.ietf-oauth-jwsreq].

4.2. Credential Leakage via Referrer Headers

Authorization codes or values of state can unintentionally be disclosed to attackers through the referrer header, by leaking either from a client's web site or from an AS's web site. Note: even if specified otherwise in [RFC7231], Section 5.5.2, the same may happen to access tokens conveyed in URI fragments due to browser implementation issues as illustrated by Chromium Issue 168213 [bug.chromium].

4.2.1. Leakage from the OAuth Client

Leakage from the OAuth client requires that the client, as a result of a successful authorization request, renders a page that

As soon as the browser navigates to the attacker's page or loads the third-party content, the attacker receives the authorization response URL and can extract code, access token, or state.

4.2.2. Leakage from the Authorization Server

In a similar way, an attacker can learn state if the authorization endpoint at the authorization server contains links or third-party content as above.

4.2.3. Consequences

An attacker that learns a valid code or access token through a referrer header can perform the attacks as described in Section 4.1.1, Section 4.5, and Section 4.6. If the attacker learns state, the CSRF protection achieved by using state is lost, resulting in CSRF attacks as described in [RFC6819], Section 4.4.1.8.

4.2.4. Proposed Countermeasures

The page rendered as a result of the OAuth authorization response and the authorization endpoint SHOULD NOT include third-party resources or links to external sites.

The following measures further reduce the chances of a successful attack:

4.3. Attacks through the Browser History

Authorization codes and access tokens can end up in the browser's history of visited URLs, enabling the attacks described in the following.

4.3.1. Code in Browser History

When a browser navigates to client.example/redirection_endpoint?code=abcd as a result of a redirect from a provider's authorization endpoint, the URL including the authorization code may end up in the browser's history. An attacker with access to the device could obtain the code and try to replay it.

Proposed countermeasures:

4.3.2. Access Token in Browser History

An access token may end up in the browser history if a client or just a web site, which already has a token, deliberately navigates to a page like "provider.com/getuserprofile?access_token=abcdef.". Actually [RFC6750] discourages this practice and asks to transfer tokens via a header, but in practice web sites often just pass access token in query parameters.

In case of implicit grant, a URL like client.example/redirection_endpoint#access_token=abcdef may also end up in the browser history as a result of a redirect from a provider's authorization endpoint.

Proposed countermeasures:

4.4. Mix-Up

Mix-up is an attack on scenarios where an OAuth client interacts with multiple authorization servers, as is usually the case when dynamic registration is used. The goal of the attack is to obtain an authorization code or an access token by tricking the client into sending those credentials to the attacker instead of using them at the respective endpoint at the authorization/resource server.

4.4.1. Attack Description

For a detailed attack description, refer to [arXiv.1601.01229] and [I-D.ietf-oauth-mix-up-mitigation]. The description here closely follows [arXiv.1601.01229], with variants of the attack outlined below.

Preconditions: For the attack to work, we assume that

Some of the attack variants described below require different preconditions.

In the following, we assume that the client is registered with H-AS (URI: https://honest.as.example, client id: 7ZGZldHQ) and with A-AS (URI: https://attacker.example, client id: 666RVZJTA).

Attack on the authorization code grant:

  1. The user selects to start the grant using H-AS (e.g., by clicking on a button at the client's website).
  2. The attacker intercepts this request and changes the user's selection to "A-AS".
  3. The client stores in the user's session that the user selected "A-AS" and redirects the user to A-AS's authorization endpoint by sending the response code 303 See Other with a Location header containing the URL https://attacker.example/authorize?response_type=code&client_id=666RVZJTA.
  4. Now the attacker intercepts this response and changes the redirection such that the user is being redirected to H-AS. The attacker also replaces the client id of the client at A-AS with the client's id at H-AS. Therefore, the browser receives a redirection (303 See Other) with a Location header pointing to https://honest.as.example/authorize?response_type=code&client_id=7ZGZldHQ
  5. Now, the user authorizes the client to access her resources at H-AS. H-AS issues a code and sends it (via the browser) back to the client.
  6. Since the client still assumes that the code was issued by A-AS, it will try to redeem the code at A-AS's token endpoint.
  7. The attacker therefore obtains code and can either exchange the code for an access token (for public clients) or perform a code injection attack as described in Section 4.5.

Variants:

4.4.2. Countermeasures

In scenarios where an OAuth client interacts with multiple authorization servers, clients MUST prevent mix-up attacks.

Potential countermeasures:

4.5. Authorization Code Injection

In such an attack, the adversary attempts to inject a stolen authorization code into a legitimate client on a device under his control. In the simplest case, the attacker would want to use the code in his own client. But there are situations where this might not be possible or intended. Examples are:

In the following attack description and discussion, we assume the presence of a web or network attacker, but not of an attacker with advanced capabilities (A3-A5).

4.5.1. Attack Description

The attack works as follows:

  1. The attacker obtains an authorization code by performing any of the attacks described above.
  2. He performs a regular OAuth authorization process with the legitimate client on his device.
  3. The attacker injects the stolen authorization code in the response of the authorization server to the legitimate client.
  4. The client sends the code to the authorization server's token endpoint, along with client id, client secret and actual redirect_uri.
  5. The authorization server checks the client secret, whether the code was issued to the particular client and whether the actual redirect URI matches the redirect_uri parameter (see [RFC6749]).
  6. If all checks succeed, the authorization server issues access and other tokens to the client, so now the attacker is able to impersonate the legitimate user.

4.5.2. Discussion

Obviously, the check in step (5.) will fail if the code was issued to another client id, e.g., a client set up by the attacker. The check will also fail if the authorization code was already redeemed by the legitimate user and was one-time use only.

An attempt to inject a code obtained via a manipulated redirect URI should also be detected if the authorization server stored the complete redirect URI used in the authorization request and compares it with the redirect_uri parameter.

[RFC6749], Section 4.1.3, requires the AS to "... ensure that the redirect_uri parameter is present if the redirect_uri parameter was included in the initial authorization request as described in Section 4.1.1, and if included ensure that their values are identical.". In the attack scenario described above, the legitimate client would use the correct redirect URI it always uses for authorization requests. But this URI would not match the tampered redirect URI used by the attacker (otherwise, the redirect would not land at the attackers page). So the authorization server would detect the attack and refuse to exchange the code.

Note: this check could also detect attempts to inject a code which had been obtained from another instance of the same client on another device, if certain conditions are fulfilled:

But this approach conflicts with the idea to enforce exact redirect URI matching at the authorization endpoint. Moreover, it has been observed that providers very often ignore the redirect_uri check requirement at this stage, maybe because it doesn't seem to be security-critical from reading the specification.

Other providers just pattern match the redirect_uri parameter against the registered redirect URI pattern. This saves the authorization server from storing the link between the actual redirect URI and the respective authorization code for every transaction. But this kind of check obviously does not fulfill the intent of the spec, since the tampered redirect URI is not considered. So any attempt to inject a code obtained using the client_id of a legitimate client or by utilizing the legitimate client on another device won't be detected in the respective deployments.

It is also assumed that the requirements defined in [RFC6749], Section 4.1.3, increase client implementation complexity as clients need to memorize or re-construct the correct redirect URI for the call to the tokens endpoint.

This document therefore recommends to instead bind every authorization code to a certain client instance on a certain device (or in a certain user agent) in the context of a certain transaction.

4.5.3. Proposed Countermeasures

There are multiple technical solutions to achieve this goal:

PKCE seems to be the most obvious solution for OAuth clients as it is available and effectively used today for similar purposes for OAuth native apps whereas nonce is appropriate for OpenId Connect clients.

Note on pre-warmed secrets: An attacker can circumvent the countermeasures described above if he is able to create or capture the respective secret or code_challenge on a device under his control, which is then used in the victim's authorization request.

Exact redirect URI matching of authorization requests can prevent the attacker from using the pre-warmed secret in the faked authorization transaction on the victim's device.

Unfortunately, it does not work for all kinds of OAuth clients. It is effective for web and JS apps and for native apps with claimed URLs. Attacks on native apps using custom schemes or redirect URIs on localhost cannot be prevented this way, except if the AS enforces one-time use for PKCE verifier or nonce values.

4.6. Access Token Injection

In such an attack, the adversary attempts to inject a stolen access token into a legitimate client on a device under his control. This will typically happen if the attacker wants to utilize a leaked access token to impersonate a user in a certain client.

To conduct the attack, the adversary starts an OAuth flow with the client and modifies the authorization response by replacing the access token issued by the authorization server or directly makes up an authorization server response including the leaked access token. Since the response includes the state value generated by the client for this particular transaction, the client does not treat the response as a CSRF and will use the access token injected by the attacker.

4.6.1. Proposed Countermeasures

There is no way to detect such an injection attack on the OAuth protocol level, since the token is issued without any binding to the transaction or the particular user agent.

The recommendation is therefore to use the authorization code grant type instead of relying on response types issuing acess tokens at the authorization endpoint. Code injection can be detected using one of the countermeasures discussed in Section 4.5.

4.7. Cross Site Request Forgery

An attacker might attempt to inject a request to the redirect URI of the legitimate client on the victim's device, e.g., to cause the client to access resources under the attacker's control.

4.7.1. Proposed Countermeasures

Use of CSRF tokens which are bound to the user agent and passed in the state parameter to the authorization server as described in [!@RFC6819]. Alternatively, PKCE provides CSRF protection.

It is important to note that:

The recommendation therefore is that AS publish their PKCE support either in AS metadata according to [RFC8418] or provide a deployment-specific way to ensure or determine PKCE support.

Additionally, standard CSRF defenses MAY be used to protect the redirection endpoint, for example the Origin header.

For more details see [owasp_csrf].

4.8. Access Token Leakage at the Resource Server

Access tokens can leak from a resource server under certain circumstances.

4.8.1. Access Token Phishing by Counterfeit Resource Server

An attacker may setup his own resource server and trick a client into sending access tokens to it that are valid for other resource servers (see Attackers A1 and A5). If the client sends a valid access token to this counterfeit resource server, the attacker in turn may use that token to access other services on behalf of the resource owner.

This attack assumes the client is not bound to one specific resource server (and its URL) at development time, but client instances are provided with the resource server URL at runtime. This kind of late binding is typical in situations where the client uses a service implementing a standardized API (e.g., for e-Mail, calendar, health, or banking) and where the client is configured by a user or administrator for a service which this user or company uses.

There are several potential mitigation strategies, which will be discussed in the following sections.

4.8.1.1. Metadata

An authorization server could provide the client with additional information about the location where it is safe to use its access tokens.

In the simplest form, this would require the AS to publish a list of its known resource servers, illustrated in the following example using a metadata parameter resource_servers:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "issuer":"https://server.somesite.example",
  "authorization_endpoint":
    "https://server.somesite.example/authorize",
  "resource_servers":[
    "email.somesite.example",
    "storage.somesite.example",
    "video.somesite.example"
  ]
  ...
} 

The AS could also return the URL(s) an access token is good for in the token response, illustrated by the example return parameter access_token_resource_server:

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache

{
  "access_token":"2YotnFZFEjr1zCsicMWpAA",
  "access_token_resource_server":
    "https://hostedresource.somesite.example/path1",
...
}

This mitigation strategy would rely on the client to enforce the security policy and to only send access tokens to legitimate destinations. Results of OAuth related security research (see for example [oauth_security_ubc] and [oauth_security_cmu]) indicate a large portion of client implementations do not or fail to properly implement security controls, like state checks. So relying on clients to prevent access token phishing is likely to fail as well. Moreover given the ratio of clients to authorization and resource servers, it is considered the more viable approach to move as much as possible security-related logic to those entities. Clearly, the client has to contribute to the overall security. But there are alternative countermeasures, as described in the next sections, which provide a better balance between the involved parties.

4.8.1.2. Sender-Constrained Access Tokens

As the name suggests, sender-constrained access token scope the applicability of an access token to a certain sender. This sender is obliged to demonstrate knowledge of a certain secret as prerequisite for the acceptance of that token at a resource server.

A typical flow looks like this:

  1. The authorization server associates data with the access token which binds this particular token to a certain client. The binding can utilize the client identity, but in most cases the AS utilizes key material (or data derived from the key material) known to the client.
  2. This key material must be distributed somehow. Either the key material already exists before the AS creates the binding or the AS creates ephemeral keys. The way pre-existing key material is distributed varies among the different approaches. For example, X.509 Certificates can be used in which case the distribution happens explicitly during the enrollment process. Or the key material is created and distributed at the TLS layer, in which case it might automatically happen during the setup of a TLS connection.
  3. The RS must implement the actual proof of possession check. This is typically done on the application level, it may utilize capabilities of the transport layer (e.g., TLS). Note: replay prevention is required as well!

There exist several proposals to demonstrate the proof of possession in the scope of the OAuth working group:

Mutual TLS and OAuth Token Binding are built on top of TLS and this way continue the successful OAuth 2.0 philosophy to leverage TLS to secure OAuth wherever possible. Both mechanisms allow prevention of access token leakage in a fairly client developer friendly way.

There are some differences between both approaches: To start with, for OAuth Token Binding, all key material is automatically managed by the TLS stack whereas mTLS requires the developer to create and maintain the key pairs and respective certificates. Use of self-signed certificates, which is supported by the draft, significantly reduces the complexity of this task. Furthermore, OAuth Token Binding allows to use different key pairs for different resource servers, which is a privacy benefit. On the other hand, [I-D.ietf-oauth-mtls] only requires widely deployed TLS features, which means it might be easier to adopt in the short term.

Application level signing approaches, like [I-D.ietf-oauth-signed-http-request] and [I-D.sakimura-oauth-jpop] have been debated for a long time in the OAuth working group without a clear outcome.

As one advantage, application-level signing allows for end-to-end protection including non-repudiation even if the TLS connection is terminated between client and resource server. But deployment experiences have revealed challenges regarding robustness (e.g., reproduction of the signature base string including correct URL) as well as state management (e.g., replay prevention).

This document therefore recommends implementors to consider one of TLS-based approaches wherever possible.

4.8.1.3. Audience Restricted Access Tokens

An audience restriction essentially restricts the resource server a particular access token can be used at. The authorization server associates the access token with a certain resource server and every resource server is obliged to verify for every request, whether the access token sent with that request was meant to be used at the particular resource server. If not, the resource server must refuse to serve the respective request. In the general case, audience restrictions limit the impact of a token leakage. In the case of a counterfeit resource server, it may (as described below) also prevent abuse of the phished access token at the legitimate resource server.

The audience can basically be expressed using logical names or physical addresses (like URLs). In order to prevent phishing, it is necessary to use the actual URL the client will send requests to. In the phishing case, this URL will point to the counterfeit resource server. If the attacker tries to use the access token at the legitimate resource server (which has a different URL), the resource server will detect the mismatch (wrong audience) and refuse to serve the request.

In deployments where the authorization server knows the URLs of all resource servers, the authorization server may just refuse to issue access tokens for unknown resource server URLs.

The client needs to tell the authorization server, at which URL it will use the access token it is requesting. It could use the mechanism proposed [I-D.ietf-oauth-resource-indicators] or encode the information in the scope value.

Instead of the URL, it is also possible to utilize the fingerprint of the resource server's X.509 certificate as audience value. This variant would also allow to detect an attempt to spoof the legit resource server's URL by using a valid TLS certificate obtained from a different CA. It might also be considered a privacy benefit to hide the resource server URL from the authorization server.

Audience restriction seems easy to use since it does not require any crypto on the client side. But since every access token is bound to a certain resource server, the client also needs to obtain different RS-specific access tokens, if it wants to access several resource services. [I-D.ietf-oauth-token-binding] has the same property, since different token binding ids must be associated with the access token. [I-D.ietf-oauth-mtls] on the other hand allows a client to use the access token at multiple resource servers.

It shall be noted that audience restrictions, or generally speaking an indication by the client to the authorization server where it wants to use the access token, has additional benefits beyond the scope of token leakage prevention. It allows the authorization server to create different access token whose format and content is specifically minted for the respective server. This has huge functional and privacy advantages in deployments using structured access tokens.

4.8.2. Compromised Resource Server

An attacker may compromise a resource server in order to get access to its resources and other resources of the respective deployment. Such a compromise may range from partial access to the system, e.g., its logfiles, to full control of the respective server.

If the attacker was able to take over full control including shell access it will be able to circumvent all controls in place and access resources without access control. It will also get access to access tokens, which are sent to the compromised system and which potentially are valid for access to other resource servers as well. Even if the attacker "only" is able to access logfiles or databases of the server system, it may get access to valid access tokens.

Preventing server breaches by way of hardening and monitoring server systems is considered a standard operational procedure and therefore out of scope of this document. This section will focus on the impact of such breaches on OAuth-related parts of the ecosystem, which is the replay of captured access tokens on the compromised resource server and other resource servers of the respective deployment.

The following measures should be taken into account by implementors in order to cope with access token replay:

4.9. Open Redirection

The following attacks can occur when an AS or client has an open redirector, i.e., a URL which causes an HTTP redirect to an attacker-controlled web site.

4.9.1. Authorization Server as Open Redirector

Attackers could try to utilize a user's trust in the authorization server (and its URL in particular) for performing phishing attacks.

[RFC6749], Section 4.1.2.1, already prevents open redirects by stating the AS MUST NOT automatically redirect the user agent in case of an invalid combination of clientid and redirecturi.

However, as described in [I-D.ietf-oauth-closing-redirectors], an attacker could also utilize a correctly registered redirect URI to perform phishing attacks. It could for example register a client via dynamic client registration [RFC7591] and intentionally send an erroneous authorization request, e.g., by using an invalid scope value, to cause the AS to automatically redirect the user agent to its phishing site.

The AS MUST take precautions to prevent this threat. Based on its risk assessment the AS needs to decide whether it can trust the redirect URI or not and SHOULD only automatically redirect the user agent, if it trusts the redirect URI. If not, it MAY inform the user that it is about to redirect her to the another site and rely on the user to decide or MAY just inform the user about the error.

4.9.2. Clients as Open Redirector

Client MUST NOT expose URLs which could be utilized as open redirector. Attackers may use an open redirector to produce URLs which appear to point to the client, which might trick users to trust the URL and follow it in her browser. Another abuse case is to produce URLs pointing to the client and utilize them to impersonate a client with an authorization server.

In order to prevent open redirection, clients should only expose such a function, if the target URLs are whitelisted or if the origin of a request can be authenticated.

4.10. 307 Redirect

At the authorization endpoint, a typical protocol flow is that the AS prompts the user to enter her credentials in a form that is then submitted (using the HTTP POST method) back to the authorization server. The AS checks the credentials and, if successful, redirects the user agent to the client's redirection endpoint.

In [RFC6749], the HTTP status code 302 is used for this purpose, but "any other method available via the user-agent to accomplish this redirection is allowed". However, when the status code 307 is used for redirection, the user agent will send the form data (user credentials) via HTTP POST to the client since this status code does not require the user agent to rewrite the POST request to a GET request (and thereby dropping the form data in the POST request body). If the relying party is malicious, it can use the credentials to impersonate the user at the AS.

In the HTTP standard [RFC6749], only the status code 303 unambigiously enforces rewriting the HTTP POST request to an HTTP GET request. For all other status codes, including the popular 302, user agents can opt not to rewrite POST to GET requests and therefore to reveal the user credentials to the client. (In practice, however, most user agents will only show this behaviour for 307 redirects.)

AS which redirect a request that potentially contains user credentials therefore MUST NOT use the HTTP 307 status code for redirection. If an HTTP redirection (and not, for example, JavaScript) is used for such a request, AS SHOULD use HTTP status code 303 "See Other".

4.11. TLS Terminating Reverse Proxies

A common deployment architecture for HTTP applications is to have the application server sitting behind a reverse proxy which terminates the TLS connection and dispatches the incoming requests to the respective application server nodes.

This section highlights some attack angles of this deployment architecture which are relevant to OAuth, and gives recommendations for security controls.

In some situations, the reverse proxy needs to pass security-related data to the upstream application servers for further processing. Examples include the IP address of the request originator, token binding ids, and authenticated TLS client certificates.

If the reverse proxy would pass through any header sent from the outside, an attacker could try to directly send the faked header values through the proxy to the application server in order to circumvent security controls that way. For example, it is standard practice of reverse proxies to accept forwarded_for headers and just add the origin of the inbound request (making it a list). Depending on the logic performed in the application server, the attacker could simply add a whitelisted IP address to the header and render a IP whitelist useless. A reverse proxy must therefore sanitize any inbound requests to ensure the authenticity and integrity of all header values relevant for the security of the application servers.

If an attacker was able to get access to the internal network between proxy and application server, he could also try to circumvent security controls in place. It is therefore important to ensure the authenticity of the communicating entities. Furthermore, the communication link between reverse proxy and application server must therefore be protected against eavesdropping, injection, and replay of messages.

4.12. Refresh Token Protection

Refresh tokens are a convenient and UX-friendly way to obtain new access tokens after the expiration of older access tokens. Refresh tokens also add to the security of OAuth since they allow the authorization server to issue access tokens with a short lifetime and reduced scope thus reducing the potential impact of access token leakage.

Refresh tokens are an attractive target for attackers since they represent the overall grant a resource owner delegated to a certain client. If an attacker is able to exfiltrate and successfully replay a refresh token, the attacker will be able to mint access tokens and use them to access resource servers on behalf of the resource owner.

[RFC6749] already provides a robust baseline protection by requiring

[RFC6749] also lays the foundation for further (implementation specific) security measures, such as refresh token expiration and revocation as well as refresh token rotation by defining respective error codes and response behavior.

This draft gives recommendations beyond the scope of [RFC6749] and clarifications.

Authorization servers MUST determine based on their risk assessment whether to issue refresh tokens to a certain client. If the authorization server decides not to issue refresh tokens, the client may refresh access tokens by utilizing other grant types, such as the authorization code grant type. In such a case, the authorization server may utilize cookies and persistent grants to optimize the user experience.

If refresh tokens are issued, those refresh tokens MUST be bound to the scope and resource servers as consented by the resource owner. This is to prevent privilege escalation by the legit client and reduce the impact of refresh token leakage.

Authorization server MUST utilize one of these methods to detect refresh token replay for public clients:

Authorization servers may revoke refresh tokens automatically in case of a security event, such as:

Refresh tokens SHOULD expire if the client has been inactive for some time, i.e., the refresh token has not been used to obtain fresh access tokens for some time. The expiration time is at the discretion of the authorization server. It might be a global value or determined based on the client policy or the grant associated with the refresh token (and its sensitivity).

4.13. Client Impersonating Resource Owner

Resource servers may make access control decisions based on the identity of the resource owner as communicated in the sub claim returned by the authorization server in a token introspection response [RFC7662] or other mechanism. If a client is able to choose its own client_id during registration with the authorization server, then there is a risk that it can register with the same sub value as a privileged user. A subsequent access token obtained under the client credentials grant may be mistaken as an access token authorized by the privileged user if the resource server does not perform additional checks.

4.13.1. Proposed Countermeasures

Authorization servers SHOULD NOT allow clients to influence their client_id or sub value or any other claim that might cause confusion with a genuine resource owner. Where this cannot be avoided, authorization servers MUST provide another means for the resource server to distinguish between access tokens authorized by a resource owner from access tokens authorized by the client itself.

5. Acknowledgements

We would like to thank Jim Manico, Phil Hunt, Nat Sakimura, Christian Mainka, Doug McDorman, Johan Peeters, Joseph Heenan, Brock Allen, Vittorio Bertocci, David Waite, Nov Matake, Tomek Stojecki, Dominick Baier, Neil Madden, William Dennis, Dick Hardt, Petteri Stenius, Annabelle Richard Backman, Aaron Parecki, George Fletscher, Brian Campbell, Konstantin Lapine, and Tim Würtele for their valuable feedback.

6. IANA Considerations

This draft includes no request to IANA.

7. Security Considerations

All relevant security considerations have been given in the functional specification.

8. References

8.1. Normative References

[oauth-v2-form-post-response-mode] Jones, M. and B. Campbell, "OAuth 2.0 Form Post Response Mode", April 2015.
[OpenID] Sakimura, N., Bradley, J., Jones, M., de Medeiros, B. and C. Mortimore, "OpenID Connect Core 1.0 incorporating errata set 1", Nov 2014.
[RFC3986] Berners-Lee, T., Fielding, R. and L. Masinter, "Uniform Resource Identifier (URI): Generic Syntax", STD 66, RFC 3986, DOI 10.17487/RFC3986, January 2005.
[RFC6749] Hardt, D., "The OAuth 2.0 Authorization Framework", RFC 6749, DOI 10.17487/RFC6749, October 2012.
[RFC6750] Jones, M. and D. Hardt, "The OAuth 2.0 Authorization Framework: Bearer Token Usage", RFC 6750, DOI 10.17487/RFC6750, October 2012.
[RFC6819] Lodderstedt, T., McGloin, M. and P. Hunt, "OAuth 2.0 Threat Model and Security Considerations", RFC 6819, DOI 10.17487/RFC6819, January 2013.
[RFC7636] Sakimura, N., Bradley, J. and N. Agarwal, "Proof Key for Code Exchange by OAuth Public Clients", RFC 7636, DOI 10.17487/RFC7636, September 2015.
[RFC7662] Richer, J., "OAuth 2.0 Token Introspection", RFC 7662, DOI 10.17487/RFC7662, October 2015.
[RFC8418] Housley, R., "Use of the Elliptic Curve Diffie-Hellman Key Agreement Algorithm with X25519 and X448 in the Cryptographic Message Syntax (CMS)", RFC 8418, DOI 10.17487/RFC8418, August 2018.

8.2. Informative References

[arXiv.1508.04324v2] Mladenov, V., Mainka, C. and J. Schwenk, "On the security of modern Single Sign-On Protocols: Second-Order Vulnerabilities in OpenID Connect", January 2016.
[arXiv.1601.01229] Fett, D., Küsters, R. and G. Schmitz, "A Comprehensive Formal Security Analysis of OAuth 2.0", January 2016.
[arXiv.1704.08539] Fett, D., Küsters, R. and G. Schmitz, "The Web SSO Standard OpenID Connect: In-Depth Formal Security Analysis and Security Guidelines", April 2017.
[arXiv.1901.11520] Fett, D., Hosseyni, P. and R. Küsters, "An Extensive Formal Security Analysis of the OpenID Financial-grade API", January 2019.
[bug.chromium] "Referer header includes URL fragment when opening link using New Tab"
[fb_fragments] "Facebook Developer Blog"
[I-D.bradley-oauth-jwt-encoded-state] Bradley, J., Lodderstedt, T. and H. Zandbelt, "Encoding claims in the OAuth 2 state parameter using a JWT", Internet-Draft draft-bradley-oauth-jwt-encoded-state-09, November 2018.
[I-D.ietf-oauth-closing-redirectors] Bradley, J., Sanso, A. and H. Tschofenig, "OAuth 2.0 Security: Closing Open Redirectors in OAuth", Internet-Draft draft-ietf-oauth-closing-redirectors-00, February 2016.
[I-D.ietf-oauth-jwsreq] Sakimura, N. and J. Bradley, "The OAuth 2.0 Authorization Framework: JWT Secured Authorization Request (JAR)", Internet-Draft draft-ietf-oauth-jwsreq-19, June 2019.
[I-D.ietf-oauth-mix-up-mitigation] Jones, M., Bradley, J. and N. Sakimura, "OAuth 2.0 Mix-Up Mitigation", Internet-Draft draft-ietf-oauth-mix-up-mitigation-01, July 2016.
[I-D.ietf-oauth-mtls] Campbell, B., Bradley, J., Sakimura, N. and T. Lodderstedt, "OAuth 2.0 Mutual TLS Client Authentication and Certificate-Bound Access Tokens", Internet-Draft draft-ietf-oauth-mtls-15, July 2019.
[I-D.ietf-oauth-pop-key-distribution] Bradley, J., Hunt, P., Jones, M., Tschofenig, H. and M. Meszaros, "OAuth 2.0 Proof-of-Possession: Authorization Server to Client Key Distribution", Internet-Draft draft-ietf-oauth-pop-key-distribution-07, March 2019.
[I-D.ietf-oauth-resource-indicators] Campbell, B., Bradley, J. and H. Tschofenig, "Resource Indicators for OAuth 2.0", Internet-Draft draft-ietf-oauth-resource-indicators-02, January 2019.
[I-D.ietf-oauth-signed-http-request] Richer, J., Bradley, J. and H. Tschofenig, "A Method for Signing HTTP Requests for OAuth", Internet-Draft draft-ietf-oauth-signed-http-request-03, August 2016.
[I-D.ietf-oauth-token-binding] Jones, M., Campbell, B., Bradley, J. and W. Denniss, "OAuth 2.0 Token Binding", Internet-Draft draft-ietf-oauth-token-binding-08, October 2018.
[I-D.sakimura-oauth-jpop] Sakimura, N., Li, K. and J. Bradley, "The OAuth 2.0 Authorization Framework: JWT Pop Token Usage", Internet-Draft draft-sakimura-oauth-jpop-04, March 2017.
[oauth_security_cmu] Chen, E., Pei, Y., Chen, S., Tian, Y., Kotcher, R. and P. Tague, "OAuth Demystified for Mobile Application Developers", November 2014.
[oauth_security_jcs_14] Bansal, C., Bhargavan, K., Delignat-Lavaud, A. and S. Maffeis, "Discovering concrete attacks on website authorization by formal analysis", April 2014.
[oauth_security_ubc] Sun, S. and K. Beznosov, "The Devil is in the (Implementation) Details: An Empirical Analysis of OAuth SSO Systems", October 2012.
[owasp] "Open Web Application Security Project Home Page"
[owasp_csrf] "Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet"
[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, DOI 10.17487/RFC2119, March 1997.
[RFC7231] Fielding, R. and J. Reschke, "Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content", RFC 7231, DOI 10.17487/RFC7231, June 2014.
[RFC7591] Richer, J., Jones, M., Bradley, J., Machulak, M. and P. Hunt, "OAuth 2.0 Dynamic Client Registration Protocol", RFC 7591, DOI 10.17487/RFC7591, July 2015.
[RFC7800] Jones, M., Bradley, J. and H. Tschofenig, "Proof-of-Possession Key Semantics for JSON Web Tokens (JWTs)", RFC 7800, DOI 10.17487/RFC7800, April 2016.
[RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, May 2017.
[RFC8414] Jones, M., Sakimura, N. and J. Bradley, "OAuth 2.0 Authorization Server Metadata", RFC 8414, DOI 10.17487/RFC8414, June 2018.
[RFC8473] Popov, A., Nystroem, M., Balfanz, D., Harper, N. and J. Hodges, "Token Binding over HTTP", RFC 8473, DOI 10.17487/RFC8473, October 2018.
[webappsec-referrer-policy] Eisinger, J. and E. Stark, "Referrer Policy", April 2017.

Appendix A. Document History

[[ To be removed from the final specification ]]

-13

-12

-11

-10

-09

-08

-07

-06

-05

-04

-03

-02

-01

-00 (WG document)

Authors' Addresses

Torsten Lodderstedt yes.com EMail: torsten@lodderstedt.net
John Bradley Yubico EMail: ve7jtb@ve7jtb.com
Andrey Labunets Facebook EMail: isciurus@fb.com
Daniel Fett yes.com EMail: mail@danielfett.de