safepath

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 5 Imported by: 2 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 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

View Source
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

func EntrySymlinkTarget(entryName, target string) error

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

func Join(root, name string) (string, error)

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

func JoinStrict(root, name string) (string, error)

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

func ResolveInRoot(root, unsafePath string) (string, error)

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

func SymlinkTarget(root, linkPath, target string) error

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.

Jump to

Keyboard shortcuts

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