safepath

package
v1.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package safepath opens a file through a chain of path components it has verified itself, and refuses any target that resolves outside a declared set of roots.

Two properties motivate it, and neither is delivered by resolving a path string and then reading it.

First, resolution must happen before the first read. On macOS the kernel traversal into a consent-gated directory IS the consent event: a background process that traverses one gets no error and no prompt it can answer — it blocks indefinitely, and no context deadline interrupts a blocked open. A lexical check on the path as written is not enough; the check has to run on the resolved target of every component, before the syscall that would traverse it. Resolution is therefore os.Lstat plus os.Readlink per component, following nothing: a resolver that followed links would fire the traversal it is called to decide about.

Second, O_NOFOLLOW on the leaf is not sufficient. It protects the leaf only, while every parent was validated by an earlier syscall — a local process that swaps a parent for a symlink in that window still gets the kernel to traverse it. So the read goes through the components as descriptors: on unix an openat chain with O_NOFOLLOW|O_DIRECTORY per component, opening the leaf relative to the last verified directory. Windows has no openat, so the leaf is opened without following a reparse point and the kernel's own final path for the handle is compared against the resolved path before a byte is read; the residual risk there is reading the wrong file rather than a blocked traversal.

Containment is a separate guarantee, and it is what makes a privileged reader safe to point at a user-supplied location: the resolved target must lie inside the roots, which come from the OS user record rather than from any variable the same user could set — a root set taken from the channel being bounded is no bound at all. It is checked at every hop, so a path that leaves the roots is refused before the components below it are stat'd.

Index

Constants

View Source
const (
	// ReasonOutsideRoots is a target that resolves outside the declared root.
	ReasonOutsideRoots = "refused_outside_user_roots"
	// ReasonDenied is a target the process is not permitted to stat or open.
	ReasonDenied = "permission_denied"
	// ReasonUnresolved is a target whose path could not be resolved to a single
	// concrete file: a symlink cycle, more hops than the budget allows, a
	// relative path, or a component swapped between resolution and open.
	ReasonUnresolved = "location_unresolved"
	// ReasonSymlink is a symlink on the path, reported only by a resolver that
	// follows none. A caller that reads a fixed set of directories owned by one
	// application has no legitimate link to follow, and refusing is the only
	// answer that cannot be talked into reading somewhere else.
	ReasonSymlink = "symlink_rejected"
)

Refusal reason codes. These are the only vocabulary a caller reports; a refusal never carries a path or an underlying error message, because both can quote the content of the file being examined.

Variables

This section is empty.

Functions

func ReasonOf

func ReasonOf(err error) string

ReasonOf returns the refusal code carried by err, or "" when err is not a refusal (a missing file is not a refusal).

Types

type Guard

type Guard func(path string) string

Guard reports a refusal reason for a path a Resolver is about to touch, or "" to allow it, consulted on the whole path and on every component before the first syscall against any of them. Its reason travels back as the refusal, so the vocabulary belongs to the caller: this package does not know why a path is off limits, only that it must not find out by touching it.

type Refusal

type Refusal struct{ Reason string }

Refusal is the error returned when a path was declined rather than missing. Reason is one of the codes above and is the whole error text: a refusal must be reportable without disclosing what was refused.

func (*Refusal) Error

func (e *Refusal) Error() string

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver resolves and opens paths under one root, declining anything that leaves it or that its guard refuses. The root must come from the OS user record — the directory service entry, the passwd entry, or the profile path registered for the account's SID — never from $HOME, $XDG_CONFIG_HOME or %APPDATA%, which the same user can set. An empty root contains nothing, so every target is refused, the correct posture when no user resolved.

func New

func New(home string, guard Guard) *Resolver

New returns a Resolver contained to home, consulting guard before every syscall. A nil guard allows every path the containment check admits.

func NewNoFollow

func NewNoFollow(home string, guard Guard) *Resolver

NewNoFollow returns a Resolver that refuses any path with a symlink on it — above or below the root, leaf or ancestor — rather than resolving the link and deciding about its target.

It is for a caller whose paths are all fixed locations owned by one application: there is no legitimate link on such a path, so a link is either a layout this build does not support or an attempt to redirect a privileged read, and the two are indistinguishable from the outside. Refusing is also the only rule with no exempt path class to reason about, which is what makes it auditable. A caller that must tolerate a symlinked home or a dotfile-managed directory wants New instead.

The strength of the refusal follows the platform's open. On unix every component is opened with O_NOFOLLOW, so no link on the path is ever traversed. Windows has no openat: the leaf is opened without following a reparse point and the handle's own final path is compared against the resolved one, so a junction swapped in above the leaf is caught after the traversal rather than before it.

func (*Resolver) Contains

func (r *Resolver) Contains(path string) bool

Contains reports whether path lies within the root. Lexical, and expects an already-resolved path; callers get that from Resolve. The nesting test is at a separator boundary: a prefix test alone would let /home/bobby pass for /home/bob.

func (*Resolver) Read

func (r *Resolver) Read(path string, max int64) (data []byte, resolved string, info os.FileInfo, truncated bool, err error)

Read resolves path and reads at most max bytes from the resolved target through the verified chain. A longer file is read to the cap with truncated set, so the caller can report an incomplete parse rather than a wrong one. A directory resolves and is described but not read: info says what the location is, data is nil, and nothing is refused. The path and info come from the same resolution and handle the bytes came from, so a separate stat would describe another moment.

func (*Resolver) ReadDirNames

func (r *Resolver) ReadDirNames(path string, max int) (names []string, resolved string, more bool, err error)

ReadDirNames resolves path and returns at most max of its immediate entries, reporting whether the directory held more. Non-recursive by construction: it returns names, not paths, so a caller cannot accidentally descend. The bound is applied at the read rather than to the result — the directories this opens are exactly the ones an attacker can fill, so a million entries has to be refused rather than measured.

func (*Resolver) Resolve

func (r *Resolver) Resolve(path string) (string, error)

Resolve returns the concrete path that path refers to, following symlinks component by component without ever letting the kernel do the following. It returns a *Refusal when the guard declines the target, when it escapes the root, when it is unreadable, or when it cannot be resolved; and an error satisfying errors.Is(err, os.ErrNotExist) when nothing is there. Absence is not a refusal — a caller stays silent about a file the machine simply does not have.

func (*Resolver) Stat

func (r *Resolver) Stat(path string) (resolved string, info os.FileInfo, err error)

Stat resolves path and returns what the resolved target is without opening it, which is what makes it usable for a location whose contents must not be read. The metadata comes from the walk, which had to lstat the leaf to know it was no link.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL