Skip to content

Commit

Permalink
fix(manifest v3): web accessible resource match rewrite is broken (#48)
Browse files Browse the repository at this point in the history
handles content script match patterns like *://*/* correctly
  • Loading branch information
samrum committed Sep 6, 2022
1 parent 17d97d3 commit 4e2ba91
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 25 deletions.
13 changes: 8 additions & 5 deletions src/manifestParser/manifestV3.ts
@@ -1,5 +1,4 @@
import { OutputBundle } from "rollup";
import { URL } from "url";
import { ParseResult } from "./manifestParser";
import {
isSingleHtmlFilename,
Expand Down Expand Up @@ -123,13 +122,17 @@ export default class ManifestV3 extends ManifestParser<Manifest> {
webAccessibleResources.add({
resources: Array.from(parsedContentScript.webAccessibleFiles),
matches: script.matches!.map((matchPattern) => {
const url = new URL(matchPattern);
const pathMatch = /[^:\/]\//.exec(matchPattern);
if (!pathMatch) {
return matchPattern;
}

if (url.pathname === "/") {
return `${url}`;
const path = matchPattern.slice(pathMatch.index + 1);
if (["/", "/*"].includes(path)) {
return matchPattern;
}

return `${url.origin}/*`;
return matchPattern.replace(path, "/*");
}),
// @ts-ignore - use_dynamic_url is a newly supported option
use_dynamic_url: true,
Expand Down
Expand Up @@ -7,11 +7,25 @@ const inputManifest = {
content_scripts: [
{
js: [`${resourceDir}/content1.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
{
js: [`${resourceDir}/content2.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
};
Expand All @@ -20,22 +34,50 @@ const expectedManifest = {
content_scripts: [
{
js: [`${resourceDir}/content1.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
{
js: [`${resourceDir}/content2.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
web_accessible_resources: [
{
resources: [`assets/${resourceDir}/content1.js`, "assets/log.js"],
matches: ["https://*/*", "http://*/*", "http://example.com/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/*",
"https://example.com/*",
],
use_dynamic_url: true,
},
{
resources: [`assets/${resourceDir}/content2.js`, "assets/log.js"],
matches: ["https://*/*", "http://*/*", "http://example.com/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/*",
"https://example.com/*",
],
use_dynamic_url: true,
},
],
Expand Down
Expand Up @@ -7,11 +7,25 @@ const inputManifest = {
content_scripts: [
{
js: [`${resourceDir}/content1.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
{
js: [`${resourceDir}/content2.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
};
Expand All @@ -20,11 +34,25 @@ const expectedManifest = {
content_scripts: [
{
js: [`${resourceDir}/content1.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
{
js: [`${resourceDir}/content2.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
web_accessible_resources: [
Expand All @@ -34,7 +62,14 @@ const expectedManifest = {
"assets/preload-helper.js",
"assets/log.js",
],
matches: ["https://*/*", "http://*/*", "http://example.com/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/*",
"https://example.com/*",
],
use_dynamic_url: true,
},
{
Expand All @@ -43,7 +78,14 @@ const expectedManifest = {
"assets/preload-helper.js",
"assets/log.js",
],
matches: ["https://*/*", "http://*/*", "http://example.com/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/*",
"https://example.com/*",
],
use_dynamic_url: true,
},
],
Expand Down
18 changes: 16 additions & 2 deletions test/fixture/index/javascript/manifestV3/contentWithNoImports.ts
Expand Up @@ -7,7 +7,14 @@ const inputManifest = {
content_scripts: [
{
js: [`${resourceDir}/content.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
};
Expand All @@ -16,7 +23,14 @@ const expectedManifest = {
content_scripts: [
{
js: [`assets/${resourceDir}/content.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
};
Expand Down
Expand Up @@ -7,11 +7,25 @@ const inputManifest = {
content_scripts: [
{
js: [`${resourceDir}/content1/content.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
{
js: [`${resourceDir}/content2/content.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
};
Expand All @@ -20,11 +34,25 @@ const expectedManifest = {
content_scripts: [
{
js: [`assets/${resourceDir}/content1/content.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
{
js: [`assets/${resourceDir}/content2/content.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
};
Expand Down
Expand Up @@ -8,7 +8,14 @@ const inputManifest = {
content_scripts: [
{
js: [`${resourceDir}/content.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
};
Expand All @@ -17,7 +24,14 @@ const expectedManifest = {
content_scripts: [
{
js: [`assets/${resourceDir}/content.js`],
matches: ["https://*/*", "http://*/*", "http://example.com/subpath/*"],
matches: [
"*://*/*",
"https://*/*",
"*://example.com/",
"https://example.com/",
"*://example.com/subpath/*",
"https://example.com/subpath/*",
],
},
],
};
Expand Down

0 comments on commit 4e2ba91

Please sign in to comment.