permessage_deflate 0.1.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 152e4441be7d3c79b3e8169d4268b2d7f5abdb09
4
- data.tar.gz: af5b39863453e9a56cbc33cd1cb96a4e0956bf9b
2
+ SHA256:
3
+ metadata.gz: 2947014b879c7bd9afe0c8eb0bb3b48bd5aa89012d6b80ab1413f4d77f50b27f
4
+ data.tar.gz: 4558f0fb7a4e1954f675654d151b63c6d7f16d8f2dadf3e5c392b6b1c0951503
5
5
  SHA512:
6
- metadata.gz: 3a80afa2e48ab51d6f2d80f2132f4c52ff3c9c80dcf3a17b4911970266f728919b068e0df991b060af8aa5e4a940df65749908b0a13c7ece32f3b7503d79929e
7
- data.tar.gz: 989ee3dfa1d4c62618cd816be874a69e4f7508dd5e8d23c4e64cfa67b6e312fcc0a4c99ed523825b82c623b486bea216672a79d169b2d4e2959a020e846a5981
6
+ metadata.gz: 9f7aa8d7ff53106a77ba388e8617008850dbc1ac36b2f0864927ac185d27f7e3a310e967b1cd974a5fabe46d818d0901c9c4ea438d8388cc3f3dc2fa2590c26e
7
+ data.tar.gz: c51480bb46fbbc174468b425c3a660e96e851938f04887712b060d711c12a52a7db43e632d022c05837f13222a9e496893e909ca05b2df6e90e289ab4aeca8cd
data/CHANGELOG.md CHANGED
@@ -1,16 +1,27 @@
1
+ ### 0.2.0 / 2026-11-16
2
+
3
+ - Implement the `max_length` option from `websocket-extensions` to limit the
4
+ size of decompressed messages
5
+ - Change license from MIT to Apache 2.0
6
+
7
+ ### 0.1.4 / 2017-09-10
8
+
9
+ - Use `9` instead of `8` as the `windowBits` parameter to zlib, to deal with
10
+ restrictions introduced in zlib v1.2.9
11
+
1
12
  ### 0.1.3 / 2016-05-20
2
13
 
3
- * Amend all warnings issued when running with -W2
14
+ - Amend all warnings issued when running with -W2
4
15
 
5
16
  ### 0.1.2 / 2015-11-06
6
17
 
7
- * The server does not send `server_max_window_bits` if the client does not ask
18
+ - The server does not send `server_max_window_bits` if the client does not ask
8
19
  for it; this works around an issue in Firefox.
9
20
 
10
21
  ### 0.1.1 / 2014-12-18
11
22
 
12
- * Don't allow configure() to be called with unrecognized options
23
+ - Don't allow configure() to be called with unrecognized options
13
24
 
14
25
  ### 0.1.0 / 2014-12-13
15
26
 
16
- * Initial release
27
+ - Initial release
data/LICENSE.md ADDED
@@ -0,0 +1,12 @@
1
+ Copyright 2014-2026 James Coglan
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4
+ this file except in compliance with the License. You may obtain a copy of the
5
+ License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software distributed
10
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
12
+ specific language governing permissions and limitations under the License.
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
- # permessage_deflate [![Build status](https://secure.travis-ci.org/faye/permessage-deflate-ruby.svg)](http://travis-ci.org/faye/permessage-deflate-ruby)
1
+ # permessage_deflate
2
2
 
