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

Why can Module.prototype.require be deleted? #41095

Closed
Melab opened this issue Dec 6, 2021 · 7 comments
Closed

Why can Module.prototype.require be deleted? #41095

Melab opened this issue Dec 6, 2021 · 7 comments
Labels
module Issues and PRs related to the module subsystem. question Issues that look for answers.

Comments

@Melab
Copy link

Melab commented Dec 6, 2021

Version

v18.0.0-pre

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

delete require("module").prototype.require

How often does it reproduce? Is there a required condition?

No.

What is the expected behavior?

"Monkey patching" Node's internals is supposed to be impossible.

What do you see instead?

No response

Additional information

Per issue #30697, Node's internals were refactored to make them immune to modification by non-builtin code. So I am wondering why is it that Module.prototype is exposed to external code that can kill Node's module loading mechanism by deleting the property require on it?

@Trott
Copy link
Member

Trott commented Dec 6, 2021

#30697 is being discussed. It is an open question as to whether and how far Node.js should go to prevent tampering with prototypes in core side effects in core of tampering with prototypes.

@DerekNonGeneric DerekNonGeneric added the question Issues that look for answers. label Dec 6, 2021
@DerekNonGeneric DerekNonGeneric changed the title Why can Module.prototype.require deleted? Why can Module.prototype.require be deleted? Dec 6, 2021
@Melab
Copy link
Author

Melab commented Dec 7, 2021

#30697 is being discussed. It is an open question as to whether and how far Node.js should go to prevent tampering with prototypes in core.

My impression was that the ECMA-262 API components were the only things that needed to be in primordials to insulate Node built-in modules from the effects of mutated intrinsics. It was also either that require used by built-in modules was supposed to be different from the one passed to "userland" code or that each module received could break its own require but not the other modules' requires. And, the return value of require("module") would contain code for custom module loaders and not code used internally.

Is the mutability of Module.prototype the way by which alternative CommonJS module loaders for Node function? If so, how would alternative loaders work if Module.prototype were protected?

@Trott
Copy link
Member

Trott commented Dec 7, 2021

@nodejs/modules

@ljharb
Copy link
Member

ljharb commented Dec 7, 2021

The primordials discussion is about language builtins; there is currently no plan or discussion or expectation I'm aware of about mutation of node's builtins themselves being supported - there's plenty of use cases that require node's builtins to be mutable, like monitoring systems.

In general, don't mutate things you don't own; the goal of primordials is that mutating something unrelated to a node builtin should not break that builtin. If you mutate any arbitrary thing you don't own, you should always expect that thing to break.

@Mesteery Mesteery added the module Issues and PRs related to the module subsystem. label Dec 7, 2021
@Melab
Copy link
Author

Melab commented Dec 8, 2021

The primordials discussion is about language builtins; there is currently no plan or discussion or expectation I'm aware of about mutation of node's builtins themselves being supported - there's plenty of use cases that require node's builtins to be mutable, like monitoring systems.

In general, don't mutate things you don't own; the goal of primordials is that mutating something unrelated to a node builtin should not break that builtin. If you mutate any arbitrary thing you don't own, you should always expect that thing to break.

Can I get an answer to the question in my second paragraph?

@ljharb
Copy link
Member

ljharb commented Dec 8, 2021

I assume that many use cases would break if it were locked down, yes - there’s a reason language and web builtins are mutable by default too.

@benjamingr
Copy link
Member

Is the mutability of Module.prototype the way by which alternative CommonJS module loaders for Node function? If so, how would alternative loaders work if Module.prototype were protected?

Mostly yes, though some use require.extensions or other mechanisms and/or internals to do this. If module internals were protected (which no one is suggesting at this point) Node would provide an alternative API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

6 participants