Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-LIB-CAPTURETIME: refuse capture-time shifts beyond 10,000 years

Nathaniel Ledford7ba1470940bc
diff --git a/crates/engine/src/cmd/manage.rs b/crates/engine/src/cmd/manage.rs
index 41b43cab9..7232ecdb8 100644
--- a/crates/engine/src/cmd/manage.rs
+++ b/crates/engine/src/cmd/manage.rs
@@ -189,7 +189,13 @@ pub fn specs() -> Vec<CommandSpec> {
}
// photos without a capture time start from their import time
let base = |s: &crate::Session, id| s.catalog.photo(id).map(|p| p.date().to_string()).unwrap_or_default();
- let mut delta = (f64_or(p, "shift", 0.0) + f64_or(p, "hours", 0.0) * 3600.0).round() as i64;
+ // seconds; a shift beyond 10,000 years is a mistake (and would overflow), not a date
+ const MAX_SHIFT: f64 = 10_000.0 * 365.25 * 86_400.0;
+ let shift = f64_or(p, "shift", 0.0) + f64_or(p, "hours", 0.0) * 3600.0;
+ if shift.is_nan() || shift.abs() > MAX_SHIFT {
+ return Err(bad(c, "the shift is more than 10,000 years"));
+ }
+ let mut delta = shift.round() as i64;
let mut each: Option<String> = None;
if let Some(t) = str_param(p, "time") {
let t = normalize_iso(t).ok_or_else(|| bad(c, format!("`{t}` is not a date (YYYY-MM-DDTHH:MM:SS)")))?;
diff --git a/crates/engine/src/tests_library.rs b/crates/engine/src/tests_library.rs
index 9dd0e6b8f..bc98b803f 100644
--- a/crates/engine/src/tests_library.rs
+++ b/crates/engine/src/tests_library.rs
@@ -470,3 +470,24 @@ fn a_library_open_elsewhere_is_refused() {
assert_eq!(other.catalog.to_snapshot(), expect);
let _ = std::fs::remove_dir_all(&dir);
}
+
+/// A capture-time shift from an agent can't overflow or write a date no one can read back: a
+/// shift of more than 10,000 years is refused and nothing changes; an ordinary one still works.
+#[test]
+fn capture_time_shift_is_bounded() {
+ let mut s = crate::Session::with_demo();
+ let id = s.catalog.photos().next().unwrap().id;
+ let before = s.catalog.photo(id).unwrap().captured.clone();
+ for p in [
+ serde_json::json!({"ids": [id.0], "shift": 1e30}),
+ serde_json::json!({"ids": [id.0], "hours": -1e15}),
+ serde_json::json!({"ids": [id.0], "shift": 9.3e18}),
+ ] {
+ let r = s.execute("photo.setCaptureTime", &p);
+ // a real refusal, not the panic guard catching an overflow ("failed unexpectedly")
+ assert!(r.as_ref().is_err_and(|e| e.to_string().contains("10,000 years")), "{p}: {r:?}");
+ assert_eq!(s.catalog.photo(id).unwrap().captured, before, "{p} changed nothing");
+ }
+ s.execute("photo.setCaptureTime", &serde_json::json!({"ids": [id.0], "hours": 1})).unwrap();
+ assert_ne!(s.catalog.photo(id).unwrap().captured, before);
+}