Skip to content

Commit 242d59d

Browse files
committed
"excludeMatches" array in scripting.registerContentScripts() API is ignored
https://bugs.webkit.org/show_bug.cgi?id=290858 rdar://145489255 Reviewed by Timothy Hatcher. Add the excluded match pattern to the excludeMatchPatterns set. * Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIScriptingCocoa.mm: (WebKit::WebExtensionContext::createInjectedContentForScripts): * Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPIScripting.mm: (TestWebKitAPI::TEST(WKWebExtensionAPIScripting, RegisteredScriptExcludesMatches)): Canonical link: https://commits.webkit.org/293106@main
1 parent be13d4b commit 242d59d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIScriptingCocoa.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@
419419
*errorMessage = toErrorString(callingAPIName, nullString(), @"script with ID '%@' has an invalid exclude match pattern '%@'", (NSString *)scriptID, matchPatternString);
420420
return false;
421421
}
422+
423+
excludeMatchPatterns.add(matchPattern.releaseNonNull());
422424
}
423425

424426
InjectedContentData injectedContentData;

Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPIScripting.mm

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,65 @@
12351235
[manager run];
12361236
}
12371237

1238+
TEST(WKWebExtensionAPIScripting, RegisteredScriptExcludeMatches)
1239+
{
1240+
TestWebKitAPI::HTTPServer server({
1241+
{ "/"_s, { { { "Content-Type"_s, "text/html"_s } }, ""_s } }
1242+
}, TestWebKitAPI::HTTPServer::Protocol::Http);
1243+
1244+
auto *backgroundScript = Util::constructScript(@[
1245+
@"browser.webNavigation.onCompleted.addListener(async () => {",
1246+
1247+
@" var expectedResults = [{",
1248+
@" id: '1',",
1249+
@" js: ['changeBackgroundColorScript.js'],",
1250+
@" matches: ['*://*/*'],",
1251+
@" persistAcrossSessions: true,",
1252+
@" excludeMatches: ['*://*.example.com/*']",
1253+
@" }]",
1254+
1255+
@" await browser.scripting.registerContentScripts([{ id: '1', matches: ['*://*/*'], js: ['changeBackgroundColorScript.js'], excludeMatches: ['*://*.example.com/*']}])",
1256+
1257+
@" var results = await browser.scripting.getRegisteredContentScripts()",
1258+
@" browser.test.assertDeepEq(results, expectedResults)",
1259+
1260+
@" results = await browser.scripting.getRegisteredContentScripts({ 'ids': ['1'] })",
1261+
@" browser.test.assertDeepEq(results, expectedResults)",
1262+
1263+
@" await browser.scripting.unregisterContentScripts()",
1264+
1265+
@" browser.test.notifyPass()",
1266+
@"})",
1267+
1268+
@"browser.test.sendMessage('Load Tab')",
1269+
]);
1270+
1271+
static auto *resources = @{
1272+
@"background.js": backgroundScript,
1273+
@"changeBackgroundColorScript.js": changeBackgroundColorScript,
1274+
};
1275+
1276+
auto manager = Util::loadExtension(scriptingManifest, resources);
1277+
auto *testContext = manager.get().context;
1278+
1279+
// Confirm that the script can't be injected on example.com.
1280+
auto *exampleURL = [NSURL URLWithString:@"https://example.com/"];
1281+
EXPECT_FALSE([testContext hasInjectedContentForURL:exampleURL]);
1282+
1283+
auto *urlRequest = server.requestWithLocalhost();
1284+
auto *url = urlRequest.URL;
1285+
1286+
auto *matchPattern = [WKWebExtensionMatchPattern matchPatternWithScheme:url.scheme host:url.host path:@"/*"];
1287+
[testContext setPermissionStatus:WKWebExtensionContextPermissionStatusGrantedExplicitly forPermission:WKWebExtensionPermissionWebNavigation];
1288+
[testContext setPermissionStatus:WKWebExtensionContextPermissionStatusGrantedExplicitly forMatchPattern:matchPattern];
1289+
1290+
[manager runUntilTestMessage:@"Load Tab"];
1291+
1292+
[manager.get().defaultTab.webView loadRequest:urlRequest];
1293+
1294+
[manager run];
1295+
}
1296+
12381297
TEST(WKWebExtensionAPIScripting, InjectedOnlyOnce)
12391298
{
12401299
TestWebKitAPI::HTTPServer server({

0 commit comments

Comments
 (0)