From cc7fb7ad30e0077a799349a53c32b73622aa33c1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 9 Apr 2025 13:56:58 +0200 Subject: [PATCH] Remove `csp_nonce` field from all template types --- src/web/build_details.rs | 2 -- src/web/builds.rs | 2 -- src/web/crate_details.rs | 7 ------- src/web/error.rs | 1 - src/web/features.rs | 2 -- src/web/mod.rs | 1 - src/web/page/web_page.rs | 4 ++-- src/web/releases.rs | 20 ++------------------ src/web/routes.rs | 8 ++------ src/web/sitemap.rs | 21 ++------------------- src/web/source.rs | 2 -- templates/base.html | 2 ++ templates/storage-change-detection.html | 2 +- 13 files changed, 11 insertions(+), 63 deletions(-) diff --git a/src/web/build_details.rs b/src/web/build_details.rs index 78b8c7235..3cf07e35c 100644 --- a/src/web/build_details.rs +++ b/src/web/build_details.rs @@ -39,7 +39,6 @@ struct BuildDetailsPage { build_details: BuildDetails, all_log_filenames: Vec, current_filename: Option, - csp_nonce: String, } impl_axum_webpage! { BuildDetailsPage } @@ -155,7 +154,6 @@ pub(crate) async fn build_details_handler( }, all_log_filenames, current_filename, - csp_nonce: String::new(), } .into_response()) } diff --git a/src/web/builds.rs b/src/web/builds.rs index e353d53a3..b3fc93618 100644 --- a/src/web/builds.rs +++ b/src/web/builds.rs @@ -49,7 +49,6 @@ struct BuildsPage { builds: Vec, limits: Limits, canonical_url: CanonicalUrl, - csp_nonce: String, } impl_axum_webpage! { BuildsPage } @@ -81,7 +80,6 @@ pub(crate) async fn build_list_handler( builds: get_builds(&mut conn, &name, &version).await?, limits: Limits::for_crate(&config, &mut conn, &name).await?, canonical_url: CanonicalUrl::from_path(format!("/crate/{name}/latest/builds")), - csp_nonce: String::new(), } .into_response()) } diff --git a/src/web/crate_details.rs b/src/web/crate_details.rs index ae5dd7922..517e2332c 100644 --- a/src/web/crate_details.rs +++ b/src/web/crate_details.rs @@ -445,7 +445,6 @@ struct CrateDetailsPage { is_library: Option, last_successful_build: Option, rustdoc: Option, // this is description_long in database - csp_nonce: String, source_size: Option, documentation_size: Option, } @@ -545,7 +544,6 @@ pub(crate) async fn crate_details_handler( is_library, last_successful_build, rustdoc, - csp_nonce: String::new(), source_size, documentation_size, } @@ -567,7 +565,6 @@ struct ReleaseList { crate_name: String, inner_path: String, target: String, - csp_nonce: String, } impl_axum_webpage! { @@ -649,7 +646,6 @@ pub(crate) async fn get_all_releases( target, inner_path, crate_name: params.name, - csp_nonce: String::new(), }; Ok(res.into_response()) } @@ -677,7 +673,6 @@ struct PlatformList { inner_path: String, use_direct_platform_links: bool, current_target: String, - csp_nonce: String, } impl_axum_webpage! { @@ -748,7 +743,6 @@ pub(crate) async fn get_all_platforms_inner( inner_path: "".into(), use_direct_platform_links: is_crate_root, current_target: "".into(), - csp_nonce: String::new(), } .into_response()); } @@ -803,7 +797,6 @@ pub(crate) async fn get_all_platforms_inner( inner_path, use_direct_platform_links: is_crate_root, current_target, - csp_nonce: String::new(), } .into_response()) } diff --git a/src/web/error.rs b/src/web/error.rs index ec70260af..ce254ef16 100644 --- a/src/web/error.rs +++ b/src/web/error.rs @@ -148,7 +148,6 @@ impl IntoResponse for AxumNope { title, message, status, - csp_nonce: String::new(), } .into_response() } diff --git a/src/web/features.rs b/src/web/features.rs index 97e7534a3..709c3352b 100644 --- a/src/web/features.rs +++ b/src/web/features.rs @@ -89,7 +89,6 @@ struct FeaturesPage { default_features: HashSet, canonical_url: CanonicalUrl, is_latest_url: bool, - csp_nonce: String, } impl FeaturesPage { @@ -167,7 +166,6 @@ pub(crate) async fn build_features_handler( default_features, is_latest_url: req_version.is_latest(), canonical_url: CanonicalUrl::from_path(format!("/crate/{}/latest/features", &name)), - csp_nonce: String::new(), } .into_response()) } diff --git a/src/web/mod.rs b/src/web/mod.rs index 8cdfb249d..5396eafcf 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -758,7 +758,6 @@ pub(crate) struct AxumErrorPage { /// The error message, displayed as a description pub message: Cow<'static, str>, pub status: StatusCode, - pub csp_nonce: String, } impl_axum_webpage! { diff --git a/src/web/page/web_page.rs b/src/web/page/web_page.rs index 9a2eb2df9..45f0bb0f5 100644 --- a/src/web/page/web_page.rs +++ b/src/web/page/web_page.rs @@ -26,8 +26,8 @@ macro_rules! impl_axum_webpage { ) => { impl $crate::web::page::web_page::AddCspNonce for $page { fn render_with_csp_nonce(&mut self, csp_nonce: String) -> askama::Result { - self.csp_nonce = csp_nonce; - self.render() + let values: [(&str, &dyn std::any::Any); 1] = [("csp_nonce", &csp_nonce)]; + self.render_with_values(&values) } } diff --git a/src/web/releases.rs b/src/web/releases.rs index f24cf5187..a4eec4065 100644 --- a/src/web/releases.rs +++ b/src/web/releases.rs @@ -238,7 +238,6 @@ async fn get_search_results( #[derive(Debug, Clone, PartialEq, Eq)] struct HomePage { recent_releases: Vec, - csp_nonce: String, } impl_axum_webpage! { @@ -250,10 +249,7 @@ pub(crate) async fn home_page(mut conn: DbConnection) -> AxumResult AxumResult, - csp_nonce: String, } impl_axum_webpage! { @@ -272,10 +267,7 @@ impl_axum_webpage! { pub(crate) async fn releases_feed_handler(mut conn: DbConnection) -> AxumResult { let recent_releases = get_releases(&mut conn, 1, RELEASES_IN_FEED, Order::ReleaseTime, true).await?; - Ok(ReleaseFeed { - recent_releases, - csp_nonce: String::new(), - }) + Ok(ReleaseFeed { recent_releases }) } #[derive(Template)] @@ -289,7 +281,6 @@ struct ViewReleases { show_previous_page: bool, page_number: i64, owner: Option, - csp_nonce: String, } impl_axum_webpage! { ViewReleases } @@ -378,7 +369,6 @@ pub(crate) async fn releases_handler( show_previous_page, page_number, owner: None, - csp_nonce: String::new(), }) } @@ -431,7 +421,6 @@ pub(super) struct Search { /// This should always be `ReleaseType::Search` pub(super) release_type: ReleaseType, pub(super) status: http::StatusCode, - pub(super) csp_nonce: String, } impl Default for Search { @@ -445,7 +434,6 @@ impl Default for Search { search_sort_by: None, release_type: ReleaseType::Search, status: http::StatusCode::OK, - csp_nonce: String::new(), } } } @@ -644,7 +632,6 @@ struct ReleaseActivity { dates: Vec, counts: Vec, failures: Vec, - csp_nonce: String, } impl_axum_webpage! { ReleaseActivity } @@ -700,7 +687,6 @@ pub(crate) async fn activity_handler(mut conn: DbConnection) -> AxumResult, active_cdn_deployments: Vec, in_progress_builds: Vec<(String, String)>, - csp_nonce: String, expand_rebuild_queue: bool, } @@ -791,7 +776,6 @@ pub(crate) async fn build_queue_handler( rebuild_queue, active_cdn_deployments, in_progress_builds, - csp_nonce: String::new(), expand_rebuild_queue: params.expand.is_some(), }) } diff --git a/src/web/routes.rs b/src/web/routes.rs index a3377416b..9971c9133 100644 --- a/src/web/routes.rs +++ b/src/web/routes.rs @@ -293,16 +293,12 @@ pub(super) fn build_axum_routes() -> AxumRouter { #[derive(Template)] #[template(path = "storage-change-detection.html")] #[derive(Debug, Clone)] - struct StorageChangeDetection { - csp_nonce: String, - } + struct StorageChangeDetection; crate::impl_axum_webpage!( StorageChangeDetection, cache_policy = |_| CachePolicy::ForeverInCdnAndBrowser, ); - StorageChangeDetection { - csp_nonce: String::new(), - } + StorageChangeDetection }), ) .route_with_tsr( diff --git a/src/web/sitemap.rs b/src/web/sitemap.rs index a4db7ddc5..72c497f11 100644 --- a/src/web/sitemap.rs +++ b/src/web/sitemap.rs @@ -22,7 +22,6 @@ use std::sync::Arc; #[derive(Debug, Clone, PartialEq, Eq)] struct SitemapIndexXml { sitemaps: Vec, - csp_nonce: String, } impl_axum_webpage! { @@ -33,10 +32,7 @@ impl_axum_webpage! { pub(crate) async fn sitemapindex_handler() -> impl IntoResponse { let sitemaps: Vec = ('a'..='z').collect(); - SitemapIndexXml { - sitemaps, - csp_nonce: String::new(), - } + SitemapIndexXml { sitemaps } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -52,7 +48,6 @@ struct SitemapRow { #[derive(Debug, Clone, PartialEq, Eq)] struct SitemapXml { releases: Vec, - csp_nonce: String, } impl_axum_webpage! { @@ -102,10 +97,7 @@ pub(crate) async fn sitemap_handler( .try_collect() .await?; - Ok(SitemapXml { - releases, - csp_nonce: String::new(), - }) + Ok(SitemapXml { releases }) } #[derive(Template)] @@ -118,7 +110,6 @@ struct AboutBuilds { limits: Limits, /// Just for the template, since this isn't shared with AboutPage active_tab: &'static str, - csp_nonce: String, } impl_axum_webpage!(AboutBuilds); @@ -131,7 +122,6 @@ pub(crate) async fn about_builds_handler( rustc_version: get_config::(&mut conn, ConfigName::RustcVersion).await?, limits: Limits::new(&config), active_tab: "builds", - csp_nonce: String::new(), }) } @@ -141,7 +131,6 @@ macro_rules! about_page { #[template(path = $template)] struct $ty { active_tab: &'static str, - csp_nonce: String, } impl_axum_webpage! { $ty } @@ -163,27 +152,22 @@ pub(crate) async fn about_handler(subpage: Option>) -> AxumResult AboutPage { active_tab: "index", - csp_nonce: String::new(), } .into_response(), "badges" => AboutPageBadges { active_tab: "badges", - csp_nonce: String::new(), } .into_response(), "metadata" => AboutPageMetadata { active_tab: "metadata", - csp_nonce: String::new(), } .into_response(), "redirections" => AboutPageRedirection { active_tab: "redirections", - csp_nonce: String::new(), } .into_response(), "download" => AboutPageDownload { active_tab: "download", - csp_nonce: String::new(), } .into_response(), _ => { @@ -193,7 +177,6 @@ pub(crate) async fn about_handler(subpage: Option>) -> AxumResult{%- block title -%} Docs.rs {%- endblock title -%} + {%- let csp_nonce = askama::get_value::("csp_nonce").unwrap() -%} + {%- block css -%}{%- endblock css -%} diff --git a/templates/storage-change-detection.html b/templates/storage-change-detection.html index 448e936ea..7364f2fb6 100644 --- a/templates/storage-change-detection.html +++ b/templates/storage-change-detection.html @@ -8,7 +8,7 @@ ../templates/theme.js when rustdoc in the current window changes the theme --> -