pathutil

package
v0.0.73 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicWriteFileInWorkspace added in v0.0.67

func AtomicWriteFileInWorkspace(workDir, relPath string, data []byte, perm os.FileMode) error

AtomicWriteFileInWorkspace writes a new same-directory inode and atomically replaces relPath, leaving any hardlink aliases to the old inode unchanged.

func AtomicWriteFilePreservingModeInWorkspace added in v0.0.67

func AtomicWriteFilePreservingModeInWorkspace(workDir, relPath string, data []byte, perm os.FileMode) error

AtomicWriteFilePreservingModeInWorkspace atomically replaces relPath while preserving the permission bits of an existing regular destination. New files use perm.

func CreateExclusiveFileInWorkspace added in v0.0.67

func CreateExclusiveFileInWorkspace(workDir, relPath string, data []byte, perm os.FileMode) error

CreateExclusiveFileInWorkspace creates relPath beneath workDir and refuses to overwrite any existing directory entry.

func MkdirAllInWorkspace added in v0.0.67

func MkdirAllInWorkspace(workDir, relPath string, perm os.FileMode) error

MkdirAllInWorkspace recursively creates a directory beneath workDir without traversing symlinks.

func OpenFileNoFollow

func OpenFileNoFollow(path string, flag int, perm os.FileMode) (*os.File, error)

OpenFileNoFollow opens a file after refusing a symlink in the final path component. Platforms with O_NOFOLLOW use the kernel flag; other platforms use an Lstat preflight so the package still compiles and fails closed for ordinary symlink attacks.

func OpenInWorkspace

func OpenInWorkspace(workDir, relPath string, flag int, perm os.FileMode) (*os.File, error)

OpenInWorkspace opens a file located beneath workDir without exposing a canonicalize-then-open TOCTOU window.

On Linux the implementation uses openat2(2) with RESOLVE_BENEATH and RESOLVE_NO_SYMLINKS so the kernel atomically refuses any symlink (final or intermediate) and any path that escapes the workspace, eliminating the race between resolution and open.

Non-Linux platforms fail closed because this package does not provide an equivalent descriptor-relative containment primitive there.

func ReadFileNoFollow

func ReadFileNoFollow(path string) ([]byte, os.FileInfo, error)

ReadFileNoFollow reads a file after refusing a symlink in the final path component. Callers should first resolve and validate workspace membership.

func ReadFileNoFollowLimit

func ReadFileNoFollowLimit(path string, maxBytes int64) ([]byte, os.FileInfo, error)

ReadFileNoFollowLimit behaves like ReadFileNoFollow but rejects regular files whose size exceeds maxBytes before reading their contents into memory. A maxBytes of zero or less disables the size check.

func RequireSingleLink(info os.FileInfo) error

RequireSingleLink rejects regular files with more than one directory entry. A path can be lexically inside the workspace while its inode is also linked outside it; refusing multiply-linked inputs prevents that alias from crossing the workspace read boundary.

func Resolve

func Resolve(workDir, inputPath string) string

Resolve resolves tool input paths relative to the agent work directory. Absolute paths are returned as-is.

func ResolveWorkspace

func ResolveWorkspace(workDir, inputPath string) (string, error)

ResolveWorkspace resolves a path and rejects paths outside the workspace.

func SuggestRelative

func SuggestRelative(inputPath string) string

SuggestRelative extracts the likely relative portion from a wrong absolute path for use in error messages.

func WriteFileNoFollow

func WriteFileNoFollow(path string, data []byte, perm os.FileMode) error

WriteFileNoFollow writes a file after refusing a symlink in the final path component. Callers should first resolve and validate workspace membership.

Types

This section is empty.

Jump to

Keyboard shortcuts

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