uri

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 12 Imported by: 4 modules, 6 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

🧭 uri

Go Reference Version Build Status Go Report Card Codecov

Resilient URL and Hostname Helpers for Go

Resilient helpers for parsing, validating, and inspecting URLs and hostnames: scheme/protocol handling, IANA top-level-domain validation, local-network detection, and SSRF-safe public-IP classification.

Most functions are forgiving by design — they take a raw string, do their best, and return an empty string or false rather than an error when the input is malformed. The Validate* functions are the exception: they return a derp error describing exactly what was wrong.

Map for the Impatient

  • URLs — url.go: ParseURL (a stricter url.Parse that requires an http/https scheme and a valid hostname), ValidateURL, IsValidURL.
  • Hostnames — hostname.go: Hostname (strip everything down to the host), ValidateHostname (RFC 1035 lengths + IANA TLD check, with IP and local names exempted).
  • Scheme / protocol — scheme.go, protocol.go: Scheme (no ://) vs Protocol (with ://, and "" when the value never had one); GuessSchemeForHostname / PrependProtocol pick http for local hosts and https for public ones.
  • Origins — host.go, origin.go: Host returns the RFC 6454 origin (default ports omitted); IsSameOrigin / NotSameOrigin compare two of them.
  • Addresses — address.go: AddressHost returns the host a URL or a @user@host handle is addressed to; IsSameHost / NotSameHost compare two of them by host alone, ignoring scheme and port, which is looser than IsSameOrigin.
  • Redirect / link safety — redirect.go, safeurl.go: IsSafeRedirectURL / NotSafeRedirectURL accept only same-site relative paths or absolute http(s) URLs, rejecting javascript:/data:/protocol-relative targets that turn an open redirect into XSS. Targets are judged as a browser parses them, so the spellings net/url reads as same-site and a browser reads as off-site (/\evil.com, \\evil.com, ///evil.com, " //evil.com") are rejected too. IsValidRedirectURL / NotValidRedirectURL add absolute, with a valid host, which is the rule for a destination named by a remote server. SafeURL applies the base rule and additionally neutralizes the value for use in an HTML attribute.
  • Same-origin paths — path.go: Path and PathAndQuery reduce any URL to a root-absolute path on this host, for a Location header or a post-login next. Like the redirect checks, they neutralize what a browser would read as off-site, including an encoded tab or newline (/%09/evil.com).
  • Local-network detection — localhost.go, loopback.go: IsLocalHostname, IsLocalURL, IsLoopback.
  • Public-IP / SSRF — public.go: IsPublicIP, NotPublicIP, IsPublicIPAddress. Apply IsPublicIP to the resolved IP at connection time (e.g. in a net.Dialer Control hook), never to a hostname string.
  • TLDs — tld.go, init.go: IsValidTLD, ValidateTLD, RefreshTLDs; the IANA list is embedded from _iana.txt.

See Also

  • benpate/derp — the error type returned by the Validate* functions.

Pull Requests Welcome

I'm trying to make uri the best it can be, and your help is greatly appreciated. If you find a bug or have an idea for a new feature, please open an issue or submit a pull request. We're all in this together! 🧭

Documentation

Overview

Package uri provides helpers for parsing, validating, and inspecting URLs and hostnames.

Most functions are forgiving: they take a raw string of any shape and return "" or FALSE rather than an error. The Validate* functions are the exception, returning a derp error that describes what was wrong. Predicates come in Is…/Not… pairs.

  • URLs and hostnames: ParseURL is a stricter url.Parse, and ValidateHostname enforces RFC 1035 label rules and the IANA TLD list, which is embedded at build time and refreshed at runtime by RefreshTLDs.
  • Schemes, protocols, and origins: Scheme, Protocol, Host, and IsSameOrigin.
  • Local-network detection: IsLoopback and IsLocalHostname, which choose http over https for a local host.
  • SSRF defense: IsPublicIP classifies a resolved net.IP against the IANA special-use registries. Apply it before connecting to a user-supplied host.
  • Redirect and link safety: IsSafeRedirectURL, IsValidRedirectURL, SafeURL, and PathAndQuery judge a target the way a browser parses it, not the way net/url does.

Index

Constants

View Source
const ProtocolHTTP = "http://"

ProtocolHTTP represents the "http://" protocol string (including the :// suffix)

View Source
const ProtocolHTTPS = "https://"

ProtocolHTTPS represents the "https://" protocol string (including the :// suffix)

View Source
const ProtocolSuffix = "://"

ProtocolSuffix represents the "://" suffix that follows the protocol name

View Source
const SchemeHTTP = "http"

SchemeHTTP represents the "http" network scheme (without the :// suffix)

View Source
const SchemeHTTPS = "https"

SchemeHTTPS represents the "https" network scheme (without the :// suffix)

Variables

This section is empty.

Functions

func AddressHost added in v0.9.0

func AddressHost(value string) string

AddressHost returns the lowercased host that a value is addressed to: an http(s) URL's host, the part of a @user@host handle (or email or acct: address) after its last "@", or empty for anything else.

func GuessProtocolForHostname

func GuessProtocolForHostname(hostname string) string

GuessProtocolForHostname returns ProtocolHTTP for a host on the local network, and ProtocolHTTPS for everything else.

func GuessSchemeForHostname

func GuessSchemeForHostname(hostname string) string

GuessSchemeForHostname returns SchemeHTTP for a host on the local network, and SchemeHTTPS for everything else.

func Host

func Host(value string) string

Host returns the lower-cased RFC 6454 origin of an http(s) URL (e.g. "https://server.com:8080"), omitting a default port, or "" for anything else.

func Hostname

func Hostname(value string) string

Hostname returns ONLY the hostname, removing the protocol, userinfo, port, path, querystring, and fragment. The result is lower-cased.

func IsLocalHostname

func IsLocalHostname(hostname string) bool

IsLocalHostname returns TRUE if the hostname is a local domain — a loopback or mDNS/internal name, or any IP address that is not publicly routable.

func IsLocalURL

func IsLocalURL(url string) bool

IsLocalURL returns TRUE if the URL contains a local hostname

func IsLoopback

func IsLoopback(domain string) bool

IsLoopback returns TRUE if the provided domain is a loopback hostname (including the RFC 6761 ".localhost" TLD and common /etc/hosts aliases) or any loopback IP address.

func IsPublicIP added in v0.0.7

func IsPublicIP(ip net.IP) bool

IsPublicIP reports whether ip is a globally routable public address that is safe to connect to, rejecting loopback, private, link-local, multicast, and special-use ranges.

func IsPublicIPAddress added in v0.0.7

func IsPublicIPAddress(value string) bool

IsPublicIPAddress reports whether the given string is a valid IP address that is also a public, safe-to-connect address. An unparseable value returns FALSE.

func IsSafeRedirectURL added in v0.3.0

func IsSafeRedirectURL(target string) bool

IsSafeRedirectURL reports whether target is safe to use as an HTTP redirect destination or a user-facing link href.

func IsSameHost added in v0.9.0

func IsSameHost(first string, second string) bool

IsSameHost returns TRUE if both values are addressed to the same host, and that host is not empty.

func IsSameOrigin added in v0.5.0

func IsSameOrigin(first string, second string) bool

IsSameOrigin returns TRUE when both values are valid http(s) URLs that share the same RFC 6454 origin (scheme, host, and port).

func IsSchemeValid

func IsSchemeValid(scheme string) bool

IsSchemeValid returns TRUE if the scheme is http or https

func IsValidHostname

func IsValidHostname(hostname string) bool

IsValidHostname returns TRUE if the provided value contains a valid hostname.

func IsValidIP4Address

func IsValidIP4Address(ip string) bool

IsValidIP4Address checks if the given string is a valid IPv4 address.

func IsValidIP6Address

func IsValidIP6Address(ip string) bool

IsValidIP6Address checks if the given string is a valid IPv6 address.

func IsValidIPAddress

func IsValidIPAddress(ip string) bool

IsValidIPAddress checks if the given string is a valid IP address (either IPv4 or IPv6).

func IsValidRedirectURL added in v0.8.0

func IsValidRedirectURL(target string) bool

IsValidRedirectURL reports whether target is an absolute http(s) URL naming a valid host, which is the rule for a destination chosen by a remote server.

func IsValidTLD

func IsValidTLD(tld string) bool

IsValidTLD returns TRUE if the provided "top level domain" is found in the IANA list.

func IsValidURL

func IsValidURL(uri string) bool

IsValidURL returns TRUE if the URL is valid

func NormalizeHost added in v0.1.1

func NormalizeHost(value string) string

NormalizeHost returns the lower-cased bare hostname of any URL-ish value: a host, "host:port", a full URL, userinfo, or bracketed IPv6. An IPv6 result is unbracketed, so it may hold colons.

func NotLocalHostname

func NotLocalHostname(hostname string) bool

NotLocalHostname returns TRUE if the hostname is NOT a local domain

func NotLocalURL

func NotLocalURL(url string) bool

NotLocalURL returns TRUE if the URL does NOT contain a local hostname

func NotLoopback

func NotLoopback(domain string) bool

NotLoopback returns TRUE if the provided domain is NOT a loopback address (otherserver.com, 192.168.0.5)

func NotPublicIP added in v0.0.7

func NotPublicIP(ip net.IP) bool

NotPublicIP returns TRUE if ip is NOT a public, safe-to-connect address.

func NotPublicIPAddress added in v0.0.7

func NotPublicIPAddress(value string) bool

NotPublicIPAddress returns TRUE if the given string is NOT a public IP address.

func NotSafeRedirectURL added in v0.6.0

func NotSafeRedirectURL(target string) bool

NotSafeRedirectURL returns TRUE if target is NOT safe to use as an HTTP redirect destination or a user-facing link href.

func NotSameHost added in v0.9.0

func NotSameHost(first string, second string) bool

NotSameHost returns TRUE if the values are NOT addressed to the same host, including when either has no host.

func NotSameOrigin added in v0.5.0

func NotSameOrigin(first string, second string) bool

NotSameOrigin returns TRUE when the two values do NOT share the same RFC 6454 http(s) origin, including when either value is not a valid http(s) URL.

func NotSchemeValid

func NotSchemeValid(scheme string) bool

NotSchemeValid returns TRUE if the scheme is NOT http or https

func NotValidHostname added in v0.0.3

func NotValidHostname(hostname string) bool

NotValidHostname returns TRUE if the provided value does NOT contain a valid hostname.

func NotValidIPAddress added in v0.0.3

func NotValidIPAddress(ip string) bool

NotValidIPAddress checks if the given string is NOT a valid IP address.

func NotValidRedirectURL added in v0.8.0

func NotValidRedirectURL(target string) bool

NotValidRedirectURL returns TRUE if target is NOT an absolute http(s) URL that is safe to use as a redirect destination.

func NotValidTLD

func NotValidTLD(tld string) bool

NotValidTLD returns TRUE if the provided "top level domain" is NOT found in the IANA list.

func NotValidURL

func NotValidURL(uri string) bool

NotValidURL returns TRUE if the URL is NOT valid

func ParseURL

func ParseURL(uri string) (*url.URL, error)

ParseURL is a drop-in replacement for url.Parse that requires a valid hostname and http/https scheme.

func Path

func Path(uri string) string

Path returns the path portion of a URL as a safe, root-absolute, same-origin reference. The scheme and host (if any) are discarded.

func PathAndQuery

func PathAndQuery(uri string) string

PathAndQuery returns the path and query portions of a URL as a safe, root-absolute, same-origin reference that can go directly into an HTTP "Location" header.

func PrependProtocol

func PrependProtocol(uri string) string

PrependProtocol adds a protocol to a URL that has none, using http for a host on the local network and https for everything else. It returns "" if the result is not valid.

func Protocol

func Protocol(uri string) string

Protocol returns the protocol portion of a URL. If the URL is not valid, or has no scheme, then an empty string is returned.

func RefreshTLDs

func RefreshTLDs()

RefreshTLDs loads the most recent TLD list from the IANA website.

func SafeURL added in v0.4.0

func SafeURL(raw string) string

SafeURL accepts exactly what IsSafeRedirectURL accepts, and returns an escaped copy of raw that is safe to place in an HTML attribute of any quote style, or "" if raw is unsafe.

func Scheme

func Scheme(uri string) string

Scheme returns the scheme portion of a URL (without the "://" suffix), or an empty string if the URL cannot be parsed.

func ValidateHostname

func ValidateHostname(hostname string) error

ValidateHostname validates a hostname and returns an error describing any issues found.

func ValidateTLD

func ValidateTLD(tld string) error

ValidateTLD returns an error if the provided top-level domain is empty or is not found in the IANA list.

func ValidateURL

func ValidateURL(uri string) error

ValidateURL returns a validation error unless the URL parses, has a valid hostname, and uses http or https as the scheme.

Types

This section is empty.

Jump to

Keyboard shortcuts

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