fs: validate the input data before opening file
PR-URL: https://github.com/nodejs/node/pull/31731 Refs: https://github.com/nodejs/node/pull/31030 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
This commit is contained in:
parent
a751389a14
commit
3e9302b2b3
@ -141,10 +141,6 @@ function validateFileHandle(handle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function writeFileHandle(filehandle, data, options) {
|
async function writeFileHandle(filehandle, data, options) {
|
||||||
if (!isArrayBufferView(data)) {
|
|
||||||
validateStringAfterArrayBufferView(data, 'data');
|
|
||||||
data = Buffer.from(data, options.encoding || 'utf8');
|
|
||||||
}
|
|
||||||
let remaining = data.length;
|
let remaining = data.length;
|
||||||
if (remaining === 0) return;
|
if (remaining === 0) return;
|
||||||
do {
|
do {
|
||||||
@ -496,6 +492,11 @@ async function writeFile(path, data, options) {
|
|||||||
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
|
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
|
||||||
const flag = options.flag || 'w';
|
const flag = options.flag || 'w';
|
||||||
|
|
||||||
|
if (!isArrayBufferView(data)) {
|
||||||
|
validateStringAfterArrayBufferView(data, 'data');
|
||||||
|
data = Buffer.from(data, options.encoding || 'utf8');
|
||||||
|
}
|
||||||
|
|
||||||
if (path instanceof FileHandle)
|
if (path instanceof FileHandle)
|
||||||
return writeFileHandle(path, data, options);
|
return writeFileHandle(path, data, options);
|
||||||
|
|
||||||
|
@ -129,18 +129,36 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
|
|||||||
.catch(throwNextTick);
|
.catch(throwNextTick);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that appendFile does not accept numbers (callback API).
|
// Test that appendFile does not accept invalid data type (callback API).
|
||||||
[false, 5, {}, [], null, undefined].forEach((data) => {
|
[false, 5, {}, [], null, undefined].forEach(async (data) => {
|
||||||
const errObj = {
|
const errObj = {
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
message: /"data"|"buffer"/
|
message: /"data"|"buffer"/
|
||||||
};
|
};
|
||||||
|
const filename = join(tmpdir.path, 'append-invalid-data.txt');
|
||||||
|
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => fs.appendFile('foobar', data, common.mustNotCall()),
|
() => fs.appendFile(filename, data, common.mustNotCall()),
|
||||||
errObj
|
errObj
|
||||||
);
|
);
|
||||||
assert.throws(() => fs.appendFileSync('foobar', data), errObj);
|
|
||||||
assert.rejects(fs.promises.appendFile('foobar', data), errObj);
|
assert.throws(
|
||||||
|
() => fs.appendFileSync(filename, data),
|
||||||
|
errObj
|
||||||
|
);
|
||||||
|
|
||||||
|
await assert.rejects(
|
||||||
|
fs.promises.appendFile(filename, data),
|
||||||
|
errObj
|
||||||
|
);
|
||||||
|
// The filename shouldn't exist if throwing error.
|
||||||
|
assert.throws(
|
||||||
|
() => fs.statSync(filename),
|
||||||
|
{
|
||||||
|
code: 'ENOENT',
|
||||||
|
message: /no such file or directory/
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test that appendFile accepts file descriptors (callback API).
|
// Test that appendFile accepts file descriptors (callback API).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user