Skip to main content

Overview

The captcha middleware verifies CAPTCHA tokens to protect forms and APIs from bots. It supports multiple providers including Google reCAPTCHA (v2/v3), hCaptcha, and Cloudflare Turnstile. Use it when you need:
  • Bot protection for forms
  • Spam prevention
  • Account creation protection
  • API abuse prevention

Installation

Quick Start

Configuration

Options

Providers

Examples

Google reCAPTCHA v2

Frontend:

Google reCAPTCHA v3

Frontend:

hCaptcha

Frontend:

Cloudflare Turnstile

Frontend:

Skip Certain Paths

Custom Token Location

Custom Error Handler

Custom Verifier

Route-Specific Protection

API Reference

Functions

Error Types

Provider Comparison

Best Practices

  • Use v3/invisible CAPTCHAs for better UX
  • Set appropriate score thresholds for v3
  • Monitor verification failures for tuning
  • Have fallback for CAPTCHA failures
  • Test with automation tools

Technical Details

Token Extraction

The middleware supports extracting tokens from three locations:
  • Form: form:field-name - Extracts from POST form data
  • Header: header:Header-Name - Extracts from HTTP headers
  • Query: query:param-name - Extracts from URL query parameters
The TokenLookup option uses the format source:key where source is one of form, header, or query.

Verification Flow

  1. Request Filtering: Skips verification for safe methods (GET, HEAD, OPTIONS) and configured skip paths
  2. Token Extraction: Retrieves token based on TokenLookup configuration
  3. Verification:
    • For custom verifiers: Calls the provided Verifier function
    • For provider verifiers: Makes HTTP POST to provider’s API with secret, token, and client IP
  4. Score Validation: For v3 providers (reCAPTCHA v3, Turnstile), validates score against MinScore threshold
  5. Error Handling: Uses custom ErrorHandler if provided, otherwise returns default 400 response

Client IP Detection

The middleware detects client IP in the following order:
  1. X-Forwarded-For header (first IP in comma-separated list)
  2. X-Real-IP header
  3. RemoteAddr from request (strips port if present)
The detected IP is sent to the provider API for enhanced verification.

Provider API Integration

Each provider has a specific verification endpoint:
  • reCAPTCHA v2/v3: https://www.google.com/recaptcha/api/siteverify
  • hCaptcha: https://hcaptcha.com/siteverify
  • Turnstile: https://challenges.cloudflare.com/turnstile/v0/siteverify
The middleware sends:
  • secret: Your secret key
  • response: The CAPTCHA token
  • remoteip: Client IP address (optional)

Default Values

Error Types

The middleware defines three specific error types:
  • ErrMissingToken: Token not found in the specified lookup location
  • ErrInvalidToken: Token verification failed (invalid or low score)
  • ErrVerifyFailed: HTTP request to provider API failed

Testing

Test Coverage

Security Considerations

  1. Secret Key - Never expose secret key in frontend code
  2. Score Tuning - Adjust v3 scores based on traffic patterns
  3. Timeout - Set reasonable timeout for verification
  4. Fallback - Have manual review process for edge cases