@@ -11,7 +11,7 @@ use sha2::{Digest, Sha256};
1111
1212use tauri_utils:: assets:: AssetKey ;
1313use tauri_utils:: config:: { AppUrl , Config , PatternKind , WindowUrl } ;
14- use tauri_utils:: html:: { inject_nonce_token, parse as parse_html, NodeRef } ;
14+ use tauri_utils:: html:: { inject_nonce_token, parse as parse_html} ;
1515
1616#[ cfg( feature = "shell-scope" ) ]
1717use tauri_utils:: config:: { ShellAllowedArg , ShellAllowedArgs , ShellAllowlistScope } ;
@@ -26,32 +26,14 @@ pub struct ContextData {
2626 pub root : TokenStream ,
2727}
2828
29- fn load_csp ( document : & mut NodeRef , key : & AssetKey , csp_hashes : & mut CspHashes ) {
30- inject_nonce_token ( document) ;
31- if let Ok ( inline_script_elements) = document. select ( "script:not(empty)" ) {
32- let mut scripts = Vec :: new ( ) ;
33- for inline_script_el in inline_script_elements {
34- let script = inline_script_el. as_node ( ) . text_contents ( ) ;
35- let mut hasher = Sha256 :: new ( ) ;
36- hasher. update ( & script) ;
37- let hash = hasher. finalize ( ) ;
38- scripts. push ( format ! ( "'sha256-{}'" , base64:: encode( & hash) ) ) ;
39- }
40- csp_hashes
41- . inline_scripts
42- . entry ( key. clone ( ) . into ( ) )
43- . or_default ( )
44- . append ( & mut scripts) ;
45- }
46- }
47-
4829fn map_core_assets (
4930 options : & AssetOptions ,
5031) -> impl Fn ( & AssetKey , & Path , & mut Vec < u8 > , & mut CspHashes ) -> Result < ( ) , EmbeddedAssetsError > {
5132 #[ cfg( feature = "isolation" ) ]
5233 let pattern = tauri_utils:: html:: PatternObject :: from ( & options. pattern ) ;
5334 let csp = options. csp ;
54- let dangerous_disable_asset_csp_modification = options. dangerous_disable_asset_csp_modification ;
35+ let dangerous_disable_asset_csp_modification =
36+ options. dangerous_disable_asset_csp_modification . clone ( ) ;
5537 move |key, path, input, csp_hashes| {
5638 if path. extension ( ) == Some ( OsStr :: new ( "html" ) ) {
5739 let mut document = parse_html ( String :: from_utf8_lossy ( input) . into_owned ( ) ) ;
@@ -61,10 +43,28 @@ fn map_core_assets(
6143 #[ cfg( target_os = "linux" ) ]
6244 :: tauri_utils:: html:: inject_csp_token ( & mut document) ;
6345
64- if !dangerous_disable_asset_csp_modification {
65- load_csp ( & mut document, key, csp_hashes) ;
46+ inject_nonce_token ( & mut document, & dangerous_disable_asset_csp_modification) ;
47+
48+ if dangerous_disable_asset_csp_modification. can_modify ( "script-src" ) {
49+ if let Ok ( inline_script_elements) = document. select ( "script:not(empty)" ) {
50+ let mut scripts = Vec :: new ( ) ;
51+ for inline_script_el in inline_script_elements {
52+ let script = inline_script_el. as_node ( ) . text_contents ( ) ;
53+ let mut hasher = Sha256 :: new ( ) ;
54+ hasher. update ( & script) ;
55+ let hash = hasher. finalize ( ) ;
56+ scripts. push ( format ! ( "'sha256-{}'" , base64:: encode( & hash) ) ) ;
57+ }
58+ csp_hashes
59+ . inline_scripts
60+ . entry ( key. clone ( ) . into ( ) )
61+ . or_default ( )
62+ . append ( & mut scripts) ;
63+ }
64+ }
6665
67- #[ cfg( feature = "isolation" ) ]
66+ #[ cfg( feature = "isolation" ) ]
67+ if dangerous_disable_asset_csp_modification. can_modify ( "style-src" ) {
6868 if let tauri_utils:: html:: PatternObject :: Isolation { .. } = & pattern {
6969 // create the csp for the isolation iframe styling now, to make the runtime less complex
7070 let mut hasher = Sha256 :: new ( ) ;
@@ -116,7 +116,14 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
116116 } = data;
117117
118118 let mut options = AssetOptions :: new ( config. tauri . pattern . clone ( ) )
119- . freeze_prototype ( config. tauri . security . freeze_prototype ) ;
119+ . freeze_prototype ( config. tauri . security . freeze_prototype )
120+ . dangerous_disable_asset_csp_modification (
121+ config
122+ . tauri
123+ . security
124+ . dangerous_disable_asset_csp_modification
125+ . clone ( ) ,
126+ ) ;
120127 let csp = if dev {
121128 config
122129 . tauri
0 commit comments