urlegit

package module
v0.1.29 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: Apache-2.0 Imports: 4 Imported by: 2

README

urlegit

URLegit is a library for validating URLs.

Build Status codecov.io Go Report Card Apache V2 License GitHub Release GoDoc

Summary

URL validation is complex and very specific to the task at hand. This library's goal is to provide a toolkit for making pre-flight checks against a URL easier.

Usage

package main

import (
	"fmt"

	"github.com/xmidt-org/urlegit"
)

func main() {
	c, err := urlegit.New(
		urlegit.OnlyScheme("https"),
		urlegit.ForbidSpecialUseDomains(),
	)

	if err != nil {
		panic(err)
	}

	url := "https://github.com"
	fmt.Printf("Is %q allowed? %t\n", url, c.Legit(url))

	// Output:
	// Is "https://github.com" allowed? true
}

Go Playground

Resources

Footnotes

This library originally started of in xmidt-org/ancla as webhook URL validation code. Here is a perma link to that code.

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/xmidt-org/urlegit"
)

func main() {
	c, err := urlegit.New(
		urlegit.OnlyAllowSchemes("https"),
		urlegit.ForbidSpecialUseDomains(),
	)

	if err != nil {
		panic(err)
	}

	url := "https://github.com"
	fmt.Printf("Is %q allowed? %t\n", url, c.Legit(url))

}
Output:
Is "https://github.com" allowed? true

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrSchemeNotAllowed     = fmt.Errorf("scheme not allowed")
	ErrHostnameEmpty        = fmt.Errorf("hostname is empty")
	ErrLoopback             = fmt.Errorf("loopback address")
	ErrDomainNotAllowed     = fmt.Errorf("domain not allowed")
	ErrRootDomainNotAllowed = fmt.Errorf("root domain not allowed")
	ErrInvalidInput         = fmt.Errorf("invalid input")
	ErrSubnetNotAllowed     = fmt.Errorf("subnet not allowed")
	ErrIPNotAllowed         = fmt.Errorf("IPs not allowed")
)

Functions

This section is empty.

Types

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

Checker is a URL validator.

func Must

func Must(opts ...Option) *Checker

Must returns a new Checker with the provided options applied. If an error occurs, it panics.

func New

func New(opts ...Option) (*Checker, error)

New returns a new Checker with the provided options applied.

func (*Checker) Legit

func (c *Checker) Legit(s string) bool

Legit returns true if the provided string is a valid URL based on the provided options.

func (*Checker) String

func (c *Checker) String() string

func (*Checker) Text

func (c *Checker) Text(s string) error

Text returns an error if the provided string is not a valid URL based on the provided options.

func (*Checker) URL

func (c *Checker) URL(u *url.URL) error

URLDetails returns an error if the provided URL is not valid based on the provided options.

func (*Checker) URLegit

func (c *Checker) URLegit(u *url.URL) bool

URLLegit returns true if the provided URL is valid based on the provided options.

type HostVador added in v0.1.0

type HostVador func(string) error

HostVador is a function that validates a host.

type IPVador added in v0.1.0

type IPVador func(*net.IP) error

IPVador is a function that validates an IP.

type Option

type Option interface {
	fmt.Stringer
	// contains filtered or unexported methods
}

Option is an option for a Checker.

func CustomHostVador added in v0.1.0

func CustomHostVador(h HostVador) Option

CustomHostVador returns an Option that will use the given HostVador to validate hosts.

func CustomIPVador added in v0.1.0

func CustomIPVador(i IPVador) Option

CustomIPVador returns an Option that will use the given IPVador to validate IPs.

func CustomSchemeVador added in v0.1.0

func CustomSchemeVador(s SchemeVador) Option

CustomSchemeVador returns an Option that will use the given SchemeVador to validate schemes.

func Error

func Error(err error) Option

Error returns an option that will cause the Checker to return the given error.

func ForbidAnyIPs added in v0.1.0

func ForbidAnyIPs() Option

ForbidAnyIPs returns an Option that disallows any IP addresses using only rules that do not result in a network call. Note that this does not apply to hostnames that are resolved to an IP address when a resolver is provided.

func ForbidDomainNames

func ForbidDomainNames(domains ...string) Option

ForbidDomainNames returns an Option that disallows the provided domain names. This is a case-insensitive match, and where the domain name is mached against the hostname starting from the end. The character '*' matches everything in the single subdomain it is used.

Example: hostname "www.example.com." will match any of the following domains:

  • "*."
  • "*.*."
  • "*.*.*."
  • "com."
  • "*.com."
  • "example.*."
  • "example.com."
  • "www.example.com."
  • "*.example.com."
  • etc...

But will not match:

  • "foo.www.example.com."
  • "*.www.example.com."
  • etc...

func ForbidLoopback

func ForbidLoopback() Option

ForbidLoopback returns an Option that disallows loopback addresses using only rules that do not result in a network call.

func ForbidSpecialUseDomains

func ForbidSpecialUseDomains() Option

ForbidSpecialUseDomains returns an Option that disallows the use of special use domains. See https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml

func ForbidSubnet

func ForbidSubnet(subnet string, resolver ...Resolver) Option

ForbidSubnet returns an Option that disallows the provided subnet addresses using only rules that do not result in a network call, unless a resolver is provided. If a resolver is provided, it will be used to resolve the hostname and check the returned IP addresses for loopback addresses. If the subnet is invalid then the Option will return an error.

func ForbidSubnets

func ForbidSubnets(subnets []string, resolver ...Resolver) Option

func OnlyAllowSchemes added in v0.1.0

func OnlyAllowSchemes(s ...string) Option

OnlyAllowSchemes returns an Option that allows any of the provided schemes. Multiple Schemes() options are allowed, but they are applied seprately.

Scheme validation is case-insensitive.

func WithResolver added in v0.1.0

func WithResolver(r Resolver) Option

WithResolver returns an Option that will use the given Resolver to resolve hostnames into IP addresses.

type Resolver

type Resolver func(host string) ([]net.IP, error)

Resolver is a function that returns a list of IP addresses for a given host.

To use the default go resolver, use the net.LookupIP function.

type SchemeVador added in v0.1.0

type SchemeVador func(string) error

SchemeVador is a function that validates a scheme.

Jump to

Keyboard shortcuts

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