58 Commits

Author SHA1 Message Date
James Coglan c7e1f86979 Run tests on some additional Ruby versions 2023-09-07 19:31:30 +01:00
James Coglan 3c0fda5b21 Test on Ruby 3.1 2022-02-06 23:44:56 +00:00
James Coglan 5891358639 As of rspec-mocks v3.10.3, mock expectations need to use explicit hashes to avoid confusion with keyword args 2022-02-06 23:36:18 +00:00
James Coglan 84b869effb Switch from Travis CI to GitHub Actions 2021-05-18 00:41:08 +01:00
James Coglan 6ea62a9965 Enable Bundler caching for Travis 2021-03-12 16:37:55 +00:00
James Coglan 1d80737dce Update Ruby versions on Travis 2021-02-23 02:37:51 +00:00
James Coglan a0f23c5d3c Merge pull request #7 from timcraft/patch-1
Test on ruby 3.0
2021-01-06 15:32:47 +00:00
timcraft 8b6c706737 Test on ruby 3.0 2021-01-06 14:56:23 +00:00
James Coglan 402222fdd9 Bump version to 0.1.5 0.1.5 2020-06-02 13:52:20 +01:00
James Coglan aa156a439d 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-7663.

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:51:02 +01:00
James Coglan 8174a4a0f9 Test on JRuby 9.{0,1,2} rather than "head" 2020-05-14 23:30:22 +01:00
James Coglan 96059802a6 Update Ruby versions 2.4 to 2.7 on Travis 2020-05-14 17:20:12 +01:00
James Coglan bd6d0acc01 Mention license change in the changelog 2019-06-13 11:40:37 +01:00
James Coglan a8c847876b Formatting change: {...} should have spaces inside the braces 2019-06-11 16:08:53 +01:00
James Coglan 242f007cb9 Bump version to 0.1.4 0.1.4 2019-06-10 12:34:21 +01:00
James Coglan 249bcc106f Change markdown formatting of docs. 2019-05-29 15:33:26 +01:00
wilkie 738a0af1dd Fix deprecation warning about =~ being called on TrueClass.
With the code as it was, it will generate a warning like such:

.../websocket-extensions-0.1.3/lib/websocket/extensions/parser.rb:41:
warning: deprecated Object#=~ is called on TrueClass; it always returns
nil

So we will avoid if the variable is TrueClass.
2019-05-24 14:42:31 +01:00
James Coglan 68c1b21129 Fix RSpec warnings about raise_error with no arguments. 2019-05-24 14:42:31 +01:00
James Coglan 8ee09b2924 Update Travis target versions. 2019-05-24 14:42:31 +01:00
James Coglan 7e64cfb6ca Switch license to Apache 2.0. 2019-05-24 14:00:37 +01:00
James Coglan 1a441fac80 Test on Ruby 2.5.0. 2018-02-19 21:03:05 +00:00
James Coglan 60318127e2 Bump version to 0.1.3. 0.1.3 2017-11-11 01:26:32 +00:00
James Coglan 392c672dd8 Bump Ruby versions on Travis. 2017-11-11 01:14:45 +00:00
James Coglan 7ef1451923 Header parser should accept uppercase letters. 2017-11-11 01:04:05 +00:00
James Coglan 76dbd000d4 Fix the version of Rubygems that Travis is using on Ruby 1.9. 2017-09-11 22:23:05 +01:00
James Coglan 4e7ae0b1f2 Use an https: link to github in the gemspec. 2017-09-11 22:02:09 +01:00
James Coglan ff9af49c92 Merge pull request #4 from EdwardBetts/spelling
correct spelling mistake
2017-09-02 12:16:53 +01:00
Edward Betts c44d436241 correct spelling mistake 2017-09-02 07:47:34 +01:00
James Coglan 7b2ecb164c Update Ruby versions in Travis config. 2017-08-01 20:34:57 +01:00
James Coglan 8592fb0288 Merge pull request #3 from junaruga/feature/travis-ruby-head
Add ruby24 and ruby head to Travis CI
2017-04-14 10:32:22 +01:00
Jun Aruga 7fe88150b2 Add Ruby 2.4.1 to .travis.yml 2017-04-03 19:03:27 +02:00
James Coglan 14967f5e89 Bump Ruby versions 2.2 and 2.3 on Travis. 2016-11-21 23:22:26 +00:00
James Coglan afe3c6c6aa Remove Rubinius from the build as it hasn't worked on Travis for weeks. 2016-10-26 09:14:20 +01:00
James Coglan 125807a973 Update Ruby versions for Travis. 2016-04-30 13:10:54 +01:00
James Coglan 92cead666f Update the Ruby versions used on Travis. 2016-03-23 09:13:20 +00:00
Jun Aruga 5b54b66cc1 Separate the license text 2016-03-23 09:08:18 +00:00
James Coglan 3d143d2c3e Create CODE_OF_CONDUCT.md. 2015-11-08 12:16:52 +00:00
James Coglan bb1179bc8c Update Rubies for Travis: 2.1.7, 2.2.3, jruby-9, rbx-2.5. 2015-10-17 13:19:33 +01:00
James Coglan 033020e78a Test on Ruby 2.2.1. 2015-03-26 09:46:03 +00:00
James Coglan cc1d6196fe 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 09:32:54 +00:00
James Coglan b9da251798 If the header from the client is invalid, just ignore it and build a pipeline with no sessions. 2015-03-14 12:56:48 +00:00
James Coglan dd6cff6c09 Test on Ruby 2.1.5 and 2.2.0. 0.1.2 2015-02-19 21:06:45 +00:00
James Coglan c73f432841 Bump version to 0.1.2. 2015-02-19 20:58:30 +00:00
James Coglan 88abae45d2 Make Extensions#close safe to call if @sessions is not yet initialised, in case the socket is closed before handshaking. 2015-02-16 09:54:08 +00:00
James Coglan 7e29a5c63e Bump version to 0.1.1. 0.1.1 2014-12-14 12:25:17 +00:00
James Coglan 3a3b0c537c Merge pull request #1 from Juanmcuello/require-strscan
Require 'strscan' to be sure StringScanner will be present.
2014-12-14 12:18:23 +00:00
Juan M. Cuello fb23226189 Require 'strscan' to be sure StringScanner will be present. 2014-12-13 21:08:45 -03:00
James Coglan 026721dbdc Don't delegate valid_frame_rsv 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:13 +00:00
James Coglan aa73df3670 Don't test on jruby-18mode. 2014-12-09 22:22:25 +00:00
James Coglan 4a093550c2 Prefix extension error messages with the name of the extension. 2014-12-08 00:55:11 +00:00