<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="rfc2629.xslt" ?>
<!-- generated by https://github.com/cabo/kramdown-rfc version 1.7.19 (Ruby 2.6.4) -->
<?rfc-ext html-pretty-print="prettyprint https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"?>
<rfc xmlns:x="http://purl.org/net/xml2rfc/ext"
      category="std"
      consensus="true"
      docName="draft-ietf-httpbis-optimistic-upgrade-00"
      ipr="trust200902"
      sortRefs="true"
      submissionType="IETF"
      symRefs="true"
      tocInclude="true"
      updates="9298">
   <x:feedback template="mailto:ietf-http-wg@w3.org?subject={docname},%20%22{section}%22\&amp;amp;body=%3c{ref}%3e:"/>
   <front>
      <title abbrev="Optimistic HTTP Upgrade Security">Security Considerations for Optimistic Use of HTTP Upgrade</title>
      <author fullname="Benjamin M. Schwartz">
         <organization>Meta Platforms, Inc.</organization>
         <address>
            <email>ietf@bemasc.net</email>
         </address>
      </author>
      <date day="28" month="August" year="2024"/>
      <area>Web and Internet Transport</area>
      <workgroup>HTTPBIS</workgroup>
      <abstract><?line 30?>
         <t>The HTTP/1.1 Upgrade mechanism allows the client to request a change to a new protocol. This document discusses the security considerations that apply to data sent by the client before this request is confirmed, and updates RFC 9298 to avoid related security issues.</t>
      </abstract>
      <note removeInRFC="true" title="About This Document">
         <t>Status information for this document may be found at <eref target="https://datatracker.ietf.org/doc/draft-ietf-httpbis-optimistic-upgrade/"/>.</t>
         <t>Source for this draft and an issue tracker can be found at <eref target="https://github.com/httpwg/http-extensions"/>.</t>
      </note>
   </front>
   <middle><?line 35?>
      <section anchor="conventions-and-definitions">
         <name>Conventions and Definitions</name>
         <t>The key words "<bcp14>MUST</bcp14>", "<bcp14>MUST NOT</bcp14>", "<bcp14>REQUIRED</bcp14>", "<bcp14>SHALL</bcp14>", "<bcp14>SHALL NOT</bcp14>", "<bcp14>SHOULD</bcp14>", "<bcp14>SHOULD NOT</bcp14>", "<bcp14>RECOMMENDED</bcp14>", "<bcp14>NOT RECOMMENDED</bcp14>", "<bcp14>MAY</bcp14>", and "<bcp14>OPTIONAL</bcp14>" in this document are to be interpreted as described in BCP 14 <xref target="RFC2119"/>
            <xref target="RFC8174"/> when, and only when, they appear in all capitals, as shown here.</t>
         <?line -18?>
      </section>
      <section anchor="background">
         <name>Background</name>
         <t>In HTTP/1.1, a client is permitted to send an "Upgrade" request header field (<xref section="7.8" sectionFormat="comma" target="RFC9110"/>) to indicate that it would like to use this connection for a protocol other than HTTP/1.1. The server replies with a "101 (Switching Protocols)" status code if it accepts the protocol change. However, that specification also permits the server to reject the upgrade request:</t>
         <ul empty="true">
            <li>
               <t>A server <bcp14>MAY</bcp14> ignore a received Upgrade header field if it wishes to continue using the current protocol on that connection.</t>
            </li>
         </ul>
         <t>This rejection of the upgrade is common, and can happen for a variety of reasons:</t>
         <t>
            <list style="symbols">
               <t>The server does not support any of the client's indicated Upgrade Tokens (i.e., the client's proposed new protocols), so it continues to use HTTP/1.1.</t>
               <t>The server knows that an upgrade to the offered protocol will not provide any improvement over HTTP/1.1 for this request to this resource, so it chooses to respond in HTTP/1.1.</t>
               <t>The server requires the client to authenticate before upgrading the protocol, so it replies with the status code "401 (Authentication Required)" and provides a challenge in an "Authorization" response header (<xref section="11.6.2" sectionFormat="comma" target="RFC9110"/>).</t>
               <t>The resource has moved, so the server replies with a 3XX redirect status code (<xref section="3.4" sectionFormat="comma" target="RFC9110"/>).</t>
            </list>
         </t>
         <t>After rejecting the upgrade, the server will continue to interpret subsequent bytes on that connection in accordance with HTTP/1.1.</t>
         <t>
            <xref target="RFC9110"/> also states:</t>
         <ul empty="true">
            <li>
               <t>A client cannot begin using an upgraded protocol on the connection until it has completely sent the request message (i.e., the client can't change the protocol it is sending in the middle of a message).</t>
            </li>
         </ul>
         <t>However, because of the possibility of rejection, the converse is not true: a client cannot necessarily begin using an upgraded protocol merely because it has finished sending the upgrade request message.</t>
         <t>In some cases, the client might expect that the upgrade will succeed. If this expectation is correct, the client might be able to reduce delay by immediately sending the first bytes of the upgraded protocol "optimistically", without waiting for the server's response. This document explores the security implications of this "optimistic" behavior.</t>
      </section>
      <section anchor="possible-security-issues">
         <name>Possible Security Issues</name>
         <t>When there are only two distinct parties involved in an HTTP/1.1 connection (i.e., the client and the server), HTTP Upgrade introduces no new security issues: each party must already be prepared for the other to send arbitrary data on the connection at any time. However, HTTP connections often involve more than two parties, if the requests or responses include third-party data. For example, a browser (party 1) might send an HTTP request to an origin (party 2) with path, headers, or body controlled by a website from a different origin (party 3). Post-upgrade protocols such as WebSocket similarly are often used to convey data chosen by a third party.</t>
         <t>If the third-party data source is untrusted, we call the data it provides "attacker-controlled". The combination of attacker-controlled data and optimistic HTTP Upgrade results in two significant security issues.</t>
         <section anchor="request-smuggling">
            <name>Request Smuggling</name>
            <t>In a Request Smuggling attack (<xref section="11.2" sectionFormat="comma" target="RFC9112"/>) the attacker-controlled data is chosen in such a way that it is interpreted by the server as an additional HTTP request. These attacks allow the attacker to speak on behalf of the client while bypassing the client's own rules about what requests it will issue. Request Smuggling can occur if the client and server have distinct interpretations of the data that flows between them.</t>
            <t>If the server accepts an HTTP Upgrade, it interprets the subsequent bytes in accordance with the new protocol. If it rejects the upgrade, it interprets those bytes as HTTP/1.1. However, the client doesn't know which interpretation the server will take until it receives the server's response status code. If it uses the new protocol optimistically, this creates a risk that the server will interpret attacker-controlled data in the upgraded protocol as an additional HTTP request issued by the client.</t>
            <t>As a trivial example, consider an upgraded protocol in which the entire post-upgrade content might be freely attacker-controlled (e.g., "connect-tcp" <xref target="I-D.ietf-httpbis-connect-tcp"/>). If the client is authenticated to the server using a connection-level authentication method such as TLS Client Certificates, the attacker could send an HTTP/1.1 POST request in the post-upgrade payload. If the client delivers this payload optimistically, and the upgrade request fails, the server would interpret the payload as a subsequent authenticated request issued by the client.</t>
         </section>
         <section anchor="parser-exploits">
            <name>Parser Exploits</name>
            <t>A related category of attacks use protocol disagreement to exploit vulnerabilities in the server's request parsing logic. These attacks apply when the HTTP client is trusted by the server, but the post-upgrade data source is not. If the server software was developed under the assumption that some or all of the HTTP request data is not attacker-controlled, optimistic use of HTTP Upgrade can cause this assumption to be violated, exposing vulnerabilities in the server's HTTP request parser.</t>
         </section>
      </section>
      <section anchor="operational-issues">
         <name>Operational Issues</name>
         <t>If the server rejects the upgrade, the connection can continue to be used for HTTP/1.1. There is no requirement to close the connection in response to an upgrade rejection, and keeping the connection open has performance advantages if additional HTTP requests to this server are likely. Thus, it is normally inappropriate to close the connection in response to a rejected upgrade.</t>
      </section>
      <section anchor="impact-on-existing-upgrade-tokens">
         <name>Impact on Existing Upgrade Tokens</name>
         <t>At the time of writing, there are four distinct Upgrade Tokens that are registered, associated with published documents, and not marked obsolete. This section considers the impact of this document's considerations on each registered Upgrade Token.</t>
         <section anchor="http">
            <name>"HTTP"</name>
            <t>
               <xref target="RFC9110"/> is the source of the requirement quoted in <xref target="background"/>. It also defines the "HTTP/*.*" family of Upgrade Tokens. In HTTP/1.1, the only potentially applicable versions of this token are "0.9", "1.0", "1.1", and "2.0".</t>
            <t>Versions "0.9" and "1.0" are sufficiently syntactically similar to HTTP/1.1 that any such "downward upgrade" would be unlikely to result in the security concerns discussed here. (An "upgrade" to version 1.1 has no effect at all.)</t>
            <t>A version number of "2.0" corresponds to HTTP/2. Every HTTP/2 connection begins with a Client Connection Preface (<xref section="3.4" sectionFormat="of" target="RFC9113"/>) that was selected to ensure that a compliant HTTP/1.1 server will not process further data on this connection. This avoids security issues if an "HTTP/2.0" Upgrade Token is used optimistically.</t>
         </section>
         <section anchor="tls">
            <name>"TLS"</name>
            <t>
               <xref target="RFC2817"/> correctly highlights the possibility of the server rejecting the upgrade. The security considerations documented here are applicable to any use of the "TLS" Upgrade Token, but no change is required in <xref target="RFC2817"/>.</t>
         </section>
         <section anchor="websocketwebsocket">
            <name>"WebSocket"/"websocket"</name>
            <t>
               <xref section="4.1" sectionFormat="of" target="RFC6455"/> says:</t>
            <ul empty="true">
               <li>
                  <t>Once the client's opening handshake has been sent, the client <bcp14>MUST</bcp14> wait for a response from the server before sending any further data.</t>
               </li>
            </ul>
            <t>Thus, optimistic use of HTTP Upgrade is already forbidden in the WebSocket protocol. Additionally, the WebSocket protocol requires high-entropy masking of client-to-server frames (<xref section="5.1" sectionFormat="of" target="RFC6455"/>).</t>
         </section>
         <section anchor="connect-udp">
            <name>"connect-udp"</name>
            <t>
               <xref section="5" sectionFormat="of" target="RFC9298"/> says:</t>
            <ul empty="true">
               <li>
                  <t>A client <bcp14>MAY</bcp14> optimistically start sending UDP packets in HTTP Datagrams before receiving the response to its UDP proxying request.</t>
               </li>
            </ul>
            <t>However, in HTTP/1.1, this "proxying request" is an HTTP Upgrade request. This upgrade is likely to be rejected in certain circumstances, such as when the UDP destination address (which is attacker-controlled) is invalid. Additionally, the contents of the "connect-udp" protocol stream can include untrusted material (i.e., the UDP packets, which might come from other applications on the client device). This creates the possibility of Request Smuggling attacks. To avoid these concerns, this text is updated as follows:</t>
            <ul empty="true">
               <li>
                  <t>When using HTTP/2 or later, a client <bcp14>MAY</bcp14> optimistically ...</t>
               </li>
            </ul>
            <t>
               <xref section="3.3" sectionFormat="of" target="RFC9298"/> describes the requirement for a successful proxy setup response, including upgrading to the "connect-udp" protocol, and says:</t>
            <ul empty="true">
               <li>
                  <t>If any of these requirements are not met, the client <bcp14>MUST</bcp14> treat this proxying attempt as failed and abort the connection.</t>
               </li>
            </ul>
            <t>However, this could be interpreted as an instruction to abort the underlying TLS and TCP connections in the event of an unsuccessful response such as "407 ("Proxy Authentication Required)". To avoid an unnecessary delay in this case, this text is hereby updated as follows:</t>
            <ul empty="true">
               <li>
                  <t>If any of these requirements are not met, the client <bcp14>MUST</bcp14> treat this proxying attempt as failed. If the "Upgrade" response header field is absent, the client <bcp14>MAY</bcp14> reuse the connection for further HTTP/1.1 requests; otherwise it <bcp14>MUST</bcp14> abort the underlying connection.</t>
               </li>
            </ul>
         </section>
      </section>
      <section anchor="guidance-for-future-upgrade-tokens">
         <name>Guidance for Future Upgrade Tokens</name>
         <t>There are now several good examples of designs that prevent the security concerns discussed in this document and may be applicable in future specifications:</t>
         <t>
            <list style="symbols">
               <t>Forbid optimistic use of HTTP Upgrade (WebSocket, <xref section="4.1" sectionFormat="of" target="RFC6455"/>).</t>
               <t>Embed a fixed preamble that terminates HTTP/1.1 processing (HTTP/2, <xref section="3.4" sectionFormat="of" target="RFC9113"/>).</t>
               <t>Apply high-entropy masking of client-to-server data (WebSocket, <xref section="5.1" sectionFormat="of" target="RFC6455"/>).</t>
            </list>
         </t>
         <t>Future specifications for Upgrade Tokens <bcp14>MUST</bcp14> account for the security issues discussed here and provide clear guidance on how clients can avoid them.</t>
      </section>
      <section anchor="iana-considerations">
         <name>IANA Considerations</name>
         <t>This document has no IANA actions.</t>
      </section>
   </middle>
   <back>
      <references anchor="sec-combined-references" title="References">
         <references anchor="sec-normative-references" title="Normative References">
            <reference anchor="RFC2119">
               <front>
                  <title>Key words for use in RFCs to Indicate Requirement Levels</title>
                  <author fullname="S. Bradner" initials="S." surname="Bradner"/>
                  <date month="March" year="1997"/>
               </front>
               <seriesInfo name="BCP" value="14"/>
               <seriesInfo name="RFC" value="2119"/>
               <seriesInfo name="DOI" value="10.17487/RFC2119"/>
            </reference>
            <reference anchor="RFC8174">
               <front>
                  <title>Ambiguity of Uppercase vs Lowercase in RFC 2119 Key Words</title>
                  <author fullname="B. Leiba" initials="B." surname="Leiba"/>
                  <date month="May" year="2017"/>
               </front>
               <seriesInfo name="BCP" value="14"/>
               <seriesInfo name="RFC" value="8174"/>
               <seriesInfo name="DOI" value="10.17487/RFC8174"/>
            </reference>
            <reference anchor="RFC9110">
               <front>
                  <title>HTTP Semantics</title>
                  <author fullname="R. Fielding"
                           initials="R."
                           role="editor"
                           surname="Fielding"/>
                  <author fullname="M. Nottingham"
                           initials="M."
                           role="editor"
                           surname="Nottingham"/>
                  <author fullname="J. Reschke"
                           initials="J."
                           role="editor"
                           surname="Reschke"/>
                  <date month="June" year="2022"/>
               </front>
               <seriesInfo name="STD" value="97"/>
               <seriesInfo name="RFC" value="9110"/>
               <seriesInfo name="DOI" value="10.17487/RFC9110"/>
            </reference>
            <reference anchor="RFC9298">
               <front>
                  <title>Proxying UDP in HTTP</title>
                  <author fullname="D. Schinazi" initials="D." surname="Schinazi"/>
                  <date month="August" year="2022"/>
               </front>
               <seriesInfo name="RFC" value="9298"/>
               <seriesInfo name="DOI" value="10.17487/RFC9298"/>
            </reference>
         </references>
         <references anchor="sec-informative-references" title="Informative References">
            <reference anchor="RFC9112">
               <front>
                  <title>HTTP/1.1</title>
                  <author fullname="R. Fielding"
                           initials="R."
                           role="editor"
                           surname="Fielding"/>
                  <author fullname="M. Nottingham"
                           initials="M."
                           role="editor"
                           surname="Nottingham"/>
                  <author fullname="J. Reschke"
                           initials="J."
                           role="editor"
                           surname="Reschke"/>
                  <date month="June" year="2022"/>
               </front>
               <seriesInfo name="STD" value="99"/>
               <seriesInfo name="RFC" value="9112"/>
               <seriesInfo name="DOI" value="10.17487/RFC9112"/>
            </reference>
            <reference anchor="I-D.ietf-httpbis-connect-tcp">
               <front>
                  <title>Template-Driven HTTP CONNECT Proxying for TCP</title>
                  <author fullname="Benjamin M. Schwartz" initials="B. M." surname="Schwartz">
                     <organization>Meta Platforms, Inc.</organization>
                  </author>
                  <date day="21" month="October" year="2024"/>
               </front>
               <seriesInfo name="Internet-Draft" value="draft-ietf-httpbis-connect-tcp-06"/>
            </reference>
            <reference anchor="RFC9113">
               <front>
                  <title>HTTP/2</title>
                  <author fullname="M. Thomson"
                           initials="M."
                           role="editor"
                           surname="Thomson"/>
                  <author fullname="C. Benfield"
                           initials="C."
                           role="editor"
                           surname="Benfield"/>
                  <date month="June" year="2022"/>
               </front>
               <seriesInfo name="RFC" value="9113"/>
               <seriesInfo name="DOI" value="10.17487/RFC9113"/>
            </reference>
            <reference anchor="RFC2817">
               <front>
                  <title>Upgrading to TLS Within HTTP/1.1</title>
                  <author fullname="R. Khare" initials="R." surname="Khare"/>
                  <author fullname="S. Lawrence" initials="S." surname="Lawrence"/>
                  <date month="May" year="2000"/>
               </front>
               <seriesInfo name="RFC" value="2817"/>
               <seriesInfo name="DOI" value="10.17487/RFC2817"/>
            </reference>
            <reference anchor="RFC6455">
               <front>
                  <title>The WebSocket Protocol</title>
                  <author fullname="I. Fette" initials="I." surname="Fette"/>
                  <author fullname="A. Melnikov" initials="A." surname="Melnikov"/>
                  <date month="December" year="2011"/>
               </front>
               <seriesInfo name="RFC" value="6455"/>
               <seriesInfo name="DOI" value="10.17487/RFC6455"/>
            </reference>
         </references>
      </references>
      <?line 161?>
      <section anchor="acknowledgments" numbered="false">
         <name>Acknowledgments</name>
         <t>Thanks to Mark Nottingham and Lucas Pardue for early reviews of this document.</t>
      </section>
   </back>
</rfc>
