@webdecoy/express 0.1.0 → 0.2.0

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.
package/README.md CHANGED
@@ -1,11 +1,16 @@
1
1
  # @webdecoy/express
2
2
 
3
- Web Decoy middleware for Express.js applications.
3
+ Web Decoy middleware for Express.js applications - Advanced bot detection with TLS fingerprinting.
4
+
5
+ [![npm version](https://badge.fury.io/js/%40webdecoy%2Fexpress.svg)](https://www.npmjs.com/package/@webdecoy/express)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
7
 
5
8
  ## Installation
6
9
 
7
10
  ```bash
8
11
  npm install @webdecoy/express
12
+ # or
13
+ yarn add @webdecoy/express
9
14
  ```
10
15
 
11
16
  ## Quick Start
@@ -38,15 +43,31 @@ app.listen(3000);
38
43
 
39
44
  ```typescript
40
45
  interface WebDecoyMiddlewareOptions {
41
- // Web Decoy SDK config
46
+ // Required: Web Decoy API key
42
47
  apiKey: string;
48
+
49
+ // Optional: API endpoint (default: 'https://api.webdecoy.com')
43
50
  apiUrl?: string;
51
+
52
+ // Optional: Threat score threshold for blocking (default: 80)
44
53
  threatScoreThreshold?: number;
45
54
 
46
- // Middleware-specific options
55
+ // Optional: Request timeout in milliseconds (default: 5000)
56
+ timeout?: number;
57
+
58
+ // Optional: Enable debug logging (default: false)
59
+ debug?: boolean;
60
+
61
+ // Optional: Paths to skip protection
47
62
  skipPaths?: string[] | RegExp[];
63
+
64
+ // Optional: Custom IP extraction function
48
65
  getIP?: (req: Request) => string;
66
+
67
+ // Optional: Custom blocked request handler
49
68
  onBlocked?: (req: Request, res: Response, detection: any) => void;
69
+
70
+ // Optional: Custom error handler
50
71
  onError?: (req: Request, res: Response, error: Error) => void;
51
72
  }
52
73
  ```
@@ -89,7 +110,7 @@ app.use(
89
110
  skipPaths: [
90
111
  '/health',
91
112
  '/metrics',
92
- /^\/static\/.*/, // Regex pattern
113
+ /^\/static\/.*/, // Regex pattern
93
114
  ],
94
115
  })
95
116
  );
@@ -114,9 +135,48 @@ app.get('/api/data', (req, res) => {
114
135
  });
115
136
  ```
116
137
 
138
+ ### Detection Info Properties
139
+
140
+ ```typescript
141
+ interface WebDecoyDetection {
142
+ decision: 'allow' | 'block' | 'challenge';
143
+ confidence: number; // 0-100 threat score
144
+ threat_level: 'MINIMAL' | 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';
145
+ bot_detected: boolean;
146
+ bot_type?: string; // e.g., "curl", "selenium"
147
+ detection_id: string;
148
+ rule_enforced: boolean;
149
+ }
150
+ ```
151
+
152
+ ## TypeScript Support
153
+
154
+ The package includes TypeScript definitions and augments the Express Request type:
155
+
156
+ ```typescript
157
+ import { Request } from 'express';
158
+
159
+ app.get('/api/data', (req: Request, res) => {
160
+ // req.webdecoy is typed
161
+ const botDetected = req.webdecoy?.bot_detected;
162
+ });
163
+ ```
164
+
165
+ ## Core SDK
166
+
167
+ This package uses [@webdecoy/node](https://www.npmjs.com/package/@webdecoy/node) under the hood. For non-Express applications or custom integrations, use the core SDK directly.
168
+
169
+ ## Getting an API Key
170
+
171
+ 1. Sign up at [app.webdecoy.com](https://app.webdecoy.com)
172
+ 2. Create a new organization and property
173
+ 3. Generate an API key in Settings
174
+
175
+ API keys start with `sk_live_` for production or `sk_test_` for testing.
176
+
117
177
  ## Documentation
118
178
 
119
- See the [main README](../../README.md) for full documentation.
179
+ For full documentation, visit the [GitHub repository](https://github.com/webdecoy/webdecoy-node).
120
180
 
121
181
  ## License
122
182
 
package/dist/index.d.mts CHANGED
@@ -39,13 +39,21 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
39
39
  * ```typescript
40
40
  * import express from 'express';
