* feat: add extractors package with shared value extraction utilities for Fiber middleware * docs: add missing line breaks for improved readability in extractors documentation * docs: update extractors documentation to clarify source types and chain behavior * docs: add Extractors Package section to What's New in v3 documentation * docs: update extractors documentation to enhance clarity and detail on usage and security considerations * style: update extractor titles for consistency in documentation * docs: remove unnecessary code block from extractors documentation * docs: update title for Extractors Package section in what's new documentation * docs: add custom extraction method and improve documentation for extractors * docs: enhance extractors documentation with error handling and RFC compliance details * docs: update middleware usage examples to specify KeyAuth for clarity * chore: remove unused go.mod and go.sum files from extractors * docs: add Audience section to README for clarity on target audience * fix: improve whitespace handling in extractor functions and update documentation * fix: update FromAuthHeader to return ErrNotFound for empty and whitespace-only headers * Remove unnecessary code and comments in extractors.md * fix: clarify documentation on query parameters and form data considerations * fix: improve extractor functions by removing unnecessary whitespace handling and updating documentation * test: add URL-encoded value extraction tests for FromQuery, FromForm, and FromParam * fix: remove unnecessary whitespace handling from extractor documentation * fix: update README to remove whitespace handling from error cases in tests * fix: clarify case sensitivity in extractor key name verification * fix: enhance README.md * fix: update extractor documentation for RFC compliance and improve descriptions * fix: clarify comments in Extractor struct for better understanding * fix: enhance security documentation for CSRF protection and custom extractors * fix: remove feature list from extractors.go documentation * fix: format remove hard tobs --------- Co-authored-by: RW <[email protected]>
3.7 KiB
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.