From 68fb73dec8f47a128de79cefda091d725996fe11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 15 Nov 2023 10:50:50 +0100 Subject: [PATCH] src: iterate on import attributes array correctly The array's length is supposed to be a multiple of two for dynamic import callbacks. Fixes: https://github.com/nodejs/node/issues/50700 --- src/module_wrap.cc | 12 ++++++++---- test/es-module/test-esm-import-attributes-errors.js | 5 +++++ test/es-module/test-esm-import-attributes-errors.mjs | 5 +++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/module_wrap.cc b/src/module_wrap.cc index bad60d02dfe953..52976d61814256 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -255,10 +255,14 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { } static Local createImportAttributesContainer( - Realm* realm, Isolate* isolate, Local raw_attributes) { + Realm* realm, + Isolate* isolate, + Local raw_attributes, + const int elements_per_attribute) { + CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0); Local attributes = Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0); - for (int i = 0; i < raw_attributes->Length(); i += 3) { + for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) { attributes ->Set(realm->context(), raw_attributes->Get(realm->context(), i).As(), @@ -304,7 +308,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo& args) { Local raw_attributes = module_request->GetImportAssertions(); Local attributes = - createImportAttributesContainer(realm, isolate, raw_attributes); + createImportAttributesContainer(realm, isolate, raw_attributes, 3); Local argv[] = { specifier, @@ -588,7 +592,7 @@ static MaybeLocal ImportModuleDynamically( } Local attributes = - createImportAttributesContainer(realm, isolate, import_attributes); + createImportAttributesContainer(realm, isolate, import_attributes, 2); Local import_args[] = { id, diff --git a/test/es-module/test-esm-import-attributes-errors.js b/test/es-module/test-esm-import-attributes-errors.js index 243277fdcd7d29..828ded7e684342 100644 --- a/test/es-module/test-esm-import-attributes-errors.js +++ b/test/es-module/test-esm-import-attributes-errors.js @@ -26,6 +26,11 @@ async function test() { { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } ); + await rejects( + import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }), + { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } + ); + await rejects( import(jsModuleDataUrl, { with: { type: 'unsupported' } }), { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } diff --git a/test/es-module/test-esm-import-attributes-errors.mjs b/test/es-module/test-esm-import-attributes-errors.mjs index c29cc459326c4b..887a1b97cc26ee 100644 --- a/test/es-module/test-esm-import-attributes-errors.mjs +++ b/test/es-module/test-esm-import-attributes-errors.mjs @@ -21,6 +21,11 @@ await rejects( { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } ); +await rejects( + import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }), + { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } +); + await rejects( import(import.meta.url, { with: { type: 'unsupported' } }), { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }