Skip to content

Override module.createRequire #9426

@SimenB

Description

@SimenB

People can do

import {createRequire} from 'module';

const myRequire = createRequire(__filename);

require('./some-file');

today, which will break out of the sandbox. We need to intercept this function when module is imported and provide our own implementation. This will be especially important when we start supporting ES Modules.

Relevant places in the code:
https://github.com/facebook/jest/blob/2ece4f979deaa2504038e8cca1f6b97925c3eb26/packages/jest-runtime/src/index.ts#L860-L866
Need to check for moduleName === 'module' and call
https://github.com/facebook/jest/blob/2ece4f979deaa2504038e8cca1f6b97925c3eb26/packages/jest-runtime/src/index.ts#L970-L973
where we already have a function to create a require from a path.

I'm not sure if each createRequire call returns its own cache or not.

Docs: https://nodejs.org/api/modules.html#modules_module_createrequire_filename

We should probably inspect the real module export and see if createRequire is there and only add ours if it is. We should also check if createRequireFromPath exist and support that as well. The latter only support a full path, while the former supports URLs.


We should probably just no-op syncBuiltinESMExports at the same time

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions