testcontainers-build 0.1.0

Build Testcontainers images from folders, remote contexts, and pluggable Git providers
Documentation
use crate::{Error, Result};
use std::path::{Path, PathBuf};

/// Validate a path relative to a build context.
/// Use forward slashes and reject parent traversal.
pub fn validate_relative(path: &str) -> Result<()> {
    if path.trim().is_empty()
        || path.starts_with(['/', '-'])
        || path.contains([':', '\\', '\0'])
        || path.split('/').any(|p| p == "..")
    {
        return Err(Error::InvalidInput(
            "expected a relative path without traversal, using forward slashes".into(),
        ));
    }
    Ok(())
}

pub(crate) fn resolve_inside(root: &Path, relative: &str) -> Result<PathBuf> {
    validate_relative(relative)?;
    let mut current = root.to_path_buf();
    for part in relative.split('/').filter(|p| !p.is_empty()) {
        current.push(part);
        if std::fs::symlink_metadata(&current)?
            .file_type()
            .is_symlink()
        {
            return Err(Error::InvalidInput(
                "context selection and Dockerfile paths cannot traverse symbolic links".into(),
            ));
        }
    }
    Ok(current)
}