Skip to main content

Overview

The fingerprint 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

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

Response:

Fraud Detection

Info Structure

API Reference

Hash Algorithm

The fingerprint hash is generated by:
  1. Collecting all components (headers, IP, etc.)
  2. Sorting component keys alphabetically
  3. Concatenating as key:value|key:value|...
  4. Computing SHA256 hash
  5. 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 contextKey struct type to avoid collisions with other middleware
  • Stores fingerprint information in the request context as an *Info pointer
  • Information persists across the entire middleware chain
Hash Generation Algorithm
  • Deterministic hashing through alphabetical key sorting
  • Uses SHA256 cryptographic hash function
  • Produces 64-character hexadecimal strings
  • Format: key1:value1|key2:value2|...|keyN:valueN
Component Collection The middleware collects fingerprint components in the following order:
  1. Headers: Iterates through configured header list
  2. IP Address: Extracted via getClientIP() helper
  3. HTTP Method: Request method if enabled
  4. Request Path: URL path if enabled
  5. Custom Components: User-defined function results
IP Address Detection The getClientIP() function implements a priority-based IP extraction:
  1. First checks X-Forwarded-For header (takes first IP in comma-separated list)
  2. Falls back to X-Real-IP header
  3. Finally uses RemoteAddr from 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 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.)