Skip to content

Commit 309dfd4

Browse files
committed
fix(runtime): keep a dot-segment http(s) scope key from widening its scope
An import-map scope key such as `http://host/a/..` parses to `http://host/`. Dropping the parser's trailing slash whenever the written key lacked one turned that into the prefix `http://host`, which also matches referrers on `http://host.evil/`; `http://host/a/.` likewise matched `/ab/...`. Only a key written with no path at all now loses the parser's slash.
1 parent 3ac5f74 commit 309dfd4

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

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

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

570+
it("does not widen an http(s) scope whose key ends in a dot segment", function (done) {
571+
var base = origin + "/esm/resolve/scope-dot-end";
572+
var scopes = {};
573+
scopes[base + "/sub/."] = { "ns-scope-dot-end": base + "/target.mjs?k=sub" };
574+
scopes[base + "/up/a/.."] = { "ns-scope-dot-end": base + "/target.mjs?k=up" };
575+
nsModule.configureLoader({ importMap: { imports: {}, scopes: scopes } });
576+
function load(dir) {
577+
return importer(origin, "scope-dot-end/" + dir).then(function (mod) {
578+
return mod.load("ns-scope-dot-end").then(function (m) {
579+
return m.query;
580+
}, function (error) {
581+
return String((error && error.message) || error);
582+
});
583+
});
584+
}
585+
Promise.all([load("sub"), load("subx"), load("up"), load("upx")]).then(function (r) {
586+
expect(r[0]).toBe("k=sub");
587+
expect(r[1]).toContain("it is not in the import map");
588+
expect(r[2]).toBe("k=up");
589+
expect(r[3]).toContain("it is not in the import map");
590+
done();
591+
}).catch(function (error) {
592+
reportRejection(error, done);
593+
});
594+
});
595+
570596
it("keeps an http(s) scope key containing U+0000 whole", function (done) {
571597
var base = origin + "/esm/resolve/nul-scope";
572598
var scopes = {};

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,14 +767,23 @@ static bool ParseImportMapEntries(v8::Isolate* isolate, v8::Local<v8::Context> c
767767

768768
// A scope key matches referrer registry keys by plain prefix, and an http(s)
769769
// referrer key is spelled by NormalizeHttpModuleUrl, so an http(s) scope key
770-
// is spelled the same way. Whether it ends in '/' stays as written: the parser
771-
// gives a path-less URL the path "/", which would narrow the prefix.
770+
// is spelled the same way. A key written with no path at all stays path-less:
771+
// the parser gives it the path "/", which would narrow the prefix. Any other
772+
// trailing '/' is the parser's and stays, so dot segments can't widen a scope
773+
// past a path-segment boundary.
772774
static std::string ImportMapScopePrefix(const std::string& scopeKey) {
773775
std::string prefix = NormalizeHttpModuleUrl(scopeKey);
774776
if (prefix.empty()) {
775777
return scopeKey;
776778
}
777-
if (scopeKey.back() != '/' && prefix.back() == '/') {
779+
const size_t colon = scopeKey.find(':');
780+
const size_t authority = colon == std::string::npos
781+
? std::string::npos
782+
: scopeKey.find_first_not_of("/\\", colon + 1);
783+
const bool writtenWithoutPath =
784+
authority != std::string::npos &&
785+
scopeKey.find_first_of("/\\?#", authority) == std::string::npos;
786+
if (writtenWithoutPath && prefix.back() == '/') {
778787
prefix.pop_back();
779788
}
780789
return prefix;

0 commit comments

Comments
 (0)