HyBi Working Group | I.F. Fette |
Internet-Draft | Google, Inc. |
Intended status: Standards Track | A. Melnikov |
Expires: April 02, 2012 | Isode Ltd |
September 30, 2011 |
The WebSocket protocol
draft-ietf-hybi-thewebsocketprotocol-17
The WebSocket protocol enables two-way communication between a client running untrusted code running in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the Origin-based security model commonly used by Web browsers. The protocol consists of an opening handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g. using XMLHttpRequest or <iframe>s and long polling).
Please send feedback to the hybi@ietf.org mailing list.
This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.
Internet-Drafts are working documents of the Internet Engineering Task Force (IETF). Note that other groups may also distribute working documents as Internet-Drafts. The list of current Internet- Drafts is at http://datatracker.ietf.org/drafts/current/.
Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as "work in progress."
This Internet-Draft will expire on April 02, 2012.
Copyright (c) 2011 IETF Trust and the persons identified as the document authors. All rights reserved.
This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (http://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License.
This section is non-normative.
Historically, creating Web applications that need bidirectional communication between a client and a server (e.g., instant messaging and gaming applications) has required an abuse of HTTP to poll the server for updates while sending upstream notifications as distinct HTTP calls.[RFC6202]
This results in a variety of problems:
A simpler solution would be to use a single TCP connection for traffic in both directions. This is what the WebSocket protocol provides. Combined with the WebSocket API, it provides an alternative to HTTP polling for two-way communication from a Web page to a remote server. [WSAPI]
The same technique can be used for a variety of Web applications: games, stock tickers, multiuser applications with simultaneous editing, user interfaces exposing server-side services in real time, etc.
The WebSocket protocol is designed to supersede existing bidirectional communication technologies which use HTTP as a transport layer to benefit from existing infrastructure (proxies, filtering, authentication). Such technologies were implemented as trade-offs between efficiency and reliability because HTTP was not initially meant to be used for bidirectional communication (see [RFC6202] for further discussion). The WebSocket protocol attempts to address the goals of existing bidirectional HTTP technologies in the context of the existing HTTP infrastructure; as such, it is designed to work over HTTP ports 80 and 443 as well as to support HTTP proxies and intermediaries, even if this implies some complexity specific to the current environment. However, the design does not limit WebSocket to HTTP, and future implementations could use a simpler handshake over a dedicated port without revinventing the entire protocol. This last point is important because the traffic patterns of interactive messaging do not closely match standard HTTP traffic and can induce unusual loads on some components.
This section is non-normative.
The protocol has two parts: a handshake, and then the data transfer.
The handshake from the client looks as follows:
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13
The handshake from the server looks as follows:
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat
The leading line from the client follows the Request-Line format. The leading line from the server follows the Status-Line format. The Request-Line and Status-Line productions are defined in [RFC2616].
After the leading line in both cases come an unordered set of header fields. The meaning of these header fields is specified in Section 4 of this document. Additional header fields may also be present, such as cookies [RFC6265]. The format and parsing of headers is as defined in [RFC2616].
Once the client and server have both sent their handshakes, and if the handshake was successful, then the data transfer part starts. This is a two-way communication channel where each side can, independently from the other, send data at will.
Clients and servers, after a successful handshake, transfer data back and forth in conceptual units referred to in this specification as "messages". On the wire a message is composed of one or more frames. The WebSocket message does not necessarily correspond to a particular network layer framing, as a fragmented message may be coalesced or split by an intermediary.
A frame has an associated type. Each frame belonging to the same message contain the same type of data. Broadly speaking, there are types for textual data, which is interpreted as UTF-8 [RFC3629] text, binary data (whose interpretation is left up to the application), and control frames, which are not intended to carry data for the application, but instead for protocol-level signaling, such as to signal that the connection should be closed. This version of the protocol defines six frame types and leaves ten reserved for future use.
This section is non-normative.
The opening handshake is intended to be compatible with HTTP-based server-side software and intermediaries, so that a single port can be used by both HTTP clients talking to that server and WebSocket clients talking to that server. To this end, the WebSocket client's handshake is an HTTP Upgrade request:
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13
In compliance with [RFC2616], header fields in the handshake may be sent by the client in any order, so the order in which different header fields are received is not significant.
The "Request-URI" of the GET method [RFC2616] is used to identify the endpoint of the WebSocket connection, both to allow multiple domains to be served from one IP address and to allow multiple WebSocket endpoints to be served by a single server.
The client includes the hostname in the Host header field of its handshake as per [RFC2616], so that both the client and the server can verify that they agree on which host is in use.
Additional header fields are used to select options in the WebSocket protocol. Typical options available in this version are the subprotocol selector (|Sec-WebSocket-Protocol|), list of extensions support by the client (|Sec-WebSocket-Extensions|), |Origin| header field, etc. The |Sec-WebSocket-Protocol| request-header field can be used to indicate what subprotocols (application-level protocols layered over the WebSocket protocol) are acceptable to the client. The server selects one or none of the acceptable protocols and echoes that value in its handshake to indicate that it has selected that protocol.
Sec-WebSocket-Protocol: chat
The |Origin| header field [I-D.ietf-websec-origin] is used to protect against unauthorized cross-origin use of a WebSocket server by scripts using the |WebSocket| API in a Web browser. The server is informed of the script origin generating the WebSocket connection request. If the server does not wish to accept connections from this origin, it can choose to reject the connection by sending an appropriate HTTP error code. This header field is sent by browser clients, for non-browser clients this header field may be sent if it makes sense in the context of those clients.
Finally, the server has to prove to the client that it received the client's WebSocket handshake, so that the server doesn't accept connections that are not WebSocket connections. This prevents an attacker from tricking a WebSocket server by sending it carefully-crafted packets using |XMLHttpRequest| [XMLHttpRequest] or a |form| submission.
To prove that the handshake was received, the server has to take two pieces of information and combine them to form a response. The first piece of information comes from the |Sec-WebSocket-Key| header field in the client handshake:
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
For this header field, the server has to take the value (as present in the header field, e.g. the base64-encoded [RFC4648] version minus any leading and trailing whitespace), and concatenate this with the Globally Unique Identifier (GUID, [RFC4122]) "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" in string form, which is unlikely to be used by network endpoints that do not understand the WebSocket protocol. A SHA-1 hash (160 bits), base64-encoded (see Section 4 of [RFC4648]), of this concatenation is then returned in the server's handshake [FIPS.180-2.2002].
Concretely, if as in the example above, |Sec-WebSocket-Key| header field had the value "dGhlIHNhbXBsZSBub25jZQ==", the server would concatenate the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" to form the string "dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11". The server would then take the SHA-1 hash of this, giving the value 0xb3 0x7a 0x4f 0x2c 0xc0 0x62 0x4f 0x16 0x90 0xf6 0x46 0x06 0xcf 0x38 0x59 0x45 0xb2 0xbe 0xc4 0xea. This value is then base64-encoded (see Section 4 of [RFC4648]), to give the value "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=". This value would then be echoed in the |Sec-WebSocket-Accept| header field.
The handshake from the server is much simpler than the client handshake. The first line is an HTTP Status-Line, with the status code 101:
HTTP/1.1 101 Switching Protocols
Any status code other than 101 indicates that the WebSocket handshake has not completed, and that the semantics of HTTP still apply. The headers follow the status code.
The |Connection| and |Upgrade| header fields complete the HTTP Upgrade. The |Sec-WebSocket-Accept| header field indicates whether the server is willing to accept the connection. If present, this header field must include a hash of the client's nonce sent in |Sec-WebSocket-Key| along with a predefined GUID. Any other value must not be interpreted as an acceptance of the connection by the server.
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
These fields are checked by the |WebSocket| client for scripted pages. If the |Sec-WebSocket-Accept| value does not match the expected value, or if the header field is missing, or if the HTTP status code is not 101, the connection will not be established and WebSocket frames will not be sent.
Option fields can also be included. In this version of the protocol, the main option field is |Sec-WebSocket-Protocol|, which indicates the subprotocol that the server has selected. WebSocket clients verify that the server included one of the values as was specified in the WebSocket client's handshake. A server that speaks multiple subprotocols has to make sure it selects one based on the client's handshake and specifies it in its handshake.
Sec-WebSocket-Protocol: chat
The server can also set cookie-related option fields to set cookies, as described in [RFC6265].
This section is non-normative.
The closing handshake is far simpler than the opening handshake.
Either peer can send a control frame with data containing a specified control sequence to begin the closing handshake (detailed in Section 5.5.1). Upon receiving such a frame, the other peer sends a close frame in response, if it hasn't already sent one. Upon receiving that control frame, the first peer then closes the connection, safe in the knowledge that no further data is forthcoming.
After sending a control frame indicating the connection should be closed, a peer does not send any further data; after receiving a control frame indicating the connection should be closed, a peer discards any further data received.
It is safe for both peers to initiate this handshake simultaneously.
The closing handshake is intended to complement the TCP closing handshake (FIN/ACK), on the basis that the TCP closing handshake is not always reliable end-to-end, especially in the presence of intercepting proxies and other intermediaries.
By sending a close frame and waiting for a close frame in response, certain cases are avoided where data may be unnecessarily lost. For instance, on some platforms, if a socket is closed with data in the receive queue, a RST packet is sent, which will then cause recv() to fail for the party that received the RST, even if there was data waiting to be read.
This section is non-normative.
The WebSocket protocol is designed on the principle that there should be minimal framing (the only framing that exists is to make the protocol frame-based instead of stream-based, and to support a distinction between Unicode text and binary frames). It is expected that metadata would be layered on top of WebSocket by the application layer, in the same way that metadata is layered on top of TCP by the application layer (e.g., HTTP).
Conceptually, WebSocket is really just a layer on top of TCP that does the following:
Other than that, WebSocket adds nothing. Basically it is intended to be as close to just exposing raw TCP to script as possible given the constraints of the Web. It's also designed in such a way that its servers can share a port with HTTP servers, by having its handshake be a valid HTTP Upgrade request mechanism also. One could conceptually use other protocols to establish client-server messaging, but the intent of WebSockets was to provide a relatively simple protocol that can coexist with HTTP and deployed HTTP infrastructure (such as proxies) that is as close to TCP as is safe for use with such infrastructure given security considerations, with targeted additions to simplify usage and make simple things simple (such as the addition of message semantics).
The protocol is intended to be extensible; future versions will likely introduce additional concepts such as multiplexing.
This section is non-normative.
The WebSocket protocol uses the origin model used by Web browsers to restrict which Web pages can contact a WebSocket server when the WebSocket protocol is used from a Web page. Naturally, when the WebSocket protocol is used by a dedicated client directly (i.e. not from a Web page through a Web browser), the origin model is not useful, as the client can provide any arbitrary origin string.
This protocol is intended to fail to establish a connection with servers of pre-existing protocols like SMTP [RFC5321] and HTTP, while allowing HTTP servers to opt-in to supporting this protocol if desired. This is achieved by having a strict and elaborate handshake, and by limiting the data that can be inserted into the connection before the handshake is finished (thus limiting how much the server can be influenced).
It is similarly intended to fail to establish a connection when data from other protocols, especially HTTP, is sent to a WebSocket server, for example as might happen if an HTML |form| were submitted to a WebSocket server. This is primarily achieved by requiring that the server prove that it read the handshake, which it can only do if the handshake contains the appropriate parts which themselves can only be sent by a WebSocket handshake. In particular, at the time of writing of this specification, fields starting with |Sec-| cannot be set by an attacker from a Web browser using only HTML and JavaScript APIs such as |XMLHttpRequest| [XMLHttpRequest].
This section is non-normative.
The WebSocket protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request.
By default the WebSocket protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over TLS [RFC2818].
This section is non-normative.
When a connection is to be made to a port that is shared by an HTTP server (a situation that is quite likely to occur with traffic to ports 80 and 443), the connection will appear to the HTTP server to be a regular GET request with an Upgrade offer. In relatively simple setups with just one IP address and a single server for all traffic to a single hostname, this might allow a practical way for systems based on the WebSocket protocol to be deployed. In more elaborate setups (e.g. with load balancers and multiple servers), a dedicated set of hosts for WebSocket connections separate from the HTTP servers is probably easier to manage. At the time of writing of this specification, it should be noted that connections on port 80 and 443 have significantly different success rates, with connections on port 443 being significantly more likely to succeed, though this may change with time.
This section is non-normative.
The client can request that the server use a specific subprotocol by including the |Sec-WebSocket-Protocol| field in its handshake. If it is specified, the server needs to include the same field and one of the selected subprotocol values in its response for the connection to be established.
These subprotocol names should be registered as per Section 11.5. To avoid potential collisions, it is recommended to use names that contain the ASCII version of the domain name of the subprotocol's originator. For example, if Example Corporation were to create a Chat subprotocol to be implemented by many servers around the Web, they could name it "chat.example.com". If the Example Organization called their competing subprotocol "chat.example.org", then the two subprotocols could be implemented by servers simultaneously, with the server dynamically selecting which subprotocol to use based on the value sent by the client.
Subprotocols can be versioned in backwards-incompatible ways by changing the subprotocol name, e.g. going from "bookings.example.net" to "v2.bookings.example.net". These subprotocols would be considered completely separate by WebSocket clients. Backwards-compatible versioning can be implemented by reusing the same subprotocol string but carefully designing the actual subprotocol to support this kind of extensibility.
All diagrams, examples, and notes in this specification are non-normative, as are all sections explicitly marked non-normative. Everything else in this specification is normative.
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC2119. [RFC2119]
Requirements phrased in the imperative as part of algorithms (such as "strip any leading space characters" or "return false and abort these steps") are to be interpreted with the meaning of the key word ("must", "should", "may", etc) used in introducing the algorithm.
Conformance requirements phrased as algorithms or specific steps MAY be implemented in any manner, so long as the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be easy to follow, and not intended to be performant.)
ASCII shall mean the character-encoding scheme defined in [ANSI.X3-4.1986].
This document makes reference to UTF-8 values and uses UTF-8 notational formats as defined in STD 63 [RFC3629].
Key Terms such as named algorithms or definitions are indicated like this.
Names of header fields or variables are indicated like |this|.
Variable values are indicated like /this/.
This document references the procedure to Fail the WebSocket Connection. This procedure is defined in Section 7.1.7.
Converting a string to ASCII lowercase means replacing all characters in the range U+0041 to U+005A (i.e. LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z) with the corresponding characters in the range U+0061 to U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z).
Comparing two strings in an ASCII case-insensitive manner means comparing them exactly, code point for code point, except that the characters in the range U+0041 to U+005A (i.e. LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z) and the corresponding characters in the range U+0061 to U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z) are considered to also match.
The term "URI" is used in this document as defined in [RFC3986].
When an implementation is required to send data as part of the WebSocket protocol, the implementation MAY delay the actual transmission arbitrarily, e.g. buffering data so as to send fewer IP packets.
Note that this document uses both [RFC5234] and [RFC2616] variants of ABNF in different sections.
This specification defines two URI schemes, using the ABNF syntax defined in RFC 5234 [RFC5234], and terminology and ABNF productions defined by the URI specification RFC 3986 [RFC3986].
ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ] wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ] host = <host, defined in [RFC3986], Section 3.2.2> port = <port, defined in [RFC3986], Section 3.2.3> path = <path-abempty, defined in [RFC3986], Section 3.3> query = <query, defined in [RFC3986], Section 3.4>
The port component is OPTIONAL; the default for "ws" is port 80, while the default for "wss" is port 443.
The URI is called "secure" (and it said that "the secure flag is set") if the scheme component matches "wss" case-insensitively.
The "resource-name" (also known as /resource name/ in Section 4.1) can be constructed by concatenating
Fragment identifiers are meaningless in the context of WebSocket URIs, and MUST NOT be used on these URIs. The character "#" in URIs MUST be escaped as %23 if used as part of the query component.
To Establish a WebSocket Connection, a client opens a connection and sends a handshake as defined in this section. A connection is defined to initially be in a CONNECTING state. A client will need to supply a /host/, /port/, /resource name/, and a /secure/ flag, which are the components of a WebSocket URI as discussed in Section 3, along with a list of /protocols/ and /extensions/ to be used. Additionally, if the client is a web browser, it supplies /origin/.
Clients running in controlled environments, e.g. browsers on mobile handsets tied to specific carriers, MAY offload the management of the connection to another agent on the network. In such a situation, the client for the purposes of this specification is considered to include both the handset software and any such agents.
CONNECT example.com:80 HTTP/1.1 Host: example.com
CONNECT example.com:80 HTTP/1.1 Host: example.com Proxy-authorization: Basic ZWRuYW1vZGU6bm9jYXBlcyE=
When the client is to Establish a WebSocket Connection given a set of (/host/, /port/, /resource name/, and /secure/ flag), along with a list of /protocols/ and /extensions/ to be used, and an /origin/ in the case of web browsers, it MUST open a connection, send an opening handshake, and read the server's handshake in response. The exact requirements of how the connection should be opened, what should be sent in the opening handshake, and how the server's response should be interpreted, are as follows in this section. In the following text, we will use terms from Section 3 such as "/host/" and "/secure/ flag" as defined in that section.
If the client is not configured to use a proxy, then a direct TCP connection SHOULD be opened to the host given by /host/ and the port given by /port/.
Once a connection to the server has been established (including a connection via a proxy or over a TLS-encrypted tunnel), the client MUST send an opening handshake to the server. The handshake consists of an HTTP upgrade request, along with a list of required and optional header fields. The requirements for this handshake are as follows.
Once the client's opening handshake has been sent, the client MUST wait for a response from the server before sending any further data. The client MUST validate the server's response as follows:
If the server's response does not conform to the requirements for the server's handshake as defined in this section and in Section 4.2.2, the client MUST Fail the WebSocket Connection.
Please note that according to [RFC2616] all header field names in both HTTP requests and HTTP responses are case-insensitive.
If the server's response is validated as provided for above, it is said that The WebSocket Connection is Established and that the WebSocket Connection is in the OPEN state. The Extensions In Use is defined to be a (possibly empty) string, the value of which is equal to the value of the |Sec-WebSocket-Extensions| header field supplied by the server's handshake, or the null value if that header field was not present in the server's handshake. The Subprotocol In Use is defined to be the value of the |Sec-WebSocket-Protocol| header field in the server's handshake, or the null value if that header field was not present in the server's handshake. Additionally, if any header fields in the server's handshake indicate that cookies should be set (as defined by [RFC6265]), these cookies are referred to as Cookies Set During the Server's Opening Handshake.
Servers MAY offload the management of the connection to other agents on the network, for example load balancers and reverse proxies. In such a situation, the server for the purposes of this specification is considered to include all parts of the server-side infrastructure from the first device to terminate the TCP connection all the way to the server that processes requests and sends responses.
EXAMPLE: For example, a data center might have a server that responds to WebSocket requests with an appropriate handshake, and then passes the connection to another server to actually process the data frames. For the purposes of this specification, the "server" is the combination of both computers.
When a client starts a WebSocket connection, it sends its part of the opening handshake. The server must parse at least part of this handshake in order to obtain the necessary information to generate the server part of the handshake.
The client's opening handshake consists of the following parts. If the server, while reading the handshake, finds that the client did not send a handshake that matches the description below (note that as per [RFC2616] the order of the header fields is not important), including but not limited to any violations of the ABNF grammar specified for the components of the handshake, the server MUST stop processing the client's handshake, and return an HTTP response with an appropriate error code (such as 400 Bad Request).
Sec-WebSocket-Accept = base64-value-non-empty base64-value-non-empty = (1*base64-data [ base64-padding ]) | base64-padding base64-data = 4base64-character base64-padding = (2base64-character "==") | (3base64-character "=") base64-character = ALPHA | DIGIT | "+" | "/"
When a client establishes a WebSocket connection to a server, the server MUST complete the following steps to accept the connection and send the server's opening handshake.
This completes the server's handshake. If the server finishes these steps without aborting the WebSocket handshake, the server considers the WebSocket connection to be established and that the WebSocket connection is in the OPEN state. At this point, the server may begin sending (and receiving) data.
This section is using ABNF syntax/rules from Section 2.1 of [RFC2616], including "implied *LWS rule".
Note that the following ABNF conventions are used in this section: Some names of the rules correspond to names of the corresponding header fields. Such rules express values of the corresponding header fields, for example the Sec-WebSocket-Key ABNF rule describes syntax of the Sec-WebSocket-Key header field value. ABNF rules with the "-Client" suffix in the name are only used in requests sent by the client to the server; ABNF rules with the "-Server" suffix in the name are only used in responses sent by the server to the client. For example, the ABNF rule Sec-WebSocket-Protocol-Client describes syntax of the Sec-WebSocket-Protocol header field value sent by the client to the server.
The following new header field can be sent during the handshake from the client to the server:
Sec-WebSocket-Key = base64-value-non-empty Sec-WebSocket-Extensions = extension-list Sec-WebSocket-Protocol-Client = 1#token Sec-WebSocket-Version-Client = version base64-value-non-empty = (1*base64-data [ base64-padding ]) | base64-padding base64-data = 4base64-character base64-padding = (2base64-character "==") | (3base64-character "=") base64-character = ALPHA | DIGIT | "+" | "/" extension-list = 1#extension extension = extension-token *( ";" extension-param ) extension-token = registered-token registered-token = token extension-param = token [ "=" (token | quoted-string) ] ;When using the quoted-string syntax variant, the value ;after quoted-string unescaping MUST conform to the 'token' ABNF. NZDIGIT = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" version = DIGIT | (NZDIGIT DIGIT) | ("1" DIGIT DIGIT) | ("2" DIGIT DIGIT) ; Limited to 0-255 range, with no leading zeros
The following new header field can be sent during the handshake from the server to the client:
Sec-WebSocket-Extensions = extension-list Sec-WebSocket-Accept = base64-value-non-empty Sec-WebSocket-Protocol-Server = token Sec-WebSocket-Version-Server = 1#version
This section provides some guidance on supporting multiple versions of the WebSocket protocol in clients and servers.
Using the WebSocket version advertisement capability (the "Sec-WebSocket-Version" header field) client can initially request the version of the WebSocket protocol that it prefers (which doesn't necessarily have to be the latest supported by the client). If the server supports the requested version and the handshake message is otherwise valid, the server will accept that version. If the server doesn't support the requested version, it MUST respond with a Sec-WebSocket-Version header field (or multiple Sec-WebSocket-Version header fields) containing all versions it is willing to use. At this point, if the client supports one of the advertised versions, it can repeat the WebSocket handshake using a new version value.
The following example demonstrates version negotiation described above:
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade ... Sec-WebSocket-Version: 25
The response from the server might look as follows:
HTTP/1.1 400 Bad Request ... Sec-WebSocket-Version: 13, 8, 7
Note that the last response from the server might also look like:
HTTP/1.1 400 Bad Request ... Sec-WebSocket-Version: 13 Sec-WebSocket-Version: 8, 7
The client now repeats the handshake that conforms to version 13:
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade ... Sec-WebSocket-Version: 13
In the WebSocket protocol, data is transmitted using a sequence of frames. To avoid confusing network intermediaries (such as intercepting proxies) and for security reasons that are further discussed in Section 10.3, a client MUST mask all frames that it sends to the server (see Section 5.3 for further details). (Note that masking is done whether or not the WebSocket protocol is running over TLS.) The server MUST close the connection upon receiving a frame that is not masked. In this case, a server MAY send a close frame with a status code of 1002 (protocol error) as defined in Section 7.4.1. A server MUST NOT mask any frames that it sends to the client. A client MUST close a connection if it detects a masked frame. In this case, it MAY use the status code 1002 (protocol error) as defined in Section 7.4.1. (These rules might be relaxed in a future specification.)
The base framing protocol defines a frame type with an opcode, a payload length, and designated locations for extension and application data, which together define the payload data. Certain bits and opcodes are reserved for future expansion of the protocol.
A data frame MAY be transmitted by either the client or the server at any time after opening handshake completion and before that endpoint has sent a close frame (Section 5.5.1).
This wire format for the data transfer part is described by the ABNF [RFC5234] given in detail in this section. (Note that unlike in other sections of this document the ABNF in this section is operating on groups of bits. The length of each group of bits is indicated in a comment. When encoded on the wire the most significant bit is the leftmost in the ABNF). A high level overview of the framing is given in the following figure. In a case of conflict between the figure below and the ABNF specified later in this section, the figure is authoritative.
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-------+-+-------------+-------------------------------+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - +-------------------------------+ | |Masking-key, if MASK set to 1 | +-------------------------------+-------------------------------+ | Masking-key (continued) | Payload Data | +-------------------------------- - - - - - - - - - - - - - - - + : Payload Data continued ... : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued ... | +---------------------------------------------------------------+
The base framing protocol is formally defined by the following ABNF [RFC5234]. It is important to note that the representation of this data is binary, not ASCII characters. As such, a field with a length of 1 bit that takes values %x0 / %x1 is represented as a single bit whose value is 0 or 1, not a full byte (octet) that stands for the characters "0" or "1" in the ASCII encoding. A field with a length of 4 bits with values between %x0-F again is represented by 4 bits, again NOT by an ASCII character or full byte (octet) with these values. [RFC5234] does not specify a character encoding - " Rules resolve into a string of terminal values, sometimes called characters. In ABNF, a character is merely a non-negative integer. In certain contexts, a specific mapping (encoding) of values into a character set (such as ASCII) will be specified." Here, the specified encoding is a binary encoding where each terminal value is encoded in the specified number of bits, which varies for each field.
ws-frame = frame-fin ; 1 bit in length frame-rsv1 ; 1 bit in length frame-rsv2 ; 1 bit in length frame-rsv3 ; 1 bit in length frame-opcode ; 4 bits in length frame-masked ; 1 bit in length frame-payload-length ; 7 bits in length [ frame-masking-key ] ; 16 or 64 bits in length frame-payload-data ; n * 8 bits in length, ; where n >= 0 frame-fin = %x0 ; more frames of this message follow / %x1 ; final frame of this message ; 1 bit in length frame-rsv1 = %x0 / %x1 ; 1 bit in length, MUST be 0 unless negotiated ; otherwise frame-rsv2 = %x0 / %x1 ; 1 bit in length, MUST be 0 unless negotiated ; otherwise frame-rsv3 = %x0 / %x1 ; 1 bit in length, MUST be 0 unless negotiated ; otherwise frame-opcode = frame-opcode-non-control / frame-opcode-control frame-opcode-non-control= %x1 ; text frame / %x2 ; binary frame / %x3-7 ; reserved for further non-control frames frame-opcode-control = %x8 ; connection close / %x9 ; ping / %xA ; pong / %xB-F ; reserved for further control frames ; 4 bits in length frame-masked = %x0 ; frame is not masked, no frame-masking-key / %x1 ; frame is masked, frame-masking-key present ; 1 bit in length frame-payload-length = %x00-7D / %x7E frame-payload-length-16 / %x7F frame-payload-length-63 ; 7 bits in length frame-payload-length-16 = %x0000-FFFF ; 16 bits in length frame-payload-length-63 = %x0000000000000000-7FFFFFFFFFFFFFFF ; 64 bits in length frame-masking-key = 4( %0x00-FF ) ; present only if frame-masked is 1 ; 32 bits in length frame-payload-data = (frame-masked-extension-data frame-masked-application-data) ; frame-masked 1 / (frame-unmasked-extension-data frame-unmasked-application-data) ; frame-masked 0 frame-masked-extension-data = *( %x00-FF ) ; reserved for future extensibility ; n*8 bits in length , where n >= 0 frame-masked-application-data = *( %x00-FF ) ; n*8 bits in length, where n >= 0 frame-unmasked-extension-data = *( %x00-FF ) ; reserved for future extensibility ; n*8 bits in length, where n >= 0 frame-unmasked-application-data = *( %x00-FF ) ; n*8 bits in length, where n >= 0
A masked frame MUST have the field frame-masked set to 1, as defined in Section 5.2.
The masking key is contained completely within the frame, as defined in Section 5.2 as frame-masking-key. It is used to mask the payload data defined in the same section as frame-payload-data, which includes extension and application data.
The masking key is a 32-bit value chosen at random by the client. When preparing a masked frame, the client MUST pick a fresh masking key from the set of allowed 32-bit values. The masking key needs to be unpredictable, thus the masking key MUST be derived from a strong source of entropy, and the masking key for a given frame MUST NOT make it simple for a server/proxy to predict the masking key for a subsequent frame. The unpredictability of the masking key is essential to prevent the author of malicious applications from selecting the bytes that appear on the wire. RFC 4086 [RFC4086] discusses what entails a suitable source of entropy for security-sensitive applications.
The masking does not affect the length of the payload data. To convert masked data into unmasked data, or vice versa, the following algorithm is applied. The same algorithm applies regardless of the direction of the translation - e.g. the same steps are applied to mask the data as to unmask the data.
Octet i of the transformed data ("transformed-octet-i") is the XOR of octet i of the original data ("original-octet-i") with octet at index i modulo 4 of the masking key ("masking-key-octet-j"):
j = i MOD 4 transformed-octet-i = original-octet-i XOR masking-key-octet-j
The payload length, indicated in the framing as frame-payload-length, does NOT include the length of the masking key. It is the length of the payload data, e.g. the number of bytes following the masking key.
The primary purpose of fragmentation is to allow sending a message that is of unknown size when the message is started without having to buffer that message. If messages couldn't be fragmented, then an endpoint would have to buffer the entire message so its length could be counted before first byte is sent. With fragmentation, a server or intermediary may choose a reasonable size buffer, and when the buffer is full write a fragment to the network.
A secondary use-case for fragmentation is for multiplexing, where it is not desirable for a large message on one logical channel to monopolize the output channel, so the MUX needs to be free to split the message into smaller fragments to better share the output channel. (Note that the multiplexing extension is not described in this document.)
Unless specified otherwise by an extension, frames have no semantic meaning. An intermediary might coalesce and/or split frames, if no extensions were negotiated by the client and the server, or if some extensions were negotiated, but the intermediary understood all the extensions negotiated and knows how to coalesce and/or split frames in presence of these extensions. One implication of this is that in absence of extensions senders and receivers must not depend on presence of specific frame boundaries.
The following rules apply to fragmentation:
Note: if control frames could not be interjected, the latency of a ping, for example, would be very long if behind a large message. Hence, the requirement of handling control frames in the middle of a fragmented message.
Implementation Note: in absence of any extension a receiver doesn't have to buffer the whole frame in order to process it. For example if a streaming API is used, a part of a frame can be delivered to the application. But note that that assumption might not hold true for all future WebSocket extensions.
Control frames are identified by opcodes where the most significant bit of the opcode is 1. Currently defined opcodes for control frames include 0x8 (Close), 0x9 (Ping), and 0xA (Pong). Opcodes 0xB-0xF are reserved for further control frames yet to be defined.
Control frames are used to communicate state about the WebSocket. Control frames can be interjected in the middle of a fragmented message.
All control frames MUST have a payload length of 125 bytes or less and MUST NOT be fragmented.
The Close frame contains an opcode of 0x8.
The Close frame MAY contain a body (the "application data" portion of the frame) that indicates a reason for closing, such as an endpoint shutting down, an endpoint having received a frame too large, or an endpoint having received a frame that does not conform to the format expected by the other endpoint. If there is a body, the first two bytes of the body MUST be a 2-byte unsigned integer (in network byte order) representing a status code with value /code/ defined in Section 7.4. Following the 2-byte integer the body MAY contain UTF-8 encoded data with value /reason/, the interpretation of which is not defined by this specification. This data is not necessarily human readable, but may be useful for debugging or passing information relevant to the script that opened the connection. As the data is not guaranteed to be human readable, clients MUST NOT show it to end users.
Close frames sent from client to server must be masked as per Section 5.3.
The application MUST NOT send any more data frames after sending a close frame.
If an endpoint receives a Close frame and that endpoint did not previously send a Close frame, the endpoint MUST send a Close frame in response. (When sending a Close frame in response the endpoint typically echos the status code it received.) It SHOULD do so as soon as practical. An endpoint MAY delay sending a close frame until its current message is sent (for instance, if the majority of a fragmented message is already sent, an endpoint MAY send the remaining fragments before sending a Close frame). However, there is no guarantee that the endpoint which has already sent a Close frame will continue to process data.
After both sending and receiving a close message, an endpoint considers the WebSocket connection closed, and MUST close the underlying TCP connection. The server MUST close the underlying TCP connection immediately; the client SHOULD wait for the server to close the connection but MAY close the connection at any time after sending and receiving a close message, e.g. if it has not received a TCP close from the server in a reasonable time period.
If a client and server both send a Close message at the same time, both endpoints will have sent and received a Close message and should consider the WebSocket connection closed and close the underlying TCP connection.
The Ping frame contains an opcode of 0x9.
A Ping frame MAY include Application Data.
Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in response, unless it already received a Close frame. It SHOULD respond with Pong frame as soon as is practical. Pong frames are discussed in Section 5.5.3.
An endpoint MAY send a Ping frame any time after the connection is established and before the connection is closed. NOTE: A ping frame may serve either as a keepalive, or to verify that the remote endpoint is still responsive.
The Pong frame contains an opcode of 0xA.
Section 5.5.2 details requirements that apply to both Ping and Pong frames.
A Pong frame sent in response to a Ping frame must have identical Application Data as found in the message body of the Ping frame being replied to.
If an endpoint receives a Ping frame and has not yet sent Pong frame(s) in response to previous Ping frame(s), the endpoint MAY elect to send a Pong frame for only the most recently processed Ping frame.
A Pong frame MAY be sent unsolicited. This serves as a unidirectional heartbeat. A response to an unsolicited pong is not expected.
Data frames (e.g. non-control frames) are identified by opcodes where the most significant bit of the opcode is 0. Currently defined opcodes for data frames include 0x1 (Text), 0x2 (Binary). Opcodes 0x3-0x7 are reserved for further non-control frames yet to be defined.
Data frames carry application-layer and/or extension-layer data. The opcode determines the interpretation of the data:
The protocol is designed to allow for extensions, which will add capabilities to the base protocol. The endpoints of a connection MUST negotiate the use of any extensions during the opening handshake. This specification provides opcodes 0x3 through 0x7 and 0xB through 0xF, the extension data field, and the frame-rsv1, frame-rsv2, and frame-rsv3 bits of the frame header for use by extensions. The negotiation of extensions is discussed in further detail in Section 9.1. Below are some anticipated uses of extensions. This list is neither complete nor prescriptive.
To Send a WebSocket Message comprising of /data/ over a WebSocket connection, an endpoint MUST perform the following steps.
To receive WebSocket data, an endpoint listens on the underlying network connection. Incoming data MUST be parsed as WebSocket frames as defined in Section 5.2. If a control frame (Section 5.5) is received, the frame MUST be handled as defined by Section 5.5. Upon receiving a data frame (Section 5.6), the endpoint MUST note the /type/ of the data as defined by the Opcode (frame-opcode) from Section 5.2. The Application Data from this frame is defined as the /data/ of the message. If the frame comprises an unfragmented message (Section 5.4), it is said that A WebSocket Message Has Been Received with type /type/ and data /data/. If the frame is part of a fragmented message, the Application Data of the subsequent data frames is concatenated to form the /data/. When the last fragment is received as indicated by the FIN bit (frame-fin), it is said that A WebSocket Message Has Been Received with data /data/ (comprised of the concatenation of the Application Data of the fragments) and type /type/ (noted from the first frame of the fragmented message). Subsequent data frames MUST be interpreted as belonging to a new WebSocket Message.
Extensions (Section 9) MAY change the semantics of how data is read, specifically including what comprises a message boundary. Extensions, in addition to adding "Extension data" before the "Application data" in a payload, MAY also modify the "Application data" (such as by compressing it).
A server MUST remove masking for data frames received from a client as described in Section 5.3.
To Close the WebSocket Connection, an endpoint closes the underlying TCP connection. An endpoint SHOULD use a method that cleanly closes the TCP connection, as well as the TLS session, if applicable, discarding any trailing bytes that may be received. An endpoint MAY close the connection via any means available when necessary, such as when under attack.
The underlying TCP connection, in most normal cases, SHOULD be closed first by the server, so that it holds the TIME_WAIT state and not the client (as this would prevent it from re-opening the connection for 2 MSL, while there is no corresponding server impact as a TIME_WAIT connection is immediately reopened upon a new SYN with a higher seq number). In abnormal cases (such as not having received a TCP Close from the server after a reasonable amount of time) a client MAY initiate the TCP Close. As such, when a server is instructed to Close the WebSocket Connection it SHOULD initiate a TCP Close immediately, and when a client is instructed to do the same, it SHOULD wait for a TCP Close from the server.
As an example of how to obtain a clean closure in C using Berkeley sockets, one would call shutdown() with SHUT_WR on the socket, call recv() until obtaining a return value of 0 indicating that the peer has also performed an orderly shutdown, and finally calling close() on the socket.
To Start the WebSocket Closing Handshake with a status code (Section 7.4) /code/ and an optional close reason (Section 7.1.6) /reason/, an endpoint MUST send a Close control frame, as described in Section 5.5.1 whose status code is set to /code/ and whose close reason is set to /reason/. Once an endpoint has both sent and received a Close control frame, that endpoint SHOULD Close the WebSocket Connection as defined in Section 7.1.1.
Upon either sending or receiving a Close control frame, it is said that The WebSocket Closing Handshake is Started and that the WebSocket connection is in the CLOSING state.
When the underlying TCP connection is closed, it is said that The WebSocket Connection is Closed and that the WebSocket connection is in the CLOSED state. If the tcp connection was closed after the WebSocket closing handshake was completed, the WebSocket connection is said to have been closed cleanly.
If the WebSocket connection could not be established, it is also said that The WebSocket Connection is Closed, but not cleanly.
As defined in Section 5.5.1 and Section 7.4, a Close control frame may contain a status code indicating a reason for closure. A closing of the WebSocket connection may be initiated by either endpoint, potentially simultaneously. The WebSocket Connection Close Code is defined as the status code (Section 7.4) contained in the first Close control frame received by the application implementing this protocol. If this Close control frame contains no status code, The WebSocket Connection Close Code is considered to be 1005. If The WebSocket Connection is Closed and no Close control frame was received by the endpoint (such as could occur if the underlying transport connection is lost), The WebSocket Connection Close Code is considered to be 1006.
NOTE: Two endpoints may not agree on the value of The WebSocket Connection Close Code. As an example, if the remote endpoint sent a Close frame but the local application has not yet read the data containing the Close frame from its socket's receive buffer, and the local application independently decided to close the connection and send a Close frame, both endpoints will have sent and received a Close frame, and will not send further Close frames. Each endpoint will see the Connection Close Code sent by the other end as the WebSocket Connection Close Code. As such, it is possible that the two endpoints may not agree on the value of The WebSocket Connection Close Code in the case that both endpoints Start the WebSocket Closing Handshake independently and at roughly the same time.
As defined in Section 5.5.1 and Section 7.4, a Close control frame may contain a status code indicating a reason for closure, followed by UTF-8 encoded data, the interpretation of said data being left to the endpoints and not defined by this protocol. A closing of the WebSocket connection may be initiated by either endpoint, potentially simultaneously. The WebSocket Connection Close Reason is defined as the UTF-8 encoded data following the status code (Section 7.4) contained in the first Close control frame received by the application implementing this protocol. If there is no such data in the Close control frame, The WebSocket Connection Close Reason is the empty string.
NOTE: Following the same logic as noted in Section 7.1.5, two endpoints may not agree on The WebSocket Connection Close Reason.
Certain algorithms and specifications require an endpoint to Fail the WebSocket Connection. To do so, the client MUST Close the WebSocket Connection, and MAY report the problem to the user (which would be especially useful for developers) in an appropriate manner. Similarly, to do so, the server MUST Close the WebSocket Connection, and SHOULD log the problem.
If The WebSocket Connection is Established prior to the point where the endpoint is required to Fail the WebSocket Connection, the endpoint SHOULD send a Close frame with an appropriate status code Section 7.4 before proceeding to Close the WebSocket Connection. An endpoint MAY omit sending a Close frame if it believes the other side is unlikely to be able to receive and process the close frame, due to the nature of the error that led to the WebSocket connection being failed in the first place. An endpoint MUST NOT continue to attempt to process data (including a responding Close frame) from the remote endpoint after being instructed to Fail the WebSocket Connection.
Except as indicated above or as specified by the application layer (e.g. a script using the WebSocket API), clients SHOULD NOT close the connection.
Certain algorithms, namely during the opening handshake, require the client to Fail the WebSocket Connection. To do so, the client MUST Fail the WebSocket Connection as defined in Section 7.1.7.
If at any point the underlying transport layer connection is unexpectedly lost, the client MUST Fail the WebSocket Connection.
Except as indicated above or as specified by the application layer (e.g. a script using the WebSocket API), clients SHOULD NOT close the connection.
Certain algorithms require or recommend that the server Abort the WebSocket Connection during the opening handshake. To do so, the server MUST simply Close the WebSocket Connection (Section 7.1.1).
Abnormal closures may be caused by any number of reasons. Such closures could be the result of a transient error, in which case reconnecting may lead to a good connection and a resumption of normal operations. Such closures may also be the result of a nontransient problem, in which case if each deployed client experiences an abnormal closure and immediately and persistently tries to reconnect, the server may experience what amounts to a denial of service attack by a large number of clients trying to reconnect. The end result of such a scenario could be that the service is unable to recover, or recovery is made much more difficult, in any sort of timely manner.
To prevent this, clients SHOULD use some form of backoff when trying to reconnect after abnormal closures as described in this section.
The first reconnect attempt SHOULD be delayed by a random amount of time. The parameters by which this random delay is chosen are left to the client to decide; a value chosen randomly between 0 and 5 seconds is a reasonable initial delay though clients MAY choose a different interval from which to select a delay length based on implementation experience and particular application.
Should the first reconnect attempt fail, subsequent reconnect attempts SHOULD be delayed by increasingly longer amounts of time, using a method such as truncated binary exponential backoff.
Servers MAY close the WebSocket connection whenever desired. Clients SHOULD NOT close the WebSocket connection arbitrarily. In either case, an endpoint initiates a closure by following the procedures to Start the WebSocket Closing Handshake (Section 7.1.2).
When closing an established connection (e.g. when sending a Close frame, after the opening handshake has completed), an endpoint MAY indicate a reason for closure. The interpretation of this reason by an endpoint, and the action an endpoint should take given this reason, are left undefined by this specification. This specification defines a set of pre-defined status codes, and specifies which ranges may be used by extensions, frameworks, and end applications. The status code and any associated textual message are optional components of a Close frame.
Endpoints MAY use the following pre-defined status codes when sending a Close frame.
When an endpoint is to interpret a byte stream as UTF-8 but finds that the byte stream is not in fact a valid UTF-8 stream, that endpoint MUST Fail the WebSocket Connection. This rule applies both during the opening handshake and during subsequent data exchange.
WebSocket clients MAY request extensions to this specification, and WebSocket servers MAY accept some or all extensions requested by the client. A server MUST NOT respond with any extension not requested by the client. If extension parameters are included in negotiations between the client and the server, those parameters MUST be chosen in accordance with the specification of the extension to which the parameters apply.
A client requests extensions by including a "Sec-WebSocket-Extensions" header field, which follows the normal rules for HTTP header fields (see [RFC2616] section 4.2) and the value of the header field is defined by the following ABNF [RFC2616]. Note that this section is using ABNF syntax/rules from [RFC2616], including "implied *LWS rule". If a value is received by either the client or the server during negotiation that does not conform to the ABNF below, the recipient of such malformed data MUST immediately Fail the WebSocket Connection.
Sec-WebSocket-Extensions = extension-list extension-list = 1#extension extension = extension-token *( ";" extension-param ) extension-token = registered-token registered-token = token extension-param = token [ "=" (token | quoted-string) ] ;When using the quoted-string syntax variant, the value ;after quoted-string unescaping MUST conform to the ;'token' ABNF.
Note that like other HTTP header fields, this header field MAY be split or combined across multiple lines. Ergo, the following are equivalent:
Sec-WebSocket-Extensions: foo Sec-WebSocket-Extensions: bar; baz=2
is exactly equivalent to
Sec-WebSocket-Extensions: foo, bar; baz=2
Any extension-token used MUST be a registered token (see Section 11.4). The parameters supplied with any given extension MUST be defined for that extension. Note that the client is only offering to use any advertised extensions, and MUST NOT use them unless the server indicates that it wishes to use the extension.
Note that the order of extensions is significant. Any interactions between multiple extensions MAY be defined in the documents defining the extensions. In the absence of such definition, the interpretation is that the header fields listed by the client in its request represent a preference of the header fields it wishes to use, with the first options listed being most preferable. The extensions listed by the server in response represent the extensions actually in use for the connection. Should the extensions modify the data and/or framing, the order of operations on the data should be assumed to be the same as the order in which the extensions are listed in the server's response in the opening handshake.
For example, if there are two extensions "foo" and "bar", if the header field |Sec-WebSocket-Extensions| sent by the server has the value "foo, bar" then operations on the data will be made as bar(foo(data)), be those changes to the data itself (such as compression) or changes to the framing that may "stack".
Non-normative examples of acceptable extension header fields (note that long lines are folded for readability):
Sec-WebSocket-Extensions: deflate-stream Sec-WebSocket-Extensions: mux; max-channels=4; flow-control, deflate-stream Sec-WebSocket-Extensions: private-extension
A server accepts one or more extensions by including a |Sec-WebSocket-Extensions| header field containing one or more extensions which were requested by the client. The interpretation of any extension parameters, and what constitutes a valid response by a server to a requested set of parameters by a client, will be defined by each such extension.
Extensions provide a mechanism for implementations to opt-in to additional protocol features. This document doesn't define any extension but implementations MAY use extensions defined separately.
This section describes some security considerations applicable to the WebSocket protocol. Specific security considerations are described in subsections of this section.
Many threats anticipated by the WebSocket protocol protect from malicious JavaScript running inside a trusted application such as a web browser, for example checking of the "Origin" header field (see below). See Section 1.6 for additional details. Such assumptions don't hold true in the case of a more capable client.
While this protocol is intended to be used by scripts in Web pages, it can also be used directly by hosts. Such hosts are acting on their own behalf, and can therefore send fake "Origin" header fields, misleading the server. Servers should therefore be careful about assuming that they are talking directly to scripts from known origins, and must consider that they might be accessed in unexpected ways. In particular, a server should not trust that any input is valid.
EXAMPLE: For example, if the server uses input as part of SQL queries, all input text should be escaped before being passed to the SQL server, lest the server be susceptible to SQL injection.
Servers that are not intended to process input from any Web page but only for certain sites SHOULD verify the "Origin" field is an origin they expect. If the origin indicated is unacceptable to the server, then it SHOULD respond to the WebSocket handshake with a reply containing HTTP 403 Forbidden status code.
The "Origin" header field protects from the attack cases when the untrusted party is typically the author of a JavaScript application that is executing in the context of the trusted client. The client itself can contact the server and via the mechanism of the "Origin" header field, determine whether to extend those communication privileges to the JavaScript application. The intent is not to prevent non-browsers from establishing connections, but rather to ensure that trusted browsers under the control of potentially malicious JavaScript cannot fake a WebSocket handshake.
In addition to endpoints being the target of attacks via WebSockets, other parts of web infrastructure, such as proxies, may be the subject of an attack.
As this protocol was being developed, an experiment was conducted to demonstrate a class of attacks on proxies that led to the poisoning of caching proxies deployed in the wild [TALKING]. The general form of the attack was to establish a connection to a server under the "attacker's" control, perform an UPGRADE on the HTTP connection similar to what the WebSocket protocol does to establish a connection, and to subsequently send data over that UPGRADEd connection that looked like a GET request for a specific known resource (which in an attack would likely be something like a widely deployed script for tracking hits, or a resource on an ad-serving network). The remote server would respond with something that looked like a response to the fake GET request, and this response would be cached by a nonzero percentage of deployed intermediaries, thus poisioning the cache. The net effect of this attack would be that if a user could be convinced to visit a website the attacker controlled, the attacker could potentially poison the cache for that user and other users behind the same cache and run malicious script on other origins, compromising the web security model.
To avoid such attacks on deployed intermediaries, it is not sufficient to prefix application supplied data with framing that is not compliant HTTP, as it is not possible to exhaustively discover and test that each nonconformant intermediary does not skip such non HTTP framing and act incorrectly on the frame payload. Thus the defence adopted is to mask all data from the client to the server, so that the remote script (attacker) does not have control over how the data being sent appears on the wire, and thus cannot construct a message that could be misinterpreted by an intermediary as an HTTP request.
Clients MUST choose a new masking key for each frame, using an algorithm that cannot be predicted by end applications that provide data. For example, each masking could be drawn from a cryptographically strong random number generator. If the same key is used, or a decipherable pattern exists for how the next key is chosen, the attacker can send a message that, when masked, could appear to be an HTTP request (by taking the message the attacker wishes to see on the wire, and masking it with the next masking key to be used, when the client applies the masking key it will effectively unmask the data.)
It is also necessary that once the transmission of a frame from a client has begun, the payload (application supplied data) of that frame must not be capable of being modified by the application. Otherwise, an attacker could send a long frame where the initial data was a known value (such as all zeros), compute the masking key being used upon receipt of the first part of the data, and then modify the data that is yet to be sent in the frame to appear as an HTTP request when masked. (This is essentially the same problem described in the previous paragraph with using a known or predictable masking key.) If additional data is to be sent or data to be sent is somehow changed, that new or changed data must be sent in a new frame and thus with a new masking key. In short, once transmission of a frame begins, the contents must not be modifiable by the remote script (application).
The threat model being protected against is one in which the client sends data that appears to be a HTTP request. As such, the channel that needs to be masked is the data from the client to the server. The data from the server to the client can be made to look like a response, but to accomplish this request the client must also be able to forge a request. As such, it was not deemed necessary to mask data in both directions (the data from the server to the client is not masked).
Despite the protection provided by masking, non-compliant HTTP proxies will still be vulnerable to poisoning attacks of this type by clients and servers that do not apply masking.
Implementations which have implementation- and/or platform-specific limitations regarding the frame size or total message size after reassembly from multiple frames MUST protect themselves against exceeding those limits. (For example, a malicious endpoint can try to exhaust its peer's memory or mount a denial of service attack by sending either a single big frame (e.g. of size 2**60), or by sending a long stream of small frames which are a part of a fragmented message.) Such an implementation SHOULD impose limit on frame sizes and the total message size after reassembly from multiple frames.
This protocol doesn't prescribe any particular way that servers can authenticate clients during the WebSocket handshake. The WebSocket server can use any client authentication mechanism available to a generic HTTP server, such as Cookies, HTTP Authentication, or TLS authentication.
Communications confidentiality and integrity is provided by running the WebSocket protocol over TLS (wss URIs). WebSocket implementations MUST support TLS, and SHOULD employ it when communicating with their peers.
For connections using TLS, the amount of benefit provided by TLS depends greatly on the strength of the algorithms negotiated during the TLS handshake. For example some TLS cipher mechanisms don't provide connection confidentiality. To achieve reasonable levels of protections, clients should use only Strong TLS algorithms. "Web Security Context: User Interface Guidelines" [W3C.REC-wsc-ui-20100812] discusses what constitutes Strong TLS algorithms. [RFC5246] provides additional guidance in Appendix A.5 and Appendix D.3.
Incoming data MUST always be validated by both clients and servers. If at any time an endpoint is faced with data that it does not understand, or that violates some criteria by which the endpoint determines safety of input, or when the endpoint sees an opening handshake that does not correspond to the values it is expecting (e.g. incorrect path or origin in the client request), the endpoint MAY drop the TCP connection. If the invalid data received after a successful WebSocket handshake, the endpoint SHOULD send a Close frame with an appropriate status code Section 7.4 before proceeding to Close the WebSocket Connection. Use of a Close frame with an appropriate status code can help in diagnosing the problem. If the invalid data is sent during the WebSocket handshake the server SHOULD return an appropriate HTTP [RFC2616] status code.
A common class of security problems arise when sending text data using using the wrong encoding. This protocol specifies that messages with a Text data type (as opposed to Binary or other types) contain UTF-8 encoded data. Although the length is still indicated and applications implementing this protocol should use the length to determine where the frame actually ends, sending data in an improper encoding may still break assumptions applications built on top of this protocol may make, leading from anything to misinterpretation of data to loss of data to potential security bugs.
The WebSocket handshake described in this document doesn't depend on any security properties of SHA-1, such as collision resistance or resistance to the second pre-image attack (as described in [RFC4270]).
"ws:" "//" authority path-abempty [ "?" query ]
A |ws| URI identifies a WebSocket server and resource name.
"wss:" "//" authority path-abempty [ "?" query ]
A |wss| URI identifies a WebSocket server and resource name, and indicates that traffic over that connection is to be protected via TLS (including standard benefits of TLS such as data confidentiality and integrity, and endpoint authentication).
This section defines a keyword for registration in the "HTTP Upgrade Tokens" registry as per RFC 2817 [RFC2817].
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Key| header field is used in the WebSocket opening handshake. It is sent from the client to the server to provide part of the information used by the server to prove that it received a valid WebSocket opening handshake. This helps ensure that the server does not accept connections from non-WebSocket clients (e.g. HTTP clients) that are being abused to send data to unsuspecting WebSocket servers.
The |Sec-WebSocket-Key| header field MUST NOT appear more than once in an HTTP request.
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Extensions| header field is used in the WebSocket opening handshake. It is initially sent from the client to the server, and then subsequently sent from the server to the client, to agree on a set of protocol-level extensions to use for the duration of the connection.
The |Sec-WebSocket-Extensions| header field MAY appear multiple times in an HTTP request (which is logically the same as a single |Sec-WebSocket-Extensions| header field that contains all values. However the |Sec-WebSocket-Extensions| header field MUST NOT appear more than once in an HTTP response.
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Accept| header field is used in the WebSocket opening handshake. It is sent from the server to the client to confirm that the server is willing to initiate the WebSocket connection.
The |Sec-WebSocket-Accept| header MUST NOT appear more than once in an HTTP response.
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Protocol| header field is used in the WebSocket opening handshake. It is sent from the client to the server and back from the server to the client to confirm the subprotocol of the connection. This enables scripts to both select a subprotocol and be sure that the server agreed to serve that subprotocol.
The |Sec-WebSocket-Protocol| header field MAY appear multiple times in an HTTP request (which is logically the same as a single |Sec-WebSocket-Protocol| header field that contains all values. However the |Sec-WebSocket-Protocol| header field MUST NOT appear more than once in an HTTP response.
This section describes a header field for registration in the Permanent Message Header Field Registry [RFC3864].
The |Sec-WebSocket-Version| header field is used in the WebSocket opening handshake. It is sent from the client to the server to indicate the protocol version of the connection. This enables servers to correctly interpret the opening handshake and subsequent data being sent from the data, and close the connection if the server cannot interpret that data in a safe manner. The |Sec-WebSocket-Version| header field is also sent from the server to the client on WebSocket handshake error, when the version received from the client does not match a version understood by the server. In such a case the header field includes the protocol version(s) supported by the server.
Note that there is no expectation that higher version numbers are necessarily backward compatible with lower version numbers.
The |Sec-WebSocket-Version| header field MAY appear multiple times in an HTTP response (which is logically the same as a single |Sec-WebSocket-Version| header field that contains all values. However the |Sec-WebSocket-Version| header field MUST NOT appear more than once in an HTTP request.
This specification requests the creation of a new IANA registry for WebSocket Extension names to be used with the WebSocket protocol in accordance with the principles set out in RFC 5226 [RFC5226].
As part of this registry IANA will maintain the following information:
WebSocket Extension names are to be subject to "First Come First Served" IANA registration policy [RFC5226].
There are no initial values in this registry.
This specification requests the creation of a new IANA registry for WebSocket Subprotocol names to be used with the WebSocket protocol in accordance with the principles set out in RFC 5226 [RFC5226].
As part of this registry IANA will maintain the following information:
WebSocket Subprotocol names are to be subject to "First Come First Served" IANA registration policy [RFC5226].
This specification requests the creation of a new IANA registry for WebSocket Version Numbers to be used with the WebSocket protocol in accordance with the principles set out in RFC 5226 [RFC5226].
As part of this registry IANA will maintain the following information:
WebSocket Version Numbers are to be subject to "IETF Review" IANA registration policy [RFC5226]. In order to improve interoperability with intermediate versions published in Internet Drafts, version numbers associated with such drafts might be registered in this registry. Note that "IETF Review" applies to registrations corresponding to Internet Drafts.
Version Number | Reference -+----------------+-----------------------------------------+- | 0 + draft-ietf-hybi-thewebsocketprotocol-00 | -+----------------+-----------------------------------------+- | 1 + draft-ietf-hybi-thewebsocketprotocol-01 | -+----------------+-----------------------------------------+- | 2 + draft-ietf-hybi-thewebsocketprotocol-02 | -+----------------+-----------------------------------------+- | 3 + draft-ietf-hybi-thewebsocketprotocol-03 | -+----------------+-----------------------------------------+- | 4 + draft-ietf-hybi-thewebsocketprotocol-04 | -+----------------+-----------------------------------------+- | 5 + draft-ietf-hybi-thewebsocketprotocol-05 | -+----------------+-----------------------------------------+- | 6 + draft-ietf-hybi-thewebsocketprotocol-06 | -+----------------+-----------------------------------------+- | 7 + draft-ietf-hybi-thewebsocketprotocol-07 | -+----------------+-----------------------------------------+- | 8 + draft-ietf-hybi-thewebsocketprotocol-08 | -+----------------+-----------------------------------------+- | 9 + Reserved | -+----------------+-----------------------------------------+- | 10 + Reserved | -+----------------+-----------------------------------------+- | 11 + Reserved | -+----------------+-----------------------------------------+- | 12 + Reserved | -+----------------+-----------------------------------------+- | 13 + RFCXXXX (RFC Editor: please update) | -+----------------+-----------------------------------------+-
IANA is asked to add initial values to the registry, with suggested numerical values as these have been used in past versions of this protocol.
This specification requests the creation of a new IANA registry for WebSocket Connection Close Code Numbers in accordance with the principles set out in RFC 5226 [RFC5226].
As part of this registry IANA will maintain the following information:
WebSocket Close Code Numbers are to be subject to different registration requirements depending on their range. Unless otherwise specified, requests are subject to "Standards Action" IANA registration policy [RFC5226]. Requests for status codes for use by this protocol, its subsequent versions or extensions are subject to any one of "Standards Action", "Specification Required" (which implies "Designated Expert") or "IESG Review" IANA registration policies and should be granted status codes in the range 1000-2999. Requests for status codes for use by libraries, frameworks and applications are subject to "First Come First Served" IANA registration policy and should be granted in the range 3000-3999. The range of status codes from 4000-4999 is designated for Private Use. Requests should indicate whether they are requesting status codes for use by the WebSocket protocol (or a future version of the protocol) or by extensions, or by libraries/frameworks/applications.
|Status Code | Meaning | Contact | Reference | -+------------+-----------------+---------------+-----------| | 1000 | Normal Closure | hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------| | 1001 | Going Away | hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------| | 1002 | Protocol error | hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------| | 1003 | Unsupported Data| hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------| | 1004 | ---Reserved---- | hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------| | 1005 | No Status Rcvd | hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------| | 1006 | Abnormal Closure| hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------| | 1007 | Invalid frame | hybi@ietf.org | RFC XXXX | | | payload data | | | -+------------+-----------------+---------------+-----------| | 1008 | Policy Violation| hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------| | 1009 | Message Too Big | hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------| | 1010 | Mandatory Ext. | hybi@ietf.org | RFC XXXX | -+------------+-----------------+---------------+-----------|
IANA is asked to add initial values to the registry, with suggested numerical values as these have been used in past versions of this protocol.
This specification requests the creation of a new IANA registry for WebSocket Opcodes in accordance with the principles set out in RFC 5226 [RFC5226].
As part of this registry IANA will maintain the following information:
WebSocket Opcode numbers are subject to "Standards Action" IANA registration policy [RFC5226].
|Opcode | Meaning | Reference | -+--------+-------------------------------------+-----------| | 0 | Continuation Frame | RFC XXXX | -+--------+-------------------------------------+-----------| | 1 | Text Frame | RFC XXXX | -+--------+-------------------------------------+-----------| | 2 | Binary Frame | RFC XXXX | -+--------+-------------------------------------+-----------| | 8 | Connection Close Frame | RFC XXXX | -+--------+-------------------------------------+-----------| | 9 | Ping Frame | RFC XXXX | -+--------+-------------------------------------+-----------| | 10 | Pong Frame | RFC XXXX | -+--------+-------------------------------------+-----------|
IANA is asked to add initial values to the registry, with suggested numerical values as these have been used in past versions of this protocol.
This specification requests the creation of a new IANA registry for WebSocket Framing Header Bits in accordance with the principles set out in RFC 5226 [RFC5226]. This registry controls assignment of the bits marked RSV1, RSV2, and RSV3 in Section 5.2.
These bits are reserved for future versions or extensions of this specification.
WebSocket Framing Header Bits assignments are subject to "Standards Action" IANA registration policy [RFC5226].
The WebSocket protocol is intended to be used by another specification to provide a generic mechanism for dynamic author-defined content, e.g. in a specification defining a scripted API.
Such a specification first needs to Establish a WebSocket Connection, providing that algorithm with:
The /host/, /port/, /resource name/, and /secure/ flag are usually obtained from a URI using the steps to parse a WebSocket URI's components. These steps fail if the URI does not specify a WebSocket.
If at any time the connection is to be closed, then the specification needs to use the Close the WebSocket Connection algorithm (Section 7.1.1).
Section 7.1.4 defines when The WebSocket Connection is Closed.
While a connection is open, the specification will need to handle the cases when A WebSocket Message Has Been Received (Section 6.2).
To send some data /data/ to an open connection, the specification needs to Send a WebSocket Message (Section 6.1).
Special thanks are due to Ian Hickson, who was the original author and editor of this protocol. The initial design of this specification benefitted from the participation of many people in the WHATWG and WHATWG mailing list. Contributions to that specification are not tracked by section, but a list of all who contributed to that specification is given in the WHATWG HTML specification at http://whatwg.org/html5.
Special thanks also to John Tamplin for providing a significant amount of text for the Data Framing section of this specification.
Special thanks also to Adam Barth for providing a significant amount of text and background research for the Data Masking section of this specification.
Special thanks to Lisa Dusseault for the Apps Area review (and for helping to start this work), Richard Barnes for the Gen-Art review and Magnus Westerlund for the Transport Area Review. Special thanks to HYBI WG past and present WG chairs who tirelessly worked behind the scene to move this work toward completion: Joe Hildebrand, Salvatore Loreto and Gabriel Montenegro. And last but not least, special thank you to the responsible Area Director Peter Saint-Andre.
Thank you to the following people who participated in discussions on the HYBI WG mailing list and contributed ideas and/or provided detailed reviews (the list is likely to be incomplete): Greg Wilkins, John Tamplin, Willy Tarreau, Maciej Stachowiak, Jamie Lokier, Scott Ferguson, Bjoern Hoehrmann, Julian Reschke, Dave Cridland, Andy Green, Eric Rescorla, Inaki Baz Castillo, Martin Thomson, Roberto Peon, Patrick McManus, Zhong Yu, Bruce Atherton, Takeshi Yoshino, Martin J. Duerst, James Graham, Simon Pieters, Roy T. Fielding, Mykyta Yevstifeyev, Len Holgate, Paul Colomiets, Piotr Kulaga, Brian Raymor, Jan Koehler, Joonas Lehtolahti, Sylvain Hellegouarch, Stephen Farrell, Sean Turner, Pete Resnick, Peter Thorson, Joe Mason, John Fallows, Alexander Philippou. Note that people listed above didn't necessarily endorse the end result of this work.