diff --git a/native/core/src/execution/delta_dv.rs b/native/core/src/execution/delta_dv.rs index a99409d2ed1..63cff89a1b1 100644 --- a/native/core/src/execution/delta_dv.rs +++ b/native/core/src/execution/delta_dv.rs @@ -47,6 +47,7 @@ use roaring::{RoaringBitmap, RoaringTreemap}; use crate::execution::operators::ExecutionError; use crate::execution::operators::ExecutionError::GeneralError; +use crate::execution::planner::redacted_url_display; use datafusion_comet_proto::spark_operator::DeltaSparkDvDescriptor; const NATIVE_MAGIC: i32 = 1681511376; @@ -630,6 +631,8 @@ async fn attach_access_plan( if dv.cardinality == 0 && dv.size_in_bytes == 0 { return Ok(file); } + // Only messages print the file's URL; reads go through `file.object_meta` and `data_store`. + let file_path = redacted_url_display(&file_path); if dv.size_in_bytes < 0 { return Err(GeneralError(format!( "Deletion vector for {file_path} has negative size {}", @@ -666,7 +669,12 @@ async fn attach_access_plan( let blob = store .get_range(&dv_store_path, offset..offset + framed_len) .await - .map_err(|e| GeneralError(format!("Failed to read deletion vector {dv_path}: {e}")))?; + .map_err(|e| { + GeneralError(format!( + "Failed to read deletion vector {}: {e}", + redacted_url_display(dv_path) + )) + })?; unframe_dv_blob(&blob, dv.size_in_bytes as usize)?.to_vec() } else { return Err(GeneralError( @@ -2751,4 +2759,84 @@ mod tests { "every single-bit flip of the inline payload must have been exercised" ); } + + /// Userinfo and query markers the test URLs carry, none of which may be shown. + const URL_MARKERS: [&str; 3] = ["alice", "SECRET", "SAS-MARKER"]; + + /// A file whose deletion vector fails before any parquet footer is read. + fn failing_dv_file(file_path: &str, dv: DeltaSparkDvDescriptor) -> DvScanFile { + let store: Arc = Arc::new(object_store::memory::InMemory::new()); + DvScanFile { + file: PartitionedFile::new("t/f0.parquet", 100), + file_path: file_path.to_string(), + dv: Some(dv), + data_store: Arc::clone(&store), + dv_store: Some((store, Path::from("t/deletion_vector_x.bin"))), + } + } + + async fn attach_error(file: DvScanFile) -> String { + match attach_access_plans(Arc::new(RuntimeEnv::default()), vec![file]).await { + Ok(_) => panic!("the deletion vector must be rejected"), + Err(e) => e.to_string(), + } + } + + #[tokio::test] + async fn dv_errors_do_not_echo_the_file_url() { + let file_path = "s3://alice:SECRET@bucket/t/f0.parquet?sig=SAS-MARKER"; + let negative_size = DeltaSparkDvDescriptor { + storage_type: "i".to_string(), + absolute_path: None, + inline_data: Some(vec![0; 4]), + offset: None, + size_in_bytes: -1, + cardinality: 1, + }; + let wrong_inline_size = DeltaSparkDvDescriptor { + size_in_bytes: 8, + ..negative_size.clone() + }; + for (dv, expected) in [ + ( + negative_size, + "Deletion vector for s3://***@bucket/t/f0.parquet has negative size -1", + ), + ( + wrong_inline_size, + "Inline deletion vector for s3://***@bucket/t/f0.parquet has 4 bytes", + ), + ] { + let msg = attach_error(failing_dv_file(file_path, dv)).await; + for marker in URL_MARKERS { + assert!(!msg.contains(marker), "{marker} echoed: {msg}"); + } + assert!( + msg.contains(expected), + "expected the file without its credentials or query: {msg}" + ); + } + } + + #[tokio::test] + async fn dv_read_error_does_not_echo_the_dv_url() { + let dv = DeltaSparkDvDescriptor { + storage_type: "p".to_string(), + absolute_path: Some( + "s3://alice:SECRET@bucket/t/deletion_vector_x.bin?sig=SAS-MARKER".to_string(), + ), + inline_data: None, + offset: Some(1), + size_in_bytes: 8, + cardinality: 1, + }; + let msg = attach_error(failing_dv_file("s3://bucket/t/f0.parquet", dv)).await; + for marker in URL_MARKERS { + assert!(!msg.contains(marker), "{marker} echoed: {msg}"); + } + assert!( + msg.contains("Failed to read deletion vector s3://***@bucket/t/deletion_vector_x.bin"), + "expected the deletion vector without its credentials or query: {msg}" + ); + } } diff --git a/native/core/src/execution/planner.rs b/native/core/src/execution/planner.rs index d9bf9c68b75..ad8035a6f64 100644 --- a/native/core/src/execution/planner.rs +++ b/native/core/src/execution/planner.rs @@ -42,6 +42,8 @@ mod delta_scan; // JVM-planned Delta sibling of the kernel handler above; see delta_spark_scan.rs. #[cfg(feature = "delta")] mod delta_spark_scan; +#[cfg(feature = "delta")] +pub(crate) use delta_spark_scan::redacted_url_display; #[cfg(feature = "contrib-lance")] mod lance_scan; diff --git a/native/core/src/execution/planner/delta_spark_scan.rs b/native/core/src/execution/planner/delta_spark_scan.rs index a12effbea42..7c6d0691f97 100644 --- a/native/core/src/execution/planner/delta_spark_scan.rs +++ b/native/core/src/execution/planner/delta_spark_scan.rs @@ -43,10 +43,13 @@ use crate::execution::operators::ExecutionError; use crate::execution::operators::ExecutionError::GeneralError; use crate::execution::planner::PhysicalPlanner; use crate::execution::planner::PlanCreationResult; -use crate::parquet::objectstore::s3_blob_fs_support::normalize_object_store_url; +use crate::parquet::objectstore::s3_blob_fs_support::{ + normalize_object_store_url, scheme_of_unparsed, +}; +use crate::parquet::objectstore::url_redaction::without_secrets; use crate::parquet::parquet_support::{ - hash_object_store_configs, object_store_registration_url, object_store_url_key, - prepare_object_store_with_config_hash, + hash_object_store_configs, is_azure_scheme, object_store_registration_url, + object_store_url_key, prepare_object_store_with_config_hash, }; /// Type name the JVM-planned Delta contrib claims within the `ContribScan` envelope. The @@ -310,56 +313,45 @@ fn url_user_info(url: &Url) -> String { url.username().to_string() } -/// A display form of `url` safe to embed in an error message: userinfo (e.g. the access/secret -/// key pair embedded as `s3a://AKIA...:secret@bucket/...`, or a Delta shallow-clone container -/// name) is replaced with a literal `***`, mirroring `DeltaScanSupport.redactedAuthority` on the -/// JVM side (`scheme://***@host[:port]`). Scheme and host/port are kept verbatim (not -/// lowercased) and the path is kept in full -- userinfo is the only secret-bearing component, -/// and dropping the path would make the two defense-in-depth checks that call this ([` -/// check_same_object_store_authority`] and [`check_store_identity`]) unable to name which file -/// triggered the error. +/// A display form of `url` safe to embed in an error message. Every call site formats a URL +/// that may come from a foreign proto producer, so this is total and never shows a credential: +/// the password, query and fragment (where a SAS token or presigned signature lives) are +/// dropped, and the username is shown as `***`, mirroring `DeltaScanSupport.redactedAuthority` +/// on the JVM side, except in an ABFS URL, where it names the container and is kept. Scheme, +/// host, port and path stay, so [`check_same_object_store_authority`] and +/// [`check_store_identity`] can name the file. A URL with nothing to hide is returned as given. /// -/// `url` need not be a valid [`Url`] -- every call site formats a `GeneralError` from a URL that -/// may originate from a foreign/bypassed proto producer, including ones a credential-bearing URL -/// can produce by FAILING to parse in the first place (e.g. `s3a://AKIA:secret@bucket:notaport/x` -/// is `Url::parse`-rejected as `InvalidPort`, but still carries userinfo), so this must be total -/// (never panic) AND must still redact on the parse-failure path -- it is exactly the credentials -/// that make a URL unusual enough to fail parsing that most need to never reach a log line. -/// The fallback below is purely textual: it looks for a `://` scheme delimiter and, within the -/// authority segment that follows (up to the next `/`, mirroring where a real URL's authority -/// ends), replaces everything up to and including the LAST `@` with `***@` -- same last-`@` split -/// as the successfully-parsed path and `DeltaScanSupport.redactedAuthority` on the JVM side. A -/// string with no `://` is treated as having no authority at all and its whole text is searched -/// for a trailing userinfo-shaped `...@host` prefix the same way. A string with neither shape -/// (no `@` anywhere before its authority ends) has no evident secret to redact and is returned -/// unchanged. -fn redacted_url_display(url: &str) -> String { - if let Ok(parsed) = Url::parse(url) { - if parsed.username().is_empty() && parsed.password().is_none() { - return url.to_string(); - } - let host_port = match (parsed.host_str(), parsed.port()) { - (Some(host), Some(port)) => format!("{host}:{port}"), - (Some(host), None) => host.to_string(), - (None, _) => String::new(), - }; - let mut redacted = format!("{}://***@{host_port}{}", parsed.scheme(), parsed.path()); - if let Some(query) = parsed.query() { - redacted.push('?'); - redacted.push_str(query); +/// A string that fails to parse is returned as given only when it has none of `://`, `@`, `?` +/// and `#`. Otherwise its structure is unknown (a `/` in a password ends the authority early, +/// for one), so only its scheme is shown, as `s3a://`. A URL with no `//` after +/// its scheme parses with an opaque path that can hold userinfo, and its scheme can be a +/// username (`alice:SECRET@bucket`), so it is shown as ``. +pub(crate) fn redacted_url_display(url: &str) -> String { + let parsed = match Url::parse(url) { + Ok(parsed) if parsed.cannot_be_a_base() => return "".to_string(), + Ok(parsed) => parsed, + Err(_) if !url.contains(['@', '?', '#']) && !url.contains("://") => return url.to_string(), + Err(_) => { + return match scheme_of_unparsed(url) { + Some(scheme) => format!("{scheme}://"), + None => "".to_string(), + } } - return redacted; - } - - let (scheme_prefix, rest) = match url.find("://") { - Some(scheme_end) => (&url[..scheme_end + 3], &url[scheme_end + 3..]), - None => ("", url), }; - let authority_len = rest.find('/').unwrap_or(rest.len()); - match rest[..authority_len].rfind('@') { - Some(at) => format!("{scheme_prefix}***@{}", &rest[at + 1..]), - None => url.to_string(), + let masks_username = !parsed.username().is_empty() && !is_azure_scheme(parsed.scheme()); + if !masks_username + && parsed.password().is_none() + && parsed.query().is_none() + && parsed.fragment().is_none() + { + return url.to_string(); } + let mut shown = without_secrets(&parsed); + if masks_username { + // Fails only for a URL that cannot have userinfo, which then has no username to mask. + let _ = shown.set_username("***"); + } + shown.to_string() } /// A store resolved by [`PartitionStoreResolver::resolve`]: the within-store `path` of the @@ -844,23 +836,20 @@ mod tests { assert_eq!(redacted_url_display(input), input); // Not a valid URL (no scheme, so `Url::parse` rejects it as relative), but does have a - // userinfo-shaped prefix before its first '/' -- must still redact it rather than leak - // it verbatim. + // userinfo-shaped prefix -- must not be echoed. let input = "secret@host/path"; let redacted = redacted_url_display(input); assert!( !redacted.contains("secret"), "expected the userinfo-shaped prefix to be redacted: {redacted}" ); - assert_eq!(redacted, "***@host/path"); + assert_eq!(redacted, ""); } #[test] fn redacted_url_display_redacts_credentials_from_a_scheme_prefixed_url_that_fails_to_parse() { - // Invalid port -- `url::Url::parse` rejects this outright (InvalidPort), so this never - // reaches the successfully-parsed branch above; it must still be caught by the fallback, - // which must recognize the `scheme://` prefix so it doesn't stop at the FIRST '/' in - // that prefix (a bug that would leave userinfo un-redacted for exactly this shape). + // Invalid port -- `url::Url::parse` rejects this outright (InvalidPort), so only the + // scheme is shown. let url = "s3a://AKIA:secret@bucket:notaport/path"; assert!(Url::parse(url).is_err(), "fixture must fail to parse"); let redacted = redacted_url_display(url); @@ -868,7 +857,152 @@ mod tests { !redacted.contains("AKIA") && !redacted.contains("secret"), "expected credentials to be redacted: {redacted}" ); - assert_eq!(redacted, "s3a://***@bucket:notaport/path"); + assert_eq!(redacted, "s3a://"); + } + + /// Userinfo, query and fragment markers the test URLs carry, none of which may be shown. + const URL_MARKERS: [&str; 4] = ["alice", "SECRET", "SAS-MARKER", "FRAG-MARKER"]; + + fn assert_no_url_marker(shown: &str) { + for marker in URL_MARKERS { + assert!(!shown.contains(marker), "{marker} echoed: {shown}"); + } + } + + #[test] + fn redacted_url_display_drops_the_query_and_fragment() { + for (url, expected) in [ + ( + "s3a://bucket/a/part-0.parquet?sig=SAS-MARKER#FRAG-MARKER", + "s3a://bucket/a/part-0.parquet", + ), + ( + "s3a://alice:SECRET@bucket/a/part-0.parquet?sig=SAS-MARKER", + "s3a://***@bucket/a/part-0.parquet", + ), + ( + "https://bucket.s3.amazonaws.com/a/part-0.parquet?X-Amz-Signature=SAS-MARKER", + "https://bucket.s3.amazonaws.com/a/part-0.parquet", + ), + ] { + let shown = redacted_url_display(url); + assert_no_url_marker(&shown); + assert_eq!(shown, expected); + } + } + + #[test] + fn redacted_url_display_keeps_the_abfs_container() { + for url in [ + "abfss://data:SECRET@acct.dfs.core.windows.net/t/p.parquet?sig=SAS-MARKER", + "abfss://data:SECRET@acct.dfs.core.windows.net/t/p.parquet", + ] { + let shown = redacted_url_display(url); + assert_no_url_marker(&shown); + assert_eq!(shown, "abfss://data@acct.dfs.core.windows.net/t/p.parquet"); + } + } + + #[test] + fn redacted_url_display_names_only_the_scheme_of_an_unparseable_url() { + for url in [ + "s3a://alice:SECRET@bucket:notaport/p?sig=SAS-MARKER", + "s3a://bucket:notaport/p?sig=SAS-MARKER", + // The `/` in the password ends the authority, which then has a bad port. + "s3a://alice:SECRET/x@bucket/p", + ] { + assert!( + Url::parse(url).is_err(), + "fixture must fail to parse: {url}" + ); + let shown = redacted_url_display(url); + assert_no_url_marker(&shown); + assert_eq!(shown, "s3a://"); + } + } + + #[test] + fn redacted_url_display_hides_opaque_and_relative_strings_with_secrets() { + for (url, expected) in [ + // No `//`, so these parse with an opaque path that keeps the userinfo; in the + // second, the "scheme" is the username. + ("s3a:alice:SECRET@bucket/p", ""), + ("alice:SECRET@bucket://x", ""), + // Relative, so they fail to parse, and carry a query or fragment. + ("t/p.parquet?sig=SAS-MARKER", ""), + ("t/p.parquet#FRAG-MARKER", ""), + ("s3a://:SECRET@bucket/p", "s3a://bucket/p"), + // Nothing to hide: shown as given. + ("t/p.parquet", "t/p.parquet"), + ("hdfs:/tmp/p.parquet", "hdfs:/tmp/p.parquet"), + ] { + let shown = redacted_url_display(url); + assert_no_url_marker(&shown); + assert_eq!(shown, expected, "for {url}"); + } + } + + #[test] + fn parse_errors_do_not_echo_the_url() { + let options = HashMap::new(); + for url in [ + "s3a://bucket:notaport/p?sig=SAS-MARKER", + "s3a://alice:SECRET/x@bucket/p?sig=SAS-MARKER", + ] { + let authority_err = check_same_object_store_authority(&[partitioned_file(url)]) + .unwrap_err() + .to_string(); + let mut resolver = + PartitionStoreResolver::new(Arc::new(RuntimeEnv::default()), &options); + let resolve_err = match resolver.resolve(url.to_string()) { + Ok(_) => panic!("{url} must fail to parse"), + Err(e) => e.to_string(), + }; + for msg in [authority_err, resolve_err] { + assert_no_url_marker(&msg); + assert!( + msg.contains("Error parsing URL s3a://: invalid port number"), + "expected the scheme and the parse error: {msg}" + ); + } + } + } + + #[test] + fn mixed_authority_error_does_not_echo_the_query() { + let files = vec![ + partitioned_file("s3a://bucket-a/part-0.parquet?sig=SAS-MARKER"), + partitioned_file("s3a://bucket-b/part-1.parquet#FRAG-MARKER"), + ]; + let msg = check_same_object_store_authority(&files) + .unwrap_err() + .to_string(); + assert_no_url_marker(&msg); + assert!( + msg.contains("found s3a://bucket-a/part-0.parquet and s3a://bucket-b/part-1.parquet"), + "expected both files without their query or fragment: {msg}" + ); + } + + #[test] + fn store_identity_error_does_not_echo_userinfo_or_query() { + let mut seen = HashMap::new(); + let data = "s3://alice:SECRET@bucket/a/part-0.parquet?sig=SAS-MARKER"; + let dv = "s3://bucket/_delta_log/deletion_vector_x.bin?sig=SAS-MARKER"; + let (data_store_url, data_user_info) = store_url_and_user_info(data); + let (dv_store_url, dv_user_info) = store_url_and_user_info(dv); + check_store_identity(&data_store_url, &data_user_info, data, &mut seen).unwrap(); + let msg = check_store_identity(&dv_store_url, &dv_user_info, dv, &mut seen) + .unwrap_err() + .to_string(); + assert_no_url_marker(&msg); + assert!( + msg.contains( + "found s3://***@bucket/a/part-0.parquet and \ + s3://bucket/_delta_log/deletion_vector_x.bin" + ), + "expected both files without their credentials or query: {msg}" + ); } #[test] @@ -922,8 +1056,8 @@ mod tests { "expected the parse-error message to redact credentials: {msg}" ); assert!( - msg.contains("***@bucket"), - "expected the parse-error message to still name the redacted host: {msg}" + msg.contains("s3a://"), + "expected the parse-error message to still name the scheme: {msg}" ); } } diff --git a/native/core/src/parquet/objectstore/s3_blob_fs_support.rs b/native/core/src/parquet/objectstore/s3_blob_fs_support.rs index 4c55ec8c4b1..1a1e9a0dfcb 100644 --- a/native/core/src/parquet/objectstore/s3_blob_fs_support.rs +++ b/native/core/src/parquet/objectstore/s3_blob_fs_support.rs @@ -102,7 +102,7 @@ pub(crate) fn normalize_object_store_url( } /// The text before `://` in a URL that failed to parse, if it is a valid scheme. -fn scheme_of_unparsed(url_str: &str) -> Option<&str> { +pub(crate) fn scheme_of_unparsed(url_str: &str) -> Option<&str> { let (scheme, _) = url_str.split_once("://")?; let mut chars = scheme.chars(); let is_scheme = chars.next().is_some_and(|c| c.is_ascii_alphabetic()) diff --git a/native/core/src/parquet/parquet_support.rs b/native/core/src/parquet/parquet_support.rs index 1a4601a7503..f3f5ce64f33 100644 --- a/native/core/src/parquet/parquet_support.rs +++ b/native/core/src/parquet/parquet_support.rs @@ -739,7 +739,7 @@ pub fn is_hdfs_scheme(url: &Url, object_store_configs: &HashMap) } /// Check if the scheme is an Azure ABFS URL. -fn is_azure_scheme(scheme: &str) -> bool { +pub(crate) fn is_azure_scheme(scheme: &str) -> bool { matches!(scheme, "abfs" | "abfss") }