Skip to content

Commit cff1666

Browse files
committed
fix(runtime): read import-map strings exactly
The import-map parser read keys, targets, scope keys and section names through Utf8Value, so an embedded U+0000 cut them short, and a cut-short target or scope key is broader than written. They now go through an exact UTF-16 to UTF-8 conversion and are kept whole: a specifier key or scope key containing U+0000 matches only those exact bytes. A target containing U+0000 is rejected with a TypeError when the map is validated, since resolving it as a path or fetching it as a URL would end it there. V8's JSON parser accepts lone surrogates, which have no UTF-8 form; a key, target or scope key holding one is rejected as not a string.
1 parent dd54307 commit cff1666

3 files changed

Lines changed: 89 additions & 13 deletions

File tree

‎docs/ns-builtin-modules.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,18 +576,19 @@ session's vocabulary. Every message is prefixed `configureLoader: `.
576576
| any import-map section other than `imports`/`scopes` | `unsupported import-map section '<name>'; only "imports" and "scopes" are supported` |
577577
| `imports` is not an object | `the "imports" section must be an object` |
578578
| `scopes` is not an object | `the "scopes" section must be an object` |
579-
| non-string scope key | `scopes: every scope key must be a string` |
579+
| non-string scope key, or one with a lone surrogate | `scopes: every scope key must be a string` |
580580
| empty scope key | `scopes: a scope key must not be empty` |
581581
| a scope's value is not an object | `scopes: the map for scope '<scope>' must be an object` |
582582
583583
Inside either section — labelled `imports` or `scope '<scope>'`:
584584
585585
| condition | message |
586586
|---|---|
587-
| non-string specifier key | `<section>: every key must be a string` |
587+
| non-string specifier key, or one with a lone surrogate | `<section>: every key must be a string` |
588588
| empty specifier key | `<section>: a specifier key must not be empty` |
589-
| non-string target | `<section>: the target for '<specifier>' must be a string` |
589+
| non-string target, or one with a lone surrogate | `<section>: the target for '<specifier>' must be a string` |
590590
| empty target | `<section>: the target for '<specifier>' must not be empty` |
591+
| target containing U+0000 | `<section>: the target for '<specifier>' must not contain U+0000` |
591592
| trailing-slash key, non-trailing-slash target | `<section>: the target for '<specifier>' must end with '/' because the specifier key does` |
592593
593594
#### Registry canonicalization

‎test-app/app/src/main/assets/app/tests/testEsmHttpLoader.js‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,21 @@ describe("HTTP ESM Loader", function () {
567567
});
568568
});
569569

