sandbox

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package sandbox confines the files a request file may read or write to a file root. Every path that comes from a request file (bodies, multipart files, `output`, certificates, keys, netrc and cookie files, unix sockets) goes through a Root; paths given on the command line do not.

Index

Constants

This section is empty.

Variables

View Source
var ErrDenied = errors.New("access denied outside file root")

ErrDenied reports a path outside the file root (after resolving `..` and symbolic links).

Functions

func WriteFileAtomicIn added in v1.4.0

func WriteFileAtomicIn(root *os.Root, rel string, data []byte, perm fs.FileMode) (err error)

WriteFileAtomicIn writes data to rel (slash-separated) inside root via a temporary file in the same directory, synced, then renamed into place and the directory synced, so a reader never sees a partial file and a write cut short leaves the previous content. Parent directories are created as needed (0o755); the file gets permission perm (before the umask).

Types

type Root

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

Root is a directory that confines file access.

func Open

func Open(dir string) (*Root, error)

Open opens dir (relative to the working directory or absolute) as a file root.

func (*Root) Chmod added in v1.4.0

func (r *Root) Chmod(name string, perm fs.FileMode) error

Chmod changes the permission of a file.

func (*Root) Close

func (r *Root) Close() error

Close releases the root.

func (*Root) Dir

func (r *Root) Dir() string

Dir returns the absolute path of the file root.

func (*Root) Lstat added in v1.4.0

func (r *Root) Lstat(name string) (fs.FileInfo, error)

Lstat describes a file without following a final symbolic link.

func (*Root) MkdirAll added in v1.4.0

func (r *Root) MkdirAll(name string, perm fs.FileMode) error

MkdirAll creates a directory and its missing parents with permission perm (before the umask).

func (*Root) Path

func (r *Root) Path(name string) (string, error)

Path returns the absolute path of name after checking that it stays in the root, for the APIs that need a path (unix sockets).

func (*Root) ReadDir added in v1.4.0

func (r *Root) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir returns the entries of a directory, sorted by name.

func (*Root) ReadFile

func (r *Root) ReadFile(name string) ([]byte, error)

ReadFile reads a file. name is relative to the root, or absolute and inside it; any other name returns an error wrapping ErrDenied.

func (*Root) Remove added in v1.4.0

func (r *Root) Remove(name string) error

Remove removes a file or an empty directory.

func (*Root) Rename added in v1.4.0

func (r *Root) Rename(oldname, newname string) error

Rename renames (moves) oldname to newname, replacing a file at newname.

func (*Root) Stat added in v1.4.0

func (r *Root) Stat(name string) (fs.FileInfo, error)

Stat describes a file, following symbolic links inside the root.

func (*Root) WriteFile

func (r *Root) WriteFile(name string, data []byte) error

WriteFile writes a file; its parent directory must exist.

func (*Root) WriteFileAtomic added in v1.4.0

func (r *Root) WriteFileAtomic(name string, data []byte, perm fs.FileMode) error

WriteFileAtomic writes a file with permission perm (before the umask) so that a reader, or a process killed while writing, never sees a partial file: see WriteFileAtomicIn. Missing parent directories are created.

Jump to

Keyboard shortcuts

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