Skip to main content

resolve_with_canonical_root

Function resolve_with_canonical_root 

Source
pub fn resolve_with_canonical_root(
    root_canon: &Path,
    request_path: &str,
) -> Result<PathBuf, StaticError>
Expand description

Resolve a request path under a pre-canonicalized root.

This function assumes root_canon is already in canonical form — root_canon should be the output of root.canonicalize() called once at server startup. Per-request resolution only canonicalizes the joined path, not the root.

§Path Traversal Protection

Segment-based traversal check rejects only path segments exactly equal to ... This allows filenames containing .. as a substring (e.g., jquery..min.js) while blocking traversal attempts like ../../etc/passwd.

§Directory Handling

If the resolved path is a directory, automatically serves index.html from that directory if it exists and doesn’t escape the root.

Symlinks are followed during canonicalization. After following symlinks, the final canonical path must stay within the server root.

§Arguments

  • root_canon - The server root in canonical form (should be output of canonicalize()).
  • request_path - The HTTP request path (e.g., /path/to/file.html).

§Returns

  • Ok(PathBuf) if the path resolves to a file within root.
  • Err(StaticError::NotFound) if the path doesn’t exist.
  • Err(StaticError::Traversal) if the path attempts to escape the root.