websocket-extensions 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/LICENSE.md +1 -1
- data/README.md +24 -11
- data/lib/websocket/extensions/parser.rb +57 -5
- data/lib/websocket/extensions.rb +33 -6
- metadata +3 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ff7dd6585eeee5f383ca0d412897efd6a39f5d31585df489554fe86fff60421b
|
|
4
|
+
data.tar.gz: 9103fcfe6b2460639c779be4bbab87e3c9e59976b689479b1bef491406a34466
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e4c6a52ab006403de6ad226055c9bd7c6029ccb87996408914481d20be222f1f0073125eebc288c12dfb5bf7726a6054e02af93e450248111905f3d61d3c79ba
|
|
7
|
+
data.tar.gz: 73b36ded049a76a8229886c731e068f2fb0fa5a97d15a1dd487e3c43ad3215fcde82500ed23772cebfc5c7f29614deb751f97440a698b42e2699793183943ff5
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
### 0.2.0 / 2026-11-16
|
|
2
|
+
|
|
3
|
+
- Add an `options` argument to the `WebSocket::Extensions.new` constructor, to
|
|
4
|
+
provide configuration parameters that should be passed on to individual
|
|
5
|
+
extensions
|
|
6
|
+
- Extend the header field parser to handle whitespace as required by the spec
|
|
7
|
+
|
|
1
8
|
### 0.1.5 / 2020-06-02
|
|
2
9
|
|
|
3
10
|
- Remove a ReDoS vulnerability in the header parser (CVE-2020-7663)
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# websocket-extensions
|
|
1
|
+
# websocket-extensions
|
|
2
2
|
|
|
3
3
|
A minimal framework that supports the implementation of WebSocket extensions in
|
|
4
4
|
a way that's decoupled from the main protocol. This library aims to allow a
|
|
@@ -82,6 +82,14 @@ exts = WebSocket::Extensions.new
|
|
|
82
82
|
exts.add(PermessageDeflate)
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
The `WebSocket::Extensions.new` constructor takes an optional argument, which is
|
|
86
|
+
a hash containing options that will be passed down to individual extensions. The
|
|
87
|
+
supported options are:
|
|
88
|
+
|
|
89
|
+
- `:max_length` - the maximum allowed size of incoming message frames, in bytes.
|
|
90
|
+
Individual extensions should ensure that messages do not exceed this size, or
|
|
91
|
+
allocate memory beyond this amount, as a result of processing the message.
|
|
92
|
+
|
|
85
93
|
In the following examples, `exts` refers to this `Extensions` instance.
|
|
86
94
|
|
|
87
95
|
#### Client sessions
|
|
@@ -205,18 +213,21 @@ An `Extension` is any object that has the following properties:
|
|
|
205
213
|
It must also implement the following methods:
|
|
206
214
|
|
|
207
215
|
```rb
|
|
208
|
-
ext.create_client_session
|
|
216
|
+
ext.create_client_session(options)
|
|
209
217
|
```
|
|
210
218
|
|
|
211
|
-
This returns a *ClientSession*, whose interface is defined below.
|
|
219
|
+
This returns a *ClientSession*, whose interface is defined below. `options` is
|
|
220
|
+
the value of the optional argument that was passed to
|
|
221
|
+
`WebSocket::Extensions.new`.
|
|
212
222
|
|
|
213
223
|
```rb
|
|
214
|
-
ext.create_server_session(offers)
|
|
224
|
+
ext.create_server_session(offers, options)
|
|
215
225
|
```
|
|
216
226
|
|
|
217
227
|
This takes an array of offer params and returns a *ServerSession*, whose
|
|
218
|
-
interface is defined below.
|
|
219
|
-
|
|
228
|
+
interface is defined below. `options` is the value of the optional argument that
|
|
229
|
+
was passed to `WebSocket::Extensions.new`. For example, if the client handshake
|
|
230
|
+
contains the offer header:
|
|
220
231
|
|
|
221
232
|
```
|
|
222
233
|
Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover; server_max_window_bits=8, \
|
|
@@ -229,7 +240,7 @@ then the `permessage-deflate` extension will receive the call:
|
|
|
229
240
|
ext.create_server_session([
|
|
230
241
|
{ 'server_no_context_takeover' => true, 'server_max_window_bits' => 8 },
|
|
231
242
|
{ 'server_max_window_bits' => 15 }
|
|
232
|
-
])
|
|
243
|
+
], options)
|
|
233
244
|
```
|
|
234
245
|
|
|
235
246
|
The extension must decide which set of parameters it wants to accept, if any,
|
|
@@ -238,8 +249,9 @@ otherwise.
|
|
|
238
249
|
|
|
239
250
|
#### *ClientSession*
|
|
240
251
|
|
|
241
|
-
A *ClientSession* is the type returned by `ext.create_client_session`.
|
|
242
|
-
implement the following methods, as well as the *Session* API listed
|
|
252
|
+
A *ClientSession* is the type returned by `ext.create_client_session(options)`.
|
|
253
|
+
It must implement the following methods, as well as the *Session* API listed
|
|
254
|
+
below.
|
|
243
255
|
|
|
244
256
|
```rb
|
|
245
257
|
client_session.generate_offer
|
|
@@ -265,8 +277,9 @@ the framework will interpret this as the client rejecting the response, and will
|
|
|
265
277
|
|
|
266
278
|
#### *ServerSession*
|
|
267
279
|
|
|
268
|
-
A *ServerSession* is the type returned by `ext.create_server_session(offers
|
|
269
|
-
must implement the following methods, as well as the *Session* API
|
|
280
|
+
A *ServerSession* is the type returned by `ext.create_server_session(offers,
|
|
281
|
+
options)`. It must implement the following methods, as well as the *Session* API
|
|
282
|
+
listed below.
|
|
270
283
|
|
|
271
284
|
```rb
|
|
272
285
|
server_session.generate_response
|
|
@@ -3,14 +3,65 @@ require 'strscan'
|
|
|
3
3
|
module WebSocket
|
|
4
4
|
class Extensions
|
|
5
5
|
|
|
6
|
+
# RFC 6455 grammar rules:
|
|
7
|
+
#
|
|
8
|
+
# Sec-WebSocket-Extensions = extension-list
|
|
9
|
+
#
|
|
10
|
+
# extension-list = 1#extension
|
|
11
|
+
#
|
|
12
|
+
# extension = extension-token *( ";" extension-param )
|
|
13
|
+
#
|
|
14
|
+
# extension-token = registered-token
|
|
15
|
+
#
|
|
16
|
+
# registered-token = token
|
|
17
|
+
#
|
|
18
|
+
# extension-param = token [ "=" (token | quoted-string) ]
|
|
19
|
+
# ; When using the quoted-string syntax variant, the value
|
|
20
|
+
# ; after quoted-string unescaping MUST conform to the
|
|
21
|
+
# ; 'token' ABNF.
|
|
22
|
+
#
|
|
23
|
+
# These make use of the following rules from RFC 2616:
|
|
24
|
+
#
|
|
25
|
+
# token = 1*<any CHAR except CTLs or separators>
|
|
26
|
+
#
|
|
27
|
+
# separators = "(" | ")" | "<" | ">" | "@"
|
|
28
|
+
# | "," | ";" | ":" | "\" | <">
|
|
29
|
+
# | "/" | "[" | "]" | "?" | "="
|
|
30
|
+
# | "{" | "}" | SP | HT
|
|
31
|
+
#
|
|
32
|
+
# quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
|
|
33
|
+
#
|
|
34
|
+
# qdtext = <any TEXT except <">>
|
|
35
|
+
#
|
|
36
|
+
# quoted-pair = "\" CHAR
|
|
37
|
+
#
|
|
38
|
+
# CHAR = <any US-ASCII character (octets 0 - 127)>
|
|
39
|
+
#
|
|
40
|
+
# CTL = <any US-ASCII control character
|
|
41
|
+
# (octets 0 - 31) and DEL (127)>
|
|
42
|
+
#
|
|
43
|
+
# TEXT = <any OCTET except CTLs, but including LWS>
|
|
44
|
+
#
|
|
45
|
+
# LWS = [CRLF] 1*( SP | HT )
|
|
46
|
+
#
|
|
47
|
+
# SP = <US-ASCII SP, space (32)>
|
|
48
|
+
#
|
|
49
|
+
# HT = <US-ASCII HT, horizontal-tab (9)>
|
|
50
|
+
|
|
6
51
|
class Parser
|
|
7
52
|
TOKEN = /([!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+)/
|
|
8
53
|
NOTOKEN = /([^!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z])/
|
|
9
54
|
QUOTED = /"((?:\\[\x00-\x7f]|[^\x00-\x08\x0a-\x1f\x7f"\\])*)"/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
55
|
+
NUMBER = /\A-?(0|[1-9][0-9]*)(\.[0-9]+)?\z/
|
|
56
|
+
|
|
57
|
+
LWS = /[ \t]*(?:\r\n[ \t]+)*/
|
|
58
|
+
EQL = %r{#{ LWS.source }=#{ LWS.source }}
|
|
59
|
+
SEM = %r{#{ LWS.source };#{ LWS.source }}
|
|
60
|
+
COM = %r{#{ LWS.source },#{ LWS.source }}
|
|
61
|
+
|
|
62
|
+
PARAM = %r{#{ TOKEN.source }(?:#{ EQL.source }(?:#{ TOKEN.source }|#{ QUOTED.source }))?}
|
|
63
|
+
EXT = %r{#{ TOKEN.source }(?:#{ SEM.source }#{ PARAM.source })*}
|
|
64
|
+
EXT_LIST = %r{\A#{ EXT.source }(?:#{ COM.source }#{ EXT.source })*\z}
|
|
14
65
|
|
|
15
66
|
ParseError = Class.new(ArgumentError)
|
|
16
67
|
|
|
@@ -51,9 +102,10 @@ module WebSocket
|
|
|
51
102
|
|
|
52
103
|
offers.push(name, offer)
|
|
53
104
|
|
|
54
|
-
scanner.scan(
|
|
105
|
+
scanner.scan(COM)
|
|
55
106
|
value = scanner.scan(EXT)
|
|
56
107
|
end
|
|
108
|
+
|
|
57
109
|
offers
|
|
58
110
|
end
|
|
59
111
|
|
data/lib/websocket/extensions.rb
CHANGED
|
@@ -3,13 +3,33 @@ module WebSocket
|
|
|
3
3
|
|
|
4
4
|
autoload :Parser, File.expand_path('../extensions/parser', __FILE__)
|
|
5
5
|
|
|
6
|
-
ExtensionError
|
|
6
|
+
class ExtensionError < ArgumentError
|
|
7
|
+
attr_reader :type
|
|
8
|
+
|
|
9
|
+
def initialize(message, type = nil)
|
|
10
|
+
super(message)
|
|
11
|
+
@type = type
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.validate_options(options, valid_keys)
|
|
16
|
+
options = options || {}
|
|
17
|
+
|
|
18
|
+
options.keys.each do |key|
|
|
19
|
+
unless valid_keys.include?(key)
|
|
20
|
+
raise ExtensionError, "Unrecognized option: #{ key.inspect }"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
options
|
|
25
|
+
end
|
|
7
26
|
|
|
8
27
|
MESSAGE_OPCODES = [1, 2]
|
|
9
28
|
|
|
10
|
-
def initialize
|
|
29
|
+
def initialize(options = {})
|
|
11
30
|
@rsv1 = @rsv2 = @rsv3 = nil
|
|
12
31
|
|
|
32
|
+
@options = Extensions.validate_options(options, [:max_length])
|
|
13
33
|
@by_name = {}
|
|
14
34
|
@in_order = []
|
|
15
35
|
@sessions = []
|
|
@@ -51,7 +71,9 @@ module WebSocket
|
|
|
51
71
|
index = {}
|
|
52
72
|
|
|
53
73
|
@in_order.each do |ext|
|
|
54
|
-
|
|
74
|
+
arity = ext.method(:create_client_session).arity
|
|
75
|
+
session = (arity == 0) ? ext.create_client_session : ext.create_client_session(@options)
|
|
76
|
+
|
|
55
77
|
next unless session
|
|
56
78
|
|
|
57
79
|
record = [ext, session]
|
|
@@ -106,7 +128,10 @@ module WebSocket
|
|
|
106
128
|
offer = offers.by_name(ext.name)
|
|
107
129
|
next if offer.empty? or reserved?(ext)
|
|
108
130
|
|
|
109
|
-
|
|
131
|
+
arity = ext.method(:create_server_session).arity
|
|
132
|
+
session = (arity == 1) ? ext.create_server_session(offer) : ext.create_server_session(offer, @options)
|
|
133
|
+
|
|
134
|
+
next unless session
|
|
110
135
|
|
|
111
136
|
reserve(ext)
|
|
112
137
|
sessions.push([ext, session])
|
|
@@ -139,7 +164,8 @@ module WebSocket
|
|
|
139
164
|
begin
|
|
140
165
|
session.process_incoming_message(msg)
|
|
141
166
|
rescue => error
|
|
142
|
-
|
|
167
|
+
type = error.respond_to?(:type) ? error.type : nil
|
|
168
|
+
raise ExtensionError.new([ext.name, error.message].join(': '), type)
|
|
143
169
|
end
|
|
144
170
|
end
|
|
145
171
|
end
|
|
@@ -149,7 +175,8 @@ module WebSocket
|
|
|
149
175
|
begin
|
|
150
176
|
session.process_outgoing_message(msg)
|
|
151
177
|
rescue => error
|
|
152
|
-
|
|
178
|
+
type = error.respond_to?(:type) ? error.type : nil
|
|
179
|
+
raise ExtensionError.new([ext.name, error.message].join(': '), type)
|
|
153
180
|
end
|
|
154
181
|
end
|
|
155
182
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: websocket-extensions
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- James Coglan
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: rspec
|
|
@@ -24,7 +23,6 @@ dependencies:
|
|
|
24
23
|
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
25
|
version: '0'
|
|
27
|
-
description:
|
|
28
26
|
email: jcoglan@gmail.com
|
|
29
27
|
executables: []
|
|
30
28
|
extensions: []
|
|
@@ -40,7 +38,6 @@ homepage: https://github.com/faye/websocket-extensions-ruby
|
|
|
40
38
|
licenses:
|
|
41
39
|
- Apache-2.0
|
|
42
40
|
metadata: {}
|
|
43
|
-
post_install_message:
|
|
44
41
|
rdoc_options:
|
|
45
42
|
- "--main"
|
|
46
43
|
- README.md
|
|
@@ -59,8 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
59
56
|
- !ruby/object:Gem::Version
|
|
60
57
|
version: '0'
|
|
61
58
|
requirements: []
|
|
62
|
-
rubygems_version: 3.
|
|
63
|
-
signing_key:
|
|
59
|
+
rubygems_version: 3.6.9
|
|
64
60
|
specification_version: 4
|
|
65
61
|
summary: Generic extension manager for WebSocket connections
|
|
66
62
|
test_files: []
|