From 9535ab754a9374450668120b58999711d31ea09e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 5 Dec 2025 10:10:00 +0100 Subject: [PATCH] Hide real build time of application --- Cargo.lock | 1 + Cargo.toml | 1 + src/controllers/assets_controller.rs | 9 ++++----- src/utils/time_utils.rs | 10 +++++++++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e0f470c..4441970 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -578,6 +578,7 @@ dependencies = [ "actix-remote-ip", "actix-session", "actix-web", + "anyhow", "askama", "base32", "base64 0.22.1", diff --git a/Cargo.toml b/Cargo.toml index c76180c..eda24b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,3 +42,4 @@ mailchecker = "6.0.19" httpdate = "1.0.3" build-time = "0.1.3" hex = "0.4.3" +anyhow = "1.0.100" diff --git a/src/controllers/assets_controller.rs b/src/controllers/assets_controller.rs index 2cc0a2e..3dfff61 100644 --- a/src/controllers/assets_controller.rs +++ b/src/controllers/assets_controller.rs @@ -3,6 +3,7 @@ use crate::utils::time_utils; use actix_web::http::header; use actix_web::{HttpRequest, HttpResponse, web}; use include_dir::{Dir, include_dir}; +use std::cmp::max; use std::ops::Add; use std::path::Path; use std::time::Duration; @@ -23,13 +24,14 @@ pub async fn assets_route(req: HttpRequest, path: web::Path) -> HttpResp Some(file) => { let res = mime_guess::from_path(path).first_or_octet_stream(); let digest = hex::encode(sha256(file.contents())); + let file_time = max(time_utils::time_start_of_day(), time_utils::build_time()); // Check if the browser already knows the file by date if let Some(c) = req.headers().get(header::IF_MODIFIED_SINCE) { let date_str = c.to_str().unwrap_or(""); if let Ok(date) = httpdate::parse_http_date(date_str) && date.add(Duration::from_secs(1)) - >= time_utils::unix_to_system_time(time_utils::build_time()) + >= time_utils::unix_to_system_time(file_time) { return HttpResponse::NotModified().finish(); } @@ -45,10 +47,7 @@ pub async fn assets_route(req: HttpRequest, path: web::Path) -> HttpResp HttpResponse::Ok() .content_type(res.to_string()) .insert_header(("etag", digest)) - .insert_header(( - "last-modified", - time_utils::unix_to_http_date(time_utils::build_time()), - )) + .insert_header(("last-modified", time_utils::unix_to_http_date(file_time))) .body(file.contents()) } } diff --git a/src/utils/time_utils.rs b/src/utils/time_utils.rs index 332dd18..65f7ef5 100644 --- a/src/utils/time_utils.rs +++ b/src/utils/time_utils.rs @@ -1,4 +1,4 @@ -use chrono::DateTime; +use chrono::{DateTime, Local, NaiveTime}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; /// Get the current time since epoch @@ -37,6 +37,14 @@ pub fn build_time() -> u64 { date.timestamp() as u64 } +/// Get the first second of the day (local time) +pub fn time_start_of_day() -> u64 { + let local: DateTime = Local::now() + .with_time(NaiveTime::from_hms_opt(0, 0, 0).unwrap()) + .unwrap(); + local.timestamp() as u64 +} + #[cfg(test)] mod test { use crate::utils::time_utils::{fmt_time, time};