1.0.0 • Published 8 months ago

@lock-sdk/ip-filter v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

🌐 IP Filter

An IP allow/block list module for the Lock Security Framework. Use it to selectively deny or permit access based on client IP addresses. Supports CIDR ranges, LRU caching, and Redis/Upstash for distributed enforcement.

🚀 Features

  • 🔒 IP whitelist and blacklist modes
  • 📍 CIDR and exact IP matching
  • ⚡ Fast in-memory or distributed caching (Redis, Upstash)
  • 🧠 Smart header detection & fallback to remote address
  • 🛠 Fail-safe options for zero-downtime

🛠 Usage

Basic Example (Blacklist)

import { secure, ipFilter } from '@lock-sdk/main';

const middleware = secure()(
  ipFilter({
    mode: 'blacklist',
    ipAddresses: ['203.0.113.5', '192.168.1.0/24'],
  })
);

Whitelist Example

ipFilter({
  mode: 'whitelist',
  ipAddresses: ['10.0.0.1', '172.16.0.0/12'],
});

⚙️ Configuration

OptionTypeDefaultDescription
mode'blacklist' | 'whitelist''blacklist'Select deny- or allowlist mode
ipAddressesstring[][]List of IPs or CIDRs
ipHeadersstring[]['cf-connecting-ip', 'x-forwarded-for', 'x-real-ip']Headers to check for client IP
useRemoteAddressbooleantrueFallback to req.socket.remoteAddress
blockStatusCodenumber403HTTP status code on block
blockMessagestring'Access denied based on your IP address'Error message if blocked
failBehavior'open' | 'closed''open'Fail-safe behavior if matching/storage fails
logFunction(msg, data?) => voidconsole.logOptional logger hook
logBlockedbooleanfalseLog blocked IPs
logAllowedbooleanfalseLog allowed IPs
storage'memory' | 'redis' | 'upstash''memory'Where to cache results
cacheTtlnumber (ms)3600000 (1 hour)TTL for IP decision cache
cacheSizenumber10000Max IPs cached in memory
redisobjectRedis configuration
upstashobjectUpstash configuration

Memory (default)

storage: 'memory',
cacheTtl: 3600000, // 1 hour
cacheSize: 10000

Redis

storage: 'redis',
redis: {
  url: 'redis://localhost:6379',
  password: 'secret',
  keyPrefix: 'ipfilter:'
}

Upstash

storage: 'upstash',
upstash: {
  url: process.env.UPSTASH_REDIS_REST_URL!,
  token: process.env.UPSTASH_REDIS_REST_TOKEN!,
  keyPrefix: 'ipfilter:'
}

🛡 Maintained By

Lock Team