gofiber/fiber · critical
fiber: keyauth middleware requires a validator function
Error message
fiber: keyauth middleware requires a validator function
What it means
keyauth middleware authenticates requests by validating an extracted API key via a caller-supplied Validator function; without config there is no Validator to call. configDefault (keyauth/config.go:103-106) panics when New() is called with zero variadic arguments so the middleware cannot be mounted in a state that would accept every key (or reject every key) without explicit policy.
Source
Thrown at middleware/keyauth/config.go:105
}
// ConfigDefault is the default config
var ConfigDefault = Config{
SuccessHandler: func(c fiber.Ctx) error {
return c.Next()
},
ErrorHandler: func(c fiber.Ctx, _ error) error {
return c.Status(fiber.StatusUnauthorized).SendString(ErrMissingOrMalformedAPIKey.Error())
},
Realm: "Restricted",
Extractor: extractors.FromAuthHeader("Bearer"),
}
// configDefault is a helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
panic("fiber: keyauth middleware requires a validator function")
}
cfg := config[0]
// Require a validator function
if cfg.Validator == nil {
panic("fiber: keyauth middleware requires a validator function")
}
// Set default values
if cfg.Extractor.Extract == nil {
cfg.Extractor = ConfigDefault.Extractor
}
if cfg.Realm == "" {
cfg.Realm = ConfigDefault.Realm
}
if cfg.SuccessHandler == nil {
cfg.SuccessHandler = ConfigDefault.SuccessHandler
}View on GitHub (pinned to 9a4c7e57fe)
Solutions
- Pass a Config with a Validator: keyauth.New(keyauth.Config{Validator: func(c fiber.Ctx, key string) (bool, error) { ... }}).
- If you are not ready to validate keys, remove the middleware from the router until the validator is implemented.
Example fix
// before
app.Use(keyauth.New())
// after
app.Use(keyauth.New(keyauth.Config{
Validator: func(c fiber.Ctx, key string) (bool, error) {
return key == os.Getenv("API_KEY"), nil
},
})) Defensive patterns
Strategy: type-guard
Validate before calling
func requireKeyAuthConfig(cfg ...keyauth.Config) (keyauth.Config, error) {
if len(cfg) == 0 {
return keyauth.Config{}, errors.New("keyauth.New requires a Config with a Validator")
}
return cfg[0], nil
}
c, err := requireKeyAuthConfig()
if err != nil { log.Fatal(err) } Type guard
// Type guard: ensure the variadic config is present before calling New.
func hasKeyAuthConfig(cfg ...keyauth.Config) bool {
return len(cfg) > 0
} Prevention
- Never call keyauth.New() with no arguments.
- Wire the Validator before adding the middleware to the router.
When it happens
Trigger: Calling keyauth.New() with no arguments — keyauth.New() instead of keyauth.New(keyauth.Config{Validator: ...}).
Common situations: Refactoring that temporarily removes the config argument, or scaffolding code that wires the middleware before implementing the validator.
Related errors
- basicauth: charset must be UTF-8
- fiber: keyauth unsupported error token
- fiber: keyauth error_description requires error
- fiber: keyauth error_uri requires error
- fiber: keyauth error_uri must be absolute
AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04).
Data as JSON: /data/errors/7b7ccc353cce8004.json.
Report an issue: GitHub.