Documentation
¶
Overview ¶
Package safepath holds the path-containment checks an archive or package extractor needs: it defends against zip-slip path traversal and symlink-escape attacks (deb data.tar, RPM CPIO payloads, APK tarballs, container rootfs layers, source archives).
All joins use filepath.Rel-based containment. That is immune to the prefix-aliasing flaw of naive strings.HasPrefix checks (root "/tmp/foo" must not admit "/tmp/foobar/evil") and still meaningful when root is "/", where every absolute path trivially passes a prefix check.
A rejected path wraps ErrEscapesRoot.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/M0Rf30/pkgformat/safepath"
)
func main() {
dst, err := safepath.Join("/srv/root", "usr/bin/tool")
fmt.Println(dst, err)
_, err = safepath.Join("/srv/root", "../../etc/passwd")
fmt.Println(errors.Is(err, safepath.ErrEscapesRoot))
}
Output: /srv/root/usr/bin/tool <nil> true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEscapesRoot = errors.New("safepath: path escapes root")
ErrEscapesRoot is returned, wrapped, when a path or symlink target resolves outside the root it is confined to.
Functions ¶
func EntrySymlinkTarget ¶
EntrySymlinkTarget validates a symlink target for an archive entry whose paths are still archive-relative (no on-disk root yet). The target is resolved against the entry's parent directory inside the archive; an escape manifests as a leading "..".
Absolute targets are accepted for the same reason as SymlinkTarget.
func Join ¶
Join joins root and an archive entry name and verifies the result stays inside root. Absolute entry names are reinterpreted as root-relative (tar/cpio writers disagree on leading slashes). Entries that resolve to root itself (e.g. tar's "./" directory entry) are allowed; use JoinStrict to reject them.
func JoinStrict ¶
JoinStrict is Join but additionally rejects entries that resolve to the root itself ("", ".", "/"). CPIO/RPM extraction uses this: a payload entry must always name something *inside* the install root.
func ResolveInRoot ¶
ResolveInRoot resolves unsafePath (absolute or relative; always treated as relative to root) component by component, following any symlinks that already exist on disk with chroot semantics: absolute link targets are reinterpreted relative to root and ".." is clamped at root. The returned host path is cleaned and is guaranteed to be lexically AND physically inside root. The final component (and any intermediate components) need not exist.
Use it before creating files/directories/links during extraction so that a symlink planted earlier by the archive (e.g. link -> /etc, or the usrmerge lib -> usr/lib) cannot redirect a later write outside root.
Security note: this is TOCTOU-safe only against archive content, i.e. the symlink layout produced by the extraction itself. It does NOT defend against a concurrent attacker on the host who can mutate the tree between resolution and use.
func SymlinkTarget ¶
SymlinkTarget validates the target of a symlink being created at linkPath (an on-disk path under root, as returned by Join/JoinStrict).
Absolute targets are accepted: real packages routinely ship absolute symlinks (e.g. /usr/lib64/libfoo.so.1 → /usr/lib/libfoo.so.1) and dpkg/rpm/apk accept them — they only resolve at runtime, when the package content lives at /.
Relative targets are resolved against the symlink's own parent directory and must stay inside root. This is defense-in-depth against symlink+write attacks where a malicious archive first plants a symlink pointing outside the root and then writes a regular file through it.
Types ¶
This section is empty.