Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ tokio = { workspace = true, features = ["test-util"] }
extenddb-storage-sqlite = { path = "../storage-sqlite" }
# Scratch directories that are removed when a test panics.
tempfile = "3"
tracing-subscriber = { workspace = true }

# `tests/private_modes.rs` sets umask 0.
[target.'cfg(unix)'.dev-dependencies]
Expand Down
98 changes: 86 additions & 12 deletions crates/engine/src/import_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,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.
Expand Down Expand Up @@ -335,12 +334,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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests call log_file_access directly, so a call site that logs the fields itself is not caught: with the export call site put back to main's inline Display log, both tests pass. The move into files.rs changes exactly these call sites; a test that runs the handler and reads the captured record would hold it.

&ctx.account_id,
"ExportTableToPointInTime",
&table_name,
&output_path,
);

let mut file = create_export_file(&output_path).await?;
Expand Down Expand Up @@ -526,6 +524,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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional. Since #830 the server's table name gate refuses TableName edbv-772-c-bad\nname with a 400 ValidationException before the handler, on both backends, so through the server only the path can carry control characters; the table name escape is defence in depth, which this comment and the body could say.

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<Mutex<Vec<u8>>>);

impl std::io::Write for Capture {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
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() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Testing section gives 195 passed for cargo test --locked -p extenddb-engine, but this head runs 870 (869 in the lib binary, 1 in tests/private_modes.rs, 0 doctests). Putting table_name or resolved_path back to Display in log_file_access fails the matching new test at import_export.rs:579 (left: 3, right: 1 for the table name), so both fields are pinned. Updating the count in the description would resolve this.

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};
Expand Down
Loading