//! NO CRATE READS THE MACHINE'S OWN TIME ZONE. //! //! Every piece of calendar arithmetic in this workspace takes an EXPLICIT IANA //! zone — the workspace's, passed in by the caller — and the recurrence sweep //! SKIPS a workspace that names no zone rather than guessing one. Guessing //! produces events an hour or a day out, which reads as a bug in the person's //! own calendar rather than in ours. //! //! WHY A GUARD AND NOT A SENTENCE. The rule used to rest on "the host runs //! `TZ=UTC`", which was true of a hosted Linux server and is not true of //! anything that runs this code today: the server now runs INSIDE CottonDB for //! Mac, on a Mac sitting in the owner's own zone, and the phone runs wherever //! its owner is. `TZ=UTC` survives only in the relay's and the push forwarder's //! systemd units — neither of which does calendar arithmetic. So the premise //! that made an ambient read harmless is gone, and the only thing left holding //! the rule is that nothing reaches for the ambient zone at all. A defence //! written into a provisioning script that ran once is not a defence that is //! running. //! //! WHAT IS SWEPT. Every `.rs` file under `crates/`, for the ways a program asks //! the operating system what zone it is in: `chrono::Local` in any spelling, and //! the `iana_time_zone` crate that reads the zone straight off the host. An //! instant is fine — `Utc::now()` is not a zone, and a clock passed in through a //! seam is the shape the engines already use. //! //! It walks from `crates/`, this crate's manifest directory's parent, and //! asserts it found the workspace before comparing anything: a sweep that //! reaches nothing passes for the wrong reason. use std::fs; use std::path::{Path, PathBuf}; /// `crates/` — the workspace this sweeps. fn crates_root() -> PathBuf { cotton_store::manifest_dir().join("..") } fn walk(dir: &Path, out: &mut Vec) { let Ok(entries) = fs::read_dir(dir) else { return }; for entry in entries.flatten() { let path = entry.path(); let name = entry.file_name(); let name = name.to_string_lossy(); if name == "target" || name == "node_modules" || name.starts_with('.') { continue; } if path.is_dir() { walk(&path, out); } else if name.ends_with(".rs") { out.push(path); } } } /// The ambient-zone read a line performs, if it performs one. /// /// Comment lines are skipped: this file's own header names every form it looks /// for, and so do a dozen engine headers explaining why they take a zone. /// `ConnectionMode::Local` and `VersionActorRole::Role::Local` are ordinary enum /// variants and have nothing to do with time, which is why the match is on the /// chrono path rather than on the word. fn ambient_zone_read(line: &str) -> Option<&'static str> { let code = line.trim_start(); if code.starts_with("//") || code.starts_with('*') || code.starts_with("#!") { return None; } if line.contains("Local::now(") { return Some("Local::now()"); } if line.contains("chrono::Local") || line.contains("chrono::offset::Local") { return Some("chrono::Local"); } if line.contains("iana_time_zone") { return Some("iana_time_zone"); } // `use chrono::{DateTime, Local};` — the brace form, where the path prefix // is written only once. if let Some((_, rest)) = line.split_once("use chrono::{") { let names = rest.split_once('}').map_or(rest, |(inside, _)| inside); if names.split(',').any(|name| name.trim() == "Local") { return Some("use chrono::{… Local …}"); } } None } /// RULE: No part of the product asks the machine it is running on what time zone it is in; a zone is always passed in. // WHY: the rule used to rest on the server running with its clock set to UTC. // The server now runs inside the Mac app, in the owner's own zone, so an ambient // read would put an event an hour or a day out — which reads as a bug in the // person's own calendar rather than in ours. #[test] fn nothing_asks_the_machine_what_time_zone_it_is_in() { let crates = crates_root(); assert!( crates.join("cotton-store").is_dir(), "the sweep's root is not the workspace — {} has no cotton-store in it", crates.display() ); let mut files = Vec::new(); walk(&crates, &mut files); assert!( files.len() > 200, "the sweep found only {} Rust files — it is not reaching the workspace, and an empty \ sweep passes for the wrong reason", files.len() ); let mut offenders = Vec::new(); for file in &files { // THIS FILE. It spells every form it hunts for, in its header and in the // reader's own unit test below, and those are the guard's subject rather // than its behaviour. if file.ends_with("no_ambient_timezone.rs") { continue; } let Ok(text) = fs::read_to_string(file) else { continue }; for (number, line) in text.lines().enumerate() { let Some(form) = ambient_zone_read(line) else { continue }; offenders.push(format!("{}:{} reads the host's zone via {form}", file.display(), number + 1)); } } assert!( offenders.is_empty(), "these read the machine's own time zone. A zone is the WORKSPACE's and is passed in; the \ server runs on somebody's Mac, in that person's zone, and a guess here puts an event an \ hour or a day out — which reads as a bug in the person's own calendar:\n {}", offenders.join("\n ") ); } /// RULE: The check that nothing reads the machine's time zone is itself proved against written-out examples of each way of doing it. // WHY: a sweep whose reader stopped matching passes over everything, and that is // the one failure a sweep cannot report about itself. #[test] fn the_reader_reads_the_forms_it_claims_to() { // A sweep whose parser stopped parsing passes over everything, so each shape // is pinned here rather than assumed. assert_eq!(ambient_zone_read(" let now = Local::now();"), Some("Local::now()")); assert_eq!(ambient_zone_read("use chrono::Local;"), Some("chrono::Local")); assert_eq!(ambient_zone_read(" let z: chrono::offset::Local = todo!();"), Some("chrono::Local")); assert_eq!(ambient_zone_read("use chrono::{DateTime, Local, Utc};"), Some("use chrono::{… Local …}")); assert_eq!( ambient_zone_read(" let tz = iana_time_zone::get_timezone();"), Some("iana_time_zone") ); // An instant is not a zone. assert_eq!(ambient_zone_read(" let now = Utc::now();"), None); // Enum variants that merely share the word. assert_eq!(ambient_zone_read(" mode: ConnectionMode::Local,"), None); assert_eq!(ambient_zone_read(" VersionActorRole::Local => \"local\","), None); // And a comment naming the form, which every engine header does. assert_eq!(ambient_zone_read("//! never `Local::now()` — the zone is the workspace's"), None); assert_eq!(ambient_zone_read(" /// use chrono::Local is what this must never do"), None); }