Skip to main content

Overview

The ipfilter middleware filters requests based on client IP addresses. Use it to allow or deny access from specific IPs or IP ranges (CIDR notation). Use it when you need:
  • Whitelist access to admin panels
  • Block known malicious IPs
  • Restrict access to internal networks
  • Geo-blocking based on IP ranges

Installation

Quick Start

Configuration

Options

Examples

Whitelist Mode

Only allow specific IPs, deny everything else:

Blacklist Mode

Allow everything except specific IPs:

Combined Allow and Deny

Localhost Only

Private Networks Only

Behind a Proxy

Custom Error Handler

Route-Specific Filtering

Group Filtering

Dynamic IP List

API Reference

Functions

CIDR Notation

The middleware supports both single IPs and CIDR notation:

Common CIDR Blocks

Technical Details

Implementation Overview

The ipfilter middleware uses Go’s net package to parse and match IP addresses and CIDR ranges efficiently. The implementation follows these key principles:
  1. Network Parsing: IP addresses and CIDR blocks are parsed at initialization time using parseNetworks(), converting them to *net.IPNet structures for efficient matching.
  2. IP Extraction: Client IPs are extracted using one of two methods:
    • Direct extraction from RemoteAddr using extractIP() when not behind a proxy
    • Using c.ClientIP() when TrustProxy is enabled (reads X-Forwarded-For header)
  3. Filtering Logic: The middleware applies a two-phase filtering process:
    • Phase 1 - Deny List Check: First checks if the IP is in the deny list. If found, immediately denies access.
    • Phase 2 - Allow List Check: If DenyByDefault is true, verifies the IP is in the allow list. Denies if not found.
  4. Single IP Normalization: Single IP addresses are automatically converted to CIDR notation:
    • IPv4 addresses: converted to /32 (e.g., 192.168.1.100/32)
    • IPv6 addresses: converted to /128 (e.g., ::1/128)
  5. Error Handling: Uses a custom handleDenied() function that either calls the user-provided ErrorHandler or returns a default 403 Forbidden response.

Performance Considerations

  • IP parsing happens once during middleware initialization, not on every request
  • Network matching uses efficient net.IPNet.Contains() method
  • Deny list is checked before allow list to fail fast on blocked IPs
  • No regex or string manipulation on hot path

Security Considerations

  1. Proxy Headers - Only enable TrustProxy if behind a trusted proxy
  2. IP Spoofing - X-Forwarded-For can be spoofed if not behind trusted proxy
  3. IPv6 - Consider both IPv4 and IPv6 addresses
  4. VPNs/Proxies - Users may bypass IP restrictions using VPNs

Best Practices

  • Use CIDR notation for ranges to simplify management
  • Enable TrustProxy only behind trusted load balancers
  • Combine with other authentication for sensitive areas
  • Log denied attempts for security monitoring

Testing

The ipfilter middleware includes comprehensive test coverage for all filtering scenarios: