Skip to content

Commit becf237

Browse files
authoredJun 9, 2022
[feature] Add the WS_NO_{BUFFER_UTIL, UTF_8_VALIDATE} variables
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.
1 parent 0792742 commit becf237

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed
 

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ necessarily need to have a C++ compiler installed on your machine.
6868
- `npm install --save-optional utf-8-validate`: Allows to efficiently check if a
6969
message contains valid UTF-8.
7070

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

7380
See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and

‎doc/ws.md

+15
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
- [websocket.terminate()](#websocketterminate)
5050
- [websocket.url](#websocketurl)
5151
- [createWebSocketStream(websocket[, options])](#createwebsocketstreamwebsocket-options)
52+
- [Environment variables](#environment-variables)
53+
- [WS_NO_BUFFER_UTIL](#ws_no_buffer_util)
54+
- [WS_NO_UTF_8_VALIDATE](#ws_no_utf_8_validate)
5255
- [Error codes](#error-codes)
5356
- [WS_ERR_EXPECTED_FIN](#ws_err_expected_fin)
5457
- [WS_ERR_EXPECTED_MASK](#ws_err_expected_mask)
@@ -587,6 +590,18 @@ The URL of the WebSocket server. Server clients don't have this attribute.
587590
Returns a `Duplex` stream that allows to use the Node.js streams API on top of a
588591
given `WebSocket`.
589592

593+
## Environment variables
594+
595+
### WS_NO_BUFFER_UTIL
596+
597+
When set to a non empty value, prevents the optional `bufferutil` dependency
598+
from being required.
599+
600+
### WS_NO_UTF_8_VALIDATE
601+
602+
When set to a non empty value, prevents the optional `utf-8-validate` dependency
603+
from being required.
604+
590605
## Error codes
591606

592607
Errors emitted by the websocket may have a `.code` property, describing the

‎lib/buffer-util.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -99,28 +99,29 @@ function toBuffer(data) {
9999
return buf;
100100
}
101101

102-
try {
103-
const bufferUtil = require('bufferutil');
104-
105-
module.exports = {
106-
concat,
107-
mask(source, mask, output, offset, length) {
102+
module.exports = {
103+
concat,
104+
mask: _mask,
105+
toArrayBuffer,
106+
toBuffer,
107+
unmask: _unmask
108+
};
109+
110+
/* istanbul ignore else */
111+
if (!process.env.WS_NO_BUFFER_UTIL) {
112+
try {
113+
const bufferUtil = require('bufferutil');
114+
115+
module.exports.mask = function (source, mask, output, offset, length) {
108116
if (length < 48) _mask(source, mask, output, offset, length);
109117
else bufferUtil.mask(source, mask, output, offset, length);
110-
},
111-
toArrayBuffer,
112-
toBuffer,
113-
unmask(buffer, mask) {
118+
};
119+
120+
module.exports.unmask = function (buffer, mask) {
114121
if (buffer.length < 32) _unmask(buffer, mask);
115122
else bufferUtil.unmask(buffer, mask);
116-
}
117-
};
118-
} catch (e) /* istanbul ignore next */ {
119-
module.exports = {
120-
concat,
121-
mask: _mask,
122-
toArrayBuffer,
123-
toBuffer,
124-
unmask: _unmask
125-
};
123+
};
124+
} catch (e) {
125+
// Continue regardless of the error.
126+
}
126127
}

‎lib/validation.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,21 @@ function _isValidUTF8(buf) {
105105
return true;
106106
}
107107

108-
try {
109-
const isValidUTF8 = require('utf-8-validate');
108+
module.exports = {
109+
isValidStatusCode,
110+
isValidUTF8: _isValidUTF8,
111+
tokenChars
112+
};
110113

111-
module.exports = {
112-
isValidStatusCode,
113-
isValidUTF8(buf) {
114+
/* istanbul ignore else */
115+
if (!process.env.WS_NO_UTF_8_VALIDATE) {
116+
try {
117+
const isValidUTF8 = require('utf-8-validate');
118+
119+
module.exports.isValidUTF8 = function (buf) {
114120
return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
115-
},
116-
tokenChars
117-
};
118-
} catch (e) /* istanbul ignore next */ {
119-
module.exports = {
120-
isValidStatusCode,
121-
isValidUTF8: _isValidUTF8,
122-
tokenChars
123-
};
121+
};
122+
} catch (e) {
123+
// Continue regardless of the error.
124+
}
124125
}

0 commit comments

Comments
 (0)
Please sign in to comment.