nodejs / nodejs/node

vfs: MemoryProvider.setReadOnly() does not prevent writes through existing file descriptors

オープン
#64,401 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

Version

latest main branch

Platform
7.1.2-arch3-1
Subsystem

vfs

What steps will reproduce the bug?
const vfs = require('node:vfs');

const provider = new vfs.MemoryProvider();
const virtualFs = vfs.create(provider, {
  emitExperimentalWarning: false,
});

virtualFs.writeFileSync('/file.txt', 'old');

const fd = virtualFs.openSync('/file.txt', 'r+');

provider.setReadOnly();

const data = Buffer.from('new');
virtualFs.writeSync(fd, data, 0, data.length, 0);

console.log(virtualFs.readFileSync('/file.txt', 'utf8')); // "new"

virtualFs.closeSync(fd);

Run with:

node --experimental-vfs reproduction.js

How often does it reproduce? Is there a required condition?

It reproduces consistently.

The writable file descriptor must be opened before setReadOnly() is called. Opening a new writable descriptor after setReadOnly() correctly throws EROFS.

What is the expected behavior? Why is that the expected behavior?

Once setReadOnly() has been called, subsequent writes through any VirtualFileSystem backed by that provider should throw an error with code === 'EROFS', including writes through file descriptors that were opened before the provider became read-only.

This follows the current documentation for MemoryProvider.setReadOnly():

Subsequent writes through any VirtualFileSystem using this provider throw EROFS.

ftruncateSync() through an existing writable descriptor should similarly throw EROFS.

What do you see instead?

writeSync() succeeds and modifies the file even though:

virtualFs.readonly === true

ftruncateSync() also succeeds through a descriptor opened before setReadOnly().

Additional information

MemoryProvider.openSync() checks provider.readonly when a writable handle is opened. However, an existing MemoryFileHandle does not retain or re-check the provider's read-only state.

File-descriptor operations such as VirtualFileSystem.writeSync() and ftruncateSync() operate directly on the stored handle, so they bypass the provider's current readonly value.

Path-based writes made after setReadOnly(), such as writeFileSync(), already throw EROFS as expected.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

名前が示されている MemoryProvider.openSync()、MemoryFileHandle、VirtualFileSystem.writeSync()、ftruncateSync() のパスから始め、次に --experimental-vfs を付けて提供された再現手順を実行します。setReadOnly() より前に開かれたディスクリプタを介した書き込みと切り詰めが EROFS をスローし、既存のパスベースの動作がそのまま維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, node.js
領域
operating-systems
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
68/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。