Documentation
¶
Overview ¶
Package fileutil provides utility functions for working with file paths and file operations.
Index ¶
- func CopyFile(src, dst string) error
- func DirExists(path string) bool
- func EnsureParentDir(path string, perm os.FileMode) error
- func ExtractFileFromTar(data []byte, path string) ([]byte, error)
- func FileExists(path string) bool
- func IsDirEmpty(path string) bool
- func ResolveExecutablePath(name string) (string, error)
- func ValidateAbsolutePath(path string) (string, error)
- func ValidateExecutablePath(path string) (string, error)
- func ValidatePathWithinBase(base, candidate string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnsureParentDir ¶ added in v0.79.3
EnsureParentDir ensures the parent directory for path exists, creating it recursively when needed.
func ExtractFileFromTar ¶ added in v0.49.0
ExtractFileFromTar extracts a single file from a tar archive. Uses Go's standard archive/tar for cross-platform compatibility instead of spawning an external tar process which may not be available on all platforms.
path must be a local, relative path (no absolute paths or ".." components). filepath.IsLocal is used to enforce this for both the search target and each tar entry name, guarding against path-traversal payloads embedded in archives.
func FileExists ¶ added in v0.47.0
FileExists checks if a file exists and is not a directory.
func IsDirEmpty ¶ added in v0.47.0
IsDirEmpty checks if a directory is empty.
func ResolveExecutablePath ¶ added in v0.83.2
ResolveExecutablePath resolves an executable from PATH and validates the resulting path.
func ValidateAbsolutePath ¶
ValidateAbsolutePath validates that a file path is absolute and safe to use. It performs the following security checks:
- Cleans the path using filepath.Clean to normalize . and .. components
- Verifies the path is absolute to prevent relative path traversal attacks
Returns the cleaned absolute path if validation succeeds, or an error if:
- The path is empty
- The path is relative (not absolute)
This function should be used before any file operations (read, write, stat, etc.) to ensure defense-in-depth security against path traversal vulnerabilities.
Example:
cleanPath, err := fileutil.ValidateAbsolutePath(userInputPath)
if err != nil {
return fmt.Errorf("invalid path: %w", err)
}
content, err := os.ReadFile(cleanPath)
func ValidateExecutablePath ¶ added in v0.83.2
ValidateExecutablePath validates that an executable path is absolute, resolves symlinks when possible, points to a file, and is executable on non-Windows platforms.
func ValidatePathWithinBase ¶ added in v0.71.5
ValidatePathWithinBase checks that candidate is located within the base directory tree. Both paths are resolved via filepath.EvalSymlinks (with filepath.Abs as fallback when a path does not yet exist) before comparison, so neither ".." components nor symlinks pointing outside base can be used to escape.
For candidate paths that do not yet exist on disk, the longest existing ancestor is resolved through EvalSymlinks before the non-existing suffix is re-appended. This prevents an in-base symlinked directory from being used to write outside the base even when the final file does not exist yet.
Returns an error when:
- Either path cannot be resolved to an absolute form.
- The resolved candidate path starts outside the resolved base directory.
Types ¶
This section is empty.