Skip to content

Commit 15fdba0

Browse files
tty: reject unknown raw mode strings
Signed-off-by: Samuel Williams <samuel.williams@shopify.com>
1 parent 1c0e5d3 commit 15fdba0

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

lib/tty.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
const {
3838
ErrnoException,
3939
codes: {
40+
ERR_INVALID_ARG_VALUE,
4041
ERR_INVALID_FD,
4142
ERR_TTY_INIT_FAILED,
4243
},
@@ -82,7 +83,15 @@ ObjectSetPrototypeOf(ReadStream.prototype, net.Socket.prototype);
8283
ObjectSetPrototypeOf(ReadStream, net.Socket);
8384

8485
ReadStream.prototype.setRawMode = function(mode) {
85-
const rawMode = mode === 'io' ? 'io' : (mode ? 'raw' : false);
86+
let rawMode;
87+
if (mode === 'io' || mode === 'raw') {
88+
rawMode = mode;
89+
} else if (typeof mode === 'string') {
90+
throw new ERR_INVALID_ARG_VALUE(
91+
'mode', mode, "must be true, false, 'raw', or 'io'");
92+
} else {
93+
rawMode = mode ? 'raw' : false;
94+
}
8695
let ttyMode = UV_TTY_MODE_NORMAL;
8796
if (rawMode === 'io') {
8897
ttyMode = UV_TTY_MODE_IO;

test/pseudo-tty/test-set-raw-mode-modes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ assert.strictEqual(process.stdin.rawMode, 'raw');
2121
process.stdin.setRawMode(false);
2222
console.log(`normal=${process.stdin.isRaw}`);
2323
assert.strictEqual(process.stdin.rawMode, false);
24+
assert.throws(
25+
() => process.stdin.setRawMode('raw-vt'),
26+
{
27+
code: 'ERR_INVALID_ARG_VALUE',
28+
name: 'TypeError',
29+
});
30+
assert.strictEqual(process.stdin.rawMode, false);
2431

2532
process.stdin.setRawMode('raw');
2633
console.log(`raw-string=${isOnlcrEnabled()}`);

0 commit comments

Comments
 (0)