[BUG] async writeFileAtomic() writes the previous file's mode and owner into the caller's options object
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 85/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, node.js
- Domain
- backend
Research direction
Start in writeFileAsync() and compare how it handles the caller's options with writeFileSync(), which copies them first. Reproduce the issue by writing two files with different modes while reusing one options object. Done when each file keeps its own mode and owner and the caller's options object remains unchanged.
Written by the indexing model from the issue text.
Description
Is there an existing issue for this?
- I have searched the existing issues
Current Behavior
The async writeFileAtomic() fills in options.mode and options.chown from the existing file by assigning them on the options object the caller passed in. If the same object is used again for a second file, the second file gets the first file's mode and owner instead of keeping its own. A 0600 file can become 0644 this way. writeFileAtomic.sync() copies the object first and does not have this problem.
Expected Behavior
Each call takes the mode and owner from its own target file, as the README says ("If mode is not specified, it will default to using the permissions from an existing file"), and the caller's options object is not changed.
Steps To Reproduce
// repro.js
const fs = require('fs')
const writeFileAtomic = require('write-file-atomic')
fs.writeFileSync('public.txt', 'a', { mode: 0o644 })
fs.writeFileSync('secret.txt', 'b', { mode: 0o600 })
const options = { encoding: 'utf8' }
;(async () => {
await writeFileAtomic('public.txt', 'hello', options)
await writeFileAtomic('secret.txt', 'token', options)
console.log((fs.statSync('secret.txt').mode & 0o777).toString(8))
console.log(options)
})()
umask 022; node repro.js
Actual:
644
{ encoding: 'utf8', mode: 33188, chown: { uid: 1010, gid: 1010 } }
Expected:
600
{ encoding: 'utf8' }
When run as root, the second file is also chowned to the first file's owner.
Environment
- write-file-atomic: 8.0.0 and main (23e111d)
- Node: 26.13.0
- OS: Debian 12, Linux 6.1
The cause is in writeFileAsync(), which assigns options.mode = stats.mode and options.chown = {...} directly, while writeFileSync() does options = Object.assign({}, options) first.
Credit: https://ucsbplse.github.io/
- Dominant language
- JavaScript
- Stars
- 256
- Forks
- 51
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from npm/write-file-atomic
-
Difficulty 2/5 1-3 hours Newbie friendliness 50/100
npm/write-file-atomic#243 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
npm/write-file-atomic#224 ·
-
PornographyOpen
Difficulty 5/5 Over a week Newbie friendliness 1/100
npm/write-file-atomic#189 · 6 reactions ·
-
Bug Needs Triage
Difficulty 3/5 1-2 days Newbie friendliness 35/100
npm/write-file-atomic#163 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 35/100
npm/write-file-atomic#156 ·
All issues in npm/write-file-atomic
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
scratchfoundation/scratch-editor#754 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 96/100
google/site-kit-wp#14102 ·
Maintainers usually reply within 2 days
-
area:growth priority:P1 type:docs
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
tecnologiasantos/bogoboom#66 ·
Maintainers usually reply within 1 day
-
desktop docs good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
ktwu01/benchmark-radar#1181 ·
Maintainers usually reply within 1 day