From 0ba4a8140a358e744cf3a782b2f8e5da45cf0d8f Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:24:20 +0000 Subject: [PATCH] fix(engine): escape the table name and path in the import/export access log ImportTable and ExportTableToPointInTime logged their access record with the table name and the resolved file path as Display fields. The path check accepts CR, LF and ESC in a file name, ImportTable logs the table name from TableCreationParameters before any check of it, and the text log format writes Display fields as they are. A caller allowed to import or export could therefore end the log line early, add a line formatted as a log line of its own, and send terminal escape sequences to whoever reads the log. Both handlers now log through log_file_access, which records the table name and the path as Debug fields. Debug escapes control characters, so the record stays one line. Both values now appear quoted in the text format, and in the JSON format table_name and resolved_path hold the Debug form; nothing in the repository reads them. Two unit tests capture the record with the text formatter and check that a path, and a table name, holding \n, \r and \x1b give one line with those characters escaped. With either field back to Display, its test fails. On a sqlite-memory dev-mode server with an export root, an export whose S3Prefix holds such a name put a forged log line and a line starting with the escape sequence into the server log of a build of main, and a single line with the name escaped into the log of a build with this change. Fixes #705. --- Cargo.lock | 1 + crates/engine/Cargo.toml | 3 + crates/engine/src/import_export.rs | 98 ++++++++++++++++++++++++++---- 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 72184565b..3265ba3a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1824,6 +1824,7 @@ dependencies = [ "sha2 0.10.9", "tokio", "tracing", + "tracing-subscriber", "uuid", ] diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 3634c9152..313a0d034 100755 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -21,3 +21,6 @@ hmac = "0.12" sha2 = "0.10" hex = "0.4" tokio = { workspace = true } + +[dev-dependencies] +tracing-subscriber = { workspace = true } diff --git a/crates/engine/src/import_export.rs b/crates/engine/src/import_export.rs index 1dce358ca..f9a69ff7e 100755 --- a/crates/engine/src/import_export.rs +++ b/crates/engine/src/import_export.rs @@ -57,12 +57,11 @@ pub async fn handle_import_table( // orphaned destination table behind. let source_path = validate_path(&input.file_source.path, &ctx.import_paths, &ctx.account_id)?; - tracing::info!( - account_id = %ctx.account_id, - operation = "ImportTable", - table_name = %tcp.table_name, - resolved_path = %source_path.display(), - "import/export file access" + log_file_access( + &ctx.account_id, + "ImportTable", + &tcp.table_name, + &source_path, ); // Check file size against limit. @@ -245,12 +244,11 @@ pub async fn handle_export_table( })?; } - tracing::info!( - account_id = %ctx.account_id, - operation = "ExportTableToPointInTime", - table_name = %table_name, - resolved_path = %output_path.display(), - "import/export file access" + log_file_access( + &ctx.account_id, + "ExportTableToPointInTime", + &table_name, + &output_path, ); // `create_new` refuses to write when anything already exists at the path, @@ -421,6 +419,82 @@ fn epoch_seconds() -> f64 { .as_secs_f64() } +/// Log the file an import reads or an export writes. The table name and the +/// path are Debug fields, which escape control characters, so a value that +/// holds a line break or a terminal escape sequence stays inside this one log +/// line. Both come from the request: ImportTable logs the table name it is +/// about to create before any check of that name. +fn log_file_access(account_id: &str, operation: &str, table_name: &str, path: &std::path::Path) { + tracing::info!( + account_id = %account_id, + operation, + table_name = ?table_name, + resolved_path = ?path, + "import/export file access" + ); +} + +#[cfg(test)] +mod file_access_log_tests { + use std::sync::{Arc, Mutex}; + + #[derive(Clone, Default)] + struct Capture(Arc>>); + + impl std::io::Write for Capture { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.lock().expect("capture lock").extend_from_slice(buf); + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + + /// A line feed, a forged log line, a carriage return and escape sequences. + const FORGED: &str = "x\n2026-10-31T23:59:59.000000Z INFO forged line\r\n\x1b[31mred\x1b[0m"; + + /// The access record for `table_name` and `path`, as the text format + /// `serve` uses writes it, without colours. + fn access_record(table_name: &str, path: &str) -> String { + let capture = Capture::default(); + let writer = capture.clone(); + let subscriber = tracing_subscriber::fmt() + .with_ansi(false) + .with_writer(move || writer.clone()) + .finish(); + tracing::subscriber::with_default(subscriber, || { + super::log_file_access("1", "ImportTable", table_name, std::path::Path::new(path)); + }); + String::from_utf8(capture.0.lock().expect("capture lock").clone()).expect("utf-8 log") + } + + fn assert_one_line_without_controls(out: &str) { + assert_eq!(out.matches('\n').count(), 1, "one line: {out:?}"); + assert!(!out.contains('\r') && !out.contains('\x1b'), "{out:?}"); + } + + #[test] + fn a_path_with_control_characters_stays_on_one_log_line() { + let out = access_record("orders", &format!("/exports/1/{FORGED}.json")); + assert_one_line_without_controls(&out); + assert!( + out.contains(r#"resolved_path="/exports/1/x\n2026-10-31"#), + "{out:?}" + ); + } + + /// ImportTable logs the table name it is about to create before any check + /// of that name. + #[test] + fn a_table_name_with_control_characters_stays_on_one_log_line() { + let out = access_record(FORGED, "/imports/1/items.json"); + assert_one_line_without_controls(&out); + assert!(out.contains(r#"table_name="x\n2026-10-31"#), "{out:?}"); + } +} + #[cfg(test)] mod export_arn_tests { use super::{INVALID_TABLE_ARN_REQUEST_MESSAGE, extract_table_name_from_arn};