Skip to content

Commit

Permalink
[feature] Add the WS_NO_{BUFFER_UTIL, UTF_8_VALIDATE} variables
Browse files Browse the repository at this point in the history
When set to non empty values, the `WS_NO_BUFFER_UTIL` and
`WS_NO_UTF_8_VALIDATE` environment variables, prevent the optional
`bufferutil` and `utf-8-validate` dependencies  from being required,
respectively.

These might be useful to enhance security in systems where a user can
put a package in the package search path of an application of another
user, due to how the Node.js resolver algorithm works.
  • Loading branch information
lpinca committed Jun 9, 2022
1 parent 0792742 commit becf237
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -68,6 +68,13 @@ necessarily need to have a C++ compiler installed on your machine.
- `npm install --save-optional utf-8-validate`: Allows to efficiently check if a
message contains valid UTF-8.

To not even try to require and use these modules, use the
[`WS_NO_BUFFER_UTIL`](./doc/ws.md#ws_no_buffer_util) and
[`WS_NO_UTF_8_VALIDATE`](./doc/ws.md#ws_no_utf_8_validate) environment
variables. These might be useful to enhance security in systems where a user can
put a package in the package search path of an application of another user, due
to how the Node.js resolver algorithm works.

## API docs

See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
Expand Down
15 changes: 15 additions & 0 deletions doc/ws.md
Expand Up @@ -49,6 +49,9 @@
- [websocket.terminate()](#websocketterminate)
- [websocket.url](#websocketurl)
- [createWebSocketStream(websocket[, options])](#createwebsocketstreamwebsocket-options)
- [Environment variables](#environment-variables)
- [WS_NO_BUFFER_UTIL](#ws_no_buffer_util)
- [WS_NO_UTF_8_VALIDATE](#ws_no_utf_8_validate)
- [Error codes](#error-codes)
- [WS_ERR_EXPECTED_FIN](#ws_err_expected_fin)
- [WS_ERR_EXPECTED_MASK](#ws_err_expected_mask)
Expand Down Expand Up @@ -587,6 +590,18 @@ The URL of the WebSocket server. Server clients don't have this attribute.
Returns a `Duplex` stream that allows to use the Node.js streams API on top of a
given `WebSocket`.

## Environment variables

### WS_NO_BUFFER_UTIL

When set to a non empty value, prevents the optional `bufferutil` dependency
from being required.

### WS_NO_UTF_8_VALIDATE

When set to a non empty value, prevents the optional `utf-8-validate` dependency
from being required.

## Error codes

Errors emitted by the websocket may have a `.code` property, describing the
Expand Down
41 changes: 21 additions & 20 deletions lib/buffer-util.js
Expand Up @@ -99,28 +99,29 @@ function toBuffer(data) {
return buf;
}

try {
const bufferUtil = require('bufferutil');

module.exports = {
concat,
mask(source, mask, output, offset, length) {
module.exports = {
concat,
mask: _mask,
toArrayBuffer,
toBuffer,
unmask: _unmask
};

/* istanbul ignore else */
if (!process.env.WS_NO_BUFFER_UTIL) {
try {
const bufferUtil = require('bufferutil');

module.exports.mask = function (source, mask, output, offset, length) {
if (length < 48) _mask(source, mask, output, offset, length);
else bufferUtil.mask(source, mask, output, offset, length);
},
toArrayBuffer,
toBuffer,
unmask(buffer, mask) {
};

module.exports.unmask = function (buffer, mask) {
if (buffer.length < 32) _unmask(buffer, mask);
else bufferUtil.unmask(buffer, mask);
}
};
} catch (e) /* istanbul ignore next */ {
module.exports = {
concat,
mask: _mask,
toArrayBuffer,
toBuffer,
unmask: _unmask
};
};
} catch (e) {
// Continue regardless of the error.
}
}
29 changes: 15 additions & 14 deletions lib/validation.js
Expand Up @@ -105,20 +105,21 @@ function _isValidUTF8(buf) {
return true;
}

try {
const isValidUTF8 = require('utf-8-validate');
module.exports = {
isValidStatusCode,
isValidUTF8: _isValidUTF8,
tokenChars
};

module.exports = {
isValidStatusCode,
isValidUTF8(buf) {
/* istanbul ignore else */
if (!process.env.WS_NO_UTF_8_VALIDATE) {
try {
const isValidUTF8 = require('utf-8-validate');

module.exports.isValidUTF8 = function (buf) {
return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
},
tokenChars
};
} catch (e) /* istanbul ignore next */ {
module.exports = {
isValidStatusCode,
isValidUTF8: _isValidUTF8,
tokenChars
};
};
} catch (e) {
// Continue regardless of the error.
}
}

0 comments on commit becf237

Please sign in to comment.