HardlinkTracker

Struct HardlinkTracker 

Source
pub struct HardlinkTracker { /* private fields */ }
Expand description

Tracks hardlink targets during extraction.

Hardlinks in archives can be used for attacks:

  1. Link to files outside the extraction directory
  2. Create multiple hardlinks to the same file (resource exhaustion)
  3. Link to sensitive files (if absolute paths allowed)

This tracker ensures:

  • Hardlinks are allowed in the security configuration
  • Targets are relative paths
  • Targets resolve within the destination directory
  • Duplicate hardlinks are detected

§Two-Pass Validation

Hardlinks require two-pass validation:

  1. First pass (during validation): Track target paths, verify they’re within bounds
  2. Second pass (after extraction): Verify targets actually exist

This is necessary because hardlink targets may appear later in the archive.

§Examples

use exarch_core::SecurityConfig;
use exarch_core::security::HardlinkTracker;
use exarch_core::types::DestDir;
use exarch_core::types::SafePath;
use std::path::Path;
use std::path::PathBuf;

let dest = DestDir::new(PathBuf::from("/tmp"))?;
let mut config = SecurityConfig::default();
config.allowed.hardlinks = true;

let mut tracker = HardlinkTracker::new();
let link = SafePath::validate(&PathBuf::from("link"), &dest, &config)?;
let target = Path::new("target.txt");

tracker.validate_hardlink(&link, target, &dest, &config)?;

Implementations§

Source§

impl HardlinkTracker

Source

pub fn new() -> Self

Creates a new hardlink tracker.

Validates that a hardlink target is safe and tracks it.

§Performance

Typical execution time: ~1-5 μs (HashMap insert + path validation)

§Errors

Returns an error if:

  • Hardlinks are not allowed in configuration
  • Target is an absolute path
  • Target would escape the destination directory
§Examples
use exarch_core::SecurityConfig;
use exarch_core::security::HardlinkTracker;
use exarch_core::types::DestDir;
use exarch_core::types::SafePath;
use std::path::Path;
use std::path::PathBuf;

let dest = DestDir::new(PathBuf::from("/tmp"))?;
let mut config = SecurityConfig::default();
config.allowed.hardlinks = true;

let mut tracker = HardlinkTracker::new();
let link = SafePath::validate(&PathBuf::from("link"), &dest, &config)?;
let target = Path::new("target.txt");

tracker.validate_hardlink(&link, target, &dest, &config)?;
Source

pub fn count(&self) -> usize

Returns the number of tracked hardlinks.

Source

pub fn has_target(&self, target: &Path) -> bool

Checks if a target path has been seen before.

Trait Implementations§

Source§

impl Debug for HardlinkTracker

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HardlinkTracker

Source§

fn default() -> HardlinkTracker

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.