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

esm: protect ESM loader from prototype pollution #45044

Merged
merged 1 commit into from Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/internal/bootstrap/loaders.js
Expand Up @@ -53,6 +53,7 @@ const {
ObjectDefineProperty,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
ReflectGet,
SafeMap,
SafeSet,
Expand Down Expand Up @@ -281,6 +282,8 @@ class BuiltinModule {
getESMFacade() {
if (this.module) return this.module;
const { ModuleWrap } = internalBinding('module_wrap');
// TODO(aduh95): move this to C++, alongside the initialization of the class.
ObjectSetPrototypeOf(ModuleWrap.prototype, null);
Comment on lines +285 to +286
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initialization is done here

node/src/module_wrap.cc

Lines 763 to 773 in 4eaaa17

void ModuleWrap::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
Local<FunctionTemplate> tpl = NewFunctionTemplate(isolate, New);
tpl->InstanceTemplate()->SetInternalFieldCount(
ModuleWrap::kInternalFieldCount);
tpl->Inherit(BaseObject::GetConstructorTemplate(env));

I couldn't find a way to change the prototype from there. @RaisinTen do you know how to do that by any chance?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 I spoke to @LeszekSwirski about this on the Chromium Slack and it seems like this is not possible to do using V8's public API, so I've opened a feature request asking for this - https://bugs.chromium.org/p/v8/issues/detail?id=13392.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK bummer. Thanks a lot for looking into this and opening the feature request issue :)

const url = `node:${this.id}`;
const nativeModule = this;
const exportsKeys = ArrayPrototypeSlice(this.exportKeys);
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/modules/esm/load.js
Expand Up @@ -59,7 +59,7 @@ async function getSource(url, context) {
if (policy?.manifest) {
policy.manifest.assertIntegrity(parsed, source);
}
return { responseURL, source };
return { __proto__: null, responseURL, source };
}


Expand Down Expand Up @@ -93,6 +93,7 @@ async function defaultLoad(url, context) {
}

return {
__proto__: null,
format,
responseURL,
source,
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/modules/esm/loader.js
Expand Up @@ -664,6 +664,7 @@ class ESMLoader {
}

return {
__proto__: null,
format,
responseURL,
source,
Expand Down Expand Up @@ -880,6 +881,7 @@ class ESMLoader {
}

return {
__proto__: null,
format,
url,
};
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/module_job.js
Expand Up @@ -215,7 +215,7 @@ class ModuleJob {
}
throw e;
}
return { module: this.module };
return { __proto__: null, module: this.module };
}
}
ObjectSetPrototypeOf(ModuleJob.prototype, null);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/resolve.js
Expand Up @@ -1017,7 +1017,7 @@ async function defaultResolve(specifier, context = {}) {
)
)
) {
return { url: parsed.href };
return { __proto__: null, url: parsed.href };
}
} catch {
// Ignore exception
Expand Down
17 changes: 17 additions & 0 deletions test/es-module/test-cjs-prototype-pollution.js
@@ -0,0 +1,17 @@
'use strict';

const { mustNotCall, mustCall } = require('../common');

Object.defineProperties(Array.prototype, {
// %Promise.all% and %Promise.allSettled% are depending on the value of
// `%Array.prototype%.then`.
then: {},
});
Object.defineProperties(Object.prototype, {
then: {
set: mustNotCall('set %Object.prototype%.then'),
get: mustNotCall('get %Object.prototype%.then'),
},
});

import('data:text/javascript,').then(mustCall());
15 changes: 15 additions & 0 deletions test/es-module/test-esm-prototype-pollution.mjs
@@ -0,0 +1,15 @@
import { mustNotCall, mustCall } from '../common/index.mjs';

Object.defineProperties(Array.prototype, {
// %Promise.all% and %Promise.allSettled% are depending on the value of
// `%Array.prototype%.then`.
then: {},
});
Object.defineProperties(Object.prototype, {
then: {
set: mustNotCall('set %Object.prototype%.then'),
get: mustNotCall('get %Object.prototype%.then'),
},
});

import('data:text/javascript,').then(mustCall());
29 changes: 26 additions & 3 deletions test/parallel/test-primordials-promise.js
Expand Up @@ -18,9 +18,32 @@ Promise.all = common.mustNotCall('%Promise%.all');
Promise.allSettled = common.mustNotCall('%Promise%.allSettled');
Promise.any = common.mustNotCall('%Promise%.any');
Promise.race = common.mustNotCall('%Promise%.race');
Promise.prototype.catch = common.mustNotCall('%Promise.prototype%.catch');
Promise.prototype.finally = common.mustNotCall('%Promise.prototype%.finally');
Promise.prototype.then = common.mustNotCall('%Promise.prototype%.then');

Object.defineProperties(Promise.prototype, {
catch: {
set: common.mustNotCall('set %Promise.prototype%.catch'),
get: common.mustNotCall('get %Promise.prototype%.catch'),
},
finally: {
set: common.mustNotCall('set %Promise.prototype%.finally'),
get: common.mustNotCall('get %Promise.prototype%.finally'),
},
then: {
set: common.mustNotCall('set %Promise.prototype%.then'),
get: common.mustNotCall('get %Promise.prototype%.then'),
},
});
Object.defineProperties(Array.prototype, {
// %Promise.all% and %Promise.allSettled% are depending on the value of
// `%Array.prototype%.then`.
then: {},
});
Object.defineProperties(Object.prototype, {
then: {
set: common.mustNotCall('set %Object.prototype%.then'),
get: common.mustNotCall('get %Object.prototype%.then'),
},
});

assertIsPromise(PromisePrototypeThen(test(), common.mustCall()));
assertIsPromise(SafePromisePrototypeFinally(test(), common.mustCall()));
Expand Down