fs: throw ERR_INVALID_THIS on illegal invocations

PR-URL: https://github.com/nodejs/node/pull/58848
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Livia Medeiros 2025-06-29 00:58:54 +08:00 committed by GitHub
parent afbf2f385a
commit d824914e61
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 0 deletions

View File

@ -18,6 +18,7 @@ const {
codes: { codes: {
ERR_DIR_CLOSED, ERR_DIR_CLOSED,
ERR_DIR_CONCURRENT_OPERATION, ERR_DIR_CONCURRENT_OPERATION,
ERR_INVALID_THIS,
ERR_MISSING_ARGS, ERR_MISSING_ARGS,
}, },
} = require('internal/errors'); } = require('internal/errors');
@ -67,6 +68,8 @@ class Dir {
} }
get path() { get path() {
if (!(#path in this))
throw new ERR_INVALID_THIS('Dir');
return this.#path; return this.#path;
} }

View File

@ -25,6 +25,7 @@ const {
validateBoolean, validateBoolean,
validateFunction, validateFunction,
validateInteger, validateInteger,
validateThisInternalField,
} = require('internal/validators'); } = require('internal/validators');
const { errorOrDestroy } = require('internal/streams/destroy'); const { errorOrDestroy } = require('internal/streams/destroy');
const fs = require('fs'); const fs = require('fs');
@ -230,9 +231,11 @@ ObjectSetPrototypeOf(ReadStream, Readable);
ObjectDefineProperty(ReadStream.prototype, 'autoClose', { ObjectDefineProperty(ReadStream.prototype, 'autoClose', {
__proto__: null, __proto__: null,
get() { get() {
validateThisInternalField(this, kFs, 'ReadStream');
return this._readableState.autoDestroy; return this._readableState.autoDestroy;
}, },
set(val) { set(val) {
validateThisInternalField(this, kFs, 'ReadStream');
this._readableState.autoDestroy = val; this._readableState.autoDestroy = val;
}, },
}); });
@ -394,9 +397,11 @@ ObjectSetPrototypeOf(WriteStream, Writable);
ObjectDefineProperty(WriteStream.prototype, 'autoClose', { ObjectDefineProperty(WriteStream.prototype, 'autoClose', {
__proto__: null, __proto__: null,
get() { get() {
validateThisInternalField(this, kFs, 'WriteStream');
return this._writableState.autoDestroy; return this._writableState.autoDestroy;
}, },
set(val) { set(val) {
validateThisInternalField(this, kFs, 'WriteStream');
this._writableState.autoDestroy = val; this._writableState.autoDestroy = val;
}, },
}); });

View File

@ -18,6 +18,13 @@ files.forEach(function(filename) {
fs.closeSync(fs.openSync(path.join(testDir, filename), 'w')); 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) { function assertDirent(dirent) {
assert(dirent instanceof fs.Dirent); assert(dirent instanceof fs.Dirent);
assert.strictEqual(dirent.isFile(), true); assert.strictEqual(dirent.isFile(), true);
@ -45,6 +52,7 @@ const invalidCallbackObj = {
// Check the opendir Sync version // Check the opendir Sync version
{ {
const dir = fs.opendirSync(testDir); const dir = fs.opendirSync(testDir);
assertDir(dir);
const entries = files.map(() => { const entries = files.map(() => {
const dirent = dir.readSync(); const dirent = dir.readSync();
assertDirent(dirent); assertDirent(dirent);
@ -67,6 +75,7 @@ const invalidCallbackObj = {
// Check the opendir async version // Check the opendir async version
fs.opendir(testDir, common.mustSucceed((dir) => { fs.opendir(testDir, common.mustSucceed((dir) => {
assertDir(dir);
let sync = true; let sync = true;
dir.read(common.mustSucceed((dirent) => { dir.read(common.mustSucceed((dirent) => {
assert(!sync); assert(!sync);
@ -120,6 +129,7 @@ fs.opendir(__filename, common.mustCall(function(e) {
async function doPromiseTest() { async function doPromiseTest() {
// Check the opendir Promise version // Check the opendir Promise version
const dir = await fs.promises.opendir(testDir); const dir = await fs.promises.opendir(testDir);
assertDir(dir);
const entries = []; const entries = [];
let i = files.length; let i = files.length;

View File

@ -56,3 +56,7 @@ function next3() {
})); }));
})); }));
} }
assert.throws(() => fs.WriteStream.prototype.autoClose, {
code: 'ERR_INVALID_THIS',
});