Skip to content

Improve Security of node_modules and package.json Resolution #43192

Closed as not planned
@arhart

Description

@arhart

What is the problem this feature will solve?

It is easy to accidentally allow another user to influence what code node loads and executes. Details can be found at HackerOne reports 1564437 (CommonJS module loading), 1564444 (ECMAScript module resolution), and 1564445 (package.json). While these behaviors are documented, the security implications are easy to overlook.

Insecure patterns around these features are in common use. For example, any combination of the following two circumstances is likely to be insecure:

  1. Attempting to require or dynamically import a module which is not guaranteed to exist in the expected location, for example by doing ANY of these:
    1. Intentionally attempting to load optional dependencies or optional peer dependencies which are not installed
    2. Forgetting to install dependencies before running a node script
    3. Creating a typo when developing a node script
  2. Running a node script when some parent directory might contain a node_modules directory that might be under control of another user, for example by doing ANY of these:
    1. Running a node script on Windows (any authenticated user is likely to have permission to create C:\node_modules, D:\node_modules, etc.)
    2. Running a node script from under a shared directory (ex: /tmp or a network share with similar permissions)
    3. Running a node script from under a home directory where an account might get created with a sibling home directory called "node_modules"

What is the feature you are proposing to solve the problem?

I suggest a multi-phase approach which can be modified by further discussion and feedback. Because there are security implications, I'd like improvements to be made as soon as possible. Because of both security and compatibility implications, a cautious approach is also desirable. Here are some potential phases:

  1. Discuss it publicly
    1. Are there other, similar security concerns that should be considered?
    2. Are there ideas for better approaches for addressing these concerns?
  2. Clearly document the existence of security concerns around these common features. There is precedent for documenting and emphasizing security warnings
    1. CommonJS module loading Loading from node_modules folders and require
    2. ECMAScript module resolution Terminology around bare specifier resolution and Resolution algorithm
    3. Program entry point
    4. Determining module system
  3. Document in more detail what the security concerns are and what steps a developer, packager, or user can take.
    1. Perhaps create a section of the documentation to discuss this and other security considerations in more detail. Perhaps it could be a sibling to Usage and example
    2. Should we document what files are loaded by relative paths and what files are loaded by absolute paths? Perhaps explicitly state that if modules are moved out from under node while it is searching for or loading them, it explicitly does not define what happens?
  4. Update behavior and corresponding documentation. There is precedent for security reasons for shipping breaking changes with an option to opt-in to the insecure behavior. See 10.19.0 during Active LTS

Proposed behavior update:

  1. Look for node_modules and package.json as immediate siblings of the scripts in question, rather than starting there and walking arbitrarily far toward the root.
  2. Add an --insecure-loader option to re-enable the old behavior.

Questions:

  1. Combined with symbolic links, hard links, and junctions, does this give package managers the needed flexibility?
  2. Is assuming that ./node_modules and ./package.json are trusted a reasonable burden to place on someone wanting to run ./script.js?

What alternatives have you considered?

  1. I initially reported these through the Node.js security processes, but they were considered "a long-standing and widely-known (or at least far from newly-discovered) problem". I was encouraged to discuss them publicly in the issue tracker.
  2. Disabling searching for node_modules and package.json relative to the running script is perhaps simpler and safer, but would presumably require some replacement to support existing use cases.
  3. A config file in a user's home directory could specify trusted search paths, but if we can find a workable solution without adding additional configuration files, that avoids some complexity.
  4. A config file could specify a ceiling for search paths, but again that introduces additional configuration.
  5. We could completely stop searching for node_modules and only look where a package.json file instructs, but that would require adding instructions to package.json and very nearly make package.json mandatory.

Edit: Replaced "from" with "from under" to clarify that the script need not be immediately in a shared directory or immediately in a home directory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions