Skip to content

Commit e690e1e

Browse files
committed
fix: validate fd in JS land + add test
1 parent 7ec745a commit e690e1e

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
@@ -8,6 +8,7 @@ const {
88
Error,
99
ErrorCaptureStackTrace,
1010
FunctionPrototypeCall,
11+
NumberIsInteger,
1112
NumberParseInt,
1213
ObjectDefineProperties,
1314
ObjectDefineProperty,
@@ -868,6 +869,10 @@ const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
868869
handleTypes[-1] = 'INVALID';
869870

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