pathutil

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pathutil provides path expansion and validation utilities.

Package pathutil provides path manipulation and safety validation utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDirectoryOfFile

func GetDirectoryOfFile(path string) string

GetDirectoryOfFile returns the directory containing the given file path.

func SafeJoin added in v0.5.0

func SafeJoin(base string, elem ...string) (string, error)

SafeJoin joins path elements and validates the result stays within the base directory. This is similar to filepath.Join but with traversal protection.

func ValidateNoPathTraversal added in v0.5.0

func ValidateNoPathTraversal(path string) error

ValidateNoPathTraversal validates that a path doesn't contain path traversal sequences. This prevents attacks like extracting files to ../../../etc/passwd.

func ValidatePathWithinBase

func ValidatePathWithinBase(targetPath string, baseDir string) error

ValidatePathWithinBase checks if targetPath is within baseDir (no path traversal escape). This is optional validation for security-sensitive operations. Pass empty baseDir to skip validation.

func ValidateRemovalPath added in v0.5.0

func ValidateRemovalPath(path string) error

ValidateRemovalPath validates that a path is safe to remove. Returns an error if the path is dangerous (empty, root, system directories).

Types

type PathExpander

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

PathExpander provides path expansion functionality.

func NewPathExpander

func NewPathExpander(renderer template.Renderer) *PathExpander

NewPathExpander creates a new PathExpander with the given template renderer.

func (*PathExpander) ExpandPath

func (p *PathExpander) ExpandPath(originalPath string, currentDir string, context map[string]interface{}) (string, error)

ExpandPath expands a path with template variables and special prefixes (~, ., ./).

func (*PathExpander) SafeExpandPath

func (p *PathExpander) SafeExpandPath(originalPath string, currentDir string, context map[string]interface{}, baseDir string) (string, error)

SafeExpandPath is like ExpandPath but validates the result is within baseDir. Pass empty baseDir to disable validation (same as ExpandPath).

Jump to

Keyboard shortcuts

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