Overview
Thefingerprint middleware generates unique fingerprints for incoming requests based on headers, IP addresses, and other request attributes. Useful for bot detection, analytics, and rate limiting.
Installation
Quick Start
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
Headers | []string | Common headers | Headers to include |
IncludeIP | bool | true | Include client IP |
IncludeMethod | bool | false | Include HTTP method |
IncludePath | bool | false | Include request path |
Custom | func(*mizu.Ctx) map[string]string | - | Custom components |
Default Headers
Examples
Basic Usage
With IP Address
Headers Only
Full Fingerprint
Custom Headers
Include Request Path
Custom Components
Rate Limiting by Fingerprint
Bot Detection
Analytics Tracking
Session Correlation
Debugging Fingerprint
Fraud Detection
Info Structure
API Reference
Hash Algorithm
The fingerprint hash is generated by:- Collecting all components (headers, IP, etc.)
- Sorting component keys alphabetically
- Concatenating as
key:value|key:value|... - Computing SHA256 hash
- Returning hex-encoded string
Technical Details
Implementation Architecture
The fingerprint middleware uses a context-based storage mechanism to preserve fingerprint information throughout the request lifecycle. The implementation follows these key design principles: Context Storage- Uses a private
contextKeystruct type to avoid collisions with other middleware - Stores fingerprint information in the request context as an
*Infopointer - Information persists across the entire middleware chain
- Deterministic hashing through alphabetical key sorting
- Uses SHA256 cryptographic hash function
- Produces 64-character hexadecimal strings
- Format:
key1:value1|key2:value2|...|keyN:valueN
- Headers: Iterates through configured header list
- IP Address: Extracted via
getClientIP()helper - HTTP Method: Request method if enabled
- Request Path: URL path if enabled
- Custom Components: User-defined function results
getClientIP() function implements a priority-based IP extraction:
- First checks
X-Forwarded-Forheader (takes first IP in comma-separated list) - Falls back to
X-Real-IPheader - Finally uses
RemoteAddrfrom the request
Performance Considerations
- Minimal memory allocation through pre-sized maps
- String builder for efficient concatenation
- Single-pass component collection
- Lazy evaluation: hash only computed once during middleware execution
Thread Safety
The middleware is thread-safe as each request receives its own context and Info struct. No shared state exists between requests.Best Practices
- Use minimal headers for privacy compliance
- Donβt rely solely on fingerprints for authentication
- Store fingerprints hashed, not raw
- Combine with other signals for bot detection
- Consider GDPR implications when storing fingerprints
- Use for analytics and fraud detection, not tracking
Testing
The fingerprint middleware includes comprehensive test coverage for all configuration options and edge cases:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew | Default middleware initialization | Generates fingerprint hash from default headers (User-Agent, Accept, etc.) |
TestWithOptions_IncludeIP | IP address inclusion | Captures client IP from RemoteAddr and includes in components |
TestWithOptions_IncludeMethod | HTTP method inclusion | Includes HTTP method (GET, POST, etc.) in fingerprint components |
TestWithOptions_IncludePath | Request path inclusion | Captures and includes the full request path in components |
TestWithOptions_Custom | Custom component function | Executes custom function and merges returned map into components |
TestHash | Hash function retrieval | Returns 64-character SHA256 hex string via Hash() helper |
TestConsistentHash | Hash consistency | Identical requests produce identical hash values |
TestHeadersOnly | Custom header filtering | Only includes specified headers, excludes others (e.g., User-Agent) |
TestWithIP | WithIP() helper function | Enables IP inclusion using convenience function |
TestFull | Full() comprehensive mode | Includes IP, Method, Path, and all default headers |
TestXForwardedFor | X-Forwarded-For header parsing | Extracts first IP from comma-separated list in X-Forwarded-For header |
Test Coverage
All test cases validate:- Correct component extraction and storage
- Proper context propagation
- Hash generation and consistency
- Configuration option handling
- Edge cases (empty headers, missing values, etc.)