From 026e8c2914c55c3fbf687a8c9616ec21b8aff058 Mon Sep 17 00:00:00 2001 From: Jeongkyu Shin Date: Fri, 20 Nov 2026 07:14:16 +0900 Subject: [PATCH 1/3] update: replace direct chrono dependency with jiff chrono is unmaintained (RUSTSEC-2026-0543). jiff reads the system time zone itself, so local timestamps keep working in the multi-threaded tokio process, unlike time's now_local(). If the zone cannot be determined it behaves like UTC, as chrono's Local did. Audit event timestamps are now jiff::Timestamp with a serde helper that writes the same RFC 3339 UTC form chrono wrote (Z suffix, 0/3/6/9 fractional digits), so audit files and Logstash output are byte-compatible. The otel exporter converts with SystemTime::try_from. TUI log, interactive, and output file name timestamps keep their strftime formats and local-vs-UTC choice. Tests cover the serialized format and deserializing chrono-written records. chrono remains in Cargo.lock through russh-sftp, so the advisory stays open until that crate changes. Refs #363 --- Cargo.lock | 64 +++++++++++++++++++- Cargo.toml | 2 +- docs/audit-logging.md | 4 +- src/commands/interactive/multiplex.rs | 3 +- src/executor/parallel.rs | 2 +- src/server/audit/event.rs | 54 +++++++++++++++-- src/server/audit/mod.rs | 1 + src/server/audit/otel.rs | 5 +- src/server/audit/timestamp.rs | 85 +++++++++++++++++++++++++++ src/ui/tui/log_buffer.rs | 8 +-- src/ui/tui/views/log_panel.rs | 2 +- src/utils/output.rs | 2 +- 12 files changed, 214 insertions(+), 18 deletions(-) create mode 100644 src/server/audit/timestamp.rs diff --git a/Cargo.lock b/Cargo.lock index daaf47fe9..8a08be7eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -408,7 +408,6 @@ dependencies = [ "bcrypt", "bytes", "cbc", - "chrono", "clap", "criterion", "crossterm", @@ -423,6 +422,7 @@ dependencies = [ "indicatif", "insta", "ipnetwork", + "jiff", "libc", "libloading", "lru 0.19.0", @@ -574,7 +574,6 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits", - "serde", "wasm-bindgen", "windows-link", ] @@ -2267,6 +2266,58 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +[[package]] +name = "jiff" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5d126d72eb017f10b302e73bdeca446bea405ee449af4d9e7866d6b082d83e" +dependencies = [ + "jiff-core", + "jiff-static", + "jiff-tzdb-platform", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", + "windows-link", +] + +[[package]] +name = "jiff-core" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b46a105a9874634dd96231021526bf24ca04955a5998c59a9c7666e634b7b8" +dependencies = [ + "log", +] + +[[package]] +name = "jiff-static" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0127e9bde3c6130d6169e7d8d68676e4be69c4bf7573c308b6f960a5089c428" +dependencies = [ + "jiff-core", + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "jiff-tzdb" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12e71a1b68a875a44a33bb7a1dd84392cb77145a3367a552a3cfe6a4f5cf3afc" + +[[package]] +name = "jiff-tzdb-platform" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc65b284afcfa8f6cf189c7274c805505314a7ab975c60e1f548bff56525420e" +dependencies = [ + "jiff-tzdb", +] + [[package]] name = "jni" version = "0.22.4" @@ -3401,6 +3452,15 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" +[[package]] +name = "portable-atomic-util" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715" +dependencies = [ + "portable-atomic", +] + [[package]] name = "potential_utf" version = "0.1.6" diff --git a/Cargo.toml b/Cargo.toml index 70ccb647e..ea5709edf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,12 +44,12 @@ tracing = "0.1.44" tracing-subscriber = { version = "0.3.23", features = ["env-filter"] } serde = { version = "1.0.229", features = ["derive"] } serde-saphyr = "2.1.1" +jiff = { version = "1.0.1", features = ["serde"] } futures = "0.3.34" async-trait = "0.1.92" indicatif = "0.18.6" rpassword = "7.5.4" dirs = "7.0" -chrono = { version = "0.4.45", features = ["serde"] } glob = "0.3.4" whoami = "2.1.3" sha1 = "0.11.0" diff --git a/docs/audit-logging.md b/docs/audit-logging.md index 58cae9682..3e75bf746 100644 --- a/docs/audit-logging.md +++ b/docs/audit-logging.md @@ -101,7 +101,7 @@ Each audit event contains: ```json { "id": "550e8400-e29b-41d4-a716-446655440000", - "timestamp": "2026-01-24T12:00:00.000Z", + "timestamp": "2026-01-24T12:00:00.123456Z", "event_type": "AuthSuccess", "session_id": "session-123", "user": "johndoe", @@ -115,6 +115,8 @@ Each audit event contains: } ``` +The `timestamp` field is an RFC 3339 UTC string with a `Z` suffix. It carries 0, 3, 6, or 9 fractional-second digits, whichever is the shortest exact representation (for example `2026-01-24T12:00:00Z`, `2026-01-24T12:00:00.123Z`, or `2026-01-24T12:00:00.123456789Z`), so parsers should accept any of these widths. + ## Exporters ### File Exporter diff --git a/src/commands/interactive/multiplex.rs b/src/commands/interactive/multiplex.rs index 17bae672d..5e687d04b 100644 --- a/src/commands/interactive/multiplex.rs +++ b/src/commands/interactive/multiplex.rs @@ -16,7 +16,6 @@ use crate::ui::Colorize; use anyhow::Result; -use chrono; use rustyline::DefaultEditor; use rustyline::config::Configurer; use rustyline::error::ReadlineError; @@ -262,7 +261,7 @@ impl InteractiveCommand { // Print output with node prefix and optional timestamp for line in output.lines() { if self.interactive_config.show_timestamps { - let timestamp = chrono::Local::now().format("%H:%M:%S"); + let timestamp = jiff::ZonedDateTime::now().strftime("%H:%M:%S"); println!( "[{} {}] {}", timestamp.to_string().dimmed(), diff --git a/src/executor/parallel.rs b/src/executor/parallel.rs index 12140e760..4381a13f5 100644 --- a/src/executor/parallel.rs +++ b/src/executor/parallel.rs @@ -1769,7 +1769,7 @@ impl ParallelExecutor { output_dir.display() ); - let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S"); + let timestamp = jiff::Timestamp::now().strftime("%Y%m%d_%H%M%S"); let mut pending_handles = handles; let mut first_ctrl_c = false; diff --git a/src/server/audit/event.rs b/src/server/audit/event.rs index 2a81ff079..e1d4b8040 100644 --- a/src/server/audit/event.rs +++ b/src/server/audit/event.rs @@ -17,7 +17,7 @@ //! This module defines the core audit event types used throughout the SSH server //! to track authentication, file operations, and other security-relevant activities. -use chrono::{DateTime, Utc}; +use jiff::Timestamp; use serde::{Deserialize, Serialize}; use std::net::IpAddr; use std::path::PathBuf; @@ -31,8 +31,9 @@ pub struct AuditEvent { /// Unique event identifier pub id: String, - /// Timestamp when the event occurred - pub timestamp: DateTime, + /// Timestamp when the event occurred (serialized as RFC 3339 UTC with `Z`) + #[serde(with = "super::timestamp")] + pub timestamp: Timestamp, /// Type of event pub event_type: EventType, @@ -185,7 +186,7 @@ impl AuditEvent { pub fn new(event_type: EventType, user: String, session_id: String) -> Self { Self { id: uuid::Uuid::new_v4().to_string(), - timestamp: Utc::now(), + timestamp: Timestamp::now(), event_type, session_id, user, @@ -333,6 +334,51 @@ mod tests { assert_eq!(deserialized.session_id, event.session_id); assert_eq!(deserialized.client_ip, event.client_ip); assert_eq!(deserialized.protocol, event.protocol); + assert_eq!(deserialized.timestamp, event.timestamp); + } + + #[test] + fn test_timestamp_serializes_as_rfc3339_utc() { + let mut event = AuditEvent::new( + EventType::SessionStart, + "erin".to_string(), + "session-202".to_string(), + ); + + let value = serde_json::to_value(&event).unwrap(); + let ts = value["timestamp"].as_str().unwrap(); + assert!(ts.ends_with('Z'), "timestamp must use Z suffix: {ts}"); + assert_eq!(ts.parse::().unwrap(), event.timestamp); + + event.timestamp = "2026-11-20T01:02:03.123456789Z".parse().unwrap(); + let value = serde_json::to_value(&event).unwrap(); + assert_eq!(value["timestamp"], "2026-11-20T01:02:03.123456789Z"); + + event.timestamp = "2026-11-20T01:02:03Z".parse().unwrap(); + let value = serde_json::to_value(&event).unwrap(); + assert_eq!(value["timestamp"], "2026-11-20T01:02:03Z"); + } + + #[test] + fn test_deserializes_chrono_written_json() { + // Records in the exact shape the previous chrono-based code wrote. + for (raw, nanos) in [ + ("2026-11-20T01:02:03.123456789Z", 123_456_789), + ("2026-11-20T01:02:03.123456Z", 123_456_000), + ("2026-11-20T01:02:03.123Z", 123_000_000), + ("2026-11-20T01:02:03Z", 0), + ] { + let json = format!( + r#"{{"id":"e1","timestamp":"{raw}","event_type":"session_start","session_id":"s1","user":"frank","client_ip":"10.0.0.1","path":null,"dest_path":null,"bytes":null,"result":"success","details":null,"protocol":"ssh"}}"# + ); + let event: AuditEvent = serde_json::from_str(&json).unwrap(); + assert_eq!(event.timestamp.as_second(), 1_795_136_523); + assert_eq!(event.timestamp.subsec_nanosecond(), nanos); + + // Re-serializing yields the identical string chrono produced. + let value = serde_json::to_value(&event).unwrap(); + assert_eq!(value["timestamp"], raw); + } } #[test] diff --git a/src/server/audit/mod.rs b/src/server/audit/mod.rs index f6edd2281..777226891 100644 --- a/src/server/audit/mod.rs +++ b/src/server/audit/mod.rs @@ -51,6 +51,7 @@ pub mod exporter; pub mod file; pub mod logstash; pub mod otel; +mod timestamp; use anyhow::Result; use std::sync::Arc; diff --git a/src/server/audit/otel.rs b/src/server/audit/otel.rs index 2e6a951a9..4fcf85816 100644 --- a/src/server/audit/otel.rs +++ b/src/server/audit/otel.rs @@ -141,7 +141,10 @@ impl OtelExporter { event.event_type, event.user, event.result ); - record.set_timestamp(event.timestamp.into()); + record.set_timestamp( + std::time::SystemTime::try_from(event.timestamp) + .unwrap_or_else(|_| std::time::SystemTime::now()), + ); record.set_observed_timestamp(std::time::SystemTime::now()); record.set_severity_number(severity); record.set_severity_text(severity_to_str(severity)); diff --git a/src/server/audit/timestamp.rs b/src/server/audit/timestamp.rs new file mode 100644 index 000000000..bcb8ad467 --- /dev/null +++ b/src/server/audit/timestamp.rs @@ -0,0 +1,85 @@ +// Copyright 2025 Lablup Inc. and Jeongkyu Shin +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Serde helper for audit event timestamps. +//! +//! Audit timestamps are serialized as RFC 3339 UTC strings with a `Z` suffix +//! and 0, 3, 6, or 9 fractional-second digits: the shortest of those widths +//! that represents the instant exactly. This is the format bssh has always +//! written, so downstream log consumers see no change. Deserialization accepts +//! any RFC 3339 timestamp, with any number of fractional digits and any offset. + +use jiff::Timestamp; +use jiff::fmt::temporal::DateTimePrinter; +use serde::{Deserialize, Deserializer, Serializer}; + +/// Format a timestamp as RFC 3339 UTC with 0, 3, 6, or 9 fractional digits. +pub(crate) fn format(timestamp: &Timestamp) -> String { + let nanos = timestamp.subsec_nanosecond(); + let precision = if nanos == 0 { + 0 + } else if nanos % 1_000_000 == 0 { + 3 + } else if nanos % 1_000 == 0 { + 6 + } else { + 9 + }; + DateTimePrinter::new() + .precision(Some(precision)) + .timestamp_to_string(timestamp) +} + +pub(crate) fn serialize( + timestamp: &Timestamp, + serializer: S, +) -> Result { + serializer.serialize_str(&format(timestamp)) +} + +pub(crate) fn deserialize<'de, D: Deserializer<'de>>( + deserializer: D, +) -> Result { + Timestamp::deserialize(deserializer) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ts(nanos: i32) -> Timestamp { + // 2026-11-20T01:02:03Z + Timestamp::new(1_795_136_523, nanos).unwrap() + } + + #[test] + fn test_format_uses_shortest_si_precision() { + assert_eq!(format(&ts(0)), "2026-11-20T01:02:03Z"); + assert_eq!(format(&ts(100_000_000)), "2026-11-20T01:02:03.100Z"); + assert_eq!(format(&ts(123_000_000)), "2026-11-20T01:02:03.123Z"); + assert_eq!(format(&ts(123_400_000)), "2026-11-20T01:02:03.123400Z"); + assert_eq!(format(&ts(123_456_000)), "2026-11-20T01:02:03.123456Z"); + assert_eq!(format(&ts(123_456_700)), "2026-11-20T01:02:03.123456700Z"); + assert_eq!(format(&ts(1)), "2026-11-20T01:02:03.000000001Z"); + } + + #[test] + fn test_parse_round_trips_every_precision() { + for nanos in [0, 100_000_000, 123_456_000, 123_456_789, 1] { + let original = ts(nanos); + let parsed: Timestamp = format(&original).parse().unwrap(); + assert_eq!(parsed, original); + } + } +} diff --git a/src/ui/tui/log_buffer.rs b/src/ui/tui/log_buffer.rs index 441d15696..d50acb508 100644 --- a/src/ui/tui/log_buffer.rs +++ b/src/ui/tui/log_buffer.rs @@ -17,7 +17,7 @@ //! This module provides a thread-safe buffer for capturing log entries //! during TUI mode, preventing logs from breaking the ratatui alternate screen. -use chrono::{DateTime, Local}; +use jiff::ZonedDateTime; use std::collections::VecDeque; use tracing::Level; @@ -40,7 +40,7 @@ pub struct LogEntry { /// Log message content pub message: String, /// Timestamp when the log was captured - pub timestamp: DateTime, + pub timestamp: ZonedDateTime, } impl LogEntry { @@ -50,7 +50,7 @@ impl LogEntry { level, target, message, - timestamp: Local::now(), + timestamp: ZonedDateTime::now(), } } @@ -82,7 +82,7 @@ impl LogEntry { }; format!( "{} [{}] {}: {}", - self.timestamp.format("%H:%M:%S"), + self.timestamp.strftime("%H:%M:%S"), level_str, self.target.rsplit("::").next().unwrap_or(&self.target), self.message diff --git a/src/ui/tui/views/log_panel.rs b/src/ui/tui/views/log_panel.rs index 6ad033805..b4b4796bb 100644 --- a/src/ui/tui/views/log_panel.rs +++ b/src/ui/tui/views/log_panel.rs @@ -104,7 +104,7 @@ pub fn render( // Add timestamp if enabled if show_timestamps { spans.push(Span::styled( - format!("{} ", entry.timestamp.format("%H:%M:%S")), + format!("{} ", entry.timestamp.strftime("%H:%M:%S")), Style::default().fg(Color::DarkGray), )); } diff --git a/src/utils/output.rs b/src/utils/output.rs index db456772d..fc7a1ac6b 100644 --- a/src/utils/output.rs +++ b/src/utils/output.rs @@ -31,7 +31,7 @@ pub async fn save_outputs_to_files( .with_context(|| format!("Failed to create output directory: {output_dir:?}"))?; // Get timestamp for unique file naming - let timestamp = chrono::Local::now().format("%Y%m%d_%H%M%S"); + let timestamp = jiff::ZonedDateTime::now().strftime("%Y%m%d_%H%M%S"); println!( "\n{} {} {:?}\n", From 5e7df5b8a417d3831e0256ce5167a722833452e8 Mon Sep 17 00:00:00 2001 From: Jeongkyu Shin Date: Fri, 20 Nov 2026 07:23:49 +0900 Subject: [PATCH 2/3] fix: correct fractional digit limit in audit timestamp docs --- src/server/audit/timestamp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/audit/timestamp.rs b/src/server/audit/timestamp.rs index bcb8ad467..0afe1c60c 100644 --- a/src/server/audit/timestamp.rs +++ b/src/server/audit/timestamp.rs @@ -18,7 +18,7 @@ //! and 0, 3, 6, or 9 fractional-second digits: the shortest of those widths //! that represents the instant exactly. This is the format bssh has always //! written, so downstream log consumers see no change. Deserialization accepts -//! any RFC 3339 timestamp, with any number of fractional digits and any offset. +//! any RFC 3339 timestamp with up to 9 fractional digits and any offset. use jiff::Timestamp; use jiff::fmt::temporal::DateTimePrinter; From f668da93553c1972646d8f3e26e1ea8666024763 Mon Sep 17 00:00:00 2001 From: Jeongkyu Shin Date: Fri, 20 Nov 2026 07:27:03 +0900 Subject: [PATCH 3/3] test: cover log entry timestamp formatting after the jiff migration LogEntry::format_with_time now formats through jiff strftime and had no test. Pin the timestamp to a fixed UTC instant and assert the HH:MM:SS prefix so the output stays stable independent of the local time zone. Validation: - cargo test --all-features --lib log_buffer: 8 passed - cargo fmt --check and cargo clippy --all-targets --all-features -- -D warnings clean Refs #363 --- src/ui/tui/log_buffer.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/ui/tui/log_buffer.rs b/src/ui/tui/log_buffer.rs index d50acb508..7c5ec1038 100644 --- a/src/ui/tui/log_buffer.rs +++ b/src/ui/tui/log_buffer.rs @@ -288,6 +288,21 @@ mod tests { assert_eq!(window, vec!["message 6", "message 7", "message 8"]); } + #[test] + fn test_log_entry_format_with_time() { + let mut entry = LogEntry::new( + Level::WARN, + "bssh::ssh::client".to_string(), + "slow".to_string(), + ); + entry.timestamp = jiff::Timestamp::from_second(1_795_136_523) + .unwrap() + .in_tz("UTC") + .unwrap(); + + assert_eq!(entry.format_with_time(), "01:02:03 [WARN] client: slow"); + } + #[test] fn test_log_entry_format() { let entry = LogEntry::new(