@@ -117,11 +117,12 @@ export async function generateManifest(
117
117
if ( wxt . config . manifestVersion === 2 ) {
118
118
convertWebAccessibleResourcesToMv2 ( manifest ) ;
119
119
convertActionToMv2 ( manifest ) ;
120
+ convertCspToMv2 ( manifest ) ;
120
121
moveHostPermissionsToPermissions ( manifest ) ;
121
122
}
122
123
123
124
if ( wxt . config . manifestVersion === 3 ) {
124
- validateMv3WebAccessbileResources ( manifest ) ;
125
+ validateMv3WebAccessibleResources ( manifest ) ;
125
126
}
126
127
127
128
stripKeys ( manifest ) ;
@@ -143,7 +144,7 @@ export async function generateManifest(
143
144
}
144
145
145
146
/**
146
- * Removes suffixes from the version, like X.Y.Z-alpha1 (which brosers don't allow), so it's a
147
+ * Removes suffixes from the version, like X.Y.Z-alpha1 (which browsers don't allow), so it's a
147
148
* simple version number, like X or X.Y or X.Y.Z, which browsers allow.
148
149
*/
149
150
function simplifyVersion ( versionName : string ) : string {
@@ -467,34 +468,28 @@ function addDevModeCsp(manifest: Manifest.WebExtensionManifest): void {
467
468
}
468
469
469
470
const extensionPagesCsp = new ContentSecurityPolicy (
470
- manifest . manifest_version === 3
471
- ? // @ts -expect-error: extension_pages is not typed
472
- ( manifest . content_security_policy ?. extension_pages ??
473
- "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';" ) // default extension_pages CSP for MV3
474
- : ( manifest . content_security_policy ??
475
- "script-src 'self'; object-src 'self';" ) , // default CSP for MV2
471
+ // @ts -expect-error: extension_pages exists, we convert MV2 CSPs to this earlier in the process
472
+ manifest . content_security_policy ?. extension_pages ??
473
+ ( manifest . manifest_version === 3
474
+ ? DEFAULT_MV3_EXTENSION_PAGES_CSP
475
+ : DEFAULT_MV2_CSP ) ,
476
476
) ;
477
477
const sandboxCsp = new ContentSecurityPolicy (
478
478
// @ts -expect-error: sandbox is not typed
479
- manifest . content_security_policy ?. sandbox ??
480
- "sandbox allow-scripts allow-forms allow-popups allow-modals; script-src 'self' 'unsafe-inline' 'unsafe-eval'; child-src 'self';" , // default sandbox CSP for MV3
479
+ manifest . content_security_policy ?. sandbox ?? DEFAULT_MV3_SANDBOX_CSP ,
481
480
) ;
482
481
483
- if ( wxt . server ) {
482
+ if ( wxt . config . command === 'serve' ) {
484
483
extensionPagesCsp . add ( 'script-src' , allowedCsp ) ;
485
484
sandboxCsp . add ( 'script-src' , allowedCsp ) ;
486
485
}
487
486
488
- if ( manifest . manifest_version === 3 ) {
489
- manifest . content_security_policy ??= { } ;
490
- // @ts -expect-error: extension_pages is not typed
491
- manifest . content_security_policy . extension_pages =
492
- extensionPagesCsp . toString ( ) ;
493
- // @ts -expect-error: sandbox is not typed
494
- manifest . content_security_policy . sandbox = sandboxCsp . toString ( ) ;
495
- } else {
496
- manifest . content_security_policy = extensionPagesCsp . toString ( ) ;
497
- }
487
+ manifest . content_security_policy ??= { } ;
488
+ // @ts -expect-error: extension_pages is not typed
489
+ manifest . content_security_policy . extension_pages =
490
+ extensionPagesCsp . toString ( ) ;
491
+ // @ts -expect-error: sandbox is not typed
492
+ manifest . content_security_policy . sandbox = sandboxCsp . toString ( ) ;
498
493
}
499
494
500
495
function addDevModePermissions ( manifest : Manifest . WebExtensionManifest ) {
@@ -613,7 +608,7 @@ export function stripPathFromMatchPattern(pattern: string) {
613
608
/**
614
609
* Converts all MV3 web accessible resources to their MV2 forms. MV3 web accessible resources are
615
610
* generated in this file, and may be defined by the user in their manifest. In both cases, when
616
- * targetting MV2, automatically convert their definitions down to the basic MV2 array.
611
+ * targeting MV2, automatically convert their definitions down to the basic MV2 array.
617
612
*/
618
613
export function convertWebAccessibleResourcesToMv2 (
619
614
manifest : Manifest . WebExtensionManifest ,
@@ -652,10 +647,21 @@ function convertActionToMv2(manifest: Manifest.WebExtensionManifest): void {
652
647
manifest . browser_action = manifest . action ;
653
648
}
654
649
650
+ function convertCspToMv2 ( manifest : Manifest . WebExtensionManifest ) : void {
651
+ if (
652
+ typeof manifest . content_security_policy === 'string' ||
653
+ manifest . content_security_policy ?. extension_pages == null
654
+ )
655
+ return ;
656
+
657
+ manifest . content_security_policy =
658
+ manifest . content_security_policy . extension_pages ;
659
+ }
660
+
655
661
/**
656
662
* Make sure all resources are in MV3 format. If not, add a wanring
657
663
*/
658
- export function validateMv3WebAccessbileResources (
664
+ export function validateMv3WebAccessibleResources (
659
665
manifest : Manifest . WebExtensionManifest ,
660
666
) : void {
661
667
if ( manifest . web_accessible_resources == null ) return ;
@@ -718,3 +724,9 @@ const mv3OnlyKeys = [
718
724
'side_panel' ,
719
725
] ;
720
726
const firefoxMv3OnlyKeys = [ 'host_permissions' ] ;
727
+
728
+ const DEFAULT_MV3_EXTENSION_PAGES_CSP =
729
+ "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';" ;
730
+ const DEFAULT_MV3_SANDBOX_CSP =
731
+ "sandbox allow-scripts allow-forms allow-popups allow-modals; script-src 'self' 'unsafe-inline' 'unsafe-eval'; child-src 'self';" ;
732
+ const DEFAULT_MV2_CSP = "script-src 'self'; object-src 'self';" ;
0 commit comments