Internal

internal/urlnorm

import "github.com/gofiber/fiber/v3/internal/urlnorm"

Package urlnorm applies the parts of the WHATWG URL parser's input handling a guard must apply before judging a URL Fiber is about to emit. Nothing normalizes an outbound URL; the parser deciding it runs in the client after.

The callers need different policies:

  • StripTabCRLF removes tab, CR and LF only.
  • AsBrowserReads also trims surrounding C0 controls and spaces.
  • Referer also folds backslashes before a query or fragment, retaining up to two leading slashes so the caller can check the original authority.
  • RootedPath forces exactly one leading slash for a generated local path; it does not trim surrounding spaces or fold backslashes inside the path.

These transformations do not authorize a redirect or check its origin.

Functions

func AsBrowserReads(location string) string

Source: internal/urlnorm/urlnorm.go:108

AsBrowserReads applies the WHATWG parser's input cleanup: tab, LF and CR removed, then surrounding C0 controls and spaces trimmed. Interior spaces and percent escapes remain unchanged here.

func LeadingSlashes(s string) int

Source: internal/urlnorm/urlnorm.go:97

LeadingSlashes returns the length of the run of slashes starting s, counting backslashes among them because the parser folds the two.

func Referer(location string) string

Source: internal/urlnorm/urlnorm.go:120

Referer normalizes an input for a subsequent same-origin check. It trims surrounding C0 controls/spaces, removes tab/newline, and folds backslashes only before the first query or fragment delimiter. It preserves raw bytes and percent escapes rather than decoding or re-encoding them.

A leading slash run is reduced to two to retain an authority; an input made entirely of slashes becomes "/". Unlike RootedPath, it must not turn a cross-origin reference into a local path before the caller checks it.

func RootedPath(location string) string

Source: internal/urlnorm/urlnorm.go:84

RootedPath returns location as what a URL composed from a named route can only be: a path on the origin now being served. Parameter values are escaped before this check, except slashes inside greedy parameters; normalization still protects route author text and direct callers.

Exactly one leading slash, never two: "//evil.com" is a network-path reference whose host is whatever follows, so it resolves against "https://good.example" as "https://evil.com". That is true of author text as much as of a value, which is why NamesAuthority refuses such a route rather than composing it.

func StripTabCRLF(s string) string

Source: internal/urlnorm/urlnorm.go:32

StripTabCRLF removes every ASCII tab, LF and CR, as the WHATWG URL parser does before parsing. Leaving them in lets "\t/evil.com" hide behind a leading slash. Returns the input unchanged, without copying, when it holds none of them.