@@ -11,7 +11,7 @@ use sha2::{Digest, Sha256};
11
11
12
12
use tauri_utils:: assets:: AssetKey ;
13
13
use 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} ;
15
15
16
16
#[ cfg( feature = "shell-scope" ) ]
17
17
use tauri_utils:: config:: { ShellAllowedArg , ShellAllowedArgs , ShellAllowlistScope } ;
@@ -26,32 +26,14 @@ pub struct ContextData {
26
26
pub root : TokenStream ,
27
27
}
28
28
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
-
48
29
fn map_core_assets (
49
30
options : & AssetOptions ,
50
31
) -> impl Fn ( & AssetKey , & Path , & mut Vec < u8 > , & mut CspHashes ) -> Result < ( ) , EmbeddedAssetsError > {
51
32
#[ cfg( feature = "isolation" ) ]
52
33
let pattern = tauri_utils:: html:: PatternObject :: from ( & options. pattern ) ;
53
34
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 ( ) ;
55
37
move |key, path, input, csp_hashes| {
56
38
if path. extension ( ) == Some ( OsStr :: new ( "html" ) ) {
57
39
let mut document = parse_html ( String :: from_utf8_lossy ( input) . into_owned ( ) ) ;
@@ -61,10 +43,28 @@ fn map_core_assets(
61
43
#[ cfg( target_os = "linux" ) ]
62
44
:: tauri_utils:: html:: inject_csp_token ( & mut document) ;
63
45
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
+ }
66
65
67
- #[ cfg( feature = "isolation" ) ]
66
+ #[ cfg( feature = "isolation" ) ]
67
+ if dangerous_disable_asset_csp_modification. can_modify ( "style-src" ) {
68
68
if let tauri_utils:: html:: PatternObject :: Isolation { .. } = & pattern {
69
69
// create the csp for the isolation iframe styling now, to make the runtime less complex
70
70
let mut hasher = Sha256 :: new ( ) ;
@@ -116,7 +116,14 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
116
116
} = data;
117
117
118
118
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
+ ) ;
120
127
let csp = if dev {
121
128
config
122
129
. tauri
0 commit comments