Skip to main content

common_datasource/
object_store.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod azblob;
16pub mod fs;
17pub mod gcs;
18pub mod oss;
19pub mod s3;
20
21use std::collections::HashMap;
22use std::path::{Component, Path, PathBuf};
23use std::sync::Arc;
24
25use common_telemetry::debug;
26use lazy_static::lazy_static;
27use object_store::ObjectStore;
28use object_store::secure_fs::SecureFsRoot;
29use regex::Regex;
30use snafu::{OptionExt, ResultExt};
31use url::{ParseError, Url};
32
33use self::azblob::build_azblob_backend;
34use self::fs::build_fs_backend;
35use self::gcs::build_gcs_backend;
36use self::s3::build_s3_backend;
37use crate::error::{self, Result};
38use crate::object_store::oss::build_oss_backend;
39use crate::util::find_dir_and_filename;
40
41pub const FS_SCHEMA: &str = "FS";
42pub const FILE_SCHEMA: &str = "FILE";
43pub const S3_SCHEMA: &str = "S3";
44pub const OSS_SCHEMA: &str = "OSS";
45pub const GCS_SCHEMA: &str = "GCS";
46pub const AZBLOB_SCHEMA: &str = "AZBLOB";
47
48/// An object store rooted at the target's parent, together with the optional
49/// target path relative to that root.
50pub struct BuiltBackend {
51    pub object_store: ObjectStore,
52    pub object_path: Option<String>,
53    local_root: Option<SecureFsRoot>,
54}
55
56impl BuiltBackend {
57    /// Checks an explicit input using the same file-type filter as directory listing.
58    pub async fn is_file(&self, path: &str) -> object_store::Result<bool> {
59        // Stat first preserves errors for dangling or inaccessible symlink targets.
60        if self.object_store.stat(path).await?.mode() != object_store::EntryMode::FILE {
61            return Ok(false);
62        }
63        if let Some(root) = &self.local_root {
64            root.is_file(path).await
65        } else {
66            Ok(true)
67        }
68    }
69}
70
71/// Controls whether SQL paths may access the local filesystem.
72#[derive(Clone, Debug, Default)]
73pub enum LocalFileAccess {
74    /// Local filesystem paths are rejected.
75    #[default]
76    Disabled,
77    /// Local filesystem paths are confined to a server-configured root.
78    Sandboxed { root: LocalFileRoot },
79}
80
81/// An opened server-controlled root for sandboxed SQL file access.
82#[derive(Clone, Debug)]
83pub struct LocalFileRoot {
84    root: Arc<SecureFsRoot>,
85    configured_path: Arc<PathBuf>,
86}
87
88impl LocalFileAccess {
89    /// Creates a sandbox rooted at a server-controlled local directory.
90    pub fn sandboxed(root: impl AsRef<Path>) -> Result<Self> {
91        let root_path = root.as_ref();
92        let configured_path =
93            std::path::absolute(root_path).with_context(|_| error::InvalidLocalFileRootSnafu {
94                root: root_path.display().to_string(),
95            })?;
96        let root =
97            SecureFsRoot::open(root_path).with_context(|_| error::InvalidLocalFileRootSnafu {
98                root: root_path.display().to_string(),
99            })?;
100        Ok(Self::Sandboxed {
101            root: LocalFileRoot {
102                root: Arc::new(root),
103                configured_path: Arc::new(configured_path),
104            },
105        })
106    }
107
108    /// Returns the canonical path of the configured sandbox root.
109    pub fn sandbox_root(&self) -> Option<&Path> {
110        match self {
111            Self::Disabled => None,
112            Self::Sandboxed { root } => Some(root.root.path()),
113        }
114    }
115
116    fn authorize(&self, location: &str, path: &Path, trailing_slash: bool) -> Result<String> {
117        let LocalFileAccess::Sandboxed { root } = self else {
118            return error::LocalFileAccessDisabledSnafu {
119                path: location.to_string(),
120            }
121            .fail();
122        };
123
124        let path = normalize_untrusted_path(path).map_err(|reason| {
125            error::LocalFileAccessDeniedSnafu {
126                path: location.to_string(),
127                reason,
128            }
129            .build()
130        })?;
131        let relative = if path.is_absolute() {
132            strip_local_prefix(&path, root.configured_path.as_path())
133                .or_else(|| strip_local_prefix(&path, root.root.path()))
134                .ok_or_else(|| {
135                    error::LocalFileAccessDeniedSnafu {
136                        path: location.to_string(),
137                        reason: "absolute path is outside the configured copy root".to_string(),
138                    }
139                    .build()
140                })?
141        } else {
142            path.as_path()
143        };
144
145        let mut authorized = relative
146            .components()
147            .filter_map(|component| match component {
148                Component::CurDir => None,
149                Component::Normal(value) => Some(value.to_string_lossy().into_owned()),
150                _ => None,
151            })
152            .collect::<Vec<_>>()
153            .join("/");
154        if trailing_slash && !authorized.is_empty() {
155            authorized.push('/');
156        }
157        Ok(authorized)
158    }
159
160    async fn open_backend_root(
161        &self,
162        location: &str,
163        relative_root: &str,
164        create: bool,
165    ) -> Result<SecureFsRoot> {
166        let LocalFileAccess::Sandboxed { root } = self else {
167            return error::LocalFileAccessDisabledSnafu {
168                path: location.to_string(),
169            }
170            .fail();
171        };
172
173        let root = root.root.clone();
174        let relative_root = relative_root.trim_matches('/').to_string();
175        common_runtime::spawn_blocking_global(move || {
176            if create {
177                root.create_subdir(relative_root)
178            } else {
179                root.open_subdir(relative_root)
180            }
181        })
182        .await
183        .context(error::JoinHandleSnafu)?
184        .map_err(|error| {
185            debug!(
186                "Failed to open an authorized local SQL path inside the copy root, path: {location}, error: {error:?}"
187            );
188            if error.kind() == std::io::ErrorKind::NotFound {
189                return error::LocalFilePathNotFoundSnafu { path: location }.build();
190            }
191            error::LocalFileAccessDeniedSnafu {
192                path: location.to_string(),
193                reason: "path could not be safely resolved within the configured copy root"
194                    .to_string(),
195            }
196            .build()
197        })
198    }
199}
200
201/// Converts a configured location into a local path.
202///
203/// Bare paths and `file://` URLs are local. Other URL schemes return `None`.
204pub fn configured_local_path(location: &str) -> Result<Option<PathBuf>> {
205    #[cfg(windows)]
206    if Path::new(location).is_absolute() {
207        return Ok(Some(PathBuf::from(location)));
208    }
209
210    let (schema, _, path) = parse_url(location)?;
211    match schema.to_uppercase().as_str() {
212        FS_SCHEMA => Ok(Some(PathBuf::from(path))),
213        FILE_SCHEMA => {
214            let url = Url::parse(location).context(error::InvalidUrlSnafu { url: location })?;
215            url.to_file_path().map(Some).map_err(|_| {
216                error::InvalidLocalFileRootConfigSnafu {
217                    root: location.to_string(),
218                    reason: "file URL must contain a local absolute path".to_string(),
219                }
220                .build()
221            })
222        }
223        _ => Ok(None),
224    }
225}
226
227fn strip_local_prefix<'a>(path: &'a Path, prefix: &Path) -> Option<&'a Path> {
228    #[cfg(not(windows))]
229    {
230        path.strip_prefix(prefix).ok()
231    }
232
233    #[cfg(windows)]
234    {
235        let mut path_components = path.components();
236        for prefix_component in prefix.components() {
237            let path_component = path_components.next()?;
238            if !windows_component_eq(path_component, prefix_component) {
239                return None;
240            }
241        }
242        Some(path_components.as_path())
243    }
244}
245
246#[cfg(windows)]
247fn windows_component_eq(left: Component<'_>, right: Component<'_>) -> bool {
248    match (left, right) {
249        (Component::Prefix(left), Component::Prefix(right)) => {
250            windows_os_str_eq(left.as_os_str(), right.as_os_str())
251        }
252        (Component::Normal(left), Component::Normal(right)) => windows_os_str_eq(left, right),
253        (Component::RootDir, Component::RootDir)
254        | (Component::CurDir, Component::CurDir)
255        | (Component::ParentDir, Component::ParentDir) => true,
256        _ => false,
257    }
258}
259
260#[cfg(windows)]
261fn windows_os_str_eq(left: &std::ffi::OsStr, right: &std::ffi::OsStr) -> bool {
262    use std::os::windows::ffi::OsStrExt;
263
264    fn ascii_lowercase(value: u16) -> u16 {
265        if (u16::from(b'A')..=u16::from(b'Z')).contains(&value) {
266            value + u16::from(b'a' - b'A')
267        } else {
268            value
269        }
270    }
271
272    left.encode_wide()
273        .map(ascii_lowercase)
274        .eq(right.encode_wide().map(ascii_lowercase))
275}
276
277fn normalize_untrusted_path(path: &Path) -> std::result::Result<PathBuf, String> {
278    let mut normalized = PathBuf::new();
279    for component in path.components() {
280        match component {
281            Component::Prefix(prefix) => normalized.push(prefix.as_os_str()),
282            Component::RootDir => normalized.push(Path::new(std::path::MAIN_SEPARATOR_STR)),
283            Component::CurDir => {}
284            Component::Normal(value) => normalized.push(value),
285            Component::ParentDir => return Err("'..' path components are not allowed".to_string()),
286        }
287    }
288    Ok(normalized)
289}
290
291/// Returns `(schema, Option<host>, path)`
292pub fn parse_url(url: &str) -> Result<(String, Option<String>, String)> {
293    #[cfg(windows)]
294    {
295        // On Windows, the URL may start with `C:/` or `C:\`.
296        if handle_windows_path(url).is_some() {
297            return Ok((FS_SCHEMA.to_string(), None, url.to_string()));
298        }
299    }
300    let parsed_url = Url::parse(url);
301    match parsed_url {
302        Ok(url) => Ok((
303            url.scheme().to_string(),
304            url.host_str().map(|s| s.to_string()),
305            url.path().to_string(),
306        )),
307        Err(ParseError::RelativeUrlWithoutBase) => {
308            Ok((FS_SCHEMA.to_string(), None, url.to_string()))
309        }
310        Err(err) => Err(err).context(error::InvalidUrlSnafu { url }),
311    }
312}
313
314pub async fn build_backend(
315    url: &str,
316    connection: &HashMap<String, String>,
317    local_file_access: &LocalFileAccess,
318) -> Result<ObjectStore> {
319    Ok(
320        build_backend_inner(url, connection, local_file_access, false, false)
321            .await?
322            .object_store,
323    )
324}
325
326/// Builds a backend and returns the target path relative to the backend root.
327pub async fn build_backend_with_path(
328    url: &str,
329    connection: &HashMap<String, String>,
330    local_file_access: &LocalFileAccess,
331) -> Result<BuiltBackend> {
332    build_backend_inner(url, connection, local_file_access, false, false).await
333}
334
335/// Builds a backend for an operation that may create the target directory.
336pub async fn build_backend_for_write(
337    url: &str,
338    connection: &HashMap<String, String>,
339    local_file_access: &LocalFileAccess,
340) -> Result<ObjectStore> {
341    Ok(
342        build_backend_inner(url, connection, local_file_access, true, false)
343            .await?
344            .object_store,
345    )
346}
347
348/// Builds a writable backend and returns the target path relative to the backend root.
349pub async fn build_backend_for_write_with_path(
350    url: &str,
351    connection: &HashMap<String, String>,
352    local_file_access: &LocalFileAccess,
353) -> Result<BuiltBackend> {
354    build_backend_inner(url, connection, local_file_access, true, true).await
355}
356
357async fn build_backend_inner(
358    url: &str,
359    connection: &HashMap<String, String>,
360    local_file_access: &LocalFileAccess,
361    create_local_root: bool,
362    require_object_path: bool,
363) -> Result<BuiltBackend> {
364    let (schema, host, path) = parse_url(url)?;
365    let normalized_schema = schema.to_uppercase();
366
367    if normalized_schema == FS_SCHEMA || normalized_schema == FILE_SCHEMA {
368        let (local_path, trailing_slash) = if normalized_schema == FILE_SCHEMA {
369            let url = Url::parse(url).context(error::InvalidUrlSnafu { url })?;
370            let path = url.to_file_path().map_err(|_| {
371                error::LocalFileAccessDeniedSnafu {
372                    path: url.to_string(),
373                    reason: "file URL must contain a local absolute path".to_string(),
374                }
375                .build()
376            })?;
377            (path, url.path().ends_with('/'))
378        } else {
379            (
380                PathBuf::from(&path),
381                path.ends_with('/') || cfg!(windows) && path.ends_with(std::path::MAIN_SEPARATOR),
382            )
383        };
384        let authorized = local_file_access.authorize(url, &local_path, trailing_slash)?;
385        let (root, object_path) = find_dir_and_filename(&authorized);
386        if require_object_path && object_path.is_none() {
387            return error::MissingObjectNameSnafu {
388                path: url.to_string(),
389            }
390            .fail();
391        }
392        let root = local_file_access
393            .open_backend_root(url, &root, create_local_root)
394            .await?;
395        return Ok(BuiltBackend {
396            object_store: build_fs_backend(&root)?,
397            object_path,
398            local_root: Some(root),
399        });
400    }
401
402    let (root, object_path) = find_dir_and_filename(&path);
403
404    let object_store = match normalized_schema.as_str() {
405        S3_SCHEMA => {
406            let host = host.context(error::EmptyHostPathSnafu {
407                url: url.to_string(),
408            })?;
409            build_s3_backend(&host, &root, connection)?
410        }
411        OSS_SCHEMA => {
412            let host = host.context(error::EmptyHostPathSnafu {
413                url: url.to_string(),
414            })?;
415            build_oss_backend(&host, &root, connection)?
416        }
417        GCS_SCHEMA => {
418            let host = host.context(error::EmptyHostPathSnafu {
419                url: url.to_string(),
420            })?;
421            build_gcs_backend(&host, &root, connection)?
422        }
423        AZBLOB_SCHEMA => {
424            let host = host.context(error::EmptyHostPathSnafu {
425                url: url.to_string(),
426            })?;
427            build_azblob_backend(&host, &root, connection)?
428        }
429        _ => error::UnsupportedBackendProtocolSnafu {
430            protocol: schema,
431            url,
432        }
433        .fail()?,
434    };
435    Ok(BuiltBackend {
436        object_store,
437        object_path,
438        local_root: None,
439    })
440}
441
442lazy_static! {
443    static ref DISK_SYMBOL_PATTERN: Regex = Regex::new(r"^([A-Za-z]:[/\\])").unwrap();
444}
445
446pub fn handle_windows_path(url: &str) -> Option<String> {
447    DISK_SYMBOL_PATTERN
448        .captures(url)
449        .map(|captures| captures[0].to_string())
450}
451
452#[cfg(test)]
453mod tests {
454    use std::collections::HashMap;
455    use std::fs;
456
457    use common_error::ext::{ErrorExt, RetryHint};
458    use common_error::status_code::StatusCode;
459    use common_test_util::temp_dir::create_temp_dir;
460    use url::Url;
461
462    use super::{
463        LocalFileAccess, build_backend, build_backend_for_write, build_backend_for_write_with_path,
464        build_backend_with_path, handle_windows_path,
465    };
466    use crate::error::Error;
467
468    #[test]
469    fn test_handle_windows_path() {
470        assert_eq!(
471            handle_windows_path("C:/to/path/file"),
472            Some("C:/".to_string())
473        );
474        assert_eq!(
475            handle_windows_path(r"C:\to\path\file"),
476            Some(r"C:\".to_string())
477        );
478        assert_eq!(handle_windows_path("https://google.com"), None);
479        assert_eq!(handle_windows_path("s3://bucket/path/to"), None);
480    }
481
482    #[cfg(windows)]
483    #[test]
484    fn test_windows_local_path_detection_and_prefix() {
485        use std::path::{Path, PathBuf};
486
487        let location = r"C:\gtdata";
488        assert_eq!(
489            super::configured_local_path(location).unwrap(),
490            Some(PathBuf::from(location))
491        );
492        assert_eq!(
493            super::parse_url(location).unwrap(),
494            ("FS".to_string(), None, location.to_string())
495        );
496        assert_eq!(
497            super::strip_local_prefix(
498                Path::new(r"c:\Data\Copy\nested\data.parquet"),
499                Path::new(r"C:\data\copy"),
500            ),
501            Some(Path::new(r"nested\data.parquet"))
502        );
503    }
504
505    #[tokio::test]
506    async fn test_local_file_access_policy() {
507        let data_home = create_temp_dir("local_file_access_policy");
508        let copy_root = data_home.path().join("copy");
509        let internal_dir = data_home.path().join("data");
510        fs::create_dir_all(&internal_dir).unwrap();
511        fs::write(internal_dir.join("secret"), "secret").unwrap();
512
513        let access = LocalFileAccess::sandboxed(&copy_root).unwrap();
514        let connection = HashMap::new();
515
516        let store = build_backend_for_write("nested/data.txt", &connection, &access)
517            .await
518            .unwrap();
519        store.write("data.txt", "first").await.unwrap();
520        store.write("data.txt", "second").await.unwrap();
521        assert_eq!(
522            fs::read_to_string(copy_root.join("nested/data.txt")).unwrap(),
523            "second"
524        );
525
526        let missing = copy_root.join("missing/directory");
527        let error = build_backend("missing/directory/data.txt", &connection, &access)
528            .await
529            .unwrap_err();
530        assert!(matches!(&error, Error::LocalFilePathNotFound { .. }));
531        assert_eq!(error.status_code(), StatusCode::InvalidArguments);
532        assert_eq!(error.retry_hint(), RetryHint::NonRetryable);
533        assert!(
534            error.to_string().contains("does not exist"),
535            "unexpected error: {error}"
536        );
537        assert!(!missing.exists());
538
539        let absolute = copy_root.join("nested/data.txt");
540        let store = build_backend(absolute.to_str().unwrap(), &connection, &access)
541            .await
542            .unwrap();
543        assert_eq!(store.read("data.txt").await.unwrap().to_vec(), b"second");
544
545        let file_url = Url::from_file_path(&absolute).unwrap().to_string();
546        let store = build_backend(&file_url, &connection, &access)
547            .await
548            .unwrap();
549        assert_eq!(store.read("data.txt").await.unwrap().to_vec(), b"second");
550
551        let internal_file = internal_dir.join("secret");
552        assert!(matches!(
553            build_backend(internal_file.to_str().unwrap(), &connection, &access).await,
554            Err(Error::LocalFileAccessDenied { .. })
555        ));
556        assert!(
557            build_backend("../escape/data.txt", &connection, &access)
558                .await
559                .is_err()
560        );
561
562        let outside = data_home.path().join("outside/new");
563        assert!(
564            build_backend(outside.to_str().unwrap(), &connection, &access)
565                .await
566                .is_err()
567        );
568        assert!(!outside.parent().unwrap().exists());
569
570        let prefix_escape = data_home.path().join("copy-not-the-root/new");
571        assert!(matches!(
572            build_backend(prefix_escape.to_str().unwrap(), &connection, &access).await,
573            Err(Error::LocalFileAccessDenied { .. })
574        ));
575        assert!(!prefix_escape.parent().unwrap().exists());
576
577        let disabled = LocalFileAccess::Disabled;
578        let error = build_backend(internal_file.to_str().unwrap(), &connection, &disabled)
579            .await
580            .unwrap_err();
581        assert!(matches!(&error, Error::LocalFileAccessDisabled { .. }));
582        assert_eq!(error.status_code(), StatusCode::InvalidArguments);
583        assert_eq!(error.retry_hint(), RetryHint::NonRetryable);
584        assert!(matches!(
585            build_backend(&file_url, &connection, &disabled).await,
586            Err(Error::LocalFileAccessDisabled { .. })
587        ));
588        assert!(matches!(
589            build_backend(outside.to_str().unwrap(), &connection, &disabled).await,
590            Err(Error::LocalFileAccessDisabled { .. })
591        ));
592        assert!(!outside.parent().unwrap().exists());
593    }
594
595    #[tokio::test]
596    async fn test_file_url_returns_decoded_backend_relative_path() {
597        let temp_dir = create_temp_dir("file_url_backend_relative_path");
598        let copy_root = temp_dir.path().join("copy root");
599        let file = copy_root.join("nested dir/data file.txt");
600        fs::create_dir_all(file.parent().unwrap()).unwrap();
601        fs::write(&file, "data").unwrap();
602
603        let location = Url::from_file_path(&file).unwrap().to_string();
604        assert!(location.contains("%20"));
605        let access = LocalFileAccess::sandboxed(&copy_root).unwrap();
606        let backend = build_backend_with_path(&location, &HashMap::new(), &access)
607            .await
608            .unwrap();
609
610        assert_eq!(backend.object_path.as_deref(), Some("data file.txt"));
611        assert_eq!(
612            backend
613                .object_store
614                .read(backend.object_path.as_deref().unwrap())
615                .await
616                .unwrap()
617                .to_vec(),
618            b"data"
619        );
620    }
621
622    #[tokio::test]
623    async fn test_write_with_path_rejects_directory_before_creation() {
624        let temp_dir = create_temp_dir("write_with_path_rejects_directory");
625        let copy_root = temp_dir.path().join("copy");
626        let target = copy_root.join("new directory");
627        let location = Url::from_directory_path(&target).unwrap().to_string();
628        let access = LocalFileAccess::sandboxed(&copy_root).unwrap();
629
630        let result = build_backend_for_write_with_path(&location, &HashMap::new(), &access).await;
631
632        assert!(matches!(result, Err(Error::MissingObjectName { .. })));
633        assert!(!target.exists());
634
635        let result = build_backend_for_write_with_path(
636            &location,
637            &HashMap::new(),
638            &LocalFileAccess::Disabled,
639        )
640        .await;
641        assert!(matches!(result, Err(Error::LocalFileAccessDisabled { .. })));
642        assert!(!target.exists());
643
644        let relative_target = copy_root.join("relative directory");
645        let result =
646            build_backend_for_write_with_path("relative directory/", &HashMap::new(), &access)
647                .await;
648        assert!(matches!(result, Err(Error::MissingObjectName { .. })));
649        assert!(!relative_target.exists());
650
651        let allowed_directory = copy_root.join("allowed directory");
652        build_backend_for_write("allowed directory/", &HashMap::new(), &access)
653            .await
654            .unwrap();
655        assert!(allowed_directory.is_dir());
656    }
657
658    #[cfg(windows)]
659    #[tokio::test]
660    async fn test_windows_backslash_path_returns_backend_relative_path() {
661        let temp_dir = create_temp_dir("windows_backend_relative_path");
662        let copy_root = temp_dir.path().join("copy");
663        let directory = copy_root.join("nested");
664        let file = directory.join("data.txt");
665        fs::create_dir_all(&directory).unwrap();
666        fs::write(&file, "data").unwrap();
667
668        let access = LocalFileAccess::sandboxed(&copy_root).unwrap();
669        let connection = HashMap::new();
670        let file_location = file.to_str().unwrap();
671        assert!(file_location.contains('\\'));
672        let backend = build_backend_with_path(file_location, &connection, &access)
673            .await
674            .unwrap();
675        assert_eq!(backend.object_path.as_deref(), Some("data.txt"));
676        assert_eq!(
677            backend
678                .object_store
679                .read(backend.object_path.as_deref().unwrap())
680                .await
681                .unwrap()
682                .to_vec(),
683            b"data"
684        );
685
686        let new_directory = copy_root.join("new directory");
687        let directory_location = format!("{}\\", new_directory.display());
688        assert!(matches!(
689            build_backend_for_write_with_path(&directory_location, &connection, &access).await,
690            Err(Error::MissingObjectName { .. })
691        ));
692        assert!(!new_directory.exists());
693    }
694
695    #[tokio::test]
696    async fn test_object_storage_ignores_local_file_policy() {
697        let cases = [
698            (
699                "s3://bucket/path/data%20file.parquet",
700                HashMap::from([
701                    ("region".to_string(), "us-east-1".to_string()),
702                    ("disable_ec2_metadata".to_string(), "true".to_string()),
703                ]),
704                "data%20file.parquet",
705            ),
706            (
707                "oss://bucket/path/file.parquet",
708                HashMap::from([
709                    ("endpoint".to_string(), "http://oss.example.com".to_string()),
710                    ("allow_anonymous".to_string(), "true".to_string()),
711                ]),
712                "file.parquet",
713            ),
714            (
715                "gcs://bucket/path/file.parquet",
716                HashMap::from([(
717                    "endpoint".to_string(),
718                    "http://storage.example.com".to_string(),
719                )]),
720                "file.parquet",
721            ),
722            (
723                "azblob://container/path/file.parquet",
724                HashMap::from([
725                    (
726                        "endpoint".to_string(),
727                        "http://storage.example.com".to_string(),
728                    ),
729                    ("account_name".to_string(), "test".to_string()),
730                ]),
731                "file.parquet",
732            ),
733        ];
734        for (location, connection, expected_path) in cases {
735            let backend = build_backend_for_write_with_path(
736                location,
737                &connection,
738                &LocalFileAccess::Disabled,
739            )
740            .await
741            .unwrap();
742            assert_eq!(backend.object_path.as_deref(), Some(expected_path));
743        }
744    }
745
746    #[cfg(unix)]
747    #[tokio::test]
748    async fn test_local_file_access_rejects_symlink_escape() {
749        use std::os::unix::fs::symlink;
750
751        let temp_dir = create_temp_dir("local_file_access_symlink");
752        let copy_root = temp_dir.path().join("copy");
753        let outside = temp_dir.path().join("outside");
754        fs::create_dir_all(&copy_root).unwrap();
755        fs::create_dir_all(&outside).unwrap();
756        fs::write(outside.join("secret"), "secret").unwrap();
757        symlink(&outside, copy_root.join("escape")).unwrap();
758        symlink(outside.join("secret"), copy_root.join("secret-link")).unwrap();
759
760        let access = LocalFileAccess::sandboxed(&copy_root).unwrap();
761        let connection = HashMap::new();
762
763        assert!(
764            build_backend("escape/secret", &connection, &access)
765                .await
766                .is_err()
767        );
768        assert!(
769            build_backend_for_write("escape/new", &connection, &access)
770                .await
771                .is_err()
772        );
773        assert!(!outside.join("new").exists());
774
775        let store = build_backend("secret-link", &connection, &access)
776            .await
777            .unwrap();
778        assert!(store.read("secret-link").await.is_err());
779        assert!(store.write("secret-link", "overwritten").await.is_err());
780        assert_eq!(
781            fs::read_to_string(outside.join("secret")).unwrap(),
782            "secret"
783        );
784    }
785}