Skip to content

Commit 0e82f72

Browse files
LiviaMedeirosaduh95
authored andcommitted
fs: throw ERR_INVALID_THIS on illegal invocations
PR-URL: #58848 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent ba42c72 commit 0e82f72

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
@@ -16,6 +16,7 @@ const {
1616
codes: {
1717
ERR_DIR_CLOSED,
1818
ERR_DIR_CONCURRENT_OPERATION,
19+
ERR_INVALID_THIS,
1920
ERR_MISSING_ARGS,
2021
},
2122
} = 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
@@ -26,6 +26,7 @@ const {
2626
validateBoolean,
2727
validateFunction,
2828
validateInteger,
29+
validateThisInternalField,
2930
} = require('internal/validators');
3031
const { errorOrDestroy } = require('internal/streams/destroy');
3132
const fs = require('fs');
@@ -231,9 +232,11 @@ ObjectSetPrototypeOf(ReadStream, Readable);
231232
ObjectDefineProperty(ReadStream.prototype, 'autoClose', {
232233
__proto__: null,
233234
get() {
235+
validateThisInternalField(this, kFs, 'ReadStream');
234236
return this._readableState.autoDestroy;
235237
},
236238
set(val) {
239+
validateThisInternalField(this, kFs, 'ReadStream');
237240
this._readableState.autoDestroy = val;
238241
},
239242
});
@@ -400,9 +403,11 @@ ObjectSetPrototypeOf(WriteStream, Writable);
400403
ObjectDefineProperty(WriteStream.prototype, 'autoClose', {
401404
__proto__: null,
402405
get() {
406+
validateThisInternalField(this, kFs, 'WriteStream');
403407
return this._writableState.autoDestroy;
404408
},
405409
set(val) {
410+
validateThisInternalField(this, kFs, 'WriteStream');
406411
this._writableState.autoDestroy = val;
407412
},
408413
});

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);
@@ -68,6 +76,7 @@ const invalidCallbackObj = {
6876

6977
// Check the opendir async version
7078
fs.opendir(testDir, common.mustSucceed((dir) => {
79+
assertDir(dir);
7180
let sync = true;
7281
dir.read(common.mustSucceed((dirent) => {
7382
assert(!sync);
@@ -121,6 +130,7 @@ fs.opendir(__filename, common.mustCall(function(e) {
121130
async function doPromiseTest() {
122131
// Check the opendir Promise version
123132
const dir = await fs.promises.opendir(testDir);
133+
assertDir(dir);
124134
const entries = [];
125135

126136
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)