41
41
  * import { webdecoy } from '@webdecoy/express';
42
+ * import { rateLimit } from '@webdecoy/node';
42
43
  *
43
44
  * const app = express();
44
45
  *
46
+ * // Rate limiting only (no API key required)
45
47
  * app.use(webdecoy({
46
- * apiKey: process.env.WEBDECOY_API_KEY,
48
+ * rules: [rateLimit({ max: 100, window: 60 })],
47
49
  * skipPaths: ['/health', '/static'],
48
50
  * }));
51
+ *
52
+ * // Full protection with API key
53
+ * app.use(webdecoy({
54
+ * apiKey: process.env.WEBDECOY_API_KEY,
55
+ * rules: [rateLimit({ max: 100, window: 60 })],
56
+ * }));
49
57
  * ```
50
58
  */
51
59
  declare function webdecoy(config: WebDecoyConfig & WebDecoyMiddlewareOptions): (req: Request, res: Response, next: NextFunction) => Promise<void>;
package/dist/index.d.ts CHANGED
@@ -39,13 +39,21 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
39
39
  * ```typescript
40
40
  * import express from 'express';
41
41
  * import { webdecoy } from '@webdecoy/express';
42
+ * import { rateLimit } from '@webdecoy/node';
42
43
  *
43
44
  * const app = express();
44
45
  *
46
+ * // Rate limiting only (no API key required)
45
47
  * app.use(webdecoy({
46
- * apiKey: process.env.WEBDECOY_API_KEY,
48
+ * rules: [rateLimit({ max: 100, window: 60 })],
47
49
  * skipPaths: ['/health', '/static'],
48
50
  * }));
51
+ *
52
+ * // Full protection with API key
53
+ * app.use(webdecoy({
54
+ * apiKey: process.env.WEBDECOY_API_KEY,
55
+ * rules: [rateLimit({ max: 100, window: 60 })],
56
+ * }));
49
57
  * ```
50
58
  */
51
59
  declare function webdecoy(config: WebDecoyConfig & WebDecoyMiddlewareOptions): (req: Request, res: Response, next: NextFunction) => Promise<void>;
package/dist/index.js CHANGED
@@ -83,6 +83,27 @@ function webdecoy(config) {
83
83
  skipLocalAnalysis: config.skipLocalAnalysis,
84
84
  metadata: config.metadata
85
85
  });
86
+ if (!result.allowed && result.ruleResult) {
87
+ const rr = result.ruleResult;
88
+ if (rr.action === "THROTTLE") {
89
+ const retryAfter = rr.metadata?.retryAfter ?? 60;
90
+ res.setHeader("Retry-After", String(retryAfter));
91
+ res.status(429).json({
92
+ error: "Too Many Requests",
93
+ message: rr.reason || "Rate limit exceeded",
94
+ retry_after: retryAfter
95
+ });
96
+ return;
97
+ }
98
+ if (rr.action === "DENY") {
99
+ res.status(403).json({
100
+ error: "Forbidden",
101
+ message: rr.reason || "Access denied by rule",
102
+ rule: rr.rule
103
+ });
104
+ return;
105
+ }
106
+ }
86
107
  if (result.allowed) {
87
108
  req.webdecoy = result.detection;
88
109
  return next();
package/dist/index.mjs CHANGED
@@ -57,6 +57,27 @@ function webdecoy(config) {
57
57
  skipLocalAnalysis: config.skipLocalAnalysis,
58
58
  metadata: config.metadata
59
59
  });
60
+ if (!result.allowed && result.ruleResult) {
61
+ const rr = result.ruleResult;
62
+ if (rr.action === "THROTTLE") {
63
+ const retryAfter = rr.metadata?.retryAfter ?? 60;
64
+ res.setHeader("Retry-After", String(retryAfter));
65
+ res.status(429).json({
66
+ error: "Too Many Requests",
67
+ message: rr.reason || "Rate limit exceeded",
68
+ retry_after: retryAfter
69
+ });
70
+ return;
71
+ }
72
+ if (rr.action === "DENY") {
73
+ res.status(403).json({
74
+ error: "Forbidden",
75
+ message: rr.reason || "Access denied by rule",
76
+ rule: rr.rule
77
+ });
78
+ return;
79
+ }
80
+ }
60
81
  if (result.allowed) {
61
82
  req.webdecoy = result.detection;
62
83
  return next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/express",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Web Decoy middleware for Express.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",