Fork of gofiber/fiber v3 based on the v3.2.0 tag. Rewrites all self-imports github.com/gofiber/fiber/v3 -> github.com/zap-proto/fiber/v3. Upstream gofiber/schema and gofiber/utils/v2 dependencies are left untouched. Co-authored-by: Hanzo Dev <[email protected]>
Extractors Package
Package providing shared value extraction utilities for Fiber middleware packages.
Audience
This README is targeted at middleware developers and contributors. If you are a Fiber framework user looking to use extractors in your application, please refer to the Extractors Guide instead.
Architecture
Core Types
Extractor: Core extraction function with metadataSource: Enumeration of extraction sources (Header, AuthHeader, Query, Form, Param, Cookie, Custom)ErrNotFound: Standardized error for missing values
Extractor Structure
type Extractor struct {
Extract func(fiber.Ctx) (string, error)
Key string // The parameter/header name used for extraction
AuthScheme string // The auth scheme used, e.g., "Bearer"
Chain []Extractor // For chained extractors, stores all extractors in the chain
Source Source // The type of source being extracted from
}
Available Functions
FromAuthHeader(authScheme string): Extract from Authorization header with optional schemeFromCookie(key string): Extract from HTTP cookiesFromParam(param string): Extract from URL path parametersFromForm(param string): Extract from form dataFromHeader(header string): Extract from custom HTTP headersFromQuery(param string): Extract from URL query parametersFromCustom(key string, fn func(fiber.Ctx) (string, error)): Define custom extraction logic with metadataChain(extractors ...Extractor): Chain multiple extractors with fallback
Source Inspection
The Source field provides security-aware extraction by explicitly identifying the origin of extracted values. This enables middleware to enforce security policies based on data source:
switch extractor.Source {
case SourceAuthHeader:
// Authorization header - commonly used for authentication tokens
case SourceHeader:
// Custom HTTP headers - application-specific data
case SourceCookie:
// HTTP cookies - client-side stored data
case SourceQuery:
// URL query parameters - visible in URLs and logs (security consideration)
case SourceForm:
// Form data - POST body data
case SourceParam:
// URL path parameters - route-based data
case SourceCustom:
// Custom extraction logic
}
Chain Behavior
The Chain function implements fallback logic:
- Returns first successful extraction (non-empty value, no error)
- If all extractors fail, returns the last error encountered or
ErrNotFound - Skips extractors with
nilExtract functions (graceful error handling) - Preserves metadata from first extractor for introspection
- Stores defensive copy for runtime inspection via the
Chainfield
Security Considerations
Source Awareness and Custom Extractors
As described in the Source Inspection section, the Source field enables middleware to enforce security policies based on data source:
- CSRF Protection: The double-submit-cookie pattern requires tokens to be submitted in both a cookie AND a form field/header. Source awareness allows CSRF middleware to verify that tokens come from both expected sources, and not for example only from cookies
- Authentication: Security middleware can enforce source-specific policies (e.g., auth tokens from headers, not query parameters)
- Audit Trails: Source information enables security analysis and compliance reporting
However, when using FromCustom, middleware cannot determine the source of the extracted value, which can limit the ability of a middleware to provide warnings about potential security risks. Documentation and examples should clearly warn about these risks when using custom extractors.