nodejs / nodejs/node

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

Open
#64,401 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the named MemoryProvider.openSync(), MemoryFileHandle, VirtualFileSystem.writeSync(), and ftruncateSync() paths, then run the provided reproduction with --experimental-vfs. Done means writes and truncation through descriptors opened before setReadOnly() throw EROFS, while the existing path-based behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.