From 480b203dea4fe136c5a798784d15e23f0f5c83cd Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sat, 24 Oct 2026 22:00:31 -0500 Subject: [PATCH] Issue #2464 - Part 9: Implement importmap integrity verification. https://bugzilla.mozilla.org/show_bug.cgi?id=1945540 --- dom/locales/en-US/chrome/dom/dom.properties | 4 +- dom/script/ImportMap.cpp | 100 +++++++++++++++++++- dom/script/ImportMap.h | 15 ++- dom/script/ModuleLoadRequest.cpp | 5 +- dom/script/ScriptLoader.cpp | 25 +++++ dom/script/ScriptLoader.h | 3 + js/src/js.msg | 1 + 7 files changed, 146 insertions(+), 7 deletions(-) diff --git a/dom/locales/en-US/chrome/dom/dom.properties b/dom/locales/en-US/chrome/dom/dom.properties index 838afae727..a6d314d84b 100644 --- a/dom/locales/en-US/chrome/dom/dom.properties +++ b/dom/locales/en-US/chrome/dom/dom.properties @@ -334,4 +334,6 @@ ImportMapEmptySpecifierKeys=Specifier keys cannot be the empty string. ImportMapAddressesNotStrings=Addresses need to be strings. ImportMapInvalidAddress=Address for “%S” was invalid. ImportMapAddressNotEndsWithSlash=An invalid address was given for the specifier key “%S”; since the specifier ended with a slash, the address needs to as well. -ImportMapScopePrefixNotParseable=The scope prefix URL “%S” was not parseable. \ No newline at end of file +ImportMapScopePrefixNotParseable=The scope prefix URL “%S” was not parseable. +ImportMapIntegrityKeyNotParseable=Import map integrity key %S was not parseable as a URL. +ImportMapIntegrityValueNotAString=Import map integrity value for %S needs to be a string. \ No newline at end of file diff --git a/dom/script/ImportMap.cpp b/dom/script/ImportMap.cpp index db0978727b..82e234a7a2 100644 --- a/dom/script/ImportMap.cpp +++ b/dom/script/ImportMap.cpp @@ -331,6 +331,58 @@ SortAndNormalizeScopes(JSContext* aCx, JS::HandleObject aOriginalMap, nsIURI* aB return normalized; } +// https://html.spec.whatwg.org/multipage/webappapis.html#normalize-a-module-integrity-map +static UniquePtr +NormalizeModuleIntegrityMap(JSContext* aCx, JS::HandleObject aOriginalMap, + nsIURI* aBaseURL, + const ReportWarningHelper& aWarning) +{ + UniquePtr normalized = MakeUnique(); + + JS::Rooted keys(aCx, JS::IdVector(aCx)); + if (!JS_Enumerate(aCx, aOriginalMap, &keys)) { + return nullptr; + } + + for (size_t i = 0; i < keys.length(); i++) { + const JS::RootedId keyId(aCx, keys[i]); + nsAutoJSString key; + NS_ENSURE_TRUE(key.init(aCx, keyId), nullptr); + + // Step 2.1. Let resolvedURL be the result of resolving a URL-like module + // specifier given key and baseURL. + auto parseResult = ResolveURLLikeModuleSpecifier(key, aBaseURL); + + // Step 2.2. If resolvedURL is null, warn and continue. + if (parseResult.isErr()) { + AutoTArray params; + params.AppendElement(key); + aWarning.Report("ImportMapIntegrityKeyNotParseable", params); + continue; + } + + JS::RootedValue idVal(aCx); + NS_ENSURE_TRUE(JS_GetPropertyById(aCx, aOriginalMap, keyId, &idVal), nullptr); + + // Step 2.3. If value is not a string, warn and continue. + if (!idVal.isString()) { + AutoTArray params; + params.AppendElement(key); + aWarning.Report("ImportMapIntegrityValueNotAString", params); + continue; + } + + nsAutoJSString value; + NS_ENSURE_TRUE(value.init(aCx, idVal), nullptr); + + // Step 2.4. Set normalized[resolvedURL] to value. + normalized->insert_or_assign(parseResult.unwrap()->GetSpecOrDefault(), + nsString(value)); + } + + return normalized; +} + // https://html.spec.whatwg.org/multipage/webappapis.html#parse-an-import-map-string // static UniquePtr @@ -431,6 +483,34 @@ ImportMap::ParseString(JSContext* aCx, JS::SourceBufferHolder& aInput, nsIURI* a } } + JS::RootedValue integrityVal(aCx); + if (!GetOwnProperty(aCx, parsedObj, "integrity", &integrityVal)) { + return nullptr; + } + + // Impl note: If parsed["integrity"] doesn't exist, we will allocate + // sortedAndNormalizedIntegrity to an empty map below. + UniquePtr sortedAndNormalizedIntegrity = nullptr; + + if (!integrityVal.isUndefined()) { + bool isMap; + if (!IsMapObject(aCx, integrityVal, &isMap)) { + return nullptr; + } + if (!isMap) { + JS_ReportErrorNumberASCII(aCx, js::GetErrorMessage, nullptr, + JSMSG_IMPORT_MAPS_INTEGRITY_NOT_A_MAP); + return nullptr; + } + + JS::RootedObject integrityObj(aCx, &integrityVal.toObject()); + sortedAndNormalizedIntegrity = NormalizeModuleIntegrityMap( + aCx, integrityObj, aBaseURL, aWarning); + if (!sortedAndNormalizedIntegrity) { + return nullptr; + } + } + // Step 7. If parsed's keys contains any items besides "imports" or // "scopes", then the user agent should report a warning to the console // indicating that an invalid top-level key was present in the import map. @@ -443,7 +523,8 @@ ImportMap::ParseString(JSContext* aCx, JS::SourceBufferHolder& aInput, nsIURI* a const JS::RootedId key(aCx, keys[i]); nsAutoJSString val; NS_ENSURE_TRUE(val.init(aCx, key), nullptr); - if (val.EqualsLiteral("imports") || val.EqualsLiteral("scopes")) { + if (val.EqualsLiteral("imports") || val.EqualsLiteral("scopes") || + val.EqualsLiteral("integrity")) { continue; } @@ -460,12 +541,27 @@ ImportMap::ParseString(JSContext* aCx, JS::SourceBufferHolder& aInput, nsIURI* a if (!sortedAndNormalizedScopes) { sortedAndNormalizedScopes = MakeUnique(); } + if (!sortedAndNormalizedIntegrity) { + sortedAndNormalizedIntegrity = MakeUnique(); + } // Step 8. Return an import map whose imports are // sortedAndNormalizedImports and whose scopes scopes are // sortedAndNormalizedScopes. return MakeUnique(std::move(sortedAndNormalizedImports), - std::move(sortedAndNormalizedScopes)); + std::move(sortedAndNormalizedScopes), + std::move(sortedAndNormalizedIntegrity)); +} + +const nsString* ImportMap::GetIntegrity(nsIURI* aURL) const { + if (!mIntegrity || !aURL) { + return nullptr; + } + auto it = mIntegrity->find(aURL->GetSpecOrDefault()); + if (it == mIntegrity->end()) { + return nullptr; + } + return &it->second; } // https://url.spec.whatwg.org/#is-special diff --git a/dom/script/ImportMap.h b/dom/script/ImportMap.h index 45a7e77026..ad79892cbc 100644 --- a/dom/script/ImportMap.h +++ b/dom/script/ImportMap.h @@ -55,6 +55,10 @@ using SpecifierMap = using ScopeMap = std::map, std::greater>; +// Integrity map from import maps. +// https://html.spec.whatwg.org/multipage/webappapis.html#module-integrity-map +using IntegrityMap = std::map; + /** * Implementation of Import maps. * https://html.spec.whatwg.org/multipage/webappapis.html#import-maps @@ -62,8 +66,11 @@ using ScopeMap = std::map, class ImportMap { public: ImportMap(mozilla::UniquePtr aImports, - mozilla::UniquePtr aScopes) - : mImports(std::move(aImports)), mScopes(std::move(aScopes)) {} + mozilla::UniquePtr aScopes, + mozilla::UniquePtr aIntegrity) + : mImports(std::move(aImports)), + mScopes(std::move(aScopes)), + mIntegrity(std::move(aIntegrity)) {} /** * Parse the JSON string from the Import map script. @@ -76,6 +83,9 @@ class ImportMap { JSContext* aCx, JS::SourceBufferHolder& aInput, nsIURI* aBaseURL, const ReportWarningHelper& aWarning); + // https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-integrity-metadata + const nsString* GetIntegrity(nsIURI* aURL) const; + // Implements // https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier static ResolveResult ResolveModuleSpecifier(ImportMap* aImportMap, @@ -96,6 +106,7 @@ class ImportMap { */ mozilla::UniquePtr mImports; mozilla::UniquePtr mScopes; + mozilla::UniquePtr mIntegrity; }; } // namespace dom diff --git a/dom/script/ModuleLoadRequest.cpp b/dom/script/ModuleLoadRequest.cpp index 310d882d21..889f4dd179 100644 --- a/dom/script/ModuleLoadRequest.cpp +++ b/dom/script/ModuleLoadRequest.cpp @@ -57,7 +57,8 @@ static VisitedURLSet* NewVisitedSetForTopLevelImport(nsIURI* aURI) { /* static */ ModuleLoadRequest* ModuleLoadRequest::CreateStaticImport( nsIURI* aURI, ModuleLoadRequest* aParent) { auto request = - new ModuleLoadRequest(aURI, aParent->mFetchOptions, SRIMetadata(), + new ModuleLoadRequest(aURI, aParent->mFetchOptions, + aParent->mLoader->ComputeModuleIntegrity(aURI), aParent->mURI, false, /* is top level */ false, /* is dynamic import */ aParent->mLoader, aParent->mVisitedSet); @@ -77,7 +78,7 @@ static VisitedURLSet* NewVisitedSetForTopLevelImport(nsIURI* aURI) { MOZ_ASSERT(aPromise); auto request = new ModuleLoadRequest( - aURI, aFetchOptions, SRIMetadata(), aBaseURL, + aURI, aFetchOptions, aLoader->ComputeModuleIntegrity(aURI), aBaseURL, true, /* is top level */ true, /* is dynamic import */ aLoader, NewVisitedSetForTopLevelImport(aURI)); diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index e31915cb7c..147b33ff3f 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -3297,5 +3297,30 @@ ScriptLoader::RegisterImportMap(UniquePtr aImportMap) mImportMap = std::move(aImportMap); } +// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-integrity-metadata +// https://html.spec.whatwg.org/multipage/webappapis.html#get-the-descendant-script-fetch-options +SRIMetadata +ScriptLoader::ComputeModuleIntegrity(nsIURI* aURI) +{ + SRIMetadata integrity; + ImportMap* importMap = GetImportMap(); + if (!importMap || !aURI) { + return integrity; + } + + // Step 2. If map's integrity[url] does not exist, return the empty string. + const nsString* metadata = importMap->GetIntegrity(aURI); + if (!metadata || metadata->IsEmpty()) { + return integrity; + } + + nsAutoCString sourceUri; + if (mDocument && mDocument->GetDocumentURI()) { + mDocument->GetDocumentURI()->GetAsciiSpec(sourceUri); + } + SRICheck::IntegrityMetadata(*metadata, sourceUri, mReporter, &integrity); + return integrity; +} + } // dom namespace } // mozilla namespace diff --git a/dom/script/ScriptLoader.h b/dom/script/ScriptLoader.h index dcca6df7d8..98d1a42e3f 100644 --- a/dom/script/ScriptLoader.h +++ b/dom/script/ScriptLoader.h @@ -612,6 +612,9 @@ public: ImportMap* GetImportMap() { return mImportMap.get(); } + // https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-integrity-metadata + SRIMetadata ComputeModuleIntegrity(nsIURI* aURI); + // https://html.spec.whatwg.org/multipage/webappapis.html#concept-import-maps-allowed bool IsImportMapAllowed() const { return mImportMapsAllowed; } diff --git a/js/src/js.msg b/js/src/js.msg index fa1fbbf438..a93bdd4bc3 100644 --- a/js/src/js.msg +++ b/js/src/js.msg @@ -617,6 +617,7 @@ MSG_DEF(JSMSG_IMPORT_MAPS_NOT_A_MAP, 0, JSEXN_TYPEERR, "the top-leve MSG_DEF(JSMSG_IMPORT_MAPS_IMPORTS_NOT_A_MAP, 0, JSEXN_TYPEERR, "the imports top-level key needs to be a JSON object") MSG_DEF(JSMSG_IMPORT_MAPS_SCOPES_NOT_A_MAP, 0, JSEXN_TYPEERR, "the scopes top-level key needs to be a JSON object") MSG_DEF(JSMSG_IMPORT_MAPS_SCOPE_VALUE_NOT_A_MAP, 1, JSEXN_TYPEERR, "the value of the scope with prefix '{0}' needs to be a JSON object") +MSG_DEF(JSMSG_IMPORT_MAPS_INTEGRITY_NOT_A_MAP, 0, JSEXN_TYPEERR, "The value of the \"integrity\" top-level key in an import map needs to be a JSON object") // Promise MSG_DEF(JSMSG_CANNOT_RESOLVE_PROMISE_WITH_ITSELF, 0, JSEXN_TYPEERR, "A promise cannot be resolved with itself.")