nodejs / nodejs/node

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

未关闭
#64,401 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
122k
派生
37.3k
平均合并
4 天 2 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 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 摘要。