fileutil

package
v0.39.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const TextSampleSize = 8192

TextSampleSize is the number of bytes sampled from a file to determine whether it is text or binary.

Variables

This section is empty.

Functions

func CopyDir

func CopyDir(src, dst string, dirMode os.FileMode) error

CopyDir recursively copies a directory from src to dst. Symlinks are skipped for security. A TOCTOU mitigation re-checks each file with Lstat before copying to detect symlinks created between walk and copy.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a single file from src to dst, preserving permissions.

func IsTextContent

func IsTextContent(content []byte) bool

IsTextContent checks if content appears to be text rather than binary. It inspects the first 8KB for null bytes, invalid UTF-8, and non-printable characters.

func SanitizeFileMode

func SanitizeFileMode(mode fs.FileMode) fs.FileMode

SanitizeFileMode strips setuid, setgid, and sticky bits from a file mode to prevent privilege escalation from untrusted template sources.

func ValidatePathContainment

func ValidatePathContainment(basePath, fullPath string) error

ValidatePathContainment checks that fullPath is contained within basePath. Both paths are resolved through filepath.EvalSymlinks (with fallback for non-existent targets) and made absolute before comparison. This prevents path traversal attacks including those using symlinks.

Types

This section is empty.

Jump to

Keyboard shortcuts

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