idnits 2.17.1 draft-ietf-oauth-browser-based-apps-08.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 (May 17, 2021) is 1075 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: November 18, 2021 Ping Identity 6 May 17, 2021 8 OAuth 2.0 for Browser-Based Apps 9 draft-ietf-oauth-browser-based-apps-08 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 November 18, 2021. 34 Copyright Notice 36 Copyright (c) 2021 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 . . . . . . . . . . . . . . . . . . 5 56 6. Application Architecture Patterns . . . . . . . . . . . . . . 5 57 6.1. Browser-Based Apps that Can Share Data with the Resource 58 Server . . . . . . . . . . . . . . . . . . . . . . . . . 6 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 . . . . . . . . . . . . . . . . . . . . . . . 9 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 . . . . . . . . . . . 11 68 9.2. Client Authentication . . . . . . . . . . . . . . . . . . 11 69 9.3. Client Impersonation . . . . . . . . . . . . . . . . . . 12 70 9.4. Cross-Site Request Forgery Protections . . . . . . . . . 12 71 9.5. Authorization Server Mix-Up Mitigation . . . . . . . . . 12 72 9.6. Cross-Domain Requests . . . . . . . . . . . . . . . . . . 13 73 9.7. Content Security Policy . . . . . . . . . . . . . . . . . 13 74 9.8. OAuth Implicit Flow . . . . . . . . . . . . . . . . . . . 13 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 . . . . . . . . . . . 16 80 10. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 16 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 . . . . . . . . . . . . . . . . . . 21 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 Many of these recommendations are derived from the OAuth 2.0 Security 111 Best Current Practice [oauth-security-topics] and browser-based apps 112 are expected to follow those recommendations as well. This draft 113 expands on and further restricts various recommendations in 114 [oauth-security-topics]. 116 2. Notational Conventions 118 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 119 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and 120 "OPTIONAL" in this document are to be interpreted as described in 121 [RFC2119]. 123 3. Terminology 125 In addition to the terms defined in referenced specifications, this 126 document uses the following terms: 128 "OAuth": In this document, "OAuth" refers to OAuth 2.0, [RFC6749] 129 and [RFC6750]. 131 "Browser-based application": An application that is dynamically 132 downloaded and executed in a web browser, usually written in 133 JavaScript. Also sometimes referred to as a "single-page 134 application", or "SPA". 136 4. Overview 138 At the time that OAuth 2.0 [RFC6749] and [RFC6750] were created, 139 browser-based JavaScript applications needed a solution that strictly 140 complied with the same-origin policy. Common deployments of OAuth 141 2.0 involved an application running on a different domain than the 142 authorization server, so it was historically not possible to use the 143 Authorization Code flow which would require a cross-origin POST 144 request. This was one of the motivations for the definition of the 145 Implicit flow, which returns the access token in the front channel 146 via the fragment part of the URL, bypassing the need for a cross- 147 origin POST request. 149 However, there are several drawbacks to the Implicit flow, generally 150 involving vulnerabilities associated with the exposure of the access 151 token in the URL. See Section 9.8 for an analysis of these attacks 152 and the drawbacks of using the Implicit flow in browsers. Additional 153 attacks and security considerations can be found in 154 [oauth-security-topics]. 156 In recent years, widespread adoption of Cross-Origin Resource Sharing 157 (CORS), which enables exceptions to the same-origin policy, allows 158 browser-based apps to use the OAuth 2.0 Authorization Code flow and 159 make a POST request to exchange the authorization code for an access 160 token at the token endpoint. In this flow, the access token is never 161 exposed in the less secure front channel. Furthermore, adding PKCE 162 to the flow ensures that even if an authorization code is 163 intercepted, it is unusable by an attacker. 165 For this reason, and from other lessons learned, the current best 166 practice for browser-based applications is to use the OAuth 2.0 167 Authorization Code flow with PKCE. 169 Browser-based applications: 171 o MUST use the OAuth 2.0 Authorization Code flow with the PKCE 172 extension when obtaining an access token 174 o MUST Protect themselves against CSRF attacks by either: 176 * ensuring the authorization server supports PKCE, or 178 * by using the OAuth 2.0 "state" parameter or the OpenID Connect 179 "nonce" parameter to carry one-time use CSRF tokens 181 o MUST Register one or more redirect URIs, and use only exact 182 registered redirect URIs in authorization requests 184 OAuth 2.0 authorization servers supporting browser-based 185 applications: 187 o MUST Require exact matching of registered redirect URIs 189 o MUST Support the PKCE extension 191 o MUST NOT issue access tokens in the authorization response 192 o If issuing refresh tokens to browser-based applications, then: 194 * MUST rotate refresh tokens on each use or use sender- 195 constrained refresh tokens, and 197 * MUST set a maximum lifetime on refresh tokens or expire if they 198 are not used in some amount of time 200 5. First-Party Applications 202 While OAuth was initially created to allow third-party applications 203 to access an API on behalf of a user, it has proven to be useful in a 204 first-party scenario as well. First-party apps are applications 205 where the same organization provides both the API and the 206 application. 208 Examples of first-party applications are a web email client provided 209 by the operator of the email account, or a mobile banking application 210 created by bank itself. (Note that there is no requirement that the 211 application actually be developed by the same company; a mobile 212 banking application developed by a contractor that is branded as the 213 bank's application is still considered a first-party application.) 214 The first-party app consideration is about the user's relationship to 215 the application and the service. 217 To conform to this best practice, first-party applications using 218 OAuth or OpenID Connect MUST use a redirect-based flow (such as the 219 OAuth Authorization Code flow) as described later in this document. 221 The resource owner password credentials grant MUST NOT be used, as 222 described in [oauth-security-topics] Section 2.4. Instead, by using 223 the Authorization Code flow and redirecting the user to the 224 authorization server, this provides the authorization server the 225 opportunity to prompt the user for multi-factor authentication 226 options, take advantage of single sign-on sessions, or use third- 227 party identity providers. In contrast, the resource owner password 228 credentials grant does not provide any built-in mechanism for these, 229 and would instead be extended with custom code. 231 6. Application Architecture Patterns 233 There are three primary architectural patterns available when 234 building browser-based applications. 236 o a JavaScript application that has methods of sharing data with 237 resource servers, such as using common-domain cookies 239 o a JavaScript application with a backend 240 o a JavaScript application with no backend, accessing resource 241 servers directly 243 These three architectures have different use cases and 244 considerations. 246 6.1. Browser-Based Apps that Can Share Data with the Resource Server 248 For simple system architectures, such as when the JavaScript 249 application is served from a domain that can share cookies with the 250 domain of the API (resource server), OAuth adds additional attack 251 vectors that could be avoided with a different solution. 253 In particular, using any redirect-based mechanism of obtaining an 254 access token enables the redirect-based attacks described in 255 [oauth-security-topics] Section 4, but if the application, 256 authorization server and resource server share a domain, then it is 257 unnecessary to use a redirect mechanism to communicate between them. 259 An additional concern with handling access tokens in a browser is 260 that as of the date of this publication, there is no secure storage 261 mechanism where JavaScript code can keep the access token to be later 262 used in an API request. Using an OAuth flow results in the 263 JavaScript code getting an access token, needing to store it 264 somewhere, and then retrieve it to make an API request. 266 Instead, a more secure design is to use an HTTP-only cookie between 267 the JavaScript application and API so that the JavaScript code can't 268 access the cookie value itself. The Secure cookie attribute should 269 be used to ensure the cookie is not included in unencrypted HTTP 270 requests. Additionally, the SameSite cookie attribute can be used to 271 prevent CSRF attacks, or alternatively, the application and API could 272 be written to use anti-CSRF tokens. 274 OAuth was originally created for third-party or federated access to 275 APIs, so it may not be the best solution in a common-domain 276 deployment. That said, using OAuth even in a common-domain 277 architecture does mean you can more easily rearchitect things later, 278 such as if you were to later add a new domain to the system. 280 6.2. JavaScript Applications with a Backend 281 +-------------+ +--------------+ +---------------+ 282 | | | | | | 283 |Authorization| | Token | | Resource | 284 | Endpoint | | Endpoint | | Server | 285 | | | | | | 286 +-------------+ +--------------+ +---------------+ 288 ^ ^ ^ 289 | (D)| (G)| 290 | v v 291 | 292 | +--------------------------------+ 293 | | | 294 | | Application | 295 (B)| | Server | 296 | | | 297 | +--------------------------------+ 298 | 299 | ^ ^ + ^ + 300 | (A)| (C)| (E)| (F)| |(H) 301 v v + v + v 303 +-------------------------------------------------+ 304 | | 305 | Browser | 306 | | 307 +-------------------------------------------------+ 309 In this architecture, commonly referred to as "backend for frontend" 310 or "BFF", the JavaScript code is loaded from a dynamic Application 311 Server (A) that also has the ability to execute code itself. This 312 enables the ability to keep all of the steps involved in obtaining an 313 access token outside of the JavaScript application. 315 In this case, the Application Server initiates the OAuth flow itself, 316 by redirecting the browser to the authorization endpoint (B). When 317 the user is redirected back, the browser delivers the authorization 318 code to the application server (C), where it can then exchange it for 319 an access token at the token endpoint (D) using its client secret. 320 The application server then keeps the access token and refresh token 321 stored internally, and creates a separate session with the browser- 322 based app via a traditional browser cookie (E). 324 When the JavaScript application in the browser wants to make a 325 request to the Resource Server, it instead makes the request to the 326 Application Server (F), and the Application Server will make the 327 request with the access token to the Resource Server (G), and forward 328 the response (H) back to the browser. 330 (Common examples of this architecture are an Angular front-end with a 331 .NET backend, or a React front-end with a Spring Boot backend.) 333 The Application Server SHOULD be considered a confidential client, 334 and issued its own client secret. The Application Server SHOULD use 335 the OAuth 2.0 Authorization Code grant with PKCE to initiate a 336 request for an access token. Detailed recommendations for 337 confidential clients can be found in [oauth-security-topics] 338 Section 2.1.1. 340 In this scenario, the session between the browser and Application 341 Server SHOULD be a session cookie provided by the Application Server. 343 Security of the connection between code running in the browser and 344 this Application Server is assumed to utilize browser-level 345 protection mechanisms. Details are out of scope of this document, 346 but many recommendations can be found in the OWASP Cheat Sheet series 347 (https://cheatsheetseries.owasp.org/), such as setting an HTTP-only 348 and Secure cookie to authenticate the session between the browser and 349 Application Server. 351 6.3. JavaScript Applications without a Backend 353 +---------------+ +--------------+ 354 | | | | 355 | Authorization | | Resource | 356 | Server | | Server | 357 | | | | 358 +---------------+ +--------------+ 360 ^ ^ ^ + 361 | | | | 362 |(B) |(C) |(D) |(E) 363 | | | | 364 | | | | 365 + v + v 367 +-----------------+ +-------------------------------+ 368 | | (A) | | 369 | Static Web Host | +-----> | Browser | 370 | | | | 371 +-----------------+ +-------------------------------+ 373 In this architecture, the JavaScript code is first loaded from a 374 static web host into the browser (A), and the application then runs 375 in the browser. This application is considered a public client, 376 since there is no way to issue it a client secret and there is no 377 other secure client authentication mechanism available in the 378 browser. 380 The code in the browser initiates the Authorization Code flow with 381 the PKCE extension (described in Section 7) (B) above, and obtains an 382 access token via a POST request (C). The JavaScript application is 383 then responsible for storing the access token (and optional refresh 384 token) as securely as possible using appropriate browser APIs. As of 385 the date of this publication there is no browser API that allows to 386 store tokens in a completely secure way. 388 When the JavaScript application in the browser wants to make a 389 request to the Resource Server, it can interact with the Resource 390 Server directly. It includes the access token in the request (D) and 391 receives the Resource Server's response (E). 393 In this scenario, the Authorization Server and Resource Server MUST 394 support the necessary CORS headers to enable the JavaScript code to 395 make this POST request from the domain on which the script is 396 executing. (See Section 9.6 for additional details.) 398 7. Authorization Code Flow 400 Browser-based applications that are public clients and use the 401 Authorization Code grant type described in Section 4.1 of OAuth 2.0 402 [RFC6749] MUST also follow these additional requirements described in 403 this section. 405 7.1. Initiating the Authorization Request from a Browser-Based 406 Application 408 Browser-based applications that are public clients MUST implement the 409 Proof Key for Code Exchange (PKCE [RFC7636]) extension when obtaining 410 an access token, and authorization servers MUST support and enforce 411 PKCE for such clients. 413 The PKCE extension prevents an attack where the authorization code is 414 intercepted and exchanged for an access token by a malicious client, 415 by providing the authorization server with a way to verify the client 416 instance that exchanges the authorization code is the same one that 417 initiated the flow. 419 Browser-based applications MUST prevent CSRF attacks against their 420 redirect URI. This can be accomplished by any of the below: 422 o using PKCE, and confirming that the authorization server supports 423 PKCE 425 o using a unique value for the OAuth 2.0 "state" parameter 427 o if the application is using OpenID Connect, by using the OpenID 428 Connect "nonce" parameter 430 7.2. Handling the Authorization Code Redirect 432 Authorization servers MUST require an exact match of a registered 433 redirect URI. As described in [oauth-security-topics] Section 4.1.1. 434 this helps to prevent attacks targeting the authorization code. 436 8. Refresh Tokens 438 Refresh tokens provide a way for applications to obtain a new access 439 token when the initial access token expires. With public clients, 440 the risk of a leaked refresh token is greater than leaked access 441 tokens, since an attacker may be able to continue using the stolen 442 refresh token to obtain new access tokens potentially without being 443 detectable by the authorization server. 445 Browser-based applications provide an attacker with several 446 opportunities by which a refresh token can be leaked, just as with 447 access tokens. As such, these applications are considered a higher 448 risk for handling refresh tokens. 450 Authorization servers may choose whether or not to issue refresh 451 tokens to browser-based applications. [oauth-security-topics] 452 describes some additional requirements around refresh tokens on top 453 of the recommendations of [RFC6749]. Applications and authorization 454 servers conforming to this BCP MUST also follow the recommendations 455 in [oauth-security-topics] around refresh tokens if refresh tokens 456 are issued to browser-based applications. 458 In particular, authorization servers: 460 o MUST either rotate refresh tokens on each use OR use sender- 461 constrained refresh tokens as described in [oauth-security-topics] 462 Section 4.13.2 464 o MUST either set a maximum lifetime on refresh tokens OR expire if 465 the refresh token has not been used within some amount of time 467 o MUST NOT extend the lifetime of the new refresh token beyond the 468 lifetime of the initial refresh token 470 o upon issuing a rotated refresh token, MUST NOT extend the lifetime 471 of the new refresh token beyond the lifetime of the initial 472 refresh token if the refresh token has a preestablished expiration 473 time 475 For example: 477 o A user authorizes an application, issuing an access token that 478 lasts 1 hour, and a refresh token that lasts 24 hours 480 o After 1 hour, the initial access token expires, so the application 481 uses the refresh token to get a new access token 483 o The authorization server returns a new access token that lasts 1 484 hour, and a new refresh token that lasts 23 hours 486 o This continues until 24 hours pass from the initial authorization 488 o At this point, when the application attempts to use the refresh 489 token after 24 hours, the request will fail and the application 490 will have to involve the user in a new authorization request 492 By limiting the overall refresh token lifetime to the lifetime of the 493 initial refresh token, this ensures a stolen refresh token cannot be 494 used indefinitely. 496 Authorization servers MAY set different policies around refresh token 497 issuance, lifetime and expiration for browser-based applications 498 compared to other public clients. 500 9. Security Considerations 502 9.1. Registration of Browser-Based Apps 504 Browser-based applications are considered public clients as defined 505 by Section 2.1 of OAuth 2.0 [RFC6749], and MUST be registered with 506 the authorization server as such. Authorization servers MUST record 507 the client type in the client registration details in order to 508 identify and process requests accordingly. 510 Authorization servers MUST require that browser-based applications 511 register one or more redirect URIs. 513 9.2. Client Authentication 515 Since a browser-based application's source code is delivered to the 516 end-user's browser, it cannot contain provisioned secrets. As such, 517 a browser-based app with native OAuth support is considered a public 518 client as defined by Section 2.1 of OAuth 2.0 [RFC6749]. 520 Secrets that are statically included as part of an app distributed to 521 multiple users should not be treated as confidential secrets, as one 522 user may inspect their copy and learn the shared secret. For this 523 reason, and those stated in Section 5.3.1 of [RFC6819], it is NOT 524 RECOMMENDED for authorization servers to require client 525 authentication of browser-based applications using a shared secret, 526 as this serves little value beyond client identification which is 527 already provided by the client_id request parameter. 529 Authorization servers that still require a statically included shared 530 secret for SPA clients MUST treat the client as a public client, and 531 not accept the secret as proof of the client's identity. Without 532 additional measures, such clients are subject to client impersonation 533 (see Section 9.3 below). 535 9.3. Client Impersonation 537 As stated in Section 10.2 of OAuth 2.0 [RFC6749], the authorization 538 server SHOULD NOT process authorization requests automatically 539 without user consent or interaction, except when the identity of the 540 client can be assured. 542 If authorization servers restrict redirect URIs to a fixed set of 543 absolute HTTPS URIs, preventing the use of wildcard domains, wildcard 544 paths, or wildcard query string components, this exact match of 545 registered absolute HTTPS URIs MAY be accepted by authorization 546 servers as proof of identity of the client for the purpose of 547 deciding whether to automatically process an authorization request 548 when a previous request for the client_id has already been approved. 550 9.4. Cross-Site Request Forgery Protections 552 Clients MUST prevent Cross-Site Request Forgery (CSRF) attacks 553 against their redirect URI. Clients can accomplish this by either 554 ensuring the authorization server supports PKCE and relying on the 555 CSRF protection that PKCE provides, or if the client is also an 556 OpenID Connect client, using the OpenID Connect "nonce" parameter, or 557 by using the "state" parameter to carry one-time-use CSRF tokens as 558 described in Section 7.1. 560 See Section 2.1 of [oauth-security-topics] for additional details. 562 9.5. Authorization Server Mix-Up Mitigation 564 Authorization server mix-up attacks mark a severe threat to every 565 client that supports at least two authorization servers. To conform 566 to this BCP such clients MUST apply countermeasures to defend against 567 mix-up attacks. 569 It is RECOMMENDED to defend against mix-up attacks by identifying and 570 validating the issuer of the authorization response. This can be 571 achieved either by using the "iss" response parameter, as defined in 572 [oauth-iss-auth-resp], or by using the "iss" Claim of the ID token 573 when OpenID Connect is used. 575 Alternative countermeasures, such as using distinct redirect URIs for 576 each issuer, SHOULD only be used if identifying the issuer as 577 described is not possible. 579 Section 4.4 of [oauth-security-topics] provides additional details 580 about mix-up attacks and the countermeasures mentioned above. 582 9.6. Cross-Domain Requests 584 To complete the Authorization Code flow, the browser-based 585 application will need to exchange the authorization code for an 586 access token at the token endpoint. If the authorization server 587 provides additional endpoints to the application, such as metadata 588 URLs, dynamic client registration, revocation, introspection, 589 discovery or user info endpoints, these endpoints may also be 590 accessed by the browser-based app. Since these requests will be made 591 from a browser, authorization servers MUST support the necessary CORS 592 headers (defined in [Fetch]) to allow the browser to make the 593 request. 595 This specification does not include guidelines for deciding whether a 596 CORS policy for the token endpoint should be a wildcard origin or 597 more restrictive. Note, however, that the browser will attempt to 598 GET or POST to the API endpoint before knowing any CORS policy; it 599 simply hides the succeeding or failing result from JavaScript if the 600 policy does not allow sharing. 602 9.7. Content Security Policy 604 A browser-based application that wishes to use either long-lived 605 refresh tokens or privileged scopes SHOULD restrict its JavaScript 606 execution to a set of statically hosted scripts via a Content 607 Security Policy ([CSP2]) or similar mechanism. A strong Content 608 Security Policy can limit the potential attack vectors for malicious 609 JavaScript to be executed on the page. 611 9.8. OAuth Implicit Flow 613 The OAuth 2.0 Implicit flow (defined in Section 4.2 of OAuth 2.0 614 [RFC6749]) works by the authorization server issuing an access token 615 in the authorization response (front channel) without the code 616 exchange step. In this case, the access token is returned in the 617 fragment part of the redirect URI, providing an attacker with several 618 opportunities to intercept and steal the access token. 620 Authorization servers MUST NOT issue access tokens in the 621 authorization response, and MUST issue access tokens only from the 622 token endpoint. 624 9.8.1. Attacks on the Implicit Flow 626 Many attacks on the Implicit flow described by [RFC6819] and 627 Section 4.1.2 of [oauth-security-topics] do not have sufficient 628 mitigation strategies. The following sections describe the specific 629 attacks that cannot be mitigated while continuing to use the Implicit 630 flow. 632 9.8.1.1. Threat: Manipulation of the Redirect URI 634 If an attacker is able to cause the authorization response to be sent 635 to a URI under their control, they will directly get access to the 636 authorization response including the access token. Several methods 637 of performing this attack are described in detail in 638 [oauth-security-topics]. 640 9.8.1.2. Threat: Access Token Leak in Browser History 642 An attacker could obtain the access token from the browser's history. 643 The countermeasures recommended by [RFC6819] are limited to using 644 short expiration times for tokens, and indicating that browsers 645 should not cache the response. Neither of these fully prevent this 646 attack, they only reduce the potential damage. 648 Additionally, many browsers now also sync browser history to cloud 649 services and to multiple devices, providing an even wider attack 650 surface to extract access tokens out of the URL. 652 This is discussed in more detail in Section 4.3.2 of 653 [oauth-security-topics]. 655 9.8.1.3. Threat: Manipulation of Scripts 657 An attacker could modify the page or inject scripts into the browser 658 through various means, including when the browser's HTTPS connection 659 is being intercepted by, for example, a corporate network. While 660 man-in-the-middle attacks are typically out of scope of basic 661 security recommendations to prevent, in the case of browser-based 662 apps they are much easier to perform. An injected script can enable 663 an attacker to have access to everything on the page. 665 The risk of a malicious script running on the page may be amplified 666 when the application uses a known standard way of obtaining access 667 tokens, namely that the attacker can always look at the 668 "window.location" variable to find an access token. This threat 669 profile is different from an attacker specifically targeting an 670 individual application by knowing where or how an access token 671 obtained via the Authorization Code flow may end up being stored. 673 9.8.1.4. Threat: Access Token Leak to Third-Party Scripts 675 It is relatively common to use third-party scripts in browser-based 676 apps, such as analytics tools, crash reporting, and even things like 677 a Facebook or Twitter "like" button. In these situations, the author 678 of the application may not be able to be fully aware of the entirety 679 of the code running in the application. When an access token is 680 returned in the fragment, it is visible to any third-party scripts on 681 the page. 683 9.8.2. Countermeasures 685 In addition to the countermeasures described by [RFC6819] and 686 [oauth-security-topics], using the Authorization Code flow with PKCE 687 extension prevents the attacks described above by avoiding returning 688 the access token in the redirect response at all. 690 When PKCE is used, if an authorization code is stolen in transport, 691 the attacker is unable to do anything with the authorization code. 693 9.8.3. Disadvantages of the Implicit Flow 695 There are several additional reasons the Implicit flow is 696 disadvantageous compared to using the standard Authorization Code 697 flow. 699 o OAuth 2.0 provides no mechanism for a client to verify that a 700 particular access token was intended for that client, which could 701 lead to misuse and possible impersonation attacks if a malicious 702 party hands off an access token it retrieved through some other 703 means to the client. 705 o Returning an access token in the front-channel redirect gives the 706 authorization server no assurance that the access token will 707 actually end up at the application, since there are many ways this 708 redirect may fail or be intercepted. 710 o Supporting the Implicit flow requires additional code, more upkeep 711 and understanding of the related security considerations, while 712 limiting the authorization server to just the Authorization Code 713 flow reduces the attack surface of the implementation. 715 o If the JavaScript application gets wrapped into a native app, then 716 [RFC8252] also requires the use of the Authorization Code flow 717 with PKCE anyway. 719 In OpenID Connect, the id_token is sent in a known format (as a JWT), 720 and digitally signed. Returning an id_token using the Implicit flow 721 ("response_type=id_token") requires the client validate the JWT 722 signature, as malicious parties could otherwise craft and supply 723 fraudulent id_tokens. Performing OpenID Connect using the 724 Authorization Code flow provides the benefit of the client not 725 needing to verify the JWT signature, as the ID token will have been 726 fetched over an HTTPS connection directly from the authorization 727 server. Additionally, in many cases an application will request both 728 an ID token and an access token, so it is simplier and provides fewer 729 attack vectors to obtain both via the Authorization Code flow. 731 9.8.4. Historic Note 733 Historically, the Implicit flow provided an advantage to browser- 734 based apps since JavaScript could always arbitrarily read and 735 manipulate the fragment portion of the URL without triggering a page 736 reload. This was necessary in order to remove the access token from 737 the URL after it was obtained by the app. 739 Modern browsers now have the Session History API (described in 740 "Session history and navigation" of [HTML]), which provides a 741 mechanism to modify the path and query string component of the URL 742 without triggering a page reload. This means modern browser-based 743 apps can use the unmodified OAuth 2.0 Authorization Code flow, since 744 they have the ability to remove the authorization code from the query 745 string without triggering a page reload thanks to the Session History 746 API. 748 9.9. Additional Security Considerations 750 The OWASP Foundation (https://www.owasp.org/) maintains a set of 751 security recommendations and best practices for web applications, and 752 it is RECOMMENDED to follow these best practices when creating an 753 OAuth 2.0 Browser-Based application. 755 10. IANA Considerations 757 This document does not require any IANA actions. 759 11. References 761 11.1. Normative References 763 [CSP2] West, M., "Content Security Policy", October 2018. 765 [Fetch] whatwg, "Fetch", 2018. 767 [oauth-iss-auth-resp] 768 Meyer zu Selhausen, K. and D. Fett, "OAuth 2.0 769 Authorization Server Issuer Identifier in Authorization 770 Response", January 2021. 772 [oauth-security-topics] 773 Lodderstedt, T., Bradley, J., Labunets, A., and D. Fett, 774 "OAuth 2.0 Security Best Current Practice", April 2021. 776 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 777 Requirement Levels", BCP 14, RFC 2119, 778 DOI 10.17487/RFC2119, March 1997, 779 . 781 [RFC6749] Hardt, D., Ed., "The OAuth 2.0 Authorization Framework", 782 RFC 6749, DOI 10.17487/RFC6749, October 2012, 783 . 785 [RFC6750] Jones, M. and D. Hardt, "The OAuth 2.0 Authorization 786 Framework: Bearer Token Usage", RFC 6750, 787 DOI 10.17487/RFC6750, October 2012, 788 . 790 [RFC6819] Lodderstedt, T., Ed., McGloin, M., and P. Hunt, "OAuth 2.0 791 Threat Model and Security Considerations", RFC 6819, 792 DOI 10.17487/RFC6819, January 2013, 793 . 795 [RFC7636] Sakimura, N., Ed., Bradley, J., and N. Agarwal, "Proof Key 796 for Code Exchange by OAuth Public Clients", RFC 7636, 797 DOI 10.17487/RFC7636, September 2015, 798 . 800 [RFC8252] Denniss, W. and J. Bradley, "OAuth 2.0 for Native Apps", 801 BCP 212, RFC 8252, DOI 10.17487/RFC8252, October 2017, 802 . 804 11.2. Informative References 806 [HTML] whatwg, "HTML", 2020. 808 Appendix A. Server Support Checklist 810 OAuth authorization servers that support browser-based apps MUST: 812 1. Require "https" scheme redirect URIs. 814 2. Require exact matching of registered redirect URIs. 816 3. Support PKCE [RFC7636]. Required to protect authorization code 817 grants sent to public clients. See Section 7.1 819 4. Support cross-domain requests at the token endpoint in order to 820 allow browsers to make the authorization code exchange request. 821 See Section 9.6 823 5. Not assume that browser-based clients can keep a secret, and 824 SHOULD NOT issue secrets to applications of this type. 826 6. Not support the Resource Owner Password grant for browser-based 827 clients. 829 7. Follow the [oauth-security-topics] recommendations on refresh 830 tokens, as well as the additional requirements described in 831 Section 8. 833 Appendix B. Document History 835 [[ To be removed from the final specification ]] 837 -08 839 o Added a note to use the "Secure" cookie attribute in addition to 840 SameSite etc 842 o Updates to bring this draft in sync with the latest Security BCP 844 o Updated text for mix-up countermeasures to reference the new "iss" 845 extension 847 o Changed "SHOULD" for refresh token rotation to MUST either use 848 rotation or sender-constraining to match the Security BCP 850 o Fixed references to other specs and extensions 851 o Editorial improvements in descriptions of the different 852 architectures 854 -07 856 o Clarify PKCE requirements apply only to issuing access tokens 858 o Change "MUST" to "SHOULD" for refresh token rotation 860 o Editorial clarifications 862 -06 864 o Added refresh token requirements to AS summary 866 o Editorial clarifications 868 -05 870 o Incorporated editorial and substantive feedback from Mike Jones 872 o Added references to "nonce" as another way to prevent CSRF attacks 874 o Updated headers in the Implicit Flow section to better represent 875 the relationship between the paragraphs 877 -04 879 o Disallow the use of the Password Grant 881 o Add PKCE support to summary list for authorization server 882 requirements 884 o Rewrote refresh token section to allow refresh tokens if they are 885 time-limited, rotated on each use, and requiring that the rotated 886 refresh token lifetimes do not extend past the lifetime of the 887 initial refresh token, and to bring it in line with the Security 888 BCP 890 o Updated recommendations on using state to reflect the Security BCP 892 o Updated server support checklist to reflect latest changes 894 o Updated the same-domain JS architecture section to emphasize the 895 architecture rather than domain 897 o Editorial clarifications in the section that talks about OpenID 898 Connect ID tokens 900 -03 902 o Updated the historic note about the fragment URL clarifying that 903 the Session History API means browsers can use the unmodified 904 authorization code flow 906 o Rephrased "Authorization Code Flow" intro paragraph to better lead 907 into the next two sections 909 o Softened "is likely a better decision to avoid using OAuth 910 entirely" to "it may be..." for common-domain deployments 912 o Updated abstract to not be limited to public clients, since the 913 later sections talk about confidential clients 915 o Removed references to avoiding OpenID Connect for same-domain 916 architectures 918 o Updated headers to better describe architectures (Apps Served from 919 a Static Web Server -> JavaScript Applications without a Backend) 921 o Expanded "same-domain architecture" section to better explain the 922 problems that OAuth has in this scenario 924 o Referenced Security BCP in implicit flow attacks where possible 926 o Minor typo corrections 928 -02 930 o Rewrote overview section incorporating feedback from Leo Tohill 932 o Updated summary recommendation bullet points to split out 933 application and server requirements 935 o Removed the allowance on hostname-only redirect URI matching, now 936 requiring exact redirect URI matching 938 o Updated Section 6.2 to drop reference of SPA with a backend 939 component being a public client 941 o Expanded the architecture section to explicitly mention three 942 architectural patterns available to JS apps 944 -01 946 o Incorporated feedback from Torsten Lodderstedt 947 o Updated abstract 949 o Clarified the definition of browser-based apps to not exclude 950 applications cached in the browser, e.g. via Service Workers 952 o Clarified use of the state parameter for CSRF protection 954 o Added background information about the original reason the 955 implicit flow was created due to lack of CORS support 957 o Clarified the same-domain use case where the SPA and API share a 958 cookie domain 960 o Moved historic note about the fragment URL into the Overview 962 Appendix C. Acknowledgements 964 The authors would like to acknowledge the work of William Denniss and 965 John Bradley, whose recommendation for native apps informed many of 966 the best practices for browser-based applications. The authors would 967 also like to thank Hannes Tschofenig and Torsten Lodderstedt, the 968 attendees of the Internet Identity Workshop 27 session at which this 969 BCP was originally proposed, and the following individuals who 970 contributed ideas, feedback, and wording that shaped and formed the 971 final specification: 973 Annabelle Backman, Brian Campbell, Brock Allen, Christian Mainka, 974 Daniel Fett, George Fletcher, Hannes Tschofenig, Janak Amarasena, 975 John Bradley, Joseph Heenan, Justin Richer, Karl McGuinness, Karsten 976 Meyer zu Selhausen, Leo Tohill, Mike Jones, Tomek Stojecki, Torsten 977 Lodderstedt, and Vittorio Bertocci. 979 Authors' Addresses 981 Aaron Parecki 982 Okta 984 Email: aaron@parecki.com 985 URI: https://aaronparecki.com 987 David Waite 988 Ping Identity 990 Email: david@alkaline-solutions.com