58 Commits

Author SHA1 Message Date
James Coglan ff2a854ead Test on recent versions of Node 2023-09-07 19:23:46 +01:00
James Coglan 7ad3317a26 Switch from Travis CI to GitHub Actions 2021-05-18 22:12:02 +01:00
James Coglan 0ae6ad9a9d Travis update: cache npm modules, remove sudo, run on Node 15 2021-03-12 21:36:56 +00:00
James Coglan 5ea0b42080 Bump version to 0.1.4 0.1.4 2020-06-02 13:52:07 +01:00
James Coglan 29496f6838 Remove ReDoS vulnerability in the Sec-WebSocket-Extensions header parser
There is a regular expression denial of service (ReDoS) vulnerability in
the parser we use to process the `Sec-WebSocket-Extensions` header. It
can be exploited by sending an opening WebSocket handshake to a server
containing a header of the form:

    Sec-WebSocket-Extensions: a;b="\c\c\c\c\c\c\c\c\c\c ...

i.e. a header containing an unclosed string parameter value whose
content is a repeating two-byte sequence of a backslash and some other
character. The parser takes exponential time to reject this header as
invalid, and this can be used to exhaust the server's capacity to
process requests.

This vulnerability has been assigned the identifier CVE-2020-7662 and
was reported by Robert McLaughlin.

We believe this flaw stems from the grammar specified for this header.
[RFC 6455][1] defines the grammar for the header as:

    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) ]

It refers to [RFC 2616][2] for the definitions of `token` and
`quoted-string`, which are:

    token          = 1*<any CHAR except CTLs or separators>
    separators     = "(" | ")" | "<" | ">" | "@"
                   | "," | ";" | ":" | "\" | <">
                   | "/" | "[" | "]" | "?" | "="
                   | "{" | "}" | SP | HT

    quoted-string  = ( <"> *(qdtext | quoted-pair ) <"> )
    qdtext         = <any TEXT except <">>
    quoted-pair    = "\" CHAR

These rely on the `CHAR`, `CTL` and `TEXT` grammars, which are:

    CHAR           = <any US-ASCII character (octets 0 - 127)>
    CTL            = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
    TEXT           = <any OCTET except CTLs, but including LWS>

Other relevant definitions to support these:

    OCTET          = <any 8-bit sequence of data>
    LWS            = [CRLF] 1*( SP | HT )
    CRLF           = CR LF

    HT             = <US-ASCII HT, horizontal-tab (9)>
    LF             = <US-ASCII LF, linefeed (10)>
    CR             = <US-ASCII CR, carriage return (13)>
    SP             = <US-ASCII SP, space (32)>

To expand some of these terms out and write them as regular expressions:

    OCTET         = [\x00-\xFF]
    CHAR          = [\x00-\x7F]
    TEXT          = [\t \x21-\x7E\x80-\xFF]

The allowable bytes for `token` are [\x00-\x7F], except [\x00-\x1F\x7F]
(leaving [\x20-\x7E]) and `separators`, which leaves the following set
of allowed chars:

    ! # $ % & ' * + - . ^ _ ` | ~ [0-9] [A-Z] [a-z]

`quoted-string` contains a repeated pattern of either `qdtext` or
`quoted-pair`. `qdtext` is any `TEXT` byte except <">, and the <">
character is ASCII 34, or 0x22. The <!> character is 0x21. So `qdtext`
can be written either positively as:

    qdtext        = [\t !\x23-\x7E\x80-\xFF]

