Help us shape the future of Go! Take the 2026 Go Developer Survey

fssecurity

package
v0.33.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPathNotAllowed = errors.New("path is outside allowed directories")

ErrPathNotAllowed is the error a path outside the allowed paths wraps.

Functions

func NormalizeAllowedPaths added in v0.33.0

func NormalizeAllowedPaths(paths []string) []string

NormalizeAllowedPaths makes every entry absolute and clean, relative to the working directory now, as each library does when it is registered. IsPathAllowed compares absolute resolved paths, so a relative entry (".", "./data", a script's own directory) would otherwise never match. Nil (no restriction) stays nil and an empty slice (deny all) stays empty; an entry that cannot be made absolute is dropped.

func RawDir added in v0.33.0

func RawDir(path string) string

RawDir returns path without its final element, like filepath.Dir but without cleaning: filepath.Dir("a/l/../b/f") is "a/b", which the kernel resolves differently from the "a/l/../b" that opening the path walks through when l is a symlink. Creating RawDir(path) with os.MkdirAll therefore makes only directories that checking path with IsPathAllowed has already walked. It returns "." for a path with no separator.

func ResolveExistingPrefix added in v0.29.0

func ResolveExistingPrefix(absPath string) string

ResolveExistingPrefix resolves absPath through symlinks as ResolvePath does, returning absPath unchanged when it cannot be resolved (a symlink loop).

func ResolvePath added in v0.33.0

func ResolvePath(absPath string) (string, bool)

ResolvePath resolves an absolute path the way the kernel does when it is opened: component by component, following each symlink (a relative target from the link's own directory) before applying any later "..". Unlike filepath.EvalSymlinks it also resolves paths that do not fully exist: a dangling symlink is followed to where it points, and a component that does not exist is taken as the plain directory os.MkdirAll would create there, so a later ".." pops it and later symlinks are still followed. It reports false for a symlink loop.

Types

type Config

type Config struct {
	// AllowedPaths is a list of absolute directory paths that file operations are restricted to.
	// If nil, all paths are allowed (no restrictions).
	// If empty slice (not nil), no paths are allowed (deny all).
	// All paths must be absolute and will be cleaned/normalized.
	AllowedPaths []string
}

Config holds the configuration for file system security

func (*Config) CheckPath added in v0.33.0

func (c *Config) CheckPath(path string) error

CheckPath returns a *DeniedError when path is outside the allowed paths, nil otherwise. A nil Config allows everything.

func (*Config) IsPathAllowed

func (c *Config) IsPathAllowed(path string) bool

IsPathAllowed checks if the given path is within the allowed paths. Returns true if the path is allowed, false otherwise.

SECURITY CRITICAL: This function prevents path traversal attacks. It handles: - Relative paths (./foo, ../foo) - Path traversal (../../etc/passwd) - Symlink attacks (by evaluating the real path) - Prefix attacks (/allowed vs /allowed-other)

type DeniedError added in v0.33.0

type DeniedError struct {
	Path string
}

DeniedError reports a path outside the allowed paths. It wraps ErrPathNotAllowed, so callers that turn Go errors into script errors can raise it as a PermissionError.

func (*DeniedError) Error added in v0.33.0

func (e *DeniedError) Error() string

func (*DeniedError) Unwrap added in v0.33.0

func (e *DeniedError) Unwrap() error

Jump to

Keyboard shortcuts

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