Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.readdir doesn't fail if directory cannot be accessed #294

Closed
warpdesign opened this issue Apr 20, 2020 · 1 comment · Fixed by #295
Closed

fs.readdir doesn't fail if directory cannot be accessed #294

warpdesign opened this issue Apr 20, 2020 · 1 comment · Fixed by #295

Comments

@warpdesign
Copy link

I created a directory with mode 0o000. When using node's fs.readdir, an error with code EACCES is thrown but when mocked, it succeeds with ['0'] as result.

I am using OSX Catalina.

Code to reproduce the problem:

const mock = require('mock-fs');
const fs = require('fs');
const util = require('util');

console.log('');

try {
  fs.rmdirSync('/tmp/denied');
} catch(e) {
  
}

fs.mkdirSync('/tmp/denied', 0o000);

const mockFs = () => {
  mock({
    ['/tmp/denied']: mock.directory({
      mode: 0o000,
      items: [
        { file: 'file' }
      ]
    })
  });
}

const read = async (shouldMock) => { 
  if (shouldMock) {
    console.log('mocked fs');
    console.log('=========');
	  mockFs();
  } else {
    console.log('native fs');
    console.log('=========');
  }
  
  try {
    const files = await util.promisify(fs.readdir)('/tmp/denied');
    console.log('success', files);
  } catch(err) {
    console.log('error', err.code);
  }
}

async function test() {
  await read(false);
  console.log('');
  await read(true);
}

test();

When running it on a terminal, I get the following output:

native fs
========
error EACCES

mocked fs
========
success [ '0' ]

Node.js version: 12.14.1
mock-fs version: 4.11.0

warpdesign pushed a commit to warpdesign/mock-fs that referenced this issue Apr 20, 2020
warpdesign pushed a commit to warpdesign/mock-fs that referenced this issue Apr 21, 2020
tschaub added a commit that referenced this issue Apr 21, 2020
fix: readdir should check for access rights, fixes #294
@tschaub
Copy link
Owner

tschaub commented Apr 24, 2020

Fix published in mock-fs@4.12.0. Thanks, @warpdesign.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants