idnits 2.17.1 draft-ietf-oauth-browser-based-apps-06.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- No issues found here. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (April 05, 2020) is 1481 days in the past. Is this intentional? Checking references for intended status: Best Current Practice ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) -- Possible downref: Non-RFC (?) normative reference: ref. 'CSP2' -- Possible downref: Non-RFC (?) normative reference: ref. 'Fetch' ** Downref: Normative reference to an Informational RFC: RFC 6819 Summary: 1 error (**), 0 flaws (~~), 1 warning (==), 3 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 Open Authentication Protocol A. Parecki 3 Internet-Draft Okta 4 Intended status: Best Current Practice D. Waite 5 Expires: October 7, 2020 Ping Identity 6 April 05, 2020 8 OAuth 2.0 for Browser-Based Apps 9 draft-ietf-oauth-browser-based-apps-06 11 Abstract 13 This specification details the security considerations and best 14 practices that must be taken into account when developing browser- 15 based applications that use OAuth 2.0. 17 Status of This Memo 19 This Internet-Draft is submitted in full conformance with the 20 provisions of BCP 78 and BCP 79. 22 Internet-Drafts are working documents of the Internet Engineering 23 Task Force (IETF). Note that other groups may also distribute 24 working documents as Internet-Drafts. The list of current Internet- 25 Drafts is at https://datatracker.ietf.org/drafts/current/. 27 Internet-Drafts are draft documents valid for a maximum of six months 28 and may be updated, replaced, or obsoleted by other documents at any 29 time. It is inappropriate to use Internet-Drafts as reference 30 material or to cite them other than as "work in progress." 32 This Internet-Draft will expire on October 7, 2020. 34 Copyright Notice 36 Copyright (c) 2020 IETF Trust and the persons identified as the 37 document authors. All rights reserved. 39 This document is subject to BCP 78 and the IETF Trust's Legal 40 Provisions Relating to IETF Documents 41 (https://trustee.ietf.org/license-info) in effect on the date of 42 publication of this document. Please review these documents 43 carefully, as they describe your rights and restrictions with respect 44 to this document. Code Components extracted from this document must 45 include Simplified BSD License text as described in Section 4.e of 46 the Trust Legal Provisions and are provided without warranty as 47 described in the Simplified BSD License. 49 Table of Contents 51 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 2 52 2. Notational Conventions . . . . . . . . . . . . . . . . . . . 3 53 3. Terminology . . . . . . . . . . . . . . . . . . . . . . . . . 3 54 4. Overview . . . . . . . . . . . . . . . . . . . . . . . . . . 3 55 5. First-Party Applications . . . . . . . . . . . . . . . . . . 4 56 6. Application Architecture Patterns . . . . . . . . . . . . . . 5 57 6.1. Browser-Based Apps that Can Share Data with the Resource 58 Server . . . . . . . . . . . . . . . . . . . . . . . . . 5 59 6.2. JavaScript Applications with a Backend . . . . . . . . . 6 60 6.3. JavaScript Applications without a Backend . . . . . . . . 8 61 7. Authorization Code Flow . . . . . . . . . . . . . . . . . . . 9 62 7.1. Initiating the Authorization Request from a Browser-Based 63 Application . . . . . . . . . . . . . . . . . . . . . . . 10 64 7.2. Handling the Authorization Code Redirect . . . . . . . . 10 65 8. Refresh Tokens . . . . . . . . . . . . . . . . . . . . . . . 10 66 9. Security Considerations . . . . . . . . . . . . . . . . . . . 11 67 9.1. Registration of Browser-Based Apps . . . . . . . . . . . 12 68 9.2. Client Authentication . . . . . . . . . . . . . . . . . . 12 69 9.3. Client Impersonation . . . . . . . . . . . . . . . . . . 12 70 9.4. Cross-Site Request Forgery Protections . . . . . . . . . 13 71 9.5. Authorization Server Mix-Up Mitigation . . . . . . . . . 13 72 9.6. Cross-Domain Requests . . . . . . . . . . . . . . . . . . 13 73 9.7. Content-Security Policy . . . . . . . . . . . . . . . . . 14 74 9.8. OAuth Implicit Flow . . . . . . . . . . . . . . . . . . . 14 75 9.8.1. Attacks on the Implicit Flow . . . . . . . . . . . . 14 76 9.8.2. Countermeasures . . . . . . . . . . . . . . . . . . . 15 77 9.8.3. Disadvantages of the Implicit Flow . . . . . . . . . 15 78 9.8.4. Historic Note . . . . . . . . . . . . . . . . . . . . 16 79 9.9. Additional Security Considerations . . . . . . . . . . . 17 80 10. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 17 81 11. References . . . . . . . . . . . . . . . . . . . . . . . . . 17 82 11.1. Normative References . . . . . . . . . . . . . . . . . . 17 83 11.2. Informative References . . . . . . . . . . . . . . . . . 18 84 Appendix A. Server Support Checklist . . . . . . . . . . . . . . 18 85 Appendix B. Document History . . . . . . . . . . . . . . . . . . 18 86 Appendix C. Acknowledgements . . . . . . . . . . . . . . . . . . 20 87 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 21 89 1. Introduction 91 This specification describes the current best practices for 92 implementing OAuth 2.0 authorization flows in applications executing 93 in a browser. 95 For native application developers using OAuth 2.0 and OpenID Connect, 96 an IETF BCP (best current practice) was published that guides 97 integration of these technologies. This document is formally known 98 as [RFC8252] or BCP 212, but nicknamed "AppAuth" after the OpenID 99 Foundation-sponsored set of libraries that assist developers in 100 adopting these practices. [RFC8252] makes specific recommendations 101 for how to securely implement OAuth in native applications, including 102 incorporating additional OAuth extensions where needed. 104 OAuth 2.0 for Browser-Based Apps addresses the similarities between 105 implementing OAuth for native apps and browser-based apps, and 106 includes additional considerations when running in a browser. This 107 is primarily focused on OAuth, except where OpenID Connect provides 108 additional considerations. 110 2. Notational Conventions 112 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 113 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and 114 "OPTIONAL" in this document are to be interpreted as described in 115 [RFC2119]. 117 3. Terminology 119 In addition to the terms defined in referenced specifications, this 120 document uses the following terms: 122 "OAuth": In this document, "OAuth" refers to OAuth 2.0, [RFC6749] 123 and [RFC6750]. 125 "Browser-based application": An application that is dynamically 126 downloaded and executed in a web browser, usually written in 127 JavaScript. Also sometimes referred to as a "single-page 128 application", or "SPA". 130 4. Overview 132 At the time that OAuth 2.0 [RFC6749] and [RFC6750] were created, 133 browser-based JavaScript applications needed a solution that strictly 134 complied with the same-origin policy. Common deployments of OAuth 135 2.0 involved an application running on a different domain than the 136 authorization server, so it was historically not possible to use the 137 authorization code flow which would require a cross-origin POST 138 request. This was one of the motivations for the definition of the 139 implicit flow, which returns the access token in the front channel 140 via the fragment part of the URL, bypassing the need for a cross- 141 origin POST request. 143 However, there are several drawbacks to the implicit flow, generally 144 involving vulnerabilities associated with the exposure of the access 145 token in the URL. See Section 9.8 for an analysis of these attacks 146 and the drawbacks of using the implicit flow in browsers. Additional 147 attacks and security considerations can be found in 148 [oauth-security-topics]. 150 In recent years, widespread adoption of Cross-Origin Resource Sharing 151 (CORS), which enables exceptions to the same-origin policy, allows 152 browser-based apps to use the OAuth 2.0 authorization code flow and 153 make a POST request to exchange the authorization code for an access 154 token at the token endpoint. In this flow, the access token is never 155 exposed in the less secure front-channel. Furthermore, adding PKCE 156 to the flow ensures that even if an authorization code is 157 intercepted, it is unusable by an attacker. 159 For this reason, and from other lessons learned, the current best 160 practice for browser-based applications is to use the OAuth 2.0 161 authorization code flow with PKCE. 163 Browser-based applications MUST: 165 o Use the OAuth 2.0 authorization code flow with the PKCE extension 167 o Protect themselves against CSRF attacks by ensuring the 168 authorization server supports PKCE, or by using the OAuth 2.0 169 "state" parameter or the OpenID Connect "nonce" parameter to carry 170 one-time use CSRF tokens 172 o Register one or more redirect URIs, and use only exact registered 173 redirect URIs in authorization requests 175 OAuth 2.0 authorization servers MUST: 177 o Require exact matching of registered redirect URIs 179 o Support the PKCE extension 181 o If issuing refresh tokens to browser-based apps, then: 183 o Rotate refresh tokens on each use 185 o Set a maximum lifetime on refresh tokens or expire if they are not 186 used in some amount of time 188 5. First-Party Applications 190 While OAuth was initially created to allow third-party applications 191 to access an API on behalf of a user, it has proven to be useful in a 192 first-party scenario as well. First-party apps are applications 193 where the same organization provides both the API and the 194 application. 196 Examples of first-party applications are a web email client provided 197 by the operator of the email account, or a mobile banking application 198 created by bank itself. (Note that there is no requirement that the 199 application actually be developed by the same company; a mobile 200 banking application developed by a contractor that is branded as the 201 bank's application is still considered a first-party application.) 202 The first-party app consideration is about the user's relationship to 203 the application and the service. 205 To conform to this best practice, first-party applications using 206 OAuth or OpenID Connect MUST use a redirect-based flow (such as the 207 OAuth Authorization Code flow) as described later in this document. 209 The Resource Owner Password Grant MUST NOT be used, as described in 210 [oauth-security-topics] section 3.4. Instead, by using the 211 Authorization Code flow and redirecting the user to the authorization 212 server, this provides the authorization server the opportunity to 213 prompt the user for multi-factor authentication options, take 214 advantage of single-sign-on sessions, or use third-party identity 215 providers. In contrast, the Password grant does not provide any 216 built-in mechanism for these, and would instead be extended with 217 custom code. 219 6. Application Architecture Patterns 221 There are three primary architectural patterns available when 222 building browser-based applications. 224 o a JavaScript application that has methods of sharing data with 225 resource servers, such as using common-domain cookies 227 o a JavaScript application with a backend 229 o a JavaScript application with no backend, accessing resource 230 servers directly 232 These three architectures have different use cases and 233 considerations. 235 6.1. Browser-Based Apps that Can Share Data with the Resource Server 237 For simple system architectures, such as when the JavaScript 238 application is served from a domain that can share cookies with the 239 domain of the API (resource server), OAuth adds additional attack 240 vectors that could be avoided with a different solution. 242 In particular, using any redirect-based mechanism of obtaining an 243 access token enables the redirect-based attacks described in 244 [oauth-security-topics], but if the application, authorization server 245 and resource server share a domain, then it is unnecessary to use a 246 redirect mechanism to communicate between them. 248 An additional concern with handling access tokens in a browser is 249 that as of the date of this publication, there is no secure storage 250 mechanism where JavaScript code can keep the access token to be later 251 used in an API request. Using an OAuth flow results in the 252 JavaScript code getting an access token, needing to store it 253 somewhere, and then retrieve it to make an API request. 255 Instead, a more secure design is to use an HTTP-only cookie between 256 the JavaScript application and API so that the JavaScript code can't 257 access the cookie value itself. Additionally, the SameSite cookie 258 attribute can be used to prevent CSRF attacks, or alternatively, the 259 application and API could be written to use anti-CSRF tokens. 261 OAuth was originally created for third-party or federated access to 262 APIs, so it may not be the best solution in a common-domain 263 deployment. That said, using OAuth even in a common-domain 264 architecture does mean you can more easily rearchitect things later, 265 such as if you were to later add a new domain to the system. 267 6.2. JavaScript Applications with a Backend 268 +-------------+ 269 | | 270 |Authorization| 271 | Server | 272 | | 273 +-------------+ 275 ^ + 276 |(A) |(B) 277 | | 278 + v 280 +-------------+ +--------------+ 281 | | +---------> | | 282 | Application | (C) | Resource | 283 | Server | | Server | 284 | | <---------+ | | 285 +-------------+ (D) +--------------+ 287 ^ + 288 | | 289 | | browser 290 | | cookie 291 | | 292 + v 294 +-------------+ 295 | | 296 | Browser | 297 | | 298 +-------------+ 300 In this architecture, the JavaScript code is loaded from a dynamic 301 Application Server that also has the ability to execute code itself. 302 This enables the ability to keep all of the steps involved in 303 obtaining an access token outside of the JavaScript application. 305 In this case, the Application Server performs the OAuth flow itself, 306 and keeps the access token and refresh token stored internally, 307 creating a separate session with the browser-based app via a 308 traditional browser cookie. 310 (Common examples of this architecture are an Angular front-end with a 311 .NET backend, or a React front-end with a Spring Boot backend.) 313 The Application Server SHOULD be considered a confidential client, 314 and issued its own client secret. The Application Server SHOULD use 315 the OAuth 2.0 authorization code grant to initiate a request for an 316 access token. Upon handling the redirect from the Authorization 317 Server, the Application Server will request an access token using the 318 authorization code returned (A), which will be returned to the 319 Application Server (B). The Application Server stores this access 320 token itself and establishes its own cookie-based session with the 321 Browser application. The Application Server can store the access 322 token either server-side, or in the cookie itself. 324 When the JavaScript application in the browser wants to make a 325 request to the Resource Server, it MUST instead make the request to 326 the Application Server, and the Application Server will make the 327 request with the access token to the Resource Server (C), and forward 328 the response (D) back to the browser. 330 Security of the connection between code running in the browser and 331 this Application Server is assumed to utilize browser-level 332 protection mechanisms. Details are out of scope of this document, 333 but many recommendations can be found in the OWASP Cheat Sheet series 334 (https://cheatsheetseries.owasp.org/), such as setting an HTTP-only 335 and Secure cookie to authenticate the session between the browser and 336 Application Server. 338 In this scenario, the session between the browser and Application 339 Server MAY be either a session cookie provided by the Application 340 Server, OR the access token itself. Note that if the access token is 341 used as the session identifier, this exposes the access token to the 342 end user even if it is not available to the JavaScript application, 343 so some authorization servers may wish to limit the capabilities of 344 these clients to mitigate risk. 346 6.3. JavaScript Applications without a Backend 347 +---------------+ +--------------+ 348 | | | | 349 | Authorization | | Resource | 350 | Server | | Server | 351 | | | | 352 +---------------+ +--------------+ 354 ^ + ^ + 355 | | | | 356 |(B) |(C) |(D) |(E) 357 | | | | 358 | | | | 359 + v + v 361 +-----------------+ +-------------------------------+ 362 | | (A) | | 363 | Static Web Host | +-----> | Browser | 364 | | | | 365 +-----------------+ +-------------------------------+ 367 In this architecture, the JavaScript code is first loaded from a 368 static web host into the browser (A), and the application then runs 369 in the browser. This application is considered a public client, 370 since there is no way to issue it a client secret and there is no 371 other secure client authentication mechanism available in the 372 browser. 374 The code in the browser initiates the authorization code flow with 375 the PKCE extension (described in Section 7) (B) above, and obtains an 376 access token via a POST request (C). The JavaScript app is then 377 responsible for storing the access token (and optional refresh token) 378 securely using appropriate browser APIs. 380 When the JavaScript application in the browser wants to make a 381 request to the Resource Server, it can include the access token in 382 the request (D) and make the request directly. 384 In this scenario, the Authorization Server and Resource Server MUST 385 support the necessary CORS headers to enable the JavaScript code to 386 make this POST request from the domain on which the script is 387 executing. (See Section 9.6 for additional details.) 389 7. Authorization Code Flow 391 Public browser-based apps that use the authorization code grant type 392 described in Section 4.1 of OAuth 2.0 [RFC6749] MUST also follow 393 these additional requirements described in this section. 395 7.1. Initiating the Authorization Request from a Browser-Based 396 Application 398 Public browser-based apps MUST implement the Proof Key for Code 399 Exchange (PKCE [RFC7636]) extension to OAuth, and authorization 400 servers MUST support PKCE for such clients. 402 The PKCE extension prevents an attack where the authorization code is 403 intercepted and exchanged for an access token by a malicious client, 404 by providing the authorization server with a way to verify the same 405 client instance that exchanges the authorization code is the same one 406 that initiated the flow. 408 Browser-based apps MUST prevent CSRF attacks against their redirect 409 URI. This can be accomplished by any of the below: 411 o using PKCE, and confirming that the authorization server supports 412 PKCE 414 o if the application is using OpenID Connect, by using the OpenID 415 Connect "nonce" parameter 417 o using a unique value for the OAuth 2.0 "state" parameter 419 Browser-based apps MUST follow the recommendations in 420 [oauth-security-topics] Section 2.1 to protect themselves during 421 redirect flows. 423 7.2. Handling the Authorization Code Redirect 425 Authorization servers MUST require an exact match of a registered 426 redirect URI. 428 8. Refresh Tokens 430 Refresh tokens provide a way for applications to obtain a new access 431 token when the initial access token expires. With public clients, 432 the risk of a leaked refresh token is greater than leaked access 433 tokens, since an attacker may be able to continue using the stolen 434 refresh token to obtain new access tokens potentially without being 435 detectable by the authorization server. 437 Browser-based applications provide an attacker with several 438 opportunities by which a refresh token can be leaked, just as with 439 access tokens. As such, these applications are considered a higher 440 risk for handling refresh tokens. 442 Authorization servers may choose whether or not to issue refresh 443 tokens to browser-based applications. [oauth-security-topics] 444 describes some additional requirements around refresh tokens on top 445 of the recommendations of [RFC6749]. Applications and authorization 446 servers conforming to this BCP MUST also follow the recommendations 447 in [oauth-security-topics] around refresh tokens if refresh tokens 448 are issued to browser-based apps. 450 In particular, authorization servers: 452 o MUST rotate refresh tokens on each use, in order to be able to 453 detect a stolen refresh token if one is replayed (described in 454 [oauth-security-topics] section 4.12) 456 o MUST either set a maximum lifetime on refresh tokens OR expire if 457 the refresh token has not been used within some amount of time 459 o upon issuing a rotated refresh token, MUST NOT extend the lifetime 460 of the new refresh token beyond the lifetime of the initial 461 refresh token if the refresh token has a preestablished expiration 462 time 464 For example: 466 o A user authorizes an application, issuing an access token that 467 lasts 1 hour, and a refresh token that lasts 24 hours 469 o After 1 hour, the initial access token expires, so the application 470 uses the refresh token to get a new access token 472 o The authorization server returns a new access token that lasts 1 473 hour, and a new refresh token that lasts 23 hours 475 o This continues until 24 hours pass from the initial authorization 477 o At this point, when the application attempts to use the refresh 478 token after 24 hours, the request will fail and the application 479 will have to involve the user in a new authorization request 481 By limiting the overall refresh token lifetime to the lifetime of the 482 initial refresh token, this ensures a stolen refresh token cannot be 483 used indefinitely. 485 9. Security Considerations 486 9.1. Registration of Browser-Based Apps 488 Browser-based applications are considered public clients as defined 489 by section 2.1 of OAuth 2.0 [RFC6749], and MUST be registered with 490 the authorization server as such. Authorization servers MUST record 491 the client type in the client registration details in order to 492 identify and process requests accordingly. 494 Authorization servers MUST require that browser-based applications 495 register one or more redirect URIs. 497 9.2. Client Authentication 499 Since a browser-based application's source code is delivered to the 500 end-user's browser, it cannot contain provisioned secrets. As such, 501 a browser-based app with native OAuth support is considered a public 502 client as defined by Section 2.1 of OAuth 2.0 [RFC6749]. 504 Secrets that are statically included as part of an app distributed to 505 multiple users should not be treated as confidential secrets, as one 506 user may inspect their copy and learn the shared secret. For this 507 reason, and those stated in Section 5.3.1 of [RFC6819], it is NOT 508 RECOMMENDED for authorization servers to require client 509 authentication of browser-based applications using a shared secret, 510 as this serves little value beyond client identification which is 511 already provided by the client_id request parameter. 513 Authorization servers that still require a statically included shared 514 secret for SPA clients MUST treat the client as a public client, and 515 not accept the secret as proof of the client's identity. Without 516 additional measures, such clients are subject to client impersonation 517 (see Section 9.3 below). 519 9.3. Client Impersonation 521 As stated in Section 10.2 of OAuth 2.0 [RFC6749], the authorization 522 server SHOULD NOT process authorization requests automatically 523 without user consent or interaction, except when the identity of the 524 client can be assured. 526 If authorization servers restrict redirect URIs to a fixed set of 527 absolute HTTPS URIs, preventing the use of wildcard domains, wildcard 528 paths, or wildcard query string components, this exact match of 529 registered absolute HTTPS URIs MAY be accepted by authorization 530 servers as proof of identity of the client for the purpose of 531 deciding whether to automatically process an authorization request 532 when a previous request for the client_id has already been approved. 534 9.4. Cross-Site Request Forgery Protections 536 Clients MUST prevent Cross-Site Request Forgery (CSRF) attacks 537 against their redirect URI. Clients can accomplish this by either 538 ensuring the authorization server supports PKCE and relying on the 539 CSRF protection that PKCE provides, or if the client is also an 540 OpenID Connect client, using the OpenID Connect "nonce" parameter, or 541 by using the "state" parameter to carry one-time-use CSRF tokens as 542 described in Section 7.1. 544 See Section 2.1 of [oauth-security-topics] for additional details. 546 9.5. Authorization Server Mix-Up Mitigation 548 The security considerations around the authorization server mix-up 549 that are referenced in Section 8.10 of [RFC8252] also apply to 550 browser-based apps. 552 Clients MUST use a unique redirect URI for each authorization server 553 used by the application. The client MUST store the redirect URI 554 along with the session data (e.g. along with "state") and MUST verify 555 that the URI on which the authorization response was received exactly 556 matches. 558 9.6. Cross-Domain Requests 560 To complete the authorization code flow, the browser-based 561 application will need to exchange the authorization code for an 562 access token at the token endpoint. If the authorization server 563 provides additional endpoints to the application, such as metadata 564 URLs, dynamic client registration, revocation, introspection, 565 discovery or user info endpoints, these endpoints may also be 566 accessed by the browser-based app. Since these requests will be made 567 from a browser, authorization servers MUST support the necessary CORS 568 headers (defined in [Fetch]) to allow the browser to make the 569 request. 571 This specification does not include guidelines for deciding whether a 572 CORS policy for the token endpoint should be a wildcard origin or 573 more restrictive. Note, however, that the browser will attempt to 574 GET or POST to the API endpoint before knowing any CORS policy; it 575 simply hides the succeeding or failing result from JavaScript if the 576 policy does not allow sharing. 578 9.7. Content-Security Policy 580 A browser-based application that wishes to use either long-lived 581 refresh tokens or privileged scopes SHOULD restrict its JavaScript 582 execution to a set of statically hosted scripts via a Content 583 Security Policy ([CSP2]) or similar mechanism. A strong Content 584 Security Policy can limit the potential attack vectors for malicious 585 JavaScript to be executed on the page. 587 9.8. OAuth Implicit Flow 589 The OAuth 2.0 Implicit flow (defined in Section 4.2 of OAuth 2.0 590 [RFC6749]) works by receiving an access token in the HTTP redirect 591 (front-channel) immediately without the code exchange step. In this 592 case, the access token is returned in the fragment part of the 593 redirect URI, providing an attacker with several opportunities to 594 intercept and steal the access token. 596 9.8.1. Attacks on the Implicit Flow 598 Many attacks on the implicit flow described by [RFC6819] and 599 [oauth-security-topics] do not have sufficient mitigation strategies. 600 The following sections describe the specific attacks that cannot be 601 mitigated while continuing to use the implicit flow. 603 9.8.1.1. Threat: Interception of the Redirect URI 605 If an attacker is able to cause the authorization response to be sent 606 to a URI under their control, they will directly get access to the 607 authorization response including the access token. Several methods 608 of performing this attack are described in detail in 609 [oauth-security-topics]. 611 9.8.1.2. Threat: Access Token Leak in Browser History 613 An attacker could obtain the access token from the browser's history. 614 The countermeasures recommended by [RFC6819] are limited to using 615 short expiration times for tokens, and indicating that browsers 616 should not cache the response. Neither of these fully prevent this 617 attack, they only reduce the potential damage. 619 Additionally, many browsers now also sync browser history to cloud 620 services and to multiple devices, providing an even wider attack 621 surface to extract access tokens out of the URL. 623 This is discussed in more detail in Section 4.3.2 of 624 [oauth-security-topics]. 626 9.8.1.3. Threat: Manipulation of Scripts 628 An attacker could modify the page or inject scripts into the browser 629 through various means, including when the browser's HTTPS connection 630 is being man-in-the-middled by, for example, a corporate network. 631 While this type of attack is typically out of scope of basic security 632 recommendations to prevent, in the case of browser-based apps it is 633 much easier to perform this kind of attack, where an injected script 634 can suddenly have access to everything on the page. 636 The risk of a malicious script running on the page may be amplified 637 when the application uses a known standard way of obtaining access 638 tokens, namely that the attacker can always look at the 639 "window.location" variable to find an access token. This threat 640 profile is different from an attacker specifically targeting an 641 individual application by knowing where or how an access token 642 obtained via the authorization code flow may end up being stored. 644 9.8.1.4. Threat: Access Token Leak to Third Party Scripts 646 It is relatively common to use third-party scripts in browser-based 647 apps, such as analytics tools, crash reporting, and even things like 648 a Facebook or Twitter "like" button. In these situations, the author 649 of the application may not be able to be fully aware of the entirety 650 of the code running in the application. When an access token is 651 returned in the fragment, it is visible to any third-party scripts on 652 the page. 654 9.8.2. Countermeasures 656 In addition to the countermeasures described by [RFC6819] and 657 [oauth-security-topics], using the authorization code with PKCE 658 extension prevents the attacks described above by avoiding returning 659 the access token in the redirect response at all. 661 When PKCE is used, if an authorization code is stolen in transport, 662 the attacker is unable to do anything with the authorization code. 664 9.8.3. Disadvantages of the Implicit Flow 666 There are several additional reasons the Implicit flow is 667 disadvantageous compared to using the standard Authorization Code 668 flow. 670 o OAuth 2.0 provides no mechanism for a client to verify that a 671 particular access token was intended for that client, which could 672 lead to misuse and possible impersonation attacks if a malicious 673 party hands off an access token it retrieved through some other 674 means to the client. 676 o Returning an access token in the front-channel redirect gives the 677 authorization server no assurance that the access token will 678 actually end up at the application, since there are many ways this 679 redirect may fail or be intercepted. 681 o Supporting the implicit flow requires additional code, more upkeep 682 and understanding of the related security considerations, while 683 limiting the authorization server to just the authorization code 684 flow reduces the attack surface of the implementation. 686 o If the JavaScript application gets wrapped into a native app, then 687 [RFC8252] also requires the use of the authorization code flow 688 with PKCE anyway. 690 In OpenID Connect, the id_token is sent in a known format (as a JWT), 691 and digitally signed. Returning an id_token using the Implicit flow 692 (response_type=id_token) requires the client validate the JWT 693 signature, as malicious parties could otherwise craft and supply 694 fraudulent id_tokens. Performing OpenID Connect using the 695 authorization code flow provides the benefit of the client not 696 needing to verify the JWT signature, as the ID token will have been 697 fetched over an HTTPS connection directly from the authorization 698 server. Additionally, in many cases an application will request both 699 an ID token and an access token, so it is simplier and provides fewer 700 attack vectors to obtain both via the authorization code flow. 702 9.8.4. Historic Note 704 Historically, the Implicit flow provided an advantage to single-page 705 apps since JavaScript could always arbitrarily read and manipulate 706 the fragment portion of the URL without triggering a page reload. 707 This was necessary in order to remove the access token from the URL 708 after it was obtained by the app. 710 Modern browsers now have the Session History API (described in 711 "Session history and navigation" of [HTML]), which provides a 712 mechanism to modify the path and query string component of the URL 713 without triggering a page reload. This means modern browser-based 714 apps can use the unmodified OAuth 2.0 authorization code flow, since 715 they have the ability to remove the authorization code from the query 716 string without triggering a page reload thanks to the Session History 717 API. 719 9.9. Additional Security Considerations 721 The OWASP Foundation (https://www.owasp.org/) maintains a set of 722 security recommendations and best practices for web applications, and 723 it is RECOMMENDED to follow these best practices when creating an 724 OAuth 2.0 Browser-Based application. 726 10. IANA Considerations 728 This document does not require any IANA actions. 730 11. References 732 11.1. Normative References 734 [CSP2] West, M., "Content Security Policy", October 2018. 736 [Fetch] whatwg, "Fetch", 2018. 738 [oauth-security-topics] 739 Lodderstedt, T., Bradley, J., Labunets, A., and D. Fett, 740 "OAuth 2.0 Security Best Current Practice", July 2019. 742 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 743 Requirement Levels", BCP 14, RFC 2119, 744 DOI 10.17487/RFC2119, March 1997, 745 . 747 [RFC6749] Hardt, D., Ed., "The OAuth 2.0 Authorization Framework", 748 RFC 6749, DOI 10.17487/RFC6749, October 2012, 749 . 751 [RFC6750] Jones, M. and D. Hardt, "The OAuth 2.0 Authorization 752 Framework: Bearer Token Usage", RFC 6750, 753 DOI 10.17487/RFC6750, October 2012, 754 . 756 [RFC6819] Lodderstedt, T., Ed., McGloin, M., and P. Hunt, "OAuth 2.0 757 Threat Model and Security Considerations", RFC 6819, 758 DOI 10.17487/RFC6819, January 2013, 759 . 761 [RFC7636] Sakimura, N., Ed., Bradley, J., and N. Agarwal, "Proof Key 762 for Code Exchange by OAuth Public Clients", RFC 7636, 763 DOI 10.17487/RFC7636, September 2015, 764 . 766 [RFC8252] Denniss, W. and J. Bradley, "OAuth 2.0 for Native Apps", 767 BCP 212, RFC 8252, DOI 10.17487/RFC8252, October 2017, 768 . 770 11.2. Informative References 772 [HTML] whatwg, "HTML", 2020. 774 Appendix A. Server Support Checklist 776 OAuth authorization servers that support browser-based apps MUST: 778 1. Require "https" scheme redirect URIs. 780 2. Require exact matching of registered redirect URIs. 782 3. Support PKCE [RFC7636]. Required to protect authorization code 783 grants sent to public clients. See Section 7.1 785 4. Support cross-domain requests at the token endpoint in order to 786 allow browsers to make the authorization code exchange request. 787 See Section 9.6 789 5. Not assume that browser-based clients can keep a secret, and 790 SHOULD NOT issue secrets to applications of this type. 792 6. Not support the Resource Owner Password grant for browser-based 793 clients. 795 7. Follow the [oauth-security-topics] recommendations on refresh 796 tokens, as well as the additional requirements described in 797 Section 8. 799 Appendix B. Document History 801 [[ To be removed from the final specification ]] 803 -06 805 o Added refresh token requirements to AS summary 807 o Editorial clarifications 809 -05 811 o Incorporated editorial and substantive feedback from Mike Jones 813 o Added references to "nonce" as another way to prevent CSRF attacks 814 o Updated headers in the Implicit Flow section to better represent 815 the relationship between the paragraphs 817 -04 819 o Disallow the use of the Password Grant 821 o Add PKCE support to summary list for authorization server 822 requirements 824 o Rewrote refresh token section to allow refresh tokens if they are 825 time-limited, rotated on each use, and requiring that the rotated 826 refresh token lifetimes do not extend past the lifetime of the 827 initial refresh token, and to bring it in line with the Security 828 BCP 830 o Updated recommendations on using state to reflect the Security BCP 832 o Updated server support checklist to reflect latest changes 834 o Updated the same-domain JS architecture section to emphasize the 835 architecture rather than domain 837 o Editorial clarifications in the section that talks about OpenID 838 Connect ID tokens 840 -03 842 o Updated the historic note about the fragment URL clarifying that 843 the Session History API means browsers can use the unmodified 844 authorization code flow 846 o Rephrased "Authorization Code Flow" intro paragraph to better lead 847 into the next two sections 849 o Softened "is likely a better decision to avoid using OAuth 850 entirely" to "it may be..." for common-domain deployments 852 o Updated abstract to not be limited to public clients, since the 853 later sections talk about confidential clients 855 o Removed references to avoiding OpenID Connect for same-domain 856 architectures 858 o Updated headers to better describe architectures (Apps Served from 859 a Static Web Server -> JavaScript Applications without a Backend) 861 o Expanded "same-domain architecture" section to better explain the 862 problems that OAuth has in this scenario 864 o Referenced Security BCP in implicit flow attacks where possible 866 o Minor typo corrections 868 -02 870 o Rewrote overview section incorporating feedback from Leo Tohill 872 o Updated summary recommendation bullet points to split out 873 application and server requirements 875 o Removed the allowance on hostname-only redirect URI matching, now 876 requiring exact redirect URI matching 878 o Updated section 6.2 to drop reference of SPA with a backend 879 component being a public client 881 o Expanded the architecture section to explicitly mention three 882 architectural patterns available to JS apps 884 -01 886 o Incorporated feedback from Torsten Lodderstedt 888 o Updated abstract 890 o Clarified the definition of browser-based apps to not exclude 891 applications cached in the browser, e.g. via Service Workers 893 o Clarified use of the state parameter for CSRF protection 895 o Added background information about the original reason the 896 implicit flow was created due to lack of CORS support 898 o Clarified the same-domain use case where the SPA and API share a 899 cookie domain 901 o Moved historic note about the fragment URL into the Overview 903 Appendix C. Acknowledgements 905 The authors would like to acknowledge the work of William Denniss and 906 John Bradley, whose recommendation for native apps informed many of 907 the best practices for browser-based applications. The authors would 908 also like to thank Hannes Tschofenig and Torsten Lodderstedt, the 909 attendees of the Internet Identity Workshop 27 session at which this 910 BCP was originally proposed, and the following individuals who 911 contributed ideas, feedback, and wording that shaped and formed the 912 final specification: 914 Annabelle Backman, Brian Campbell, Brock Allen, Christian Mainka, 915 Daniel Fett, George Fletcher, Hannes Tschofenig, Janak Amarasena, 916 John Bradley, Joseph Heenan, Justin Richer, Karl McGuinness, Leo 917 Tohill, Mike Jones, Tomek Stojecki, Torsten Lodderstedt, and Vittorio 918 Bertocci. 920 Authors' Addresses 922 Aaron Parecki 923 Okta 925 Email: aaron@parecki.com 926 URI: https://aaronparecki.com 928 David Waite 929 Ping Identity 931 Email: david@alkaline-solutions.com