Skip to main content

validate_media_path

Function validate_media_path 

Source
pub fn validate_media_path(
    path: &str,
    repo_root: &Path,
    static_folder: &str,
) -> Result<PathBuf, MbrError>
Expand description

Validates a media path from a query parameter.

  • URL-decodes the path
  • Rejects paths containing “..” (directory traversal)
  • Validates the path resolves within the repository root OR the static folder

§Arguments

  • path - The URL-encoded path from the query parameter
  • repo_root - The repository root directory
  • static_folder - The static folder path (may be relative to repo_root, e.g., “../static”)

§Returns

  • Ok(PathBuf) - The validated, canonical path to the media file
  • Err(MbrError) - If the path is invalid or attempts directory traversal

§Security

When static_folder points outside repo_root (e.g., ../static), paths are validated against BOTH directories. This allows serving assets from external static folders while maintaining path traversal protection. Content root takes precedence if the file exists in both locations.