Skip to content

fs.access() not reporting error on Windows #19192

Description

@dougwit
  • Version: 9.6.1
  • Platform: Windows 7 Professional x64
  • Subsystem: fs

fs.access() returns no error when given a file/directory with no priveleges.

Code example:

'use strict'

const fs = require('fs')

fs.access('./restricted', fs.constants.R_OK || fs.constants.W_OK, err => {
  if (err) {
    console.log(err)
  } else {
    console.log('no access() error')
  }
})

fs.stat('./restricted', (err, stat) => {
  if (err) {
    console.log(err)
  } else {
    console.log('no stat() error')
  }
})

Output:

no access() error
{ Error: EPERM: operation not permitted
  errno: -4048,
  code: 'EPERM',
  syscall: 'stat',
  path: 'C:\\Workspaces\\restricted' }

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    windowsIssues and PRs related to the Windows platform.
    on Mar 7, 2018
  2. joyeecheung commented on Mar 7, 2018

    @joyeecheung
    Member

    See #7700 , fs.access does not check ACL on Windows

  3. joyeecheung commented on Mar 7, 2018

    @joyeecheung
    Member

    Setting a doc label because the documentation seems to be misleading:

    fs.constants.R_OK - path can be read by the calling process.
    fs.constants.W_OK - path can be written by the calling process.

  4. added
    docIssues and PRs related to Node.js documentation.
    on Mar 7, 2018
  5. joyeecheung commented on Mar 7, 2018

    @joyeecheung
    Member

    Also cc @bnoordhuis is that a wont-fix on Windows?

  6. bzoz commented on Mar 8, 2018

    @bzoz
    Contributor

    This can be probably fixed in libuv, there is AccessCheck function in WinAPI that seems to do just that. For now lets just document this.

  7. bnoordhuis commented on Mar 8, 2018

    @bnoordhuis
    Member

    This can be probably fixed in libuv

    I don't know if we should (but I also don't know if we shouldn't); we don't check ACLs on other platforms.

  8. dougwit commented on Mar 8, 2018

    @dougwit
    Author

    Not all platforms use ACLs as their native permissions mechanism. Ignoring ACLs makes fs.access() essentially useless on Windows, and the function fails to do what it says it does:

    Tests a user's permissions for the file or directory specified by path.

  9. bnoordhuis commented on Mar 13, 2018

    @bnoordhuis
    Member

    Determining whether an ACL applies is a Hard Problem on many platforms. Doing it on one platform but not others makes it inconsistent. You're welcome to open a pull request but I expect other collaborators will raise the same concern.

  10. dlong500 commented on Jul 20, 2018

    @dlong500

    I agree that this behavior makes fs.access useless on Windows except for simply checking that a file/folder exists at all.

    As far as consistency is concerned, things are already inconsistent in behavior if fs.access works as designed on one platform but not on another, so I don't see anything wrong with implementing platform-specific internals to create more consistency in the API behavior.

  11. suchakraborty commented on Jun 10, 2020

    @suchakraborty

    Actually in 10.15.3 LTS, it does NOTHING, not even file existence check. I dont even have a D drive on my machine, and C:\dev does exist.

    PS C:\Users\sc> node -e "const fs = require('fs'); fs.access('D:\\afsd', (err) => {console.log(``${err} is the error.``)})"
     is the error.
    PS C:\Users\sc> node -e "const fs = require('fs'); fs.access('C:\\dev', (err) => {console.log(``${err} is the error.``)})"
     is the error.
    
  12. stelladraco27 commented on Mar 1, 2022

    @stelladraco27

    I am having this exact issue. I am reading a directory's contents into an array, and getting the stat for each item. I attempted to use fs.access to filter out un-readable folders/files but it would not throw an error. I ended up using the error fs.stat provides to do my filtering. A janky solution, don't like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.fsIssues and PRs related to file-system APIs and the fs module.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions