Middleware

middleware/hostauthorization

github.com/gofiber/fiber/v3/middleware/hostauthorization

import "github.com/gofiber/fiber/v3/middleware/hostauthorization"

Variables

ConfigDefault is the default config.

Source: middleware/hostauthorization/config.go:56

var ConfigDefault = Config{}

ErrForbiddenHost is returned when the Host header does not match any allowed host.

Source: middleware/hostauthorization/config.go:10

var ErrForbiddenHost = errors.New("hostauthorization: forbidden host")

Functions

func New(config ...Config) fiber.Handler

Source: middleware/hostauthorization/hostauthorization.go:348

New creates a new host authorization middleware handler.

Types

type Config

Source: middleware/hostauthorization/config.go:13

Config defines the config for the host authorization middleware.

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	// Use this to exclude health check endpoints or other paths from host validation.
	//
	// Optional. Default: nil
	Next func(c fiber.Ctx) bool

	// AllowedHostsFunc is a dynamic validator called only when no static
	// AllowedHosts rule matches. Receives the normalized hostname: port stripped,
	// trailing dot removed, IPv6 brackets removed, lowercased, IDN converted to
	// Punycode. An IPv6 address is passed in its RFC 5952 Section 4 form ("::1")
	// whatever spelling the client sent, an IPv4-mapped one as IPv4 ("127.0.0.1").
	// The string is only valid during the call; copy it to keep it.
	// Return true to allow.
	//
	// Optional. Default: nil
	AllowedHostsFunc func(host string) bool

	// ErrorHandler is called when a request is rejected.
	// Receives ErrForbiddenHost as the error.
	//
	// Optional. Default: returns 403 Forbidden.
	ErrorHandler fiber.ErrorHandler

	// AllowedHosts is the list of permitted host values.
	// Supports two match types:
	//   - Exact:     "api.myapp.com"
	//   - Subdomain: "*.myapp.com" (matches any subdomain, NOT the bare domain — list both for apex+subdomains)
	//
	// Entries are normalized at startup: port stripped, trailing dot removed,
	// lowercased, IDN labels converted to Punycode, IPv6 addresses written in
	// their RFC 5952 Section 4 form, RFC 1035 length limits enforced
	// (≤253 total / ≤63 per-label). An IPv4-mapped entry is stored as its IPv4
	// address ("[::ffff:10.0.0.5]" admits "10.0.0.5"). An entry no request could
	// match panics: a URL, a "%", a port that is not numeric, brackets around
	// anything but an IPv6 address, another colon that is not part of an IPv6
	// address, or a "*." entry holding an IP address.
	//
	// Required if AllowedHostsFunc is nil.
	AllowedHosts []string
}
Fields
  • Next func(c fiber.Ctx) bool

    Next defines a function to skip this middleware when returned true. Use this to exclude health check endpoints or other paths from host validation.

    Optional. Default: nil

  • AllowedHostsFunc func(host string) bool

    AllowedHostsFunc is a dynamic validator called only when no static AllowedHosts rule matches. Receives the normalized hostname: port stripped, trailing dot removed, IPv6 brackets removed, lowercased, IDN converted to Punycode. An IPv6 address is passed in its RFC 5952 Section 4 form ("::1") whatever spelling the client sent, an IPv4-mapped one as IPv4 ("127.0.0.1"). The string is only valid during the call; copy it to keep it. Return true to allow.

    Optional. Default: nil

  • ErrorHandler fiber.ErrorHandler

    ErrorHandler is called when a request is rejected. Receives ErrForbiddenHost as the error.

    Optional. Default: returns 403 Forbidden.

  • AllowedHosts []string

    AllowedHosts is the list of permitted host values. Supports two match types:

    • Exact: "api.myapp.com"
    • Subdomain: "*.myapp.com" (matches any subdomain, NOT the bare domain — list both for apex+subdomains)

    Entries are normalized at startup: port stripped, trailing dot removed, lowercased, IDN labels converted to Punycode, IPv6 addresses written in their RFC 5952 Section 4 form, RFC 1035 length limits enforced (≤253 total / ≤63 per-label). An IPv4-mapped entry is stored as its IPv4 address ("[::ffff:10.0.0.5]" admits "10.0.0.5"). An entry no request could match panics: a URL, a "%", a port that is not numeric, brackets around anything but an IPv6 address, another colon that is not part of an IPv6 address, or a "*." entry holding an IP address.

    Required if AllowedHostsFunc is nil.