Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed May 16, 2024
1 parent d960de6 commit ef8e1e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/web/websocket/permessage-deflate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { createInflateRaw } = require('node:zlib')
const { createInflateRaw, Z_DEFAULT_WINDOWBITS } = require('node:zlib')
const { isValidClientWindowBits } = require('./util')

const tail = Buffer.from([0x00, 0x00, 0xff, 0xff])
const kBuffer = Symbol('kBuffer')
Expand All @@ -14,6 +15,7 @@ class PerMessageDeflate {

constructor (extensions) {
this.#options.clientNoContextTakeover = extensions.has('client_no_context_takeover')
this.#options.clientMaxWindowBits = extensions.get('client_max_window_bits')
}

decompress (chunk, fin, callback) {
Expand All @@ -23,7 +25,18 @@ class PerMessageDeflate {
// 2. Decompress the resulting data using DEFLATE.

if (!this.#inflate) {
this.#inflate = createInflateRaw(/* TODO */)
let windowBits = Z_DEFAULT_WINDOWBITS

if (this.#options.clientMaxWindowBits) { // empty values default to Z_DEFAULT_WINDOWBITS
if (!isValidClientWindowBits(this.#options.clientMaxWindowBits)) {
callback(new Error('Invalid client_max_window_bits'))
return
}

windowBits = Number.parseInt(this.#options.clientMaxWindowBits)
}

this.#inflate = createInflateRaw({ windowBits })
this.#inflate[kBuffer] = []
this.#inflate[kLength] = 0

Expand Down
20 changes: 19 additions & 1 deletion lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ function parseExtensions (extensions) {
return extensionList
}

/**
* @see https://www.rfc-editor.org/rfc/rfc7692#section-7.1.2.2
* @description "client-max-window-bits = 1*DIGIT"
* @param {string} value
*/
function isValidClientWindowBits (value) {
for (let i = 0; i < value.length; i++) {
const byte = value.charCodeAt(i)

if (byte < 0x30 || byte > 0x39) {
return false
}
}

return true
}

// https://nodejs.org/api/intl.html#detecting-internationalization-support
const hasIntl = typeof process.versions.icu === 'string'
const fatalDecoder = hasIntl ? new TextDecoder('utf-8', { fatal: true }) : undefined
Expand Down Expand Up @@ -291,5 +308,6 @@ module.exports = {
isContinuationFrame,
isTextBinaryFrame,
isValidOpcode,
parseExtensions
parseExtensions,
isValidClientWindowBits
}

0 comments on commit ef8e1e0

Please sign in to comment.