Skip to content

Commit 96514a8

Browse files
targosrichardlau
authored andcommitted
src: iterate on import attributes array correctly
The array's length is supposed to be a multiple of two for dynamic import callbacks. Fixes: #50700 PR-URL: #50703 Backport-PR-URL: #51136 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Shelley Vohr <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 06646e1 commit 96514a8

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/module_wrap.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,14 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
250250
}
251251

252252
static Local<Object> createImportAttributesContainer(
253-
Environment* env, Isolate* isolate, Local<FixedArray> raw_attributes) {
253+
Environment* env,
254+
Isolate* isolate,
255+
Local<FixedArray> raw_attributes,
256+
const int elements_per_attribute) {
257+
CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
254258
Local<Object> attributes =
255259
Object::New(isolate, v8::Null(env->isolate()), nullptr, nullptr, 0);
256-
for (int i = 0; i < raw_attributes->Length(); i += 3) {
260+
for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) {
257261
attributes
258262
->Set(env->context(),
259263
raw_attributes->Get(env->context(), i).As<String>(),
@@ -299,7 +303,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
299303

300304
Local<FixedArray> raw_attributes = module_request->GetImportAssertions();
301305
Local<Object> attributes =
302-
createImportAttributesContainer(env, isolate, raw_attributes);
306+
createImportAttributesContainer(env, isolate, raw_attributes, 3);
303307

304308
Local<Value> argv[] = {
305309
specifier,
@@ -583,7 +587,7 @@ static MaybeLocal<Promise> ImportModuleDynamically(
583587
options->Get(context, HostDefinedOptions::kID).As<Symbol>();
584588

585589
Local<Object> attributes =
586-
createImportAttributesContainer(env, isolate, import_attributes);
590+
createImportAttributesContainer(env, isolate, import_attributes, 2);
587591

588592
Local<Value> import_args[] = {
589593
id,

test/es-module/test-esm-import-attributes-errors.js

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ async function test() {
2626
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
2727
);
2828

29+
await rejects(
30+
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
31+
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
32+
);
33+
2934
await rejects(
3035
import(jsModuleDataUrl, { with: { type: 'unsupported' } }),
3136
{ code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }

test/es-module/test-esm-import-attributes-errors.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ await rejects(
2121
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
2222
);
2323

24+
await rejects(
25+
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
26+
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
27+
);
28+
2429
await rejects(
2530
import(import.meta.url, { with: { type: 'unsupported' } }),
2631
{ code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }

0 commit comments

Comments
 (0)