1.1.0 โ€ข Published 8 months ago

@lock-sdk/bot-detection v1.1.0

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

๐Ÿค– Bot Detector

A powerful module in the Lock Security Framework that detects bots using heuristics, fingerprints, request behavior, and header/user-agent analysis. Automatically block suspicious traffic or redirect to CAPTCHA.

๐Ÿš€ Features

  • ๐Ÿ” Detects headless browsers and bad user-agents
  • ๐Ÿ•ต๏ธ Behavioral heuristics: frequency, burst, session replay
  • ๐Ÿง  Optional browser fingerprint mismatch checks
  • ๐Ÿงพ Suspicious or missing header detection
  • ๐Ÿ—ƒ Memory, Redis, or Upstash storage for request/session tracking
  • ๐Ÿช Configurable bypass via query param
  • ๐Ÿ” Optional CAPTCHA redirect with return URL

๐Ÿ›  Usage

Basic Example (Redirect to CAPTCHA if suspicious)

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

const middleware = secure()(
  botDetector({
    enabled: true,
    captchaRedirectUrl: '/verify-human',
  })
);

โš™๏ธ Configuration

OptionTypeDefaultDescription
enabledbooleantrueEnable or disable bot detection
captchaRedirectUrlstring/captchaRedirect URL for detected bots
redirectStatusCodenumber302HTTP code for redirect
redirectMessagestringSee aboveText fallback if redirect fails
includeOriginalUrlbooleantrueAdd returnTo query param
allowQueryParamBypassbooleanfalseAllow skipping via URL param
bypassParamstring_botcheckParam name for bypass
bypassValuestringbypassParam value to allow bypass
failBehavior'open' | 'closed''closed'Whether to fail open if error

๐Ÿง  User Agent Checks

userAgent: {
  enabled: true,
  blockEmpty: true,
  blockedPatterns: ['bot', 'curl', 'wget'],
  requiredPatterns: ['mozilla', 'chrome']
}

๐Ÿ•ต๏ธ Behavior Heuristics

behavior: {
  enabled: true,
  minRequestInterval: 50, // ms
  maxSessionRequests: 1000, // per session
  sessionDuration: 3600000, // 1 hour
  checkPathPatterns: true
}

๐Ÿงพ Header Analysis

headers: {
  enabled: true,
  required: ['accept', 'accept-language'],
  suspicious: {
    accept: ['*/*'],
    'accept-language': ['']
  },
  checkBrowserFingerprint: true
}

๐Ÿ”Ž Fingerprinting

fingerprinting: {
  enabled: true,
  cookieName: '__bot_fp',
  hashHeaderName: 'x-browser-fingerprint'
}

๐Ÿงฉ Storage Options

BackendDescription
memoryIn-process memory store (default)
redisSupports clustering, TTL, etc.
upstashServerless Redis variant
storage: 'redis',
redis: {
  url: 'redis://localhost:6379',
  keyPrefix: 'bot:',
}

๐Ÿง  Caching

cache: {
  ttl: 3600000, // 1 hour
  size: 10000
}

๐Ÿ“› Event Types

Event CodeDescription
bot.detectedBot was detected and blocked
suspicious.behaviorBehavioral patterns were suspicious
invalid.user.agentUser agent string was abnormal
suspicious.headersHeaders were missing or suspicious
browser.fingerprint.mismatchFingerprint didn't match expected pattern

๐Ÿ” Bypass Protection

To enable bypass using a URL like /api/ping?_botcheck=bypass:

allowQueryParamBypass: true,
bypassParam: '_botcheck',
bypassValue: 'bypass'

๐Ÿ›ก Maintained By

Lock Team

1.1.0

8 months ago

1.0.0

8 months ago

0.1.0

8 months ago