routepath

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPath           = errors.New("invalid path")
	ErrBackslashInPath       = errors.New("path contains backslash")
	ErrNullByteInPath        = errors.New("path contains null byte")
	ErrInvalidPercentEscape  = errors.New("invalid percent escape sequence")
	ErrPathEscapesRoot       = errors.New("path escapes root via ..")
	ErrEncodedSlashInSegment = errors.New("encoded slash (%2F) in non-catch-all segment")
)

Path canonicalization errors.

Functions

func CanonicalizeAndValidateNavPath

func CanonicalizeAndValidateNavPath(path string) (string, error)

CanonicalizeAndValidateNavPath canonicalizes and validates a navigation path. This is used for NAV_* patches and ctx.Navigate() to ensure security.

Per Section 4.2 (Full Navigation), NAV_* payloads MUST be relative paths only:

  • MUST start with "/"
  • MUST NOT be a full URL (no "http://", "https://", "//")

Returns the canonicalized path with query string, or an error if invalid.

func DecodePathSegments

func DecodePathSegments(path string) ([]string, error)

DecodePathSegments decodes all segments of a path. This splits the path by "/" and decodes each segment individually. For catch-all segments, the full remainder is decoded but "/" is preserved.

func DecodeSegment

func DecodeSegment(segment string, isCatchAll bool) (string, error)

DecodeSegment decodes a single path segment. For non-catch-all params, if decoding produces "/" (i.e., %2F was present), this returns an error as it indicates a path smuggling attempt.

func SafePathUnescape

func SafePathUnescape(path string) (string, error)

SafePathUnescape decodes percent-escapes in a path while preserving encoded path separators (%2F, %5C). This prevents path smuggling where different layers interpret separators differently.

func SplitPathAndQuery

func SplitPathAndQuery(input string) (path, query string)

SplitPathAndQuery splits a path into path and query components. The query is returned without the leading "?".

Types

type CanonicalizeResult

type CanonicalizeResult struct {
	// Path is the canonicalized path (without query string).
	Path string

	// Query is the query string (without leading "?").
	Query string

	// Changed indicates if the path was modified during canonicalization.
	Changed bool
}

CanonicalizeResult contains the result of path canonicalization.

func CanonicalizePath

func CanonicalizePath(input string) (CanonicalizeResult, error)

CanonicalizePath normalizes a URL path according to the routing contract.

Per Section 1.2 (Path Canonicalization), the following transformations are applied:

  • Remove trailing slash (except for root "/")
  • Collapse multiple slashes (/blog//post → /blog/post)
  • Remove "." segments (/blog/./post → /blog/post)
  • Resolve ".." segments (/blog/../other → /other)

The following inputs are rejected with an error:

  • Paths containing backslash (\)
  • Paths containing NUL byte (%00)
  • Invalid percent-escapes (e.g., %GG, %2)
  • ".." that would escape root (e.g., /../secret)

The input may include a query string, which is preserved but not canonicalized.

Jump to

Keyboard shortcuts

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