pathjail

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT 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 pathjail confines file operations to a directory tree.

It exists because the obvious guard does not work. Checking a path with Lstat and then handing the same string to MkdirAll, CreateTemp, Rename or Remove is two independent resolutions with a gap between them, and every operation after the check re-resolves the ANCESTORS as well. A store that verified only its own directory and file was therefore writable outside its tree whenever any component above them was a link: the check passed, and the syscall that followed went somewhere else.

On Windows the same guard failed for a second, independent reason. A junction is a reparse point but NOT a symlink, so os.Lstat reports it as ModeIrregular with ModeSymlink clear, and a guard testing only ModeSymlink returns "not a link" when pointed straight at one. Junctions also need no privilege to create, unlike Windows symlinks, so this is reachable by exactly the unprivileged user a workspace guard is meant to contain.

The fix for both is to stop naming things by path. Open a handle on the root once and address everything below it relative to that handle, so the kernel resolves each component against an object that cannot be swapped underneath. os.Root is that handle: it refuses any component that would escape, including a Windows reparse point, and it is the same idea the sandbox uses for ACL materialization.

Index

Constants

This section is empty.

Variables

View Source
var ErrEscapes = errors.New("path escapes its root")

ErrEscapes reports a target that is not inside the root it was confined to. Returned before anything is opened, so a caller cannot act on the path first.

View Source
var ErrReparse = errors.New("refusing to write through a link")

ErrReparse reports a link or reparse point where a real file or directory was required.

Functions

func CreateTemp

func CreateTemp(handle *os.Root, dir, prefix, suffix string) (*os.File, string, error)

CreateTemp makes an O_EXCL temp file named prefix.<random>suffix inside dir, relative to handle, and returns it with its relative path.

O_EXCL rather than a fixed name: a link planted at a predictable temp path between the check and the create is the same race this package exists to close, and O_EXCL makes the kernel refuse it rather than us.

func Open

func Open(root, dir string) (*os.Root, string, error)

Open confines dir to root and returns a handle on root plus dir expressed relative to it. The caller closes the handle.

root is resolved normally, because it is the boundary the caller chose and trusts (a workspace the operator opened, say, which may legitimately sit under a link). Everything BELOW it is resolved against the returned handle and cannot escape.

func RefuseReparse

func RefuseReparse(handle *os.Root, relative string) error

RefuseReparse fails when relative names a link or reparse point.

Absent is fine: a name that does not exist yet is what a create is for.

This is belt to os.Root's braces. The handle already refuses to traverse a reparse point, so this exists to give the FINAL component a clear error instead of an "escapes from parent" one, and to keep the refusal explicit at the point a reader looks for it.

ModeIrregular is tested alongside ModeSymlink deliberately: that is what a Windows junction reports, and a guard checking only ModeSymlink lets one straight through.

Types

This section is empty.

Jump to

Keyboard shortcuts

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