Skip to content

fs: throw ERR_INVALID_THIS on illegal invocations #58848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
codes: {
ERR_DIR_CLOSED,
ERR_DIR_CONCURRENT_OPERATION,
ERR_INVALID_THIS,
ERR_MISSING_ARGS,
},
} = require('internal/errors');
Expand Down Expand Up @@ -67,6 +68,8 @@ class Dir {
}

get path() {
if (!(#path in this))
throw new ERR_INVALID_THIS('Dir');
return this.#path;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
validateBoolean,
validateFunction,
validateInteger,
validateThisInternalField,
} = require('internal/validators');
const { errorOrDestroy } = require('internal/streams/destroy');
const fs = require('fs');
Expand Down Expand Up @@ -230,9 +231,11 @@ ObjectSetPrototypeOf(ReadStream, Readable);
ObjectDefineProperty(ReadStream.prototype, 'autoClose', {
__proto__: null,
get() {
validateThisInternalField(this, kFs, 'ReadStream');
return this._readableState.autoDestroy;
},
set(val) {
validateThisInternalField(this, kFs, 'ReadStream');
this._readableState.autoDestroy = val;
},
});
Expand Down Expand Up @@ -394,9 +397,11 @@ ObjectSetPrototypeOf(WriteStream, Writable);
ObjectDefineProperty(WriteStream.prototype, 'autoClose', {
__proto__: null,
get() {
validateThisInternalField(this, kFs, 'WriteStream');
return this._writableState.autoDestroy;
},
set(val) {
validateThisInternalField(this, kFs, 'WriteStream');
this._writableState.autoDestroy = val;
},
});
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-fs-opendir.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ files.forEach(function(filename) {
fs.closeSync(fs.openSync(path.join(testDir, filename), 'w'));
});

function assertDir(dir) {
assert(dir instanceof fs.Dir);
assert.throws(() => dir.constructor.prototype.path, {
code: 'ERR_INVALID_THIS',
});
}

function assertDirent(dirent) {
assert(dirent instanceof fs.Dirent);
assert.strictEqual(dirent.isFile(), true);
Expand Down Expand Up @@ -45,6 +52,7 @@ const invalidCallbackObj = {
// Check the opendir Sync version
{
const dir = fs.opendirSync(testDir);
assertDir(dir);
const entries = files.map(() => {
const dirent = dir.readSync();
assertDirent(dirent);
Expand All @@ -67,6 +75,7 @@ const invalidCallbackObj = {

// Check the opendir async version
fs.opendir(testDir, common.mustSucceed((dir) => {
assertDir(dir);
let sync = true;
dir.read(common.mustSucceed((dirent) => {
assert(!sync);
Expand Down Expand Up @@ -120,6 +129,7 @@ fs.opendir(__filename, common.mustCall(function(e) {
async function doPromiseTest() {
// Check the opendir Promise version
const dir = await fs.promises.opendir(testDir);
assertDir(dir);
const entries = [];

let i = files.length;
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-fs-write-stream-autoclose-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ function next3() {
}));
}));
}

assert.throws(() => fs.WriteStream.prototype.autoClose, {
code: 'ERR_INVALID_THIS',
});
Loading