@@ -18,7 +18,7 @@ use tauri_utils::acl::resolved::Resolved;
1818use tauri_utils:: assets:: AssetKey ;
1919use tauri_utils:: config:: { CapabilityEntry , Config , FrontendDist , PatternKind } ;
2020use tauri_utils:: html:: {
21- inject_nonce_token, parse as parse_html, serialize_node as serialize_html_node,
21+ inject_nonce_token, parse as parse_html, serialize_node as serialize_html_node, NodeRef ,
2222} ;
2323use tauri_utils:: platform:: Target ;
2424use tauri_utils:: tokens:: { map_lit, str_lit} ;
@@ -38,11 +38,30 @@ pub struct ContextData {
3838 pub capabilities : Option < Vec < PathBuf > > ,
3939}
4040
41+ fn inject_script_hashes ( document : & NodeRef , key : & AssetKey , csp_hashes : & mut CspHashes ) {
42+ if let Ok ( inline_script_elements) = document. select ( "script:not(empty)" ) {
43+ let mut scripts = Vec :: new ( ) ;
44+ for inline_script_el in inline_script_elements {
45+ let script = inline_script_el. as_node ( ) . text_contents ( ) ;
46+ let mut hasher = Sha256 :: new ( ) ;
47+ hasher. update ( & script) ;
48+ let hash = hasher. finalize ( ) ;
49+ scripts. push ( format ! (
50+ "'sha256-{}'" ,
51+ base64:: engine:: general_purpose:: STANDARD . encode( hash)
52+ ) ) ;
53+ }
54+ csp_hashes
55+ . inline_scripts
56+ . entry ( key. clone ( ) . into ( ) )
57+ . or_default ( )
58+ . append ( & mut scripts) ;
59+ }
60+ }
61+
4162fn map_core_assets (
4263 options : & AssetOptions ,
4364) -> impl Fn ( & AssetKey , & Path , & mut Vec < u8 > , & mut CspHashes ) -> Result < ( ) , EmbeddedAssetsError > {
44- #[ cfg( feature = "isolation" ) ]
45- let pattern = tauri_utils:: html:: PatternObject :: from ( & options. pattern ) ;
4665 let csp = options. csp ;
4766 let dangerous_disable_asset_csp_modification =
4867 options. dangerous_disable_asset_csp_modification . clone ( ) ;
@@ -55,38 +74,7 @@ fn map_core_assets(
5574 inject_nonce_token ( & document, & dangerous_disable_asset_csp_modification) ;
5675
5776 if dangerous_disable_asset_csp_modification. can_modify ( "script-src" ) {
58- if let Ok ( inline_script_elements) = document. select ( "script:not(empty)" ) {
59- let mut scripts = Vec :: new ( ) ;
60- for inline_script_el in inline_script_elements {
61- let script = inline_script_el. as_node ( ) . text_contents ( ) ;
62- let mut hasher = Sha256 :: new ( ) ;
63- hasher. update ( & script) ;
64- let hash = hasher. finalize ( ) ;
65- scripts. push ( format ! (
66- "'sha256-{}'" ,
67- base64:: engine:: general_purpose:: STANDARD . encode( hash)
68- ) ) ;
69- }
70- csp_hashes
71- . inline_scripts
72- . entry ( key. clone ( ) . into ( ) )
73- . or_default ( )
74- . append ( & mut scripts) ;
75- }
76- }
77-
78- #[ cfg( feature = "isolation" ) ]
79- if dangerous_disable_asset_csp_modification. can_modify ( "style-src" ) {
80- if let tauri_utils:: html:: PatternObject :: Isolation { .. } = & pattern {
81- // create the csp for the isolation iframe styling now, to make the runtime less complex
82- let mut hasher = Sha256 :: new ( ) ;
83- hasher. update ( tauri_utils:: pattern:: isolation:: IFRAME_STYLE ) ;
84- let hash = hasher. finalize ( ) ;
85- csp_hashes. styles . push ( format ! (
86- "'sha256-{}'" ,
87- base64:: engine:: general_purpose:: STANDARD . encode( hash)
88- ) ) ;
89- }
77+ inject_script_hashes ( & document, key, csp_hashes) ;
9078 }
9179
9280 * input = serialize_html_node ( & document) ;
@@ -101,16 +89,34 @@ fn map_isolation(
10189 _options : & AssetOptions ,
10290 dir : PathBuf ,
10391) -> impl Fn ( & AssetKey , & Path , & mut Vec < u8 > , & mut CspHashes ) -> Result < ( ) , EmbeddedAssetsError > {
104- move |_key, path, input, _csp_hashes| {
92+ // create the csp for the isolation iframe styling now, to make the runtime less complex
93+ let mut hasher = Sha256 :: new ( ) ;
94+ hasher. update ( tauri_utils:: pattern:: isolation:: IFRAME_STYLE ) ;
95+ let hash = hasher. finalize ( ) ;
96+ let iframe_style_csp_hash = format ! (
97+ "'sha256-{}'" ,
98+ base64:: engine:: general_purpose:: STANDARD . encode( hash)
99+ ) ;
100+
101+ move |key, path, input, csp_hashes| {
105102 if path. extension ( ) == Some ( OsStr :: new ( "html" ) ) {
106- let isolation_html = tauri_utils :: html :: parse ( String :: from_utf8_lossy ( input) . into_owned ( ) ) ;
103+ let isolation_html = parse_html ( String :: from_utf8_lossy ( input) . into_owned ( ) ) ;
107104
108105 // this is appended, so no need to reverse order it
109106 tauri_utils:: html:: inject_codegen_isolation_script ( & isolation_html) ;
110107
111108 // temporary workaround for windows not loading assets
112109 tauri_utils:: html:: inline_isolation ( & isolation_html, & dir) ;
113110
111+ inject_nonce_token (
112+ & isolation_html,
113+ & tauri_utils:: config:: DisabledCspModificationKind :: Flag ( false ) ,
114+ ) ;
115+
116+ inject_script_hashes ( & isolation_html, key, csp_hashes) ;
117+
118+ csp_hashes. styles . push ( iframe_style_csp_hash. clone ( ) ) ;
119+
114120 * input = isolation_html. to_string ( ) . as_bytes ( ) . to_vec ( )
115121 }
116122
0 commit comments