Skip to content

Commit d824914

Browse files
fs: throw ERR_INVALID_THIS on illegal invocations
PR-URL: #58848 Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent afbf2f3 commit d824914

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

lib/internal/fs/dir.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
codes: {
1919
ERR_DIR_CLOSED,
2020
ERR_DIR_CONCURRENT_OPERATION,
21+
ERR_INVALID_THIS,
2122
ERR_MISSING_ARGS,
2223
},
2324
} = require('internal/errors');
@@ -67,6 +68,8 @@ class Dir {
6768
}
6869

6970
get path() {
71+
if (!(#path in this))
72+
throw new ERR_INVALID_THIS('Dir');
7073
return this.#path;
7174
}
7275

lib/internal/fs/streams.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
validateBoolean,
2626
validateFunction,
2727
validateInteger,
28+
validateThisInternalField,
2829
} = require('internal/validators');
2930
const { errorOrDestroy } = require('internal/streams/destroy');
3031
const fs = require('fs');
@@ -230,9 +231,11 @@ ObjectSetPrototypeOf(ReadStream, Readable);
230231
ObjectDefineProperty(ReadStream.prototype, 'autoClose', {
231232
__proto__: null,
232233
get() {
234+
validateThisInternalField(this, kFs, 'ReadStream');
233235
return this._readableState.autoDestroy;
234236
},
235237
set(val) {
238+
validateThisInternalField(this, kFs, 'ReadStream');
236239
this._readableState.autoDestroy = val;
237240
},
238241
});
@@ -394,9 +397,11 @@ ObjectSetPrototypeOf(WriteStream, Writable);
394397
ObjectDefineProperty(WriteStream.prototype, 'autoClose', {
395398
__proto__: null,
396399
get() {
400+
validateThisInternalField(this, kFs, 'WriteStream');
397401
return this._writableState.autoDestroy;
398402
},
399403
set(val) {
404+
validateThisInternalField(this, kFs, 'WriteStream');
400405
this._writableState.autoDestroy = val;
401406
},
402407
});

test/parallel/test-fs-opendir.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ files.forEach(function(filename) {
1818
fs.closeSync(fs.openSync(path.join(testDir, filename), 'w'));
1919
});
2020

21+
function assertDir(dir) {
22+
assert(dir instanceof fs.Dir);
23+
assert.throws(() => dir.constructor.prototype.path, {
24+
code: 'ERR_INVALID_THIS',
25+
});
26+
}
27+
2128
function assertDirent(dirent) {
2229
assert(dirent instanceof fs.Dirent);
2330
assert.strictEqual(dirent.isFile(), true);
@@ -45,6 +52,7 @@ const invalidCallbackObj = {
4552
// Check the opendir Sync version
4653
{
4754
const dir = fs.opendirSync(testDir);
55+
assertDir(dir);
4856
const entries = files.map(() => {
4957
const dirent = dir.readSync();
5058
assertDirent(dirent);
@@ -67,6 +75,7 @@ const invalidCallbackObj = {
6775

6876
// Check the opendir async version
6977
fs.opendir(testDir, common.mustSucceed((dir) => {
78+
assertDir(dir);
7079
let sync = true;
7180
dir.read(common.mustSucceed((dirent) => {
7281
assert(!sync);
@@ -120,6 +129,7 @@ fs.opendir(__filename, common.mustCall(function(e) {
120129
async function doPromiseTest() {
121130
// Check the opendir Promise version
122131
const dir = await fs.promises.opendir(testDir);
132+
assertDir(dir);
123133
const entries = [];
124134

125135
let i = files.length;

test/parallel/test-fs-write-stream-autoclose-option.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ function next3() {
5656
}));
5757
}));
5858
}
59+
60+
assert.throws(() => fs.WriteStream.prototype.autoClose, {
61+
code: 'ERR_INVALID_THIS',
62+
});

0 commit comments

Comments
 (0)