Skip to main content

extract_safe

Function extract_safe 

Source
pub fn extract_safe(source: impl Read, target: &Path) -> Result<(), Error>
Expand description

Extract a .tar.gz archive from source into target, rejecting any entry that would escape the target or is not a regular file/directory.

§Pre-extraction checks

Before iterating entries, this function:

  1. Canonicalizes target (caller must create it first).
  2. Walks every ancestor of target up to the filesystem root and verifies none is a symlink.
  3. Lists every direct child of target and verifies none is a symlink.

§Per-entry checks

For each entry:

  • Only Regular and Directory types are accepted; all others (symlinks, hardlinks, devices, etc.) are rejected.
  • Path components are scanned: .., RootDir, and Prefix are rejected.
  • Absolute paths are rejected.
  • A lexical prefix check confirms the resolved path stays within target (case-insensitive on Windows).