Skip to content

Commit 53db798

Browse files
committed
fix: validate fd in JS land + add test
1 parent a3158f4 commit 53db798

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,
@@ -879,6 +880,10 @@ const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
879880
handleTypes[-1] = 'INVALID';
880881

881882
function guessHandleType(fd) {
883+
if (!NumberIsInteger(fd)) {
884+
return 'INVALID';
885+
}
886+
882887
const type = _guessHandleType(fd);
883888
return handleTypes[type];
884889
}
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)