Skip to content

Commit 35b42c2

Browse files
committed
fix: validate fd in JS land + add test
1 parent 2827aaa commit 35b42c2

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

lib/internal/util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
ErrorCaptureStackTrace,
1010
FunctionPrototypeCall,
1111
FunctionPrototypeSymbolHasInstance,
12+
NumberIsInteger,
1213
NumberParseInt,
1314
ObjectDefineProperties,
1415
ObjectDefineProperty,
@@ -901,6 +902,10 @@ const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
901902
handleTypes[-1] = 'INVALID';
902903

903904
function guessHandleType(fd) {
905+
if (!NumberIsInteger(fd)) {
906+
return 'INVALID';
907+
}
908+
904909
const type = _guessHandleType(fd);
905910
return handleTypes[type];
906911
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { strictEqual } = require('assert');
5+
const { guessHandleType } = require('os');
6+
7+
strictEqual(guessHandleType(0), 'TTY', 'stdin reported to not be a tty, but it is');
8+
strictEqual(guessHandleType(1), 'TTY', 'stdout reported to not be a tty, but it is');
9+
strictEqual(guessHandleType(2), 'TTY', 'stderr reported to not be a tty, but it is');
10+
11+
strictEqual(guessHandleType(-1), 'INVALID', '-1 reported to be a tty, but it is not');
12+
strictEqual(guessHandleType(55555), 'UNKNOWN', '55555 reported to be a tty, but it is not');
13+
strictEqual(guessHandleType(2 ** 31), 'INVALID', '2^31 reported to be a tty, but it is not');
14+
strictEqual(guessHandleType(1.1), 'INVALID', '1.1 reported to be a tty, but it is not');
15+
strictEqual(guessHandleType('1'), 'INVALID', '\'1\' reported to be a tty, but it is not');

0 commit comments

Comments
 (0)