Skip to content

Commit 302fd68

Browse files
committed
fix: validate fd in JS land + add test
1 parent da7ee62 commit 302fd68

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/internal/util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
Error,
99
ErrorCaptureStackTrace,
1010
FunctionPrototypeCall,
11+
NumberIsInteger,
1112
NumberParseInt,
1213
ObjectDefineProperties,
1314
ObjectDefineProperty,
@@ -867,6 +868,10 @@ const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
867868
handleTypes[-1] = 'INVALID';
868869

869870
function guessHandleType(fd) {
871+
if (!NumberIsInteger(fd)) {
872+
return 'INVALID';
873+
}
874+
870875
const type = _guessHandleType(fd);
871876
return handleTypes[type];
872877
}
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)