Skip to content

Commit 85d2bc0

Browse files
authored
Fix web_resources match patterns (#178)
* Fix web_resources match patterns
1 parent d829630 commit 85d2bc0

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import { cleanMatches } from "../clean-matches";
3+
4+
describe('cleanMatches', () => {
5+
it('does not handle non-urls', () => {
6+
expect(cleanMatches(['<all_urls>'])).toEqual(['<all_urls>']);
7+
});
8+
9+
it('handles wildcards', () => {
10+
expect(
11+
cleanMatches([
12+
'*://*/some/path',
13+
'https://*.google.com/foo*bar',
14+
])
15+
).toEqual([
16+
'*://*/*',
17+
'https://*.google.com/*',
18+
]);
19+
});
20+
21+
it('cleans up all types of pathnames', () => {
22+
expect(
23+
cleanMatches([
24+
'https://example.com',
25+
'https://example.com/some/path/*',
26+
'https://example.com/some/path',
27+
])
28+
).toEqual([
29+
'https://example.com/*',
30+
'https://example.com/*',
31+
'https://example.com/*',
32+
]);
33+
});
34+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
/**
3+
* From the docs at https://developer.chrome.com/docs/extensions/reference/manifest/web-accessible-resources#manifest_declaration
4+
* > Google Chrome emits an "Invalid match pattern" error if the pattern has a path other than '/*'.
5+
*
6+
* We need to ensure that paths are cleaned up from the matches to avoid this error.
7+
*/
8+
export function cleanMatches(matches: string[]) {
9+
return matches.map(match => {
10+
try {
11+
const url = new URL(
12+
// Using a wildcard for the scheme (`*://`) is supported, but URL cannot parse it
13+
match.replace(/^\*:\/\//, 'https://')
14+
);
15+
16+
// URL.pathname will return `/` even if the URL is just `https://example.com`
17+
if (match.endsWith(url.pathname)) {
18+
return `${match.substring(0, match.length - url.pathname.length)}/*`;
19+
} else if (url.pathname === '/') {
20+
return `${match}/*`;
21+
}
22+
23+
return match;
24+
} catch {
25+
// Special cases like <all_urls> will fail the URL parse, but we can ignore them
26+
return match;
27+
}
28+
})
29+
}

programs/develop/webpack/plugin-extension/feature-web-resources/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type Manifest
66
} from '../../webpack-types'
77
import * as utils from '../../lib/utils'
8+
import { cleanMatches } from './clean-matches'
89

910
/**
1011
* ResourcesPlugin is responsible for adding resources required
@@ -64,7 +65,9 @@ export class WebResourcesPlugin {
6465
resources: resources.filter(
6566
(resource) => !resource.endsWith('.map')
6667
),
67-
matches
68+
// We pass `matches` from `content_scripts` to `web_accessible_resources`,
69+
// but `web_accessible_resources` has stricter rules, so we need to sanitize them
70+
matches: cleanMatches(matches),
6871
})
6972
}
7073
} else {

0 commit comments

Comments
 (0)