<?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.29 (Ruby 3.2.2) -->
<?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-05"
     ipr="trust200902"
     sortRefs="true"
     submissionType="IETF"
     symRefs="true"
     tocInclude="true"
     updates="9112, 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 Protocol Transitions in HTTP/1.1</title>
      <author fullname="Benjamin M. Schwartz">
         <organization>Meta Platforms, Inc.</organization>
         <address>
            <email>ietf@bemasc.net</email>
         </address>
      </author>
      <date day="20" month="August" year="2025"/>
      <area>Web and Internet Transport</area>
      <workgroup>HTTPBIS</workgroup>
      <abstract><?line 33?>
         <t>In HTTP/1.1, the client can request a change to a new protocol on the existing connection. This document discusses the security considerations that apply to data sent by the client before this request is confirmed, and updates RFC 9112 and 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 38?>
      <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 and later, a single connection can be used for many requests. In HTTP/2 and HTTP/3, these requests can be multiplexed, as each request is distinguished explicitly by its stream ID. However, in HTTP/1.1, requests are strictly sequential, and each new request is distinguished implicitly by the closure of the preceding request.</t>
         <t>HTTP/1.1 is also the only version of HTTP that allows the client to change the protocol used for the remainder of the connection. There are two mechanisms to request such a protocol transition. One mechanism is the Upgrade request header field (<xref section="7.8" sectionFormat="comma" target="HTTP"/>), which indicates that the client 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.</t>
         <t>The other mechanism is the HTTP CONNECT method. This method indicates that the client wishes to establish a TCP connection to the specified host and port. The server replies with a 2xx (Successful) response to indicate that the request was accepted and a TCP connection was established. After this point, the TCP connection is acting as a TCP tunnel, not an HTTP/1.1 connection.</t>
         <t>Both of these mechanisms also permit the server to reject the request. For example, <xref target="HTTP"/> says:</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>and</t>
         <ul empty="true">
            <li>
               <t>A server <bcp14>MUST</bcp14> reject a CONNECT request that targets an empty or invalid port number, typically by responding with a 400 (Bad Request) status code.</t>
            </li>
         </ul>
         <t>Rejections are common, and can happen for a variety of reasons. A request using Upgrade might be rejected if:</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 field (<xref section="11.6.2" sectionFormat="comma" target="HTTP"/>).</t>
               <t>The resource has moved, so the server replies with a 3xx (Redirection) status code (<xref section="3.4" sectionFormat="comma" target="HTTP"/>).</t>
            </list>
         </t>
         <t>Similarly, a CONNECT request might be rejected if:</t>
         <t>
            <list style="symbols">
               <t>The server does not support HTTP CONNECT.</t>
               <t>The specified destination is not allowed under server policy.</t>
               <t>The destination cannot be resolved, is unreachable, or does not accept the connection.</t>
               <t>The proxy requires the client to authenticate before proceeding.</t>
            </list>
         </t>
         <t>After rejecting a request, the server will continue to interpret subsequent bytes on that connection in accordance with HTTP/1.1.</t>
         <t>
            <xref target="HTTP"/> 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 a new protocol merely because it has finished sending the corresponding request message.</t>
         <t>In some cases, the client might expect that the protocol transition will succeed. If this expectation is correct, the client might be able to reduce delay by immediately sending the first bytes of the new 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), protocol transitions introduce 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 content controlled by a website from a different origin (party 3). Post-transition 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 protocol transitions 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 a protocol transition request, it interprets the subsequent bytes in accordance with the new protocol. If it rejects the request, it interprets those bytes as HTTP/1.1. However, the client cannot 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 new protocol as an additional HTTP request issued by the client.</t>
            <t>As a trivial example, consider an HTTP CONNECT client providing connectivity to an untrusted application. If the client is authenticated to the proxy server using a connection-level authentication method such as TLS Client Certificates, the attacker could send an HTTP/1.1 POST request for the proxy server at the beginning of its TCP connection. If the client delivers this data optimistically, and the CONNECT request fails, the server would misinterpret the application's data 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-transition 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 transmission 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 transition request, the connection can continue to be used for HTTP/1.1. There is no requirement to close the connection in response to a rejected transition, 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 transition.</t>
      </section>
      <section anchor="existing">
         <name>Impact on HTTP Upgrade with Existing Upgrade Tokens</name>
         <t>This section describes the impact of this document's considerations on some registered upgrade tokens that are believed to be in use at the time of writing.</t>
         <section anchor="tls">
            <name>"TLS"</name>
            <t>The "TLS" family of upgrade tokens was defined in <xref target="RFC2817"/>, which correctly highlights the possibility of the server rejecting the upgrade. If a client ignores this possibility and sends TLS data optimistically, the result cannot be valid HTTP/1.1: the first octet of a TLS connection must be 22 (ContentType.handshake), but this is not an allowed character in an HTTP/1.1 method. A compliant HTTP/1.1 server will treat this as a parsing error and close the connection without processing further requests.</t>
         </section>
         <section anchor="websocketwebsocket">
            <name>"WebSocket"/"websocket"</name>
            <t>
               <xref section="4.1" sectionFormat="of" target="WEBSOCKET"/> 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="WEBSOCKET"/>).</t>
         </section>
         <section anchor="connect-udp">
            <name>"connect-udp"</name>
            <t>
               <xref section="5" sectionFormat="of" target="CONNECT-UDP"/> 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 document replaces that text to exclude HTTP/1.1 from any optimistic sending, as follows:</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, but only if the HTTP version in use is HTTP/2 or later. Clients <bcp14>MUST NOT</bcp14> send UDP packets optimistically in HTTP/1.x due to the risk of request smuggling attacks.</t>
               </li>
            </ul>
         </section>
         <section anchor="connect-ip">
            <name>"connect-ip"</name>
            <t>The "connect-ip" upgrade token is defined in <xref target="CONNECT-IP"/>. <xref section="11" sectionFormat="of" target="CONNECT-IP"/> forbids clients from sending packets optimistically in HTTP/1.1, avoiding this issue.</t>
         </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 reduce or eliminate 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 (<xref section="4.1" sectionFormat="of" target="WEBSOCKET"/>, <xref section="11" sectionFormat="of" target="CONNECT-IP"/>).</t>
               <t>Embed a fixed preamble that deliberately terminates HTTP/1.1 processing (<xref section="3.4" sectionFormat="of" target="RFC9113"/>).</t>
               <t>Apply high-entropy masking of client-to-server data (<xref section="5.1" sectionFormat="of" target="WEBSOCKET"/>).</t>
            </list>
         </t>
         <t>Future specifications for upgrade tokens should account for the security issues discussed here and provide clear guidance on how implementations can avoid them.</t>
         <section anchor="selection-of-request-methods">
            <name>Selection of Request Methods</name>
            <t>Some upgrade tokens, such as "TLS", are defined for use with any ordinary HTTP method. The upgraded protocol continues to provide HTTP semantics, and will convey the response to this HTTP request.</t>
            <t>The other upgrade tokens mentioned in <xref target="existing"/> do not preserve HTTP semantics, so the method is not relevant. All of these upgrade tokens are specified only for GET requests with no content.</t>
            <t>Future specifications for upgrade tokens should restrict their use to GET requests with no content if the HTTP method is otherwise irrelevant and the request does not need to carry any message content. This improves consistency with other upgrade tokens and simplifies server implementation.</t>
         </section>
      </section>
      <section anchor="guidance-for-http-connect">
         <name>Guidance for HTTP CONNECT</name>
         <t>This document updates RFC 9112 to include the remaining text of this section. The requirements in this section apply only to HTTP/1.1.</t>
         <t>Proxy clients that send CONNECT requests on behalf of untrusted TCP clients <bcp14>MUST</bcp14> do one or both of the following:</t>
         <t>
            <list style="numbers">
               <t>Wait for a 2xx (Successful) response before forwarding any TCP payload data.</t>
               <t>Send a "Connection: close" request header.</t>
            </list>
         </t>
         <t>Proxy clients that don't implement at least one of these two behaviors are vulnerable to a trivial request smuggling attack (<xref section="11.2" sectionFormat="comma" target="RFC9112"/>).</t>
         <t>At the time of writing, some proxy clients are believed to be vulnerable as described. As a mitigation, proxy servers <bcp14>MUST</bcp14> close the underlying connection when rejecting a CONNECT request, without processing any further requests on that connection, unless the client is known to wait for a 2xx (Successful) response before forwarding TCP payload data. This requirement applies whether or not the request includes a "close" connection option.</t>
         <t>Note that this mitigation will frequently impair the performance of correctly implemented clients, especially when returning a 407 (Proxy Authentication Required) response. This performance loss can be avoided by using HTTP/2 or HTTP/3, which are not vulnerable to this attack.</t>
      </section>
      <section anchor="security-considerations">
         <name>Security Considerations</name>
         <t>This document describes security considerations related to optimistic use of protocol transitions in HTTP/1.1.</t>
      </section>
      <section anchor="iana-considerations">
         <name>IANA Considerations</name>
         <t>This document has no IANA actions.</t>
      </section>
   </middle>
   <back>
      <displayreference target="RFC9112" to="HTTP/1.1"/>
      <displayreference target="RFC9113" to="HTTP/2"/>
      <references anchor="sec-combined-references" title="References">
         <references anchor="sec-normative-references" title="Normative 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="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="HTTP">
               <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="CONNECT-UDP">
               <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="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="WEBSOCKET">
               <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>
            <reference anchor="CONNECT-IP">
               <front>
                  <title>Proxying IP in HTTP</title>
                  <author fullname="T. Pauly" initials="T." role="editor" surname="Pauly"/>
                  <author fullname="D. Schinazi" initials="D." surname="Schinazi"/>
                  <author fullname="A. Chernyakhovsky" initials="A." surname="Chernyakhovsky"/>
                  <author fullname="M. Kühlewind" initials="M." surname="Kühlewind"/>
                  <author fullname="M. Westerlund" initials="M." surname="Westerlund"/>
                  <date month="October" year="2023"/>
               </front>
               <seriesInfo name="RFC" value="9484"/>
               <seriesInfo name="DOI" value="10.17487/RFC9484"/>
            </reference>
         </references>
      </references>
      <?line 196?>
      <section anchor="acknowledgments" numbered="false">
         <name>Acknowledgments</name>
         <t>This document benefited from valuable reviews and suggestions by:</t>
         <t>
            <list style="symbols">
               <t>Mike Bishop</t>
               <t>Mark Nottingham</t>
               <t>Kazuho Oku</t>
               <t>Lucas Pardue</t>
               <t>David Schinazi</t>
               <t>Glenn Strauss</t>
               <t>Michael Sweet</t>
               <t>Willy Tarreau</t>
               <t>Martin Thomson</t>
            </list>
         </t>
      </section>
   </back>
</rfc>
