idnits 2.17.1 draft-ietf-oauth-browser-based-apps-05.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 (February 28, 2020) is 1511 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: August 31, 2020 Ping Identity 6 February 28, 2020 8 OAuth 2.0 for Browser-Based Apps 9 draft-ietf-oauth-browser-based-apps-05 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 August 31, 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 . . . . . . . . . . . 16 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 5. First-Party Applications 183 While OAuth was initially created to allow third-party applications 184 to access an API on behalf of a user, it has proven to be useful in a 185 first-party scenario as well. First-party apps are applications 186 where the same organization provides both the API and the 187 application. 189 Examples of first-party applications are a web email client provided 190 by the operator of the email account, or a mobile banking application 191 created by bank itself. (Note that there is no requirement that the 192 application actually be developed by the same company; a mobile 193 banking application developed by a contractor that is branded as the 194 bank's application is still considered a first-party application.) 195 The first-party app consideration is about the user's relationship to 196 the application and the service. 198 To conform to this best practice, first-party applications using 199 OAuth or OpenID Connect MUST use a redirect-based flow (such as the 200 OAuth Authorization Code flow) as described later in this document. 202 The Resource Owner Password Grant MUST NOT be used, as described in 203 [oauth-security-topics] section 3.4. Instead, by using the 204 Authorization Code flow and redirecting the user to the authorization 205 server, this provides the authorization server the opportunity to 206 prompt the user for multi-factor authentication options, take 207 advantage of single-sign-on sessions, or use third-party identity 208 providers. In contrast, the Password grant does not provide any 209 built-in mechanism for these, and would instead be extended with 210 custom code. 212 6. Application Architecture Patterns 214 There are three primary architectural patterns available when 215 building browser-based applications. 217 o a JavaScript application that has methods of sharing data with 218 resource servers, such as using common-domain cookies 220 o a JavaScript application with a backend 222 o a JavaScript application with no backend, accessing resource 223 servers directly 225 These three architectures have different use cases and 226 considerations. 228 6.1. Browser-Based Apps that Can Share Data with the Resource Server 230 For simple system architectures, such as when the JavaScript 231 application is served from a domain that can share cookies with the 232 domain of the API (resource server), OAuth adds additional attack 233 vectors that could be avoided with a different solution. 235 In particular, using any redirect-based mechanism of obtaining an 236 access token enables the redirect-based attacks described in 237 [oauth-security-topics], but if the application, authorization server 238 and resource server share a domain, then it is unnecessary to use a 239 redirect mechanism to communicate between them. 241 An additional concern with handling access tokens in a browser is 242 that as of the date of this publication, there is no secure storage 243 mechanism where JavaScript code can keep the access token to be later 244 used in an API request. Using an OAuth flow results in the 245 JavaScript code getting an access token, needing to store it 246 somewhere, and then retrieve it to make an API request. 248 Instead, a more secure design is to use an HTTP-only cookie between 249 the JavaScript application and API so that the JavaScript code can't 250 access the cookie value itself. Additionally, the SameSite cookie 251 attribute can be used to prevent CSRF attacks, or alternatively, the 252 application and API could be written to use anti-CSRF tokens. 254 OAuth was originally created for third-party or federated access to 255 APIs, so it may not be the best solution in a common-domain 256 deployment. That said, using OAuth even in a common-domain 257 architecture does mean you can more easily rearchitect things later, 258 such as if you were to later add a new domain to the system. 260 6.2. JavaScript Applications with a Backend 261 +-------------+ 262 | | 263 |Authorization| 264 | Server | 265 | | 266 +-------------+ 268 ^ + 269 |(A) |(B) 270 | | 271 + v 273 +-------------+ +--------------+ 274 | | +---------> | | 275 | Application | (C) | Resource | 276 | Server | | Server | 277 | | <---------+ | | 278 +-------------+ (D) +--------------+ 280 ^ + 281 | | 282 | | browser 283 | | cookie 284 | | 285 + v 287 +-------------+ 288 | | 289 | Browser | 290 | | 291 +-------------+ 293 In this architecture, the JavaScript code is loaded from a dynamic 294 Application Server that also has the ability to execute code itself. 295 This enables the ability to keep all of the steps involved in 296 obtaining an access token outside of the JavaScript application. 298 In this case, the Application Server performs the OAuth flow itself, 299 and keeps the access token and refresh token stored internally, 300 creating a separate session with the browser-based app via a 301 traditional browser cookie. 303 (Common examples of this architecture are an Angular front-end with a 304 .NET backend, or a React front-end with a Spring Boot backend.) 306 The Application Server SHOULD be considered a confidential client, 307 and issued its own client secret. The Application Server SHOULD use 308 the OAuth 2.0 authorization code grant to initiate a request for an 309 access token. Upon handling the redirect from the Authorization 310 Server, the Application Server will request an access token using the 311 authorization code returned (A), which will be returned to the 312 Application Server (B). The Application Server stores this access 313 token itself and establishes its own cookie-based session with the 314 Browser application. The Application Server can store the access 315 token either server-side, or in the cookie itself. 317 When the JavaScript application in the browser wants to make a 318 request to the Resource Server, it MUST instead make the request to 319 the Application Server, and the Application Server will make the 320 request with the access token to the Resource Server (C), and forward 321 the response (D) back to the browser. 323 Security of the connection between code running in the browser and 324 this Application Server is assumed to utilize browser-level 325 protection mechanisms. Details are out of scope of this document, 326 but many recommendations can be found in the OWASP Cheat Sheet series 327 (https://cheatsheetseries.owasp.org/), such as setting an HTTP-only 328 and Secure cookie to authenticate the session between the browser and 329 Application Server. 331 In this scenario, the session between the browser and Application 332 Server MAY be either a session cookie provided by the Application 333 Server, OR the access token itself. Note that if the access token is 334 used as the session identifier, this exposes the access token to the 335 end user even if it is not available to the JavaScript application, 336 so some authorization servers may wish to limit the capabilities of 337 these clients to mitigate risk. 339 6.3. JavaScript Applications without a Backend 340 +---------------+ +--------------+ 341 | | | | 342 | Authorization | | Resource | 343 | Server | | Server | 344 | | | | 345 +---------------+ +--------------+ 347 ^ + ^ + 348 | | | | 349 |(B) |(C) |(D) |(E) 350 | | | | 351 | | | | 352 + v + v 354 +-----------------+ +-------------------------------+ 355 | | (A) | | 356 | Static Web Host | +-----> | Browser | 357 | | | | 358 +-----------------+ +-------------------------------+ 360 In this architecture, the JavaScript code is first loaded from a 361 static web host into the browser (A), and the application then runs 362 in the browser. This application is considered a public client, 363 since there is no way to issue it a client secret and there is no 364 other secure client authentication mechanism available in the 365 browser. 367 The code in the browser initiates the authorization code flow with 368 the PKCE extension (described in Section 7) (B) above, and obtains an 369 access token via a POST request (C). The JavaScript app is then 370 responsible for storing the access token (and optional refresh token) 371 securely using appropriate browser APIs. 373 When the JavaScript application in the browser wants to make a 374 request to the Resource Server, it can include the access token in 375 the request (D) and make the request directly. 377 In this scenario, the Authorization Server and Resource Server MUST 378 support the necessary CORS headers to enable the JavaScript code to 379 make this POST request from the domain on which the script is 380 executing. (See Section 9.6 for additional details.) 382 7. Authorization Code Flow 384 Public browser-based apps that use the authorization code grant type 385 described in Section 4.1 of OAuth 2.0 [RFC6749] MUST also follow 386 these additional requirements described in this section. 388 7.1. Initiating the Authorization Request from a Browser-Based 389 Application 391 Public browser-based apps MUST implement the Proof Key for Code 392 Exchange (PKCE [RFC7636]) extension to OAuth, and authorization 393 servers MUST support PKCE for such clients. 395 The PKCE extension prevents an attack where the authorization code is 396 intercepted and exchanged for an access token by a malicious client, 397 by providing the authorization server with a way to verify the same 398 client instance that exchanges the authorization code is the same one 399 that initiated the flow. 401 Browser-based apps MUST prevent CSRF attacks against their redirect 402 URI. This can be accomplished by any of the below: 404 o using PKCE, and confirming that the authorization server supports 405 PKCE 407 o if the application is using OpenID Connect, by using the OpenID 408 Connect "nonce" parameter 410 o using a unique value for the OAuth 2.0 "state" parameter 412 Browser-based apps MUST follow the recommendations in 413 [oauth-security-topics] Section 2.1 to protect themselves during 414 redirect flows. 416 7.2. Handling the Authorization Code Redirect 418 Authorization servers MUST require an exact match of a registered 419 redirect URI. 421 8. Refresh Tokens 423 Refresh tokens provide a way for applications to obtain a new access 424 token when the initial access token expires. With public clients, 425 the risk of a leaked refresh token is greater than leaked access 426 tokens, since an attacker may be able to continue using the stolen 427 refresh token to obtain new access tokens potentially without being 428 detectable by the authorization server. 430 Browser-based applications provide an attacker with several 431 opportunities by which a refresh token can be leaked, just as with 432 access tokens. As such, these applications are considered a higher 433 risk for handling refresh tokens. 435 Authorization servers may choose whether or not to issue refresh 436 tokens to browser-based applications. [oauth-security-topics] 437 describes some additional requirements around refresh tokens on top 438 of the recommendations of [RFC6749]. Applications and authorization 439 servers conforming to this BCP MUST also follow the recommendations 440 in [oauth-security-topics] around refresh tokens if refresh tokens 441 are issued to browser-based apps. 443 In particular, authorization servers: 445 o MUST rotate refresh tokens on each use, in order to be able to 446 detect a stolen refresh token if one is replayed (described in 447 [oauth-security-topics] section 4.12) 449 o MUST either set a maximum lifetime on refresh tokens OR expire if 450 the refresh token has not been used within some amount of time 452 o upon issuing a rotated refresh token, MUST NOT extend the lifetime 453 of the new refresh token beyond the lifetime of the initial 454 refresh token if the refresh token has a preestablished expiration 455 time 457 For example: 459 o A user authorizes an application, issuing an access token that 460 lasts 1 hour, and a refresh token that lasts 24 hours 462 o After 1 hour, the initial access token expires, so the application 463 uses the refresh token to get a new access token 465 o The authorization server returns a new access token that lasts 1 466 hour, and a new refresh token that lasts 23 hours 468 o This continues until 24 hours pass from the initial authorization 470 o At this point, when the application attempts to use the refresh 471 token after 24 hours, the request will fail and the application 472 will have to involve the user in a new authorization request 474 By limiting the overall refresh token lifetime to the lifetime of the 475 initial refresh token, this ensures a stolen refresh token cannot be 476 used indefinitely. 478 9. Security Considerations 479 9.1. Registration of Browser-Based Apps 481 Browser-based applications are considered public clients as defined 482 by section 2.1 of OAuth 2.0 [RFC6749], and MUST be registered with 483 the authorization server as such. Authorization servers MUST record 484 the client type in the client registration details in order to 485 identify and process requests accordingly. 487 Authorization servers MUST require that browser-based applications 488 register one or more redirect URIs. 490 9.2. Client Authentication 492 Since a browser-based application's source code is delivered to the 493 end-user's browser, it cannot contain provisioned secrets. As such, 494 a browser-based app with native OAuth support is considered a public 495 client as defined by Section 2.1 of OAuth 2.0 [RFC6749]. 497 Secrets that are statically included as part of an app distributed to 498 multiple users should not be treated as confidential secrets, as one 499 user may inspect their copy and learn the shared secret. For this 500 reason, and those stated in Section 5.3.1 of [RFC6819], it is NOT 501 RECOMMENDED for authorization servers to require client 502 authentication of browser-based applications using a shared secret, 503 as this serves little value beyond client identification which is 504 already provided by the client_id request parameter. 506 Authorization servers that still require a statically included shared 507 secret for SPA clients MUST treat the client as a public client, and 508 not accept the secret as proof of the client's identity. Without 509 additional measures, such clients are subject to client impersonation 510 (see Section 9.3 below). 512 9.3. Client Impersonation 514 As stated in Section 10.2 of OAuth 2.0 [RFC6749], the authorization 515 server SHOULD NOT process authorization requests automatically 516 without user consent or interaction, except when the identity of the 517 client can be assured. 519 If authorization servers restrict redirect URIs to a fixed set of 520 absolute HTTPS URIs, preventing the use of wildcard domains, wildcard 521 paths, or wildcard query string components, this exact match of 522 registered absolute HTTPS URIs MAY be accepted by authorization 523 servers as proof of identity of the client for the purpose of 524 deciding whether to automatically process an authorization request 525 when a previous request for the client_id has already been approved. 527 9.4. Cross-Site Request Forgery Protections 529 Clients MUST prevent Cross-Site Request Forgery (CSRF) attacks 530 against their redirect URI. Clients can accomplish this by either 531 ensuring the authorization server supports PKCE and relying on the 532 CSRF protection that PKCE provides, or if the client is also an 533 OpenID Connect client, using the OpenID Connect "nonce" parameter, or 534 by using the "state" parameter to carry one-time-use CSRF tokens as 535 described in Section 7.1. 537 See Section 2.1 of [oauth-security-topics] for additional details. 539 9.5. Authorization Server Mix-Up Mitigation 541 The security considerations around the authorization server mix-up 542 that are referenced in Section 8.10 of [RFC8252] also apply to 543 browser-based apps. 545 Clients MUST use a unique redirect URI for each authorization server 546 used by the application. The client MUST store the redirect URI 547 along with the session data (e.g. along with "state") and MUST verify 548 that the URI on which the authorization response was received exactly 549 matches. 551 9.6. Cross-Domain Requests 553 To complete the authorization code flow, the browser-based 554 application will need to exchange the authorization code for an 555 access token at the token endpoint. If the authorization server 556 provides additional endpoints to the application, such as metadata 557 URLs, dynamic client registration, revocation, introspection, 558 discovery or user info endpoints, these endpoints may also be 559 accessed by the browser-based app. Since these requests will be made 560 from a browser, authorization servers MUST support the necessary CORS 561 headers (defined in [Fetch]) to allow the browser to make the 562 request. 564 This specification does not include guidelines for deciding whether a 565 CORS policy for the token endpoint should be a wildcard origin or 566 more restrictive. Note, however, that the browser will attempt to 567 GET or POST to the API endpoint before knowing any CORS policy; it 568 simply hides the succeeding or failing result from JavaScript if the 569 policy does not allow sharing. 571 9.7. Content-Security Policy 573 A browser-based application that wishes to use either long-lived 574 refresh tokens or privileged scopes SHOULD restrict its JavaScript 575 execution to a set of statically hosted scripts via a Content 576 Security Policy ([CSP2]) or similar mechanism. A strong Content 577 Security Policy can limit the potential attack vectors for malicious 578 JavaScript to be executed on the page. 580 9.8. OAuth Implicit Flow 582 The OAuth 2.0 Implicit flow (defined in Section 4.2 of OAuth 2.0 583 [RFC6749]) works by receiving an access token in the HTTP redirect 584 (front-channel) immediately without the code exchange step. In this 585 case, the access token is returned in the fragment part of the 586 redirect URI, providing an attacker with several opportunities to 587 intercept and steal the access token. 589 9.8.1. Attacks on the Implicit Flow 591 Many attacks on the implicit flow described by [RFC6819] and 592 [oauth-security-topics] do not have sufficient mitigation strategies. 593 The following sections describe the specific attacks that cannot be 594 mitigated while continuing to use the implicit flow. 596 9.8.1.1. Threat: Interception of the Redirect URI 598 If an attacker is able to cause the authorization response to be sent 599 to a URI under their control, they will directly get access to the 600 fragment carrying the access token. Several methods of performing 601 this attack are described in detail in [oauth-security-topics]. 603 9.8.1.2. Threat: Access Token Leak in Browser History 605 An attacker could obtain the access token from the browser's history. 606 The countermeasures recommended by [RFC6819] are limited to using 607 short expiration times for tokens, and indicating that browsers 608 should not cache the response. Neither of these fully prevent this 609 attack, they only reduce the potential damage. 611 Additionally, many browsers now also sync browser history to cloud 612 services and to multiple devices, providing an even wider attack 613 surface to extract access tokens out of the URL. 615 This is discussed in more detail in Section 4.3.2 of 616 [oauth-security-topics]. 618 9.8.1.3. Threat: Manipulation of Scripts 620 An attacker could modify the page or inject scripts into the browser 621 through various means, including when the browser's HTTPS connection 622 is being man-in-the-middled by, for example, a corporate network. 623 While this type of attack is typically out of scope of basic security 624 recommendations to prevent, in the case of browser-based apps it is 625 much easier to perform this kind of attack, where an injected script 626 can suddenly have access to everything on the page. 628 The risk of a malicious script running on the page may be amplified 629 when the application uses a known standard way of obtaining access 630 tokens, namely that the attacker can always look at the 631 "window.location" variable to find an access token. This threat 632 profile is different from an attacker specifically targeting an 633 individual application by knowing where or how an access token 634 obtained via the authorization code flow may end up being stored. 636 9.8.1.4. Threat: Access Token Leak to Third Party Scripts 638 It is relatively common to use third-party scripts in browser-based 639 apps, such as analytics tools, crash reporting, and even things like 640 a Facebook or Twitter "like" button. In these situations, the author 641 of the application may not be able to be fully aware of the entirety 642 of the code running in the application. When an access token is 643 returned in the fragment, it is visible to any third-party scripts on 644 the page. 646 9.8.2. Countermeasures 648 In addition to the countermeasures described by [RFC6819] and 649 [oauth-security-topics], using the authorization code with PKCE 650 extension prevents the attacks described above by avoiding returning 651 the access token in the redirect URI at all. 653 When PKCE is used, if an authorization code is stolen in transport, 654 the attacker is unable to do anything with the authorization code. 656 9.8.3. Disadvantages of the Implicit Flow 658 There are several additional reasons the Implicit flow is 659 disadvantageous compared to using the standard Authorization Code 660 flow. 662 o OAuth 2.0 provides no mechanism for a client to verify that an 663 access token was issued to it, which could lead to misuse and 664 possible impersonation attacks if a malicious party hands off an 665 access token it retrieved through some other means to the client. 667 o Returning an access token in the front channel redirect gives the 668 authorization server no assurance that the access token will 669 actually end up at the application, since there are many ways this 670 redirect may fail or be intercepted. 672 o Supporting the implicit flow requires additional code, more upkeep 673 and understanding of the related security considerations, while 674 limiting the authorization server to just the authorization code 675 flow reduces the attack surface of the implementation. 677 o If the JavaScript application gets wrapped into a native app, then 678 [RFC8252] also requires the use of the authorization code flow 679 with PKCE anyway. 681 In OpenID Connect, the id_token is sent in a known format (as a JWT), 682 and digitally signed. Returning an id_token using the Implicit flow 683 (response_type=id_token) requires the client validate the JWT 684 signature, as malicious parties could otherwise craft and supply 685 fraudulent id_tokens. Performing OpenID Connect using the 686 authorization code flow provides the benefit of the client not 687 needing to verify the JWT signature, as the ID token will have been 688 fetched over an HTTPS connection directly from the authorization 689 server. Additionally, in many cases an application will request both 690 an ID token and an access token, so it is simplier and provides fewer 691 attack vectors to obtain both via the authorization code flow. 693 9.8.4. Historic Note 695 Historically, the Implicit flow provided an advantage to single-page 696 apps since JavaScript could always arbitrarily read and manipulate 697 the fragment portion of the URL without triggering a page reload. 698 This was necessary in order to remove the access token from the URL 699 after it was obtained by the app. 701 Modern browsers now have the Session History API (described in 702 "Session history and navigation" of [HTML]), which provides a 703 mechanism to modify the path and query string component of the URL 704 without triggering a page reload. This means modern browser-based 705 apps can use the unmodified OAuth 2.0 authorization code flow, since 706 they have the ability to remove the authorization code from the query 707 string without triggering a page reload thanks to the Session History 708 API. 710 9.9. Additional Security Considerations 712 The OWASP Foundation (https://www.owasp.org/) maintains a set of 713 security recommendations and best practices for web applications, and 714 it is RECOMMENDED to follow these best practices when creating an 715 OAuth 2.0 Browser-Based application. 717 10. IANA Considerations 719 This document does not require any IANA actions. 721 11. References 723 11.1. Normative References 725 [CSP2] West, M., "Content Security Policy", October 2018. 727 [Fetch] whatwg, "Fetch", 2018. 729 [oauth-security-topics] 730 Lodderstedt, T., Bradley, J., Labunets, A., and D. Fett, 731 "OAuth 2.0 Security Best Current Practice", July 2019. 733 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 734 Requirement Levels", BCP 14, RFC 2119, 735 DOI 10.17487/RFC2119, March 1997, 736 . 738 [RFC6749] Hardt, D., Ed., "The OAuth 2.0 Authorization Framework", 739 RFC 6749, DOI 10.17487/RFC6749, October 2012, 740 . 742 [RFC6750] Jones, M. and D. Hardt, "The OAuth 2.0 Authorization 743 Framework: Bearer Token Usage", RFC 6750, 744 DOI 10.17487/RFC6750, October 2012, 745 . 747 [RFC6819] Lodderstedt, T., Ed., McGloin, M., and P. Hunt, "OAuth 2.0 748 Threat Model and Security Considerations", RFC 6819, 749 DOI 10.17487/RFC6819, January 2013, 750 . 752 [RFC7636] Sakimura, N., Ed., Bradley, J., and N. Agarwal, "Proof Key 753 for Code Exchange by OAuth Public Clients", RFC 7636, 754 DOI 10.17487/RFC7636, September 2015, 755 . 757 [RFC8252] Denniss, W. and J. Bradley, "OAuth 2.0 for Native Apps", 758 BCP 212, RFC 8252, DOI 10.17487/RFC8252, October 2017, 759 . 761 11.2. Informative References 763 [HTML] whatwg, "HTML", 2020. 765 Appendix A. Server Support Checklist 767 OAuth authorization servers that support browser-based apps MUST: 769 1. Require "https" scheme redirect URIs. 771 2. Require exact matching of registered redirect URIs. 773 3. Support PKCE [RFC7636]. Required to protect authorization code 774 grants sent to public clients. See Section 7.1 776 4. Support cross-domain requests at the token endpoint in order to 777 allow browsers to make the authorization code exchange request. 778 See Section 9.6 780 5. Not assume that browser-based clients can keep a secret, and 781 SHOULD NOT issue secrets to applications of this type. 783 6. Not support the Resource Owner Password grant for browser-based 784 clients. 786 7. Follow the [oauth-security-topics] recommendations on refresh 787 tokens, as well as the additional requirements described in 788 Section 8. 790 Appendix B. Document History 792 [[ To be removed from the final specification ]] 794 -05 796 o Incorporated editorial and substantive feedback from Mike Jones 798 o Added references to "nonce" as another way to prevent CSRF attacks 800 o Updated headers in the Implicit Flow section to better represent 801 the relationship between the paragraphs 803 -04 805 o Disallow the use of the Password Grant 807 o Add PKCE support to summary list for authorization server 808 requirements 810 o Rewrote refresh token section to allow refresh tokens if they are 811 time-limited, rotated on each use, and requiring that the rotated 812 refresh token lifetimes do not extend past the lifetime of the 813 initial refresh token, and to bring it in line with the Security 814 BCP 816 o Updated recommendations on using state to reflect the Security BCP 818 o Updated server support checklist to reflect latest changes 820 o Updated the same-domain JS architecture section to emphasize the 821 architecture rather than domain 823 o Editorial clarifications in the section that talks about OpenID 824 Connect ID tokens 826 -03 828 o Updated the historic note about the fragment URL clarifying that 829 the Session History API means browsers can use the unmodified 830 authorization code flow 832 o Rephrased "Authorization Code Flow" intro paragraph to better lead 833 into the next two sections 835 o Softened "is likely a better decision to avoid using OAuth 836 entirely" to "it may be..." for common-domain deployments 838 o Updated abstract to not be limited to public clients, since the 839 later sections talk about confidential clients 841 o Removed references to avoiding OpenID Connect for same-domain 842 architectures 844 o Updated headers to better describe architectures (Apps Served from 845 a Static Web Server -> JavaScript Applications without a Backend) 847 o Expanded "same-domain architecture" section to better explain the 848 problems that OAuth has in this scenario 850 o Referenced Security BCP in implicit flow attacks where possible 852 o Minor typo corrections 854 -02 856 o Rewrote overview section incorporating feedback from Leo Tohill 857 o Updated summary recommendation bullet points to split out 858 application and server requirements 860 o Removed the allowance on hostname-only redirect URI matching, now 861 requiring exact redirect URI matching 863 o Updated section 6.2 to drop reference of SPA with a backend 864 component being a public client 866 o Expanded the architecture section to explicitly mention three 867 architectural patterns available to JS apps 869 -01 871 o Incorporated feedback from Torsten Lodderstedt 873 o Updated abstract 875 o Clarified the definition of browser-based apps to not exclude 876 applications cached in the browser, e.g. via Service Workers 878 o Clarified use of the state parameter for CSRF protection 880 o Added background information about the original reason the 881 implicit flow was created due to lack of CORS support 883 o Clarified the same-domain use case where the SPA and API share a 884 cookie domain 886 o Moved historic note about the fragment URL into the Overview 888 Appendix C. Acknowledgements 890 The authors would like to acknowledge the work of William Denniss and 891 John Bradley, whose recommendation for native apps informed many of 892 the best practices for browser-based applications. The authors would 893 also like to thank Hannes Tschofenig and Torsten Lodderstedt, the 894 attendees of the Internet Identity Workshop 27 session at which this 895 BCP was originally proposed, and the following individuals who 896 contributed ideas, feedback, and wording that shaped and formed the 897 final specification: 899 Annabelle Backman, Brian Campbell, Brock Allen, Christian Mainka, 900 Daniel Fett, George Fletcher, Hannes Tschofenig, Janak Amarasena, 901 John Bradley, Joseph Heenan, Justin Richer, Karl McGuinness, Leo 902 Tohill, Mike Jones, Tomek Stojecki, Torsten Lodderstedt, and Vittorio 903 Bertocci. 905 Authors' Addresses 907 Aaron Parecki 908 Okta 910 Email: aaron@parecki.com 911 URI: https://aaronparecki.com 913 David Waite 914 Ping Identity 916 Email: david@alkaline-solutions.com