570+
it("keeps an http(s) scope key containing U+0000 whole", function (done) {
571+
var base = origin + "/esm/resolve/nul-scope";
572+
var scopes = {};
573+
scopes[base + "/a\u0000/b/"] = { "ns-nul-scope": base + "/target.mjs?k=nul" };
574+
nsModule.configureLoader({ importMap: { imports: {}, scopes: scopes } });
575+
importer(origin, "nul-scope/a").then(function (mod) {
576+
return rejectionMessage(mod.load("ns-nul-scope"));
577+
}).then(function (message) {
578+
expect(message).toContain("not in the import map");
579+
done();
580+
}).catch(function (error) {
581+
reportRejection(error, done);
582+
});
583+
});
584+
570585
it("rejects an import() whose import-map target resolves to nothing", function (done) {
571586
nsModule.configureLoader({
572587
importMap: {
@@ -870,6 +885,53 @@ describe("HTTP ESM Loader", function () {
870885
}).toThrowError(TypeError, /must be a string/);
871886
});
872887

888+
it("rejects a target containing U+0000 and keeps the previous map", function (done) {
889+
setMap({ imports: { "ns-nul-prev": "~/esm/query-map/stripped.mjs" } });
890+
expect(function () {
891+
setMap({
892+
imports: {
893+
"ns-nul-target": "~/esm/query-map/literal.mjs\u0000/../../x.mjs",
894+
},
895+
});
896+
}).toThrowError(TypeError,
897+
"configureLoader: imports: the target for 'ns-nul-target' " +
898+
"must not contain U+0000");
899+
import("ns-nul-prev").then(function (ns) {
900+
expect(ns.name).toBe("stripped");
901+
done();
902+
}).catch(function (error) {
903+
expect("rejected: " + formatError(error)).toBe("resolved");
904+
done();
905+
});
906+
});
907+
908+
it("matches a specifier key containing U+0000 whole", function (done) {
909+
setMap({ imports: { "ns-nul-key\u0000x": "~/esm/query-map/literal.mjs" } });
910+
import("ns-nul-key").then(function () {
911+
expect("resolved").toBe("rejected");
912+
done();
913+
}, function (error) {
914+
expect(String((error && error.message) || error)).toContain("Cannot find module");
915+
done();
916+
});
917+
});
918+
919+
it("rejects a key, target or scope key with a lone surrogate as not a string",
920+
function () {
921+
var scopes = {};
922+
scopes["/a\uD800/"] = { "pkg": "~/esm/query-map/literal.mjs" };
923+
expect(function () {
924+
setMap({ imports: { "pkg\uD800": "~/esm/query-map/literal.mjs" } });
925+
}).toThrowError(TypeError, "configureLoader: imports: every key must be a string");
926+
expect(function () {
927+
setMap({ imports: { "pkg": "~/esm/query-map/literal\uDC00.mjs" } });
928+
}).toThrowError(TypeError,
929+
"configureLoader: imports: the target for 'pkg' must be a string");
930+
expect(function () {
931+
setMap({ imports: {}, scopes: scopes });
932+
}).toThrowError(TypeError, "configureLoader: scopes: every scope key must be a string");
933+
});
934+
873935
it("rejects a non-object scope map", function () {
874936
expect(function () {
875937
setMap({ scopes: { "/a/": "not-an-object" } });

‎test-app/runtime/src/main/cpp/ModuleInternalCallbacks.cpp‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <cstring>
1313
#include <memory>
1414
#include <mutex>
15+
#include <optional>
1516
#include <string>
1617
#include <time.h>
1718
#include <vector>
@@ -716,12 +717,14 @@ static bool ParseImportMapEntries(v8::Isolate* isolate, v8::Local<v8::Context> c
716717
*error = sectionLabel + ": every key must be a string";
717718
return false;
718719
}
719-
v8::String::Utf8Value keyUtf8(isolate, keyVal);
720-
if (!*keyUtf8) {
720+
// V8's JSON parser accepts lone surrogates, which have no UTF-8 form, so
721+
// a key or target holding one is reported as not a string.
722+
const std::optional<std::string> keyUtf8 = ToExactUtf8(isolate, keyVal.As<v8::String>());
723+
if (!keyUtf8.has_value()) {
721724
*error = sectionLabel + ": every key must be a string";
722725
return false;
723726
}
724-
const std::string specifier(*keyUtf8);
727+
const std::string& specifier = *keyUtf8;
725728
if (specifier.empty()) {
726729
*error = sectionLabel + ": a specifier key must not be empty";
727730
return false;
@@ -732,16 +735,22 @@ static bool ParseImportMapEntries(v8::Isolate* isolate, v8::Local<v8::Context> c
732735
*error = sectionLabel + ": the target for '" + specifier + "' must be a string";
733736
return false;
734737
}
735-
v8::String::Utf8Value valueUtf8(isolate, value);
736-
if (!*valueUtf8) {
738+
const std::optional<std::string> valueUtf8 = ToExactUtf8(isolate, value.As<v8::String>());
739+
if (!valueUtf8.has_value()) {
737740
*error = sectionLabel + ": the target for '" + specifier + "' must be a string";
738741
return false;
739742
}
740-
const std::string target(*valueUtf8);
743+
const std::string& target = *valueUtf8;
741744
if (target.empty()) {
742745
*error = sectionLabel + ": the target for '" + specifier + "' must not be empty";
743746
return false;
744747
}
748+
// A target resolves as a filesystem path or is fetched as a URL, and both
749+
// go through C-string APIs that would end it at U+0000.
750+
if (target.find('\0') != std::string::npos) {
751+
*error = sectionLabel + ": the target for '" + specifier + "' must not contain U+0000";
752+
return false;
753+
}
745754

746755
// A trailing-slash key maps a whole subtree, so its target must name one
747756
// too — otherwise the remainder would be pasted onto a file path.
@@ -811,8 +820,7 @@ static bool ParseImportMap(v8::Isolate* isolate, const std::string& json,
811820
v8::Local<v8::Value> sectionVal;
812821
std::string name;
813822
if (sections->Get(context, i).ToLocal(&sectionVal) && sectionVal->IsString()) {
814-
v8::String::Utf8Value utf8(isolate, sectionVal);
815-
if (*utf8) name = *utf8;
823+
name = ToExactUtf8(isolate, sectionVal.As<v8::String>()).value_or(std::string());
816824
}
817825
if (name == "imports") {
818826
hasImports = true;
@@ -859,8 +867,13 @@ static bool ParseImportMap(v8::Isolate* isolate, const std::string& json,
859867
*error = "scopes: every scope key must be a string";
860868
return false;
861869
}
862-
v8::String::Utf8Value scopeUtf8(isolate, scopeKeyVal);
863-
const std::string scopePrefix(*scopeUtf8 ? *scopeUtf8 : "");
870+
const std::optional<std::string> scopeUtf8 =
871+
ToExactUtf8(isolate, scopeKeyVal.As<v8::String>());
872+
if (!scopeUtf8.has_value()) {
873+
*error = "scopes: every scope key must be a string";
874+
return false;
875+
}
876+
const std::string& scopePrefix = *scopeUtf8;
864877
if (scopePrefix.empty()) {
865878
*error = "scopes: a scope key must not be empty";
866879
return false;

0 commit comments

Comments
 (0)