or negatively, as:

    qdtext        = [^\x00-\x08\x0A-\x1F\x7F"]

We use the negative definition here. The other alternative in the
`quoted-string` pattern is:

    quoted-pair   = \\[\x00-\x7F]

The problem is that the set of bytes matched by `qdtext` includes <\>,
and intersects with the second element of `quoted-pair`. That means the
sequence \c can be matched as either two `qdtext` bytes, or as a single
`quoted-pair`. When the regex engine fails to find a trailing <"> to
close the string, it back-tracks and tries every alternate parse for the
string, which doubles with each pair of bytes in the input.

To fix the ReDoS flaw we need to rewrite the repeating pattern so that
none of its alternate branches can match the same text. For example, we
could try dividing the set of bytes [\x00-\xFF] into those that must not
follow a <\>, those that may follow a <\>, and those that must be
preceded by <\>, and thereby construct a pattern of the form:

    (A|\?B|\C)*

where A, B and C have no characters in common. In our case the three
branch patterns would be:

    A   =   qdtext - CHAR   =   [\x80-\xFF]
    B   =   qdtext & CHAR   =   [\t !\x23-\x7E]
    C   =   CHAR - qdtext   =   [\x00-\x08\x0A-\x1F\x7F"]

These sets do not intersect, and notice <"> appears in set C so must be
preceded by <\>. But we still have a problem: <\> (0x5C) and all the
alphabetic characters are in set B, so the pattern \?B can match all
these:

    c
    \
    \c

So the sequence \c\c\c... still produces exponential back-tracking. It
also fails to parse input like this correctly:

    Sec-WebSocket-Extensions: a; b="c\", d"

Because the grammar allows a single backslash to appear by itself, this
is arguably a syntax error where the parameter `b` has value `c\` and
then a new extension `d` begins with a <"> appearing where it should
not.

So the core problem is with the grammar itself: `qdtext` matches a
single backslash <\>, and `quoted-pair` matches a pair <\\>. So given a
sequence of backslashes there's no canonical parse and the grammar is
ambiguous.

[RFC 7230][3] remedies this problem and makes the grammar clearer.
First, it defines `token` explicitly rather than implicitly:

    token          = 1*tchar

    tchar          = "!" / "#" / "$" / "%" / "&" / "'" / "*"
                   / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
                   / DIGIT / ALPHA

And second, it defines `quoted-string` so that backslashes cannot appear
on their own:

     quoted-string  = DQUOTE *( qdtext / quoted-pair ) DQUOTE
     qdtext         = HTAB / SP /%x21 / %x23-5B / %x5D-7E / obs-text
     obs-text       = %x80-FF
     quoted-pair    = "\" ( HTAB / SP / VCHAR / obs-text )

where VCHAR is any printing ASCII character 0x21-0x7E. Notice `qdtext`
is just our previous definition but with 5C excluded, so it cannot
accept a single backslash.

This commit makes this modification to our matching patterns, and
thereby removes the ReDoS vector. Technically this means it does not
match the grammar of RFC 6455, but we expect this to have little or no
practical impact, especially since the one main protocol extension,
`permessage-deflate` ([RFC 7692][4]), does not have any string-valued
parameters.

[1]: https://tools.ietf.org/html/rfc6455#section-9.1
[2]: https://tools.ietf.org/html/rfc2616#section-2.2
[3]: https://tools.ietf.org/html/rfc7230#section-3.2.6
[4]: https://tools.ietf.org/html/rfc7692
2020-06-02 13:26:04 +01:00
James Coglan 4a76c75efb Add Node versions 13 and 14 on Travis 2020-05-14 23:52:32 +01:00
James Coglan 44a677a9c0 Formatting change: {...} should have spaces inside the braces 2019-06-11 15:54:09 +01:00
James Coglan f6c50aba0c Let npm reformat package.json 2019-06-10 12:25:53 +01:00
James Coglan 2d211f3705 Change markdown formatting of docs. 2019-05-29 15:38:13 +01:00
James Coglan 0b620834cc Update Travis target versions. 2019-05-24 14:05:57 +01:00
James Coglan 729a465307 Switch license to Apache 2.0. 2019-05-24 13:59:25 +01:00
James Coglan 2af2c18251 Bump version to 0.1.3. 0.1.3 2017-11-11 01:24:29 +00:00
James Coglan 50bcedda78 Test on Node v9. 2017-11-11 01:05:57 +00:00
James Coglan e3aa5246d6 Avoid errors caused by extension names or parameters having names that clash with things in Object.prototype. 2017-11-11 00:45:16 +00:00
James Coglan 1e58c148cb Header parser should accept uppercase letters. 2017-11-11 00:44:44 +00:00
James Coglan 5f040a15af Bump version to 0.1.2. 0.1.2 2017-09-10 17:48:15 +01:00
James Coglan 654d9b0acc Move the license into its own file. 2017-09-10 17:44:46 +01:00
James Coglan c37d0611c7 Use package.json instead of .npmignore to set files in the package. 2017-09-10 17:44:00 +01:00
James Coglan d8d38e54e6 Catch synchronous errors thrown by extensions. 2017-09-08 21:18:07 +01:00
James Coglan fb84d36546 Fix a couple of race conditions in Pipeline.
While improving error handling, I found two situations where Pipeline
fails to close() correctly because it believes there are more messages
to emit.

The first is fixed by the change to `Cell.pending()`. If another message
is pushed into the pipeline after one of the cells has stopped, all the
cells get their pending count bumped. However, this new message will
never enter the queue inside the stopped cell, so its pending count will
never reach zero. So, we only increment the pending count for cells that
are not stopped.

The second is fixed by the change to `Functor._flushQueue()`. Say a cell
processes two messages in turn, M1 and M2. Both begin being processed
before either returns a result. M1 generates an error, while processing
of M2 never completes. In this situation, the error should indicate the
end of the stream but because M2 never completes, the pending count
never reaches zero. So, if we see a record with an error, we should
truncate the queue and this point and set pending=0 so the functor is
considered complete.
2017-09-08 20:59:13 +01:00
James Coglan 36cc2c5c73 Correct a spelling error in the spec. 2017-09-02 12:18:05 +01:00
James Coglan b315aa08d6 Drop testing for io.js releases, which barely anybody is still using. 2017-08-01 23:48:09 +01:00
James Coglan b23eb5b890 Drop support for Node 0.6, add Node 7 and 8. 2017-08-01 00:53:15 +01:00
James Coglan 7319766a5e Remove non-breaking spaces from README. 2016-10-08 03:09:55 +01:00
James Coglan 9418affa01 Test on Node 6.0. 2016-04-30 13:08:34 +01:00
James Coglan b27d4cebf8 Create CODE_OF_CONDUCT.md. 2015-11-08 12:16:15 +00:00
James Coglan 2792339b4d Add a missing semicolon. 2015-11-06 22:10:26 +00:00
James Coglan 5e5f1f454f Test on Node 5. 2015-11-05 21:22:19 +00:00
James Coglan 2365c0aef2 Use Travis containers. 2015-10-17 12:54:29 +01:00
James Coglan 6669e323c3 Test on major versions of iojs and node 4. 2015-10-17 12:50:54 +01:00
James Coglan c8f31cc1c7 Reversing the previous commit; generateResponse() should throw on invalid heders (as should activate()), because the server should fail the connection in this event. 2015-03-26 08:30:23 +00:00
James Coglan 62ac506b80 If the header from the client is invalid, just ignore it and build a pipeline with no sessions. 2015-03-14 12:56:41 +00:00
James Coglan 89104ddd48 Bump version to 0.1.1. 0.1.1 2015-02-19 09:35:51 +00:00
James Coglan 2a39f8eaac Test on Node 0.12 and io.js. 2015-02-19 09:25:34 +00:00
James Coglan eb6718f6c8 Fix some alignment from where I renamed a variable. 2015-02-16 19:50:40 +00:00
James Coglan a04471aecc If Extensions.close() is called before the handshake is done, don't throw an error, just callback immediately. 2015-02-16 08:53:42 +00:00
James Coglan 6a9e862c07 Correct typos pointed out by @glasser in https://github.com/faye/websocket-extensions-node/commit/1b77290e8279f654089eac963942309fb19b9312#commitcomment-9753067 2015-02-16 08:10:12 +00:00
James Coglan 1b77290e82 Put a structure in place to preserve ordering of messages and errors, and defer closing of sessions while messages are still in the pipeline.
This is designed to address these issues:

* https://github.com/faye/permessage-deflate-node/issues/1
* https://github.com/faye/permessage-deflate-node/pull/3
* https://github.com/faye/websocket-driver-node/issues/11
2015-02-15 13:14:22 +00:00
James Coglan ea98426dc1 Don't delegate validFrameRsv() to extensions; it's a weird interface and we can figure it out from the extension metadata. 0.1.0 2014-12-12 00:54:39 +00:00
James Coglan 007cc0ac22 Prefix extension error messages with the name of the extension. 2014-12-08 00:54:03 +00:00
James Coglan bd986843a9 Update the interface we expect for Message objects. They no longer expose the frame structure because extensions should not be required to leave frame boundaries intact. 2014-12-06 22:09:17 +00:00
James Coglan 97e453781e Document the framework's requirements on message ordering. 2014-12-02 23:38:14 +00:00
James Coglan a28cdfdb1a Make sure every session processes messages in both directions in the same order the messages arrive at the driver. 2014-12-01 22:03:49 +00:00
James Coglan 2b45d787e7 Some fixes discovered while porting to Ruby. 2014-12-01 21:02:08 +00:00
James Coglan 94f2cd0806 Add the Travis badge. 2014-11-29 00:57:57 +00:00
James Coglan 7dfe2520a9 I forgot to add the 'type' field to the extensions in the tests. 2014-11-29 00:54:01 +00:00
James Coglan c16b00cb1f Fix the Extension definition table. 2014-11-29 00:50:49 +00:00
James Coglan 82078c49a2 Hope GitHub likes these tables. 2014-11-29 00:47:36 +00:00
James Coglan 80a7c2211a Add a close() method to sessions. 2014-11-29 00:43:41 +00:00
James Coglan c1adebdb04 Document the API protocols in the README. 2014-11-29 00:36:09 +00:00