@@ -1486,6 +1486,35 @@ static void KillAsyncGraphLoadsForIsolate(v8::Isolate* isolate) {
14861486static void AsyncGraphEnqueue (const std::shared_ptr<AsyncGraphLoad>& load,
14871487 const ModuleResolution& resolution);
14881488
1489+ // A specifier's UTF-8, sized by length, or nullopt when it contains U+0000:
1490+ // the path and URL APIs behind resolution read C strings and would load
1491+ // whatever precedes it.
1492+ static std::optional<std::string> ModuleSpecifierUtf8 (v8::Isolate* isolate,
1493+ v8::Local<v8::String> specifier) {
1494+ v8::String::Utf8Value utf8 (isolate, specifier);
1495+ std::string spec = *utf8 ? std::string (*utf8, utf8.length ()) : std::string ();
1496+ if (spec.find (' \0 ' ) != std::string::npos) {
1497+ return std::nullopt ;
1498+ }
1499+ return spec;
1500+ }
1501+
1502+ static std::string NulSpecifierMessage (v8::Isolate* isolate, v8::Local<v8::String> specifier) {
1503+ v8::String::Utf8Value utf8 (isolate, specifier);
1504+ std::string shown;
1505+ if (*utf8) {
1506+ for (int i = 0 ; i < utf8.length (); i++) {
1507+ if ((*utf8)[i] == ' \0 ' ) {
1508+ shown += " \\ u0000" ;
1509+ } else {
1510+ shown += (*utf8)[i];
1511+ }
1512+ }
1513+ }
1514+ return " Cannot resolve module specifier \" " + shown +
1515+ " \" : a module specifier must not contain U+0000" ;
1516+ }
1517+
14891518// Walk `mod`'s static module requests and enqueue every edge the walk can
14901519// resolve. JS thread only; `moduleKey` is the registry key the module was
14911520// registered under, which is also the referrer for relative resolution.
@@ -1499,18 +1528,18 @@ static void AsyncGraphWalkModuleRequests(
14991528 v8::Local<v8::ModuleRequest> request =
15001529 requests->Get (i).As <v8::ModuleRequest>();
15011530 if (request.IsEmpty ()) continue ;
1502- v8::Local<v8::String> specV8 = request->GetSpecifier ();
1503- v8::String::Utf8Value specUtf8 (isolate, specV8);
1504- if (!*specUtf8) continue ;
1531+ // A specifier holding U+0000 is left for the resolver to refuse at link.
1532+ const std::optional<std::string> spec =
1533+ ModuleSpecifierUtf8 (isolate, request->GetSpecifier ());
1534+ if (!spec.has_value ()) continue ;
15051535 // Builtins are served by the resolver from the builtin registry, and an
15061536 // unresolved specifier (typically a bare name with no import-map entry,
15071537 // or one an HTTP importer may not use) stays on the resolver's lazy path —
15081538 // where it either resolves later or fails with the resolver's own
15091539 // message. An unmapped bare specifier's subtree is therefore not
15101540 // discovered here; any HTTP edge inside it is pathological and lands on
15111541 // the synchronous anomaly guard.
1512- const ModuleResolution resolution =
1513- ResolveSpecifierToPath (*specUtf8, moduleKey);
1542+ const ModuleResolution resolution = ResolveSpecifierToPath (*spec, moduleKey);
15141543 if (resolution.kind != ModuleResolution::Kind::kHttp &&
15151544 resolution.kind != ModuleResolution::Kind::kFile ) {
15161545 continue ;
@@ -2265,8 +2294,13 @@ v8::MaybeLocal<v8::Module> ResolveModuleCallback(
22652294 return v8::MaybeLocal<v8::Module>();
22662295 }
22672296
2268- v8::String::Utf8Value specUtf8 (isolate, specifier);
2269- const std::string rawSpec = *specUtf8 ? *specUtf8 : " " ;
2297+ const std::optional<std::string> spec = ModuleSpecifierUtf8 (isolate, specifier);
2298+ if (!spec.has_value ()) {
2299+ isolate->ThrowException (v8::Exception::Error (
2300+ ArgConverter::ConvertToV8String (isolate, NulSpecifierMessage (isolate, specifier))));
2301+ return v8::MaybeLocal<v8::Module>();
2302+ }
2303+ const std::string& rawSpec = *spec;
22702304 if (rawSpec.empty ()) return v8::MaybeLocal<v8::Module>();
22712305
22722306 const std::string referrerPath =
@@ -2530,9 +2564,21 @@ v8::MaybeLocal<v8::Promise> ImportModuleDynamicallyCallback(
25302564 auto & modulesInFlight = moduleState->modulesInFlight ;
25312565 auto & httpDynamicWaiters = moduleState->httpDynamicWaiters ;
25322566
2533- v8::String::Utf8Value specUtf8 (isolate, specifier);
2534- const char * cSpec = (*specUtf8) ? *specUtf8 : " <invalid>" ;
2535- TNS_DEBUG (Esm, " [dyn-import] -> %s" , cSpec);
2567+ const std::optional<std::string> specUtf8 = ModuleSpecifierUtf8 (isolate, specifier);
2568+ if (!specUtf8.has_value ()) {
2569+ v8::EscapableHandleScope nulScope (isolate);
2570+ v8::Local<v8::Promise::Resolver> nulResolver;
2571+ if (!v8::Promise::Resolver::New (context).ToLocal (&nulResolver)) {
2572+ return v8::MaybeLocal<v8::Promise>();
2573+ }
2574+ nulResolver
2575+ ->Reject (context, v8::Exception::Error (ArgConverter::ConvertToV8String (
2576+ isolate, NulSpecifierMessage (isolate, specifier))))
2577+ .FromMaybe (false );
2578+ return nulScope.Escape (nulResolver->GetPromise ());
2579+ }
2580+ std::string rawSpec = *specUtf8;
2581+ TNS_DEBUG (Esm, " [dyn-import] -> %s" , rawSpec.c_str ());
25362582 if (LogCategoryEnabled (LogCategory::Esm)) {
25372583 v8::Local<v8::Value> resName = resource_name;
25382584 if (!resName.IsEmpty () && resName->IsString ()) {
@@ -2543,8 +2589,6 @@ v8::MaybeLocal<v8::Promise> ImportModuleDynamicallyCallback(
25432589 }
25442590 }
25452591
2546- std::string rawSpec = cSpec ? std::string (cSpec) : std::string ();
2547-
25482592 // Builtin modules never touch the loader below; the namespace comes straight
25492593 // from the realm's synthetic module.
25502594 if (NsBuiltinModules::IsBuiltinScheme (rawSpec)) {
0 commit comments