Help us shape the future of Go! Take the 2026 Go Developer Survey

redirect

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package redirect validates where the library may send a browser or a credential and where it serves routes: same-origin return paths, login locations carrying a return path, configured external URLs and redirect targets, the endpoints of identity providers and route path prefixes. Every function is safe for concurrent use; all but To are pure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsoluteTarget

func AbsoluteTarget(raw, externalURL string) (string, error)

AbsoluteTarget validates a configured redirect target (a provider's ErrorURL or PostLogoutURL) and returns it absolute: a same-origin path (SafeReturnPath) is prefixed with externalURL (an ExternalURL result), so a forward-auth redirect reaches the browser unchanged; an absolute http(s) URL is kept. A fragment is refused, so a query appended later (ErrorURL?code=...) stays in the query. Errors never repeat the value.

func ExternalURL

func ExternalURL(raw string) (string, error)

ExternalURL validates an absolute http(s) URL without user info, query or fragment and trims a trailing "/". Errors never repeat the value, so a misplaced credential does not end up in a log.

func IsPathPrefix

func IsPathPrefix(prefix string) bool

IsPathPrefix reports whether prefix can prefix a provider's routes: "/"-separated segments of unreserved URL characters (A-Z a-z 0-9 . _ ~ -), no trailing "/", and no "." or ".." segment that path cleaning would remove, so the routes are neither ServeMux wildcards nor unreachable.

func IsSecureURL

func IsSecureURL(raw string) bool

IsSecureURL reports whether raw is an absolute https URL with a host name, or an http URL on a loopback host (localhost, 127.0.0.0/8, ::1), without user info: credentials never travel in clear text, tests run on 127.0.0.1 (design D16).

func IsTarget

func IsTarget(raw string) bool

IsTarget reports whether raw is a valid redirect target that takes a query: a same-origin path (SafeReturnPath) or an absolute http(s) URL with a host, without a fragment (AbsoluteTarget's rule).

func LoginLocation

func LoginLocation(loginURL, returnPath string) string

LoginLocation appends return_to=<returnPath> to loginURL (keeps an existing query); empty returnPath appends nothing.

func ReturnPath

func ReturnPath(request *http.Request) string

ReturnPath returns SafeReturnPath(request query return_to) or "/".

func SafeReturnPath

func SafeReturnPath(raw string) (string, bool)

SafeReturnPath accepts only same-origin relative paths: starts with "/", second byte not "/" or "\", valid UTF-8 (JSON would store each invalid byte as six), no control characters or "\" anywhere, url.Parse succeeds with empty Scheme, Host and Opaque, <= 2048 bytes. Returns the path as sent plus "?"+RawQuery when present (fragment dropped).

func To

func To(writer http.ResponseWriter, location string)

To answers 302 Found to location with Cache-Control no-store.

func WithQuery

func WithQuery(target string, values url.Values) string

WithQuery appends the encoded values to target, keeping a query target already has.

Types

This section is empty.

Jump to

Keyboard shortcuts

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