Skip to content

Commit

Permalink
Handle the case where an extension could be unloaded after compiling …
Browse files Browse the repository at this point in the history
…DNR rules

https://bugs.webkit.org/show_bug.cgi?id=271493
rdar://123900589

Reviewed by Timothy Hatcher.

If the extension has been unloaded, iterating the user content controllers will lead to a crash.

* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionContextCocoa.mm:
(WebKit::WebExtensionContext::addDeclarativeNetRequestRulesToPrivateUserContentControllers):
(WebKit::WebExtensionContext::compileDeclarativeNetRequestRules):

Canonical link: https://commits.webkit.org/276578@main
  • Loading branch information
b-weinstein committed Mar 22, 2024
1 parent a50b786 commit 7f19d5a
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4114,6 +4114,10 @@ void effectiveAppearanceDidChange(Inspector::ExtensionAppearance appearance) ove
if (!ruleList)
return;

// The extension could have been unloaded before this was called.
if (!isLoaded())
return;

for (auto& controller : extensionController()->allPrivateUserContentControllers())
controller.addContentRuleList(*ruleList, m_baseURL);
});
Expand Down Expand Up @@ -4159,6 +4163,12 @@ void effectiveAppearanceDidChange(Inspector::ExtensionAppearance appearance) ove

dispatch_async(dispatch_get_main_queue(), makeBlockPtr([this, protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler), previouslyLoadedHash = String { previouslyLoadedHash }, hashOfWebKitRules = String { hashOfWebKitRules }, webKitRules = String { webKitRules }]() mutable {
API::ContentRuleListStore::defaultStore().lookupContentRuleListFile(declarativeNetRequestContentRuleListFilePath(), uniqueIdentifier().isolatedCopy(), [this, protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler), previouslyLoadedHash, hashOfWebKitRules, webKitRules](RefPtr<API::ContentRuleList> foundRuleList, std::error_code) mutable {
// The extension could have been unloaded before this was called.
if (!isLoaded()) {
completionHandler(false);
return;
}

if (foundRuleList) {
if ([previouslyLoadedHash isEqualToString:hashOfWebKitRules]) {
for (auto& userContentController : userContentControllers())
Expand All @@ -4176,6 +4186,12 @@ void effectiveAppearanceDidChange(Inspector::ExtensionAppearance appearance) ove
return;
}

// The extension could have been unloaded before this was called.
if (!isLoaded()) {
completionHandler(false);
return;
}

[m_state setObject:hashOfWebKitRules forKey:lastLoadedDeclarativeNetRequestHashStateKey];
writeStateToStorage();

Expand Down

0 comments on commit 7f19d5a

Please sign in to comment.