Skip to content

Commit bc5b5e6

Browse files
authored
chore(linux): remove CSP tag on custom protocol response (#8984)
1 parent 6cb601d commit bc5b5e6

5 files changed

Lines changed: 12 additions & 26 deletions

File tree

.changes/csp-header-linux.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch:enhance
3+
"tauri-utils": patch:enhance
4+
"tauri-codegen": patch:enhance
5+
---
6+
7+
Do not include a CSP tag in the application HTML and rely on the custom protocol response header instead.

core/tauri-codegen/src/context.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub struct ContextData {
4040

4141
fn map_core_assets(
4242
options: &AssetOptions,
43-
target: Target,
4443
) -> impl Fn(&AssetKey, &Path, &mut Vec<u8>, &mut CspHashes) -> Result<(), EmbeddedAssetsError> {
4544
#[cfg(feature = "isolation")]
4645
let pattern = tauri_utils::html::PatternObject::from(&options.pattern);
@@ -53,10 +52,6 @@ fn map_core_assets(
5352
if csp {
5453
let document = parse_html(String::from_utf8_lossy(input).into_owned());
5554

56-
if target == Target::Linux {
57-
::tauri_utils::html::inject_csp_token(&document);
58-
}
59-
6055
inject_nonce_token(&document, &dangerous_disable_asset_csp_modification);
6156

6257
if dangerous_disable_asset_csp_modification.can_modify("script-src") {
@@ -176,15 +171,15 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
176171
path
177172
)
178173
}
179-
EmbeddedAssets::new(assets_path, &options, map_core_assets(&options, target))?
174+
EmbeddedAssets::new(assets_path, &options, map_core_assets(&options))?
180175
}
181176
FrontendDist::Files(files) => EmbeddedAssets::new(
182177
files
183178
.iter()
184179
.map(|p| config_parent.join(p))
185180
.collect::<Vec<_>>(),
186181
&options,
187-
map_core_assets(&options, target),
182+
map_core_assets(&options),
188183
)?,
189184
_ => unimplemented!(),
190185
},

core/tauri-runtime-wry/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2854,7 +2854,6 @@ fn handle_user_message<T: UserEvent>(
28542854
}
28552855
// Getters
28562856
WebviewMessage::Url(tx) => {
2857-
println!("url getter");
28582857
tx.send(webview.url().parse().unwrap()).unwrap();
28592858
}
28602859
WebviewMessage::Position(tx) => {

core/tauri-utils/src/html.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ use crate::config::{DisabledCspModificationKind, PatternKind};
2323
#[cfg(feature = "isolation")]
2424
use crate::pattern::isolation::IsolationJavascriptCodegen;
2525

26-
/// The token used on the CSP tag content.
27-
pub const CSP_TOKEN: &str = "__TAURI_CSP__";
2826
/// The token used for script nonces.
2927
pub const SCRIPT_NONCE_TOKEN: &str = "__TAURI_SCRIPT_NONCE__";
3028
/// The token used for style nonces.
@@ -168,11 +166,6 @@ pub fn inject_csp(document: &NodeRef, csp: &str) {
168166
});
169167
}
170168

171-
/// Injects a content security policy token to the HTML.
172-
pub fn inject_csp_token(document: &NodeRef) {
173-
inject_csp(document, CSP_TOKEN)
174-
}
175-
176169
fn create_csp_meta_tag(csp: &str) -> NodeRef {
177170
NodeRef::new_element(
178171
QualName::new(None, ns!(html), LocalName::from("meta")),
@@ -298,12 +291,12 @@ mod tests {
298291
];
299292
for html in htmls {
300293
let document = kuchiki::parse_html().one(html);
301-
super::inject_csp_token(&document);
294+
let csp = "csp-string";
295+
super::inject_csp(&document, csp);
302296
assert_eq!(
303297
document.to_string(),
304298
format!(
305-
r#"<html><head><meta http-equiv="Content-Security-Policy" content="{}"></head><body></body></html>"#,
306-
super::CSP_TOKEN
299+
r#"<html><head><meta http-equiv="Content-Security-Policy" content="{csp}"></head><body></body></html>"#,
307300
)
308301
);
309302
}

core/tauri/src/protocol/tauri.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,6 @@ fn get_response<R: Runtime>(
164164
if let Some(handler) = &web_resource_request_handler {
165165
handler(request, &mut response);
166166
}
167-
// if it's an HTML file, we need to set the CSP meta tag on Linux
168-
#[cfg(target_os = "linux")]
169-
if let Some(response_csp) = response.headers().get("Content-Security-Policy") {
170-
let response_csp = String::from_utf8_lossy(response_csp.as_bytes());
171-
let html = String::from_utf8_lossy(response.body());
172-
let body = html.replacen(tauri_utils::html::CSP_TOKEN, &response_csp, 1);
173-
*response.body_mut() = body.as_bytes().to_vec().into();
174-
}
175167

176168
Ok(response)
177169
}

0 commit comments

Comments
 (0)