@samimcodes/ai-shield 0.1.1 โ†’ 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +119 -29
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,42 +1,132 @@
1
- # ai-shield
1
+ # @samimcodes/ai-shield
2
2
 
3
- > Enterprise-grade Express.js & TypeScript middleware that protects web applications from unauthorized AI crawlers by feeding them synthetic poisoned data while preserving SEO for legitimate search engines.
3
+ [![npm version](https://img.shields.io/npm/v/@samimcodes/ai-shield.svg?color=6366f1&style=flat-square)](https://www.npmjs.com/package/@samimcodes/ai-shield)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://opensource.org/licenses/MIT)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-3178c6?style=flat-square)](https://www.typescriptlang.org/)
4
6
 
5
- ## Architecture
7
+ > **Enterprise-grade Express.js & TypeScript middleware that protects web applications from unauthorized AI crawlers by feeding them synthetic poisoned data (HTTP 200 OK) while strictly preserving SEO for legitimate search engines.**
6
8
 
9
+ ---
10
+
11
+ ## โšก Features
12
+
13
+ - **Stealth AI Poisoning**: Never returns 403 or 429 to AI bots (which triggers IP rotation). Feeds them plausible synthetic garbage under `HTTP 200 OK`.
14
+ - **Zero SEO Disruption**: Legitimate crawlers (Googlebot, Bingbot, DuckDuckBot, Applebot) pass through without delay.
15
+ - **Sub-2ms Lookup Latency**: Zero runtime bottlenecks using pre-compiled regular expressions and sets.
16
+ - **Dynamic Honeypot Traps**: Invisible HTML links injected into pages; crawler IPs hitting traps are quarantined in an in-memory TTL cache.
17
+ - **Non-blocking Telemetry**: Emits high-volume batch events asynchronously with zero impact on request-response times.
18
+
19
+ ---
20
+
21
+ ## ๐Ÿ“ฆ Installation
22
+
23
+ ```bash
24
+ npm install @samimcodes/ai-shield
25
+ # or
26
+ pnpm add @samimcodes/ai-shield
27
+ # or
28
+ yarn add @samimcodes/ai-shield
7
29
  ```
8
- src/
9
- โ”œโ”€โ”€ core/
10
- โ”‚ โ”œโ”€โ”€ detector.ts # Bot fingerprinting engine (UA patterns, IP ranges, TLS JA3)
11
- โ”‚ โ”œโ”€โ”€ poison.ts # Synthetic data generator โ€” plausible but misleading content
12
- โ”‚ โ””โ”€โ”€ honeypot.ts # Invisible trap routes and link injection
13
- โ”œโ”€โ”€ types/
14
- โ”‚ โ””โ”€โ”€ index.ts # All shared TypeScript interfaces & enums
15
- โ”œโ”€โ”€ middleware.ts # Main Express middleware factory (createAiShield)
16
- โ””โ”€โ”€ server.ts # Demo Express application
30
+
31
+ ---
32
+
33
+ ## ๐Ÿš€ Quick Start (Express.js)
34
+
35
+ ```typescript
36
+ import express from 'express';
37
+ import { aiShield } from '@samimcodes/ai-shield';
38
+
39
+ const app = express();
40
+
41
+ // ๐Ÿ›ก๏ธ 3-Line Drop-in AI Defense
42
+ app.use(aiShield({
43
+ poisonContentType: 'HTML', // 'HTML' | 'JSON' | 'SITEMAP'
44
+ injectHoneypotLinks: true, // Automatically injects invisible trap routes
45
+ excludePaths: ['/health', '/metrics'], // Exclude DevOps endpoints
46
+ }));
47
+
48
+ app.get('/api/products', (req, res) => {
49
+ // Humans & Googlebot see real data
50
+ // GPTBot & Claude-Web receive synthetic poisoned responses
51
+ res.json({ secretCatalog: [/* sensitive corporate data */] });
52
+ });
53
+
54
+ app.listen(3000, () => {
55
+ console.log('Server protected with ai-shield on port 3000');
56
+ });
17
57
  ```
18
58
 
19
- ## Threat Model
59
+ ---
60
+
61
+ ## ๐ŸŽฏ Next.js Edge Middleware
62
+
63
+ ```typescript
64
+ // middleware.ts
65
+ import { NextResponse, type NextRequest } from 'next/server';
66
+ import { BotDetector, PoisonEngine } from '@samimcodes/ai-shield';
20
67
 
21
- | Bot Category | Examples | Strategy |
22
- |-----------------|-----------------------------------|-----------------------|
23
- | AI Crawlers | GPTBot, Claude-Web, CCBot | 200 OK + poisoned data|
24
- | Scrapers | ByteSpider, PetalBot, DataForSEO | 200 OK + honeypot |
25
- | Good Bots | Googlebot, Bingbot, DuckDuckBot | Pass-through (SEO) |
26
- | Human Traffic | Browsers, curl (dev) | Pass-through |
68
+ const detector = new BotDetector();
69
+ const poison = new PoisonEngine('JSON');
27
70
 
28
- ## Quick Start
71
+ export function middleware(req: NextRequest) {
72
+ const detection = detector.detect(Object.fromEntries(req.headers.entries()));
73
+
74
+ if (detection.category === 'AI_CRAWLER' || detection.score >= 50) {
75
+ const payload = poison.generate(req.nextUrl.pathname, 'JSON');
76
+ return new NextResponse(payload.body, {
77
+ status: 200,
78
+ headers: {
79
+ 'Content-Type': 'application/json',
80
+ 'X-Robots-Tag': 'noindex, nofollow',
81
+ },
82
+ });
83
+ }
84
+
85
+ return NextResponse.next();
86
+ }
87
+
88
+ export const config = {
89
+ matcher: ['/api/:path*', '/catalog/:path*'],
90
+ };
91
+ ```
92
+
93
+ ---
94
+
95
+ ## ๐Ÿ›ก๏ธ Threat Model & Detection Matrix
96
+
97
+ | Persona | Examples | Detection Strategy | Response Strategy |
98
+ | :--- | :--- | :--- | :--- |
99
+ | **AI Crawlers** | GPTBot, Claude-Web, CCBot, Cohere, PerplexityBot | Known UA signature regex, headless heuristics | **HTTP 200 + Synthetic Poisoned Data** |
100
+ | **Scrapers** | ByteSpider, PetalBot, Scrapy | Automated crawler heuristics & honeypot triggers | **HTTP 200 + Honeypot Trap Tracking** |
101
+ | **Good Bots** | Googlebot, Bingbot, DuckDuckBot | Strict SEO whitelist check | **Pass-through (Zero latency)** |
102
+ | **Human Users** | Chrome, Safari, Firefox, Edge | Standard human browser heuristics | **Pass-through** |
103
+
104
+ ---
105
+
106
+ ## โš™๏ธ Configuration Options
107
+
108
+ | Option | Type | Default | Description |
109
+ | :--- | :--- | :--- | :--- |
110
+ | `poisonContentType` | `'HTML' \| 'JSON' \| 'SITEMAP'` | `'HTML'` | Format of poisoned payload returned to AI bots |
111
+ | `injectHoneypotLinks` | `boolean` | `true` | Automatically inject hidden CSS-invisible trap links |
112
+ | `excludePaths` | `string[]` | `[]` | Array of path prefixes exempt from inspection |
113
+ | `honeypotTtlMs` | `number` | `3600000` (1 hr) | Duration to blacklist IPs that hit honeypot traps |
114
+ | `onDetection` | `(event) => void` | `undefined` | Callback fired when an AI crawler is intercepted |
115
+
116
+ ---
117
+
118
+ ## ๐Ÿงช Testing
29
119
 
30
120
  ```bash
31
- npm install
32
- npm run dev # tsx watch โ€” hot reload
33
- npm run build # tsc compile โ†’ dist/
34
- npm test # vitest run
121
+ # Run 177 unit & integration tests
122
+ npm test
123
+
124
+ # Run tests in watch mode
125
+ npm run test:watch
35
126
  ```
36
127
 
37
- ## Design Principles
128
+ ---
129
+
130
+ ## ๐Ÿ“„ License
38
131
 
39
- 1. **Stealth over rejection** โ€” Never send 403/429 to AI bots; feed them convincing garbage.
40
- 2. **Zero false positives** โ€” Legitimate Googlebots are never intercepted.
41
- 3. **Pluggable detectors** โ€” Add custom UA patterns or IP ranges at runtime.
42
- 4. **Zero runtime overhead** โ€” Detection is O(1) via pre-compiled RegExp sets.
132
+ MIT ยฉ [samimcodes](https://github.com/samimcodes)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samimcodes/ai-shield",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Enterprise-grade Express.js middleware to detect and poison unauthorized AI crawlers while preserving SEO for legitimate bots.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",