3
- Implements the
4
- [permessage-deflate](https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression)
3
+ Implements the [permessage-deflate](https://tools.ietf.org/html/rfc7692)
5
4
  WebSocket protocol extension as a plugin for
6
5
  [websocket-extensions](https://github.com/faye/websocket-extensions-ruby).
7
6
 
@@ -11,7 +10,7 @@ WebSocket protocol extension as a plugin for
11
10
  $ gem install permessage_deflate
12
11
  ```
13
12
 
14
- ## Usage
13
+ ## Usage
15
14
 
16
15
  Add the plugin to your extensions:
17
16
 
@@ -44,47 +43,24 @@ the peer, and those that are negotiated as part of the protocol. The settings
44
43
  only affecting the compressor are described fully in the [Zlib
45
44
  documentation](http://ruby-doc.org/stdlib-2.1.0/libdoc/zlib/rdoc/Zlib/Deflate.html#method-c-new):
46
45
 
47
- * `:level`: sets the compression level, can be an integer from `0` to `9`, or
46
+ - `:level`: sets the compression level, can be an integer from `0` to `9`, or
48
47
  one of the constants `Zlib::NO_COMPRESSION`, `Zlib::BEST_SPEED`,
49
48
  `Zlib::BEST_COMPRESSION`, or `Zlib::DEFAULT_COMPRESSION`
50
- * `:mem_level`: sets how much memory the compressor allocates, can be an integer
49
+ - `:mem_level`: sets how much memory the compressor allocates, can be an integer
51
50
  from `1` to `9`, or one of the constants `Zlib::MAX_MEM_LEVEL`, or
52
51
  `Zlib::DEF_MEM_LEVEL`
53
- * `:strategy`: can be one of the constants `Zlib::FILTERED`,
52
+ - `:strategy`: can be one of the constants `Zlib::FILTERED`,
54
53
  `Zlib::HUFFMAN_ONLY`, `Zlib::RLE`, `Zlib::FIXED`, or `Zlib::DEFAULT_STRATEGY`
55
54
 
56
55
  The other options relate to settings that are negotiated via the protocol and
57
56
  can be used to set the local session's behaviour and control that of the peer:
58
57
 
59
- * `:no_context_takeover`: if `true`, stops the session reusing a deflate context
58
+ - `:no_context_takeover`: if `true`, stops the session reusing a deflate context
60
59
  between messages
61
- * `:request_no_context_takeover`: if `true`, makes the session tell the other
60
+ - `:request_no_context_takeover`: if `true`, makes the session tell the other
62
61
  peer not to reuse a deflate context between messages
63
- * `:max_window_bits`: an integer from `8` to `15` inclusive that sets the
62
+ - `:max_window_bits`: an integer from `8` to `15` inclusive that sets the
64
63
  maximum size of the session's sliding window; a lower window size will be used
65
64
  if requested by the peer
66
- * `:request_max_window_bits`: an integer from `8` to `15` inclusive to ask the
65
+ - `:request_max_window_bits`: an integer from `8` to `15` inclusive to ask the
67
66
  other peer to use to set its maximum sliding window size, if supported
68
-
69
- ## License
70
-
71
- (The MIT License)
72
-
73
- Copyright (c) 2014-2016 James Coglan
74
-
75
- Permission is hereby granted, free of charge, to any person obtaining a copy of
76
- this software and associated documentation files (the 'Software'), to deal in
77
- the Software without restriction, including without limitation the rights to
78
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
79
- the Software, and to permit persons to whom the Software is furnished to do so,
80
- subject to the following conditions:
81
-
82
- The above copyright notice and this permission notice shall be included in all
83
- copies or substantial portions of the Software.
84
-
85
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
86
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
87
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
88
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
89
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
90
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -11,29 +11,29 @@ class PermessageDeflate
11
11
  true
12
12
  end
13
13
 
14
- def initialize(options, params)
15
- super(options)
14
+ def initialize(options, session_opts, params)
15
+ super(options, session_opts)
16
16
  @params = params
17
17
  end
18
18
 
19
19
  def generate_response
20
20
  response = {}
21
21
 
22
- # https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression#section-8.1.1.1
22
+ # https://tools.ietf.org/html/rfc7692#section-7.1.1.1
23
23
 
24
24
  @own_context_takeover = !@accept_no_context_takeover &&
25
25
  !@params['server_no_context_takeover']
26
26
 
27
27
  response['server_no_context_takeover'] = true unless @own_context_takeover
28
28
 
29
- # https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression#section-8.1.1.2
29
+ # https://tools.ietf.org/html/rfc7692#section-7.1.1.2
30
30
 
31
31
  @peer_context_takeover = !@request_no_context_takeover &&
32
32
  !@params['client_no_context_takeover']
33
33
 
34
34
  response['client_no_context_takeover'] = true unless @peer_context_takeover
35
35
 
36
- # https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression#section-8.1.2.1
36
+ # https://tools.ietf.org/html/rfc7692#section-7.1.2.1
37
37
 
38
38
  @own_window_bits = [ @accept_max_window_bits || MAX_WINDOW_BITS,
39
39
  @params['server_max_window_bits'] || MAX_WINDOW_BITS
@@ -45,7 +45,7 @@ class PermessageDeflate
45
45
  response['server_max_window_bits'] = @own_window_bits
46
46
  end
47
47
 
48
- # https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression#section-8.1.2.2
48
+ # https://tools.ietf.org/html/rfc7692#section-7.1.2.2
49
49
 
50
50
  if client_max = @params['client_max_window_bits']
51
51
  client_max = MAX_WINDOW_BITS if client_max == true
@@ -1,6 +1,15 @@
1
1
  class PermessageDeflate
2
2
  class Session
3
3
 
4
+ class SessionError < StandardError
5
+ attr_reader :type
6
+
7
+ def initialize(message, type)
8
+ super(message)
9
+ @type = type
10
+ end
11
+ end
12
+
4
13
  VALID_PARAMS = [
5
14
  'server_no_context_takeover',
6
15
  'client_no_context_takeover',
@@ -8,8 +17,11 @@ class PermessageDeflate
8
17
  'client_max_window_bits'
9
18
  ]
10
19
 
11
- MAX_WINDOW_BITS = 15
12
- VALID_WINDOW_BITS = [8, 9, 10, 11, 12, 13, 14, 15]
20
+ MIN_WINDOW_BITS = 9
21
+ MAX_WINDOW_BITS = 15
22
+ VALID_WINDOW_BITS = [8, 9, 10, 11, 12, 13, 14, 15]
23
+ DEFAULT_MAX_LENGTH = 0x3ffffff # 64 MB
24
+ RAW_TAIL_BYTES = [0x00, 0x00, 0xff, 0xff].pack('C*')
13
25
 
14
26
  def self.valid_params?(params)
15
27
  return false unless params.keys.all? { |k| VALID_PARAMS.include?(k) }
@@ -30,7 +42,7 @@ class PermessageDeflate
30
42
  true
31
43
  end
32
44
 
33
- def initialize(options)
45
+ def initialize(options, session_opts)
34
46
  @level = options.fetch(:level, Zlib::DEFAULT_COMPRESSION)
35
47
  @mem_level = options.fetch(:mem_level, Zlib::DEF_MEM_LEVEL)
36
48
  @strategy = options.fetch(:strategy, Zlib::DEFAULT_STRATEGY)
@@ -40,6 +52,8 @@ class PermessageDeflate
40
52
  @request_no_context_takeover = options.fetch(:request_no_context_takeover, false)
41
53
  @request_max_window_bits = options.fetch(:request_max_window_bits, nil)
42
54
 
55
+ @max_length = session_opts.fetch(:max_length, DEFAULT_MAX_LENGTH)
56
+
43
57
  @deflate = @inflate = nil
44
58
  end
45
59
 
@@ -47,9 +61,21 @@ class PermessageDeflate
47
61
  return message unless message.rsv1
48
62
 
49
63
  inflate = get_inflate
64
+ chunks = []
65
+ length = 0
66
+
67
+ inflate.inflate(message.data) do |chunk|
68
+ chunks << chunk
69
+ length += chunk.bytesize
50
70
 
51
- message.data = inflate.inflate(message.data) +
52
- inflate.inflate([0x00, 0x00, 0xff, 0xff].pack('C*'))
71
+ if length > @max_length
72
+ free(inflate)
73
+ raise SessionError.new('WebSocket frame length too large after decompression', 1009)
74
+ end
75
+ end
76
+
77
+ chunks << inflate.inflate(RAW_TAIL_BYTES)
78
+ message.data = chunks.join('')
53
79
 
54
80
  free(inflate) unless @inflate
55
81
  message
@@ -83,14 +109,20 @@ class PermessageDeflate
83
109
 
84
110
  def get_inflate
85
111
  return @inflate if @inflate
86
- inflate = Zlib::Inflate.new(-@peer_window_bits)
112
+
113
+ window_bits = [@peer_window_bits, MIN_WINDOW_BITS].max
114
+ inflate = Zlib::Inflate.new(-window_bits)
115
+
87
116
  @inflate = inflate if @peer_context_takeover
88
117
  inflate
89
118
  end
90
119
 
91
120
  def get_deflate
92
121
  return @deflate if @deflate
93
- deflate = Zlib::Deflate.new(@level, -@own_window_bits, @mem_level, @strategy)
122
+
123
+ window_bits = [@own_window_bits, MIN_WINDOW_BITS].max
124
+ deflate = Zlib::Deflate.new(@level, -window_bits, @mem_level, @strategy)
125
+
94
126
  @deflate = deflate if @own_context_takeover
95
127
  deflate
96
128
  end
@@ -21,7 +21,7 @@ class PermessageDeflate
21
21
  def self.validate_options(options, valid_keys)
22
22
  options.keys.each do |key|
23
23
  unless valid_keys.include?(key)
24
- raise ConfigurationError, "Unrecognized option: #{key.inspect}"
24
+ raise ConfigurationError, "Unrecognized option: #{ key.inspect }"
25
25
  end
26
26
  end
27
27
  end
@@ -41,13 +41,13 @@ class PermessageDeflate
41
41
  PermessageDeflate.new(options)
42
42
  end
43
43
 
44
- def create_client_session
45
- ClientSession.new(@options || {})
44
+ def create_client_session(session_opts = {})
45
+ ClientSession.new(@options || {}, session_opts)
46
46
  end
47
47
 
48
- def create_server_session(offers)
48
+ def create_server_session(offers, session_opts = {})
49
49
  offers.each do |offer|
50
- return ServerSession.new(@options || {}, offer) if ServerSession.valid_params?(offer)
50
+ return ServerSession.new(@options || {}, session_opts, offer) if ServerSession.valid_params?(offer)
51
51
  end
52
52
  nil
53
53
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: permessage_deflate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
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: 2016-05-20 00:00:00.000000000 Z
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: []
@@ -32,16 +30,16 @@ extra_rdoc_files:
32
30
  - README.md
33
31
  files:
34
32
  - CHANGELOG.md
33
+ - LICENSE.md
35
34
  - README.md
36
35
  - lib/permessage_deflate.rb
37
36
  - lib/permessage_deflate/client_session.rb
38
37
  - lib/permessage_deflate/server_session.rb
39
38
  - lib/permessage_deflate/session.rb
40
- homepage: http://github.com/faye/permessage-deflate-ruby
39
+ homepage: https://github.com/faye/permessage-deflate-ruby
41
40
  licenses:
42
- - MIT
41
+ - Apache-2.0
43
42
  metadata: {}
44
- post_install_message:
45
43
  rdoc_options:
46
44
  - "--main"
47
45
  - README.md
@@ -60,9 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
58
  - !ruby/object:Gem::Version
61
59
  version: '0'
62
60
  requirements: []
63
- rubyforge_project:
64
- rubygems_version: 2.5.1
65
- signing_key:
61
+ rubygems_version: 3.6.9
66
62
  specification_version: 4
67
63
  summary: Per-message DEFLATE compression extension for WebSocket connections
68
64
  test_files: []