-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
perf: optimize CSP nonce generation #14340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
34e0f59
1a01a36
8f6d625
8c42c83
b46856b
282ed06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "tauri": "patch:perf" | ||
| --- | ||
|
|
||
| perf: optimize CSP nonce generation |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ use std::{ | |
| borrow::Cow, | ||
| collections::HashMap, | ||
| fmt, | ||
| fmt::Write, | ||
| sync::{atomic::AtomicBool, Arc, Mutex, MutexGuard}, | ||
| }; | ||
|
|
||
|
|
@@ -127,7 +128,7 @@ fn replace_csp_nonce( | |
| directive: &str, | ||
| hashes: Vec<String>, | ||
| ) { | ||
| let mut nonces = Vec::new(); | ||
| let mut nonces = Vec::with_capacity(asset.matches(token).count()); | ||
| *asset = replace_with_callback(asset, token, || { | ||
| #[cfg(target_pointer_width = "64")] | ||
| let mut raw = [0u8; 8]; | ||
|
|
@@ -141,17 +142,23 @@ fn replace_csp_nonce( | |
| nonce.to_string() | ||
| }); | ||
|
|
||
| if !(nonces.is_empty() && hashes.is_empty()) { | ||
| let nonce_sources = nonces | ||
| .into_iter() | ||
| .map(|n| format!("'nonce-{n}'")) | ||
| .collect::<Vec<String>>(); | ||
| if !nonces.is_empty() || !hashes.is_empty() { | ||
| let sources = csp.entry(directive.into()).or_default(); | ||
| let self_source = "'self'".to_string(); | ||
| if !sources.contains(&self_source) { | ||
| let self_source = "'self'"; | ||
| if !sources.contains(self_source) { | ||
| sources.push(self_source); | ||
| } | ||
| sources.extend(nonce_sources); | ||
| #[cfg(target_pointer_width = "64")] | ||
| let mut buf = String::with_capacity(28); | ||
| #[cfg(target_pointer_width = "32")] | ||
| let mut buf = String::with_capacity(20); | ||
| #[cfg(target_pointer_width = "16")] | ||
| let mut buf = String::with_capacity(14); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these capacities meant to be how many bytes to alloc to store If we want to be a bit fancy-pants (😛) we could use this helper function: const fn nonce_source_capacity() -> usize {
// 'nonce-' prefix + digits of usize::max + closing quote
8 + usize::MAX.ilog10() as usize + 1
}(You can test that by replacing usize for u64, u32 and u16 respectively) |
||
| for nonce in nonces { | ||
| buf.clear(); | ||
| write!(&mut buf, "'nonce-{}'", nonce).unwrap(); | ||
| sources.push(buf.clone()); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reusing for nonce in nonces {
let mut buf = String::with_capacity(nonce_capacity());
write!(&mut buf, "'nonce-{}'", nonce).unwrap();
sources.push(buf);
}Something like that ^ would allocate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I honestly doubt these optimizations will actually make any differences, format! Itself is quite optimized to estimate the buffer size already |
||
| sources.extend(hashes); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lucasfernog do you remember why we used
usizehere at the first place (tracing back to cf54dcf)? It doesn't quite make sense to meUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I don't get the question, but the code is still using a
usizethere, just that it's using its underlying byte representation instead, right?In any case, seems like this code can be simplified down to:
Or even:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant it doesn't make sense to make nonce length depends on target pointer size