Open Authentication Protocol | T. Lodderstedt, Ed. |
Internet-Draft | YES.com AG |
Intended status: Best Current Practice | J. Bradley |
Expires: September 19, 2018 | Yubico |
A. Labunets | |
March 18, 2018 |
OAuth 2.0 Security Best Current Practice
draft-ietf-oauth-security-topics-05
This document describes best current security practices 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 cover new threats relevant due to the broader application of OAuth 2.0.
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 September 19, 2018.
Copyright (c) 2018 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.
It's been a while since OAuth has been published in RFC 6749 and RFC 6750. Since publication, OAuth 2.0 has gotten massive traction in the market and became the standard for API protection and, as foundation of OpenID Connect, 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 relationsship 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 [I-D.ietf-oauth-discovery] 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.
The remainder of the document is organized as follows: The next section summarizes the most important recommendations of the OAuth working group for every OAuth implementor. Afterwards, a detailed analyses of the threats and implementation issues, which can be found in the wild today, is given along with a discussion of potential counter measures.
This section describes the set of security mechanisms the authors believe should be taken into consideration by the OAuth working group to be recommended to OAuth implementers.
Authorization servers shall 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 shall avoid any redirects or forwards, which can be parameterized by URI query parameters, in order to provide a further layer of defence against token leakage. If there is a need for this kind of redirects, clients are advised to implement appropriate counter measures against open redirection, e.g. as described by the OWASP.
Clients shall ensure to only process redirect responses of the OAuth authorization server they send the respective request to and in the same user agent this request was initiated in. In particular, clients shall implement appropriate XSRF prevention by utilizing one-time use XSRF tokens carried in the STATE parameter, which are securely bound to the user agent. Moreover, the client shall memorize which authorization server it 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. Furthermore, clients should use AS-specific redirect URIs as a means to identify the AS a particular response came from. Matching this with the before mentioned information regarding the AS the client sent the request to helps to detect mix-up attacks.
Note: [I-D.bradley-oauth-jwt-encoded-state] gives advice on how to implement XSRF prevention and AS matching using signed JWTs in the STATE parameter.
Clients shall use PKCE 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. 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 of the for the same purpose.
Note: although PKCE so far was recommended as mechanism to protect native apps, this advice applies to all kinds of OAuth clients, including web applications.
Authorization servers shall consider the recommendations given in [RFC6819], section 4.4.1.1, on authorization code replay prevention.
Authorization servers shall use TLS-based methods for sender constraint access tokens as described in section Section 3.7.1.2, such as token binding [I-D.ietf-oauth-token-binding] or Mutual TLS for OAuth 2.0 [I-D.ietf-oauth-mtls]. It is also recommend to use end-to-end TLS whenever possible.
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 have been observed in the wild, which utilized flaws in the pattern matching implementation or concrete configurations. Such a flaw 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:
For a public client using the grant type code, an attack would look as follows:
Let's assume the redirect URL pattern "https://*.example.com/*" had been registered for the client "s6BhdRkqt3". This pattern allows redirect URIs from any host residing in the domain example.com. So if an attacker manages to establish a host or subdomain in "example.com" he can impersonate the legitimate client. Assume the attacker sets up the host "evil.example.com".
GET /authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz &redirect_uri=https%3A%2F%2Fevil.example.com%2Fcb HTTP/1.1 Host: server.example.com
Note: This attack will not directly work for confidential clients, since the code exchange requires authentication with the legitimate client's secret. The attacker will need to utilize the legitimate client to redeem the code (e.g. by performing a code injection attack). This kind of injections is covered in Section Code Injection.
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 attacks. 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 strict redirect URI patterns (but not strict URL matching!).
Assume the pattern for client "s6BhdRkqt3" is "https://client.example.com/cb?*", i.e. any parameter is allowed for redirects to "https://client.example.com/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 a HTTP 302.
GET /authorize?response_type=token&client_id=s6BhdRkqt3&state=xyz &redirect_uri=https%3A%2F%2Fclient.example.com%2Fcb%26redirect_to %253Dhttps%253A%252F%252Fclient.evil.com%252Fcb HTTP/1.1 Host: server.example.com
HTTP/1.1 302 Found Location: https://client.example.com/cb? redirect_to%3Dhttps%3A%2F%2Fclient.evil.com%2Fcb #access_token=2YotnFZFEjr1zCsicMWpAA&...
HTTP/1.1 302 Found Location: https://client.evil.com/cb
https://client.evil.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA&...
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 shall compare the two URIs using simple string comparison as defined in [RFC3986], Section 6.2.1..
This would cause the following impacts:
Additional recommendations:
Alternatives to exact redirect URI matching:
It is possible authorization codes are unintentionally disclosed to attackers, if a OAuth client renders a page containing links to other pages (ads, faq, ...) as result of a successful authorization request.
If the user clicks onto one of those links and the target is under the control of an attacker, it can get access to the response URL in the referrer header.
It is also possible that an attacker injects cross-domain content somehow into the page, such as <img> (e.g if this is a blog web site). The implication is obviously the same: loading this content by browser results in leaking referrer with a code.
There are some means to prevent leakage as described above:
When a browser navigates to "client.com/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:
An access token may end up in the browser history if a a client or just a web site, which already has a token, deliberately navigates to a page like "provider.com/get_user_profile?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.com/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:
Mix-up is another kind of attack on more dynamic OAuth scenarios (or at least scenarios where a OAuth client interacts with multiple authorization servers). 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 (which acts as MITM between client and authorization server)
A detailed description of the attack and potential countermeasures is given in [I-D.ietf-oauth-mix-up-mitigation].
Potential mitigations:
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:
How does an attack look like?
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 malware pretending to be the legitimate client 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 attempt 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 spec.
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.
The authors therefore propose to the working group to drop this feature in favor of more effective and (hopefully) simpler approaches to code injection prevention as described in the following section.
The general proposal is to 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. There are multiple technical solutions to achieve this goal:
PKCE seem to be the most obvious solution for OAuth clients as it 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.
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.
Proposed mitigation: use of XSRF tokens (one-time use), which are bound to the user agent and passed in the state parameter to the authorization server. For more details see [owasp_csrf].
An attacker may setup his own resource server and trick a client into sending access tokens to it, which are valid for other resource servers. 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 a certain resource server (and the respective URL) at development time, but client instances are configured with an resource server's URL at runtime. This kind of late binding is typical in situations, where the client uses a standard API, e.g. for e-Mail, calendar, health, or banking and is configured by an user or administrator for the standard-based service, this particular user or company uses.
There are several potential mitigation strategies, which will be discussed in the following sections.
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.example.com", "authorization_endpoint":"https://server.example.com/authorize", “resource_servers”:[ “email.example.com”, ”storage.example.com”, ”video.example.com”] ... }
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.example.com/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 counter measures, as described in the next sections, which provide a better balance between the involved parties.
As the name suggests, sender constraint 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:
There exists several proposals to demonstrate the proof of possession in the scope of the OAuth working group:
[I-D.ietf-oauth-mtls] and [I-D.ietf-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, in [I-D.ietf-oauth-token-binding] all key material is automatically managed by the TLS stack whereas [I-D.ietf-oauth-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 reduce the complexity of this task. Furthermore, [I-D.ietf-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.
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 send 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 see 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.campbell-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.
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 shall be taken into account by implementors in order to cope with access token replay:
Attackers could try to utilize a user's trust in the authorization server (and its URL in particular) for performing phishing attacks. The attacker could send an authorization request with an invalid combination of client_id and redirect_uri. [RFC6749], section 4.1.2.1, already states that the AS MUST NOT automatically redirect the user agent in this case to prevent open redirection.
But as described in [I-D.ietf-oauth-closing-redirectors], the attacker could also attempt to register a client and intentionally send an erroneous authorization request, e.g. by using an invalid scope value. According to [RFC6749], the AS would send the user agent to the redirect_uri with an invalid_request error response. This is dangerous because the authorization server would serve as an open redirector. Therefore this draft recommends that every invalid authorization request MUST NOT automatically redirect the user agent to the client's redirect URI.
Client MUST not expose URLs which could be utilized as open redirector. An open redirector is a way to cause the recipient of a HTTP request to issue a redirect to a target URL that is passed as a parameter. Attackers may utilize such a mechanism 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.
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 give 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 would be able to get access to the internal network between proxy and application server, it 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 tapping and injection (including replay prevention).
We would like to thank Jim Manico, Phil Hunt, Nat Sakimura, Christian Mainka, and Brian Campbell for their valuable feedback.
This draft includes no request to IANA.
All relevant security considerations have been given in the functional specification.
[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. |
[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. |
[[ To be removed from the final specification ]]
-05
-04
-03
-02
-01
-00 (WG document)