Skip to content

Commit 3e9302b

Browse files
committed
fs: validate the input data before opening file
PR-URL: #31731 Refs: #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>
1 parent a751389 commit 3e9302b

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

lib/internal/fs/promises.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,6 @@ function validateFileHandle(handle) {
141141
}
142142

143143
async function writeFileHandle(filehandle, data, options) {
144-
if (!isArrayBufferView(data)) {
145-
validateStringAfterArrayBufferView(data, 'data');
146-
data = Buffer.from(data, options.encoding || 'utf8');
147-
}
148144
let remaining = data.length;
149145
if (remaining === 0) return;
150146
do {
@@ -496,6 +492,11 @@ async function writeFile(path, data, options) {
496492
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
497493
const flag = options.flag || 'w';
498494

495+
if (!isArrayBufferView(data)) {
496+
validateStringAfterArrayBufferView(data, 'data');
497+
data = Buffer.from(data, options.encoding || 'utf8');
498+
}
499+
499500
if (path instanceof FileHandle)
500501
return writeFileHandle(path, data, options);
501502

test/parallel/test-fs-append-file.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,36 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
129129
.catch(throwNextTick);
130130
}
131131

132-
// Test that appendFile does not accept numbers (callback API).
133-
[false, 5, {}, [], null, undefined].forEach((data) => {
132+
// Test that appendFile does not accept invalid data type (callback API).
133+
[false, 5, {}, [], null, undefined].forEach(async (data) => {
134134
const errObj = {
135135
code: 'ERR_INVALID_ARG_TYPE',
136136
message: /"data"|"buffer"/
137137
};
138+
const filename = join(tmpdir.path, 'append-invalid-data.txt');
139+
140+
assert.throws(
141+
() => fs.appendFile(filename, data, common.mustNotCall()),
142+
errObj
143+
);
144+
138145
assert.throws(
139-
() => fs.appendFile('foobar', data, common.mustNotCall()),
146+
() => fs.appendFileSync(filename, data),
140147
errObj
141148
);
142-
assert.throws(() => fs.appendFileSync('foobar', data), errObj);
143-
assert.rejects(fs.promises.appendFile('foobar', data), errObj);
149+
150+
await assert.rejects(
151+
fs.promises.appendFile(filename, data),
152+
errObj
153+
);
154+
// The filename shouldn't exist if throwing error.
155+
assert.throws(
156+
() => fs.statSync(filename),
157+
{
158+
code: 'ENOENT',
159+
message: /no such file or directory/
160+
}
161+
);
144162
});
145163

146164
// Test that appendFile accepts file descriptors (callback API).

0 commit comments

Comments
 (0)