[BUG] async writeFileAtomic() writes the previous file's mode and owner into the caller's options object

Aperta Adatta ai principianti
#245 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js
Ambito
backend

Direzione di ricerca

Inizia da writeFileAsync() e confronta il modo in cui gestisce le opzioni del chiamante con writeFileSync(), che prima le copia. Riproduci il problema scrivendo due file con modalità diverse e riutilizzando lo stesso oggetto delle opzioni. Il lavoro è completato quando ogni file mantiene la propria modalità e il proprio proprietario e l’oggetto delle opzioni del chiamante rimane invariato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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/

Lingua principale
JavaScript
Stelle
256
Fork
51
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di npm/write-file-atomic

Tutte le issue di npm/write-file-atomic

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.