pathutil

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package pathutil provides path manipulation utilities for the server package.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPath is returned when the resolved file path cannot be made absolute.
	ErrInvalidPath = errors.New("invalid file path")
	// ErrInvalidImagesDir is returned when the images directory path cannot be made absolute.
	ErrInvalidImagesDir = errors.New("invalid images directory")
	// ErrPathTraversal is returned when the file path escapes the images directory.
	ErrPathTraversal = errors.New("path traversal detected")
)

Sentinel errors returned by SafeImagePath.

Functions

func RemoveImagesDirPrefix

func RemoveImagesDirPrefix(normalizedImagesDir, path string) (string, error)

RemoveImagesDirPrefix removes images dir prefix with path traversal check. normalizedImagesDir should be the pre-normalized result of filepath.ToSlash(imagesDir). Returns an error if the resulting path contains path traversal sequences (..).

func SafeImagePath added in v0.7.0

func SafeImagePath(imagesDir, filePath string) (string, error)

SafeImagePath resolves a file path within the images directory and ensures it does not escape the images root via "../" path traversal. It returns the absolute safe path or one of the sentinel errors ErrInvalidPath, ErrInvalidImagesDir, or ErrPathTraversal.

The check is simple: filepath.Join normalizes "../" components, so comparing the joined absolute path against the images directory prefix catches any traversal attempt. Symlinks inside the images directory are not resolved — the OS kernel handles them transparently when the path is used for serving.

Types

This section is empty.

Jump to

Keyboard shortcuts

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