File tree Expand file tree Collapse file tree 3 files changed +67
-1
lines changed
programs/develop/webpack/plugin-extension/feature-web-resources Expand file tree Collapse file tree 3 files changed +67
-1
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 5
5
type Manifest
6
6
} from '../../webpack-types'
7
7
import * as utils from '../../lib/utils'
8
+ import { cleanMatches } from './clean-matches'
8
9
9
10
/**
10
11
* ResourcesPlugin is responsible for adding resources required
@@ -64,7 +65,9 @@ export class WebResourcesPlugin {
64
65
resources : resources . filter (
65
66
( resource ) => ! resource . endsWith ( '.map' )
66
67
) ,
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 ) ,
68
71
} )
69
72
}
70
73
} else {
You can’t perform that action at this time.
0 commit comments