Skip to content

Commit

Permalink
squash: harden string validation in fh.writeFile and fh.appendFile
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros committed Apr 3, 2022
1 parent b3d04aa commit 720ef24
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 7 additions & 5 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,10 @@ the end of the file.
<!-- YAML
added: v10.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/41677
description: The `data` parameter no longer accepts objects with an
own `toString` function property.
- version:
- v15.14.0
- v14.18.0
Expand All @@ -710,17 +714,15 @@ changes:
strings anymore.
-->
* `data` {string|Buffer|TypedArray|DataView|Object|AsyncIterable|Iterable
|Stream}
* `data` {string|Buffer|TypedArray|DataView|AsyncIterable|Iterable|Stream}
* `options` {Object|string}
* `encoding` {string|null} The expected character encoding when `data` is a
string. **Default:** `'utf8'`
* Returns: {Promise}
Asynchronously writes data to a file, replacing the file if it already exists.
`data` can be a string, a buffer, an {AsyncIterable} or {Iterable} object, or an
object with an own `toString` function
property. The promise is resolved with no arguments upon success.
`data` can be a string, a buffer, an {AsyncIterable} or {Iterable} object.
The promise is resolved with no arguments upon success.
If `options` is a string, then it specifies the `encoding`.
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const {
validateOffsetLengthWrite,
validateRmOptions,
validateRmdirOptions,
validateStringAfterArrayBufferView,
validatePrimitiveStringAfterArrayBufferView,
warnOnNonPortableTemplate,
} = require('internal/fs/utils');
Expand Down Expand Up @@ -823,7 +822,7 @@ async function writeFile(path, data, options) {
const flag = options.flag || 'w';

if (!isArrayBufferView(data) && !isCustomIterable(data)) {
validateStringAfterArrayBufferView(data, 'data');
validatePrimitiveStringAfterArrayBufferView(data, 'data');
data = Buffer.from(data, options.encoding || 'utf8');
}

Expand Down

0 comments on commit 720ef24

Please sign in to comment.