@webdecoy/express 0.6.0 → 0.8.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/dist/index.d.mts +43 -1
- package/dist/index.d.ts +43 -1
- package/dist/index.js +53 -4
- package/dist/index.mjs +56 -2
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -7,6 +7,41 @@ export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionRe
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
interface WebDecoyMiddlewareOptions extends ProtectOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Whether a blocking verdict actually blocks. Defaults to `'monitor'`.
|
|
12
|
+
*
|
|
13
|
+
* MONITOR IS THE DEFAULT ON PURPOSE, and this changed in 0.7.0.
|
|
14
|
+
*
|
|
15
|
+
* Before, installing this middleware with an API key began returning 403 to
|
|
16
|
+
* any request whose server-side score cleared `threatScoreThreshold` — which
|
|
17
|
+
* defaults to 80 — and there was no supported way to watch first. `dryRun` on
|
|
18
|
+
* a rule governs that rule, not the score, and `onBlocked` did not receive
|
|
19
|
+
* `next`, so "record it and serve the request anyway" could not be expressed.
|
|
20
|
+
*
|
|
21
|
+
* Found by installing it on a live site that takes payments: the homepage
|
|
22
|
+
* returned `{"error":"Forbidden"}` on the first request, as did an ordinary
|
|
23
|
+
* `python-requests` user agent.
|
|
24
|
+
*
|
|
25
|
+
* Nobody adopts a defence by having it break their site on the first install.
|
|
26
|
+
* Watch what it would have done, then set `mode: 'enforce'`.
|
|
27
|
+
*/
|
|
28
|
+
mode?: 'monitor' | 'enforce';
|
|
29
|
+
/**
|
|
30
|
+
* Inject a hidden honeytoken link into HTML responses, and arm the tripwire it
|
|
31
|
+
* points at. Defaults to **on** when an apiKey is present (#482).
|
|
32
|
+
*
|
|
33
|
+
* The SDK used to generate a honeytoken and ask the developer to embed it.
|
|
34
|
+
* Almost nobody did: `sdk_tripwire` had FOUR rows in production, ever, while
|
|
35
|
+
* the WordPress plugin — which injects the link itself — has real coverage.
|
|
36
|
+
*
|
|
37
|
+
* A trap hit is the only detection here that needs no score, no JavaScript, no
|
|
38
|
+
* fingerprint and no IP. It is also the only one that scores: honeypot signals
|
|
39
|
+
* are weighted 38% against user-agent's 1%, which is why every rule-less `sdk`
|
|
40
|
+
* detection ever recorded came out at 0.
|
|
41
|
+
*
|
|
42
|
+
* Set `false` to opt out, or place the link yourself for apps that stream.
|
|
43
|
+
*/
|
|
44
|
+
honeytoken?: boolean;
|
|
10
45
|
/**
|
|
11
46
|
* Custom function to extract IP address from request
|
|
12
47
|
* By default, uses req.ip or x-forwarded-for header
|
|
@@ -16,7 +51,14 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
|
|
|
16
51
|
* Custom function to handle blocked requests
|
|
17
52
|
* By default, returns 403 Forbidden
|
|
18
53
|
*/
|
|
19
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Called when a request would be blocked.
|
|
56
|
+
*
|
|
57
|
+
* `next` is passed so a handler can record the verdict and continue — the
|
|
58
|
+
* omission that made monitoring impossible. Call exactly one of `next()` or a
|
|
59
|
+
* response method.
|
|
60
|
+
*/
|
|
61
|
+
onBlocked?: (req: Request, res: Response, detection: any, next: NextFunction) => void;
|
|
20
62
|
/**
|
|
21
63
|
* Custom function to handle errors
|
|
22
64
|
* By default, logs error and allows request (fail open)
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,41 @@ export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionRe
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
interface WebDecoyMiddlewareOptions extends ProtectOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Whether a blocking verdict actually blocks. Defaults to `'monitor'`.
|
|
12
|
+
*
|
|
13
|
+
* MONITOR IS THE DEFAULT ON PURPOSE, and this changed in 0.7.0.
|
|
14
|
+
*
|
|
15
|
+
* Before, installing this middleware with an API key began returning 403 to
|
|
16
|
+
* any request whose server-side score cleared `threatScoreThreshold` — which
|
|
17
|
+
* defaults to 80 — and there was no supported way to watch first. `dryRun` on
|
|
18
|
+
* a rule governs that rule, not the score, and `onBlocked` did not receive
|
|
19
|
+
* `next`, so "record it and serve the request anyway" could not be expressed.
|
|
20
|
+
*
|
|
21
|
+
* Found by installing it on a live site that takes payments: the homepage
|
|
22
|
+
* returned `{"error":"Forbidden"}` on the first request, as did an ordinary
|
|
23
|
+
* `python-requests` user agent.
|
|
24
|
+
*
|
|
25
|
+
* Nobody adopts a defence by having it break their site on the first install.
|
|
26
|
+
* Watch what it would have done, then set `mode: 'enforce'`.
|
|
27
|
+
*/
|
|
28
|
+
mode?: 'monitor' | 'enforce';
|
|
29
|
+
/**
|
|
30
|
+
* Inject a hidden honeytoken link into HTML responses, and arm the tripwire it
|
|
31
|
+
* points at. Defaults to **on** when an apiKey is present (#482).
|
|
32
|
+
*
|
|
33
|
+
* The SDK used to generate a honeytoken and ask the developer to embed it.
|
|
34
|
+
* Almost nobody did: `sdk_tripwire` had FOUR rows in production, ever, while
|
|
35
|
+
* the WordPress plugin — which injects the link itself — has real coverage.
|
|
36
|
+
*
|
|
37
|
+
* A trap hit is the only detection here that needs no score, no JavaScript, no
|
|
38
|
+
* fingerprint and no IP. It is also the only one that scores: honeypot signals
|
|
39
|
+
* are weighted 38% against user-agent's 1%, which is why every rule-less `sdk`
|
|
40
|
+
* detection ever recorded came out at 0.
|
|
41
|
+
*
|
|
42
|
+
* Set `false` to opt out, or place the link yourself for apps that stream.
|
|
43
|
+
*/
|
|
44
|
+
honeytoken?: boolean;
|
|
10
45
|
/**
|
|
11
46
|
* Custom function to extract IP address from request
|
|
12
47
|
* By default, uses req.ip or x-forwarded-for header
|
|
@@ -16,7 +51,14 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
|
|
|
16
51
|
* Custom function to handle blocked requests
|
|
17
52
|
* By default, returns 403 Forbidden
|
|
18
53
|
*/
|
|
19
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Called when a request would be blocked.
|
|
56
|
+
*
|
|
57
|
+
* `next` is passed so a handler can record the verdict and continue — the
|
|
58
|
+
* omission that made monitoring impossible. Call exactly one of `next()` or a
|
|
59
|
+
* response method.
|
|
60
|
+
*/
|
|
61
|
+
onBlocked?: (req: Request, res: Response, detection: any, next: NextFunction) => void;
|
|
20
62
|
/**
|
|
21
63
|
* Custom function to handle errors
|
|
22
64
|
* By default, logs error and allows request (fail open)
|
package/dist/index.js
CHANGED
|
@@ -27,6 +27,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
27
27
|
|
|
28
28
|
// src/middleware.ts
|
|
29
29
|
var import_node = require("@webdecoy/node");
|
|
30
|
+
var import_node2 = require("@webdecoy/node");
|
|
30
31
|
function defaultGetIP(req) {
|
|
31
32
|
const forwardedFor = req.headers["x-forwarded-for"];
|
|
32
33
|
if (forwardedFor) {
|
|
@@ -39,7 +40,7 @@ function defaultGetIP(req) {
|
|
|
39
40
|
}
|
|
40
41
|
return req.ip || req.socket.remoteAddress || "127.0.0.1";
|
|
41
42
|
}
|
|
42
|
-
function defaultOnBlocked(req, res, detection) {
|
|
43
|
+
function defaultOnBlocked(req, res, detection, _next) {
|
|
43
44
|
res.status(403).json({
|
|
44
45
|
error: "Forbidden",
|
|
45
46
|
message: "Access denied by Web Decoy protection",
|
|
@@ -64,6 +65,16 @@ function webdecoy(config) {
|
|
|
64
65
|
const sdk = new import_node.WebDecoy(config);
|
|
65
66
|
const getIP2 = config.getIP || defaultGetIP;
|
|
66
67
|
const onBlocked = config.onBlocked || defaultOnBlocked;
|
|
68
|
+
const mode = config.mode ?? "monitor";
|
|
69
|
+
const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
|
|
70
|
+
let token = null;
|
|
71
|
+
if (honeytokenEnabled) {
|
|
72
|
+
void (0, import_node2.siteHoneytoken)({ secret: config.apiKey }).then((t) => {
|
|
73
|
+
token = t;
|
|
74
|
+
sdk.addRule((0, import_node2.tripwire)({ paths: t.activePaths, includeDefaults: false }));
|
|
75
|
+
}).catch(() => {
|
|
76
|
+
});
|
|
77
|
+
}
|
|
67
78
|
const onError = config.onError || defaultOnError;
|
|
68
79
|
const skipPaths = config.skipPaths;
|
|
69
80
|
return async (req, res, next) => {
|
|
@@ -84,6 +95,44 @@ function webdecoy(config) {
|
|
|
84
95
|
skipLocalAnalysis: config.skipLocalAnalysis,
|
|
85
96
|
metadata: config.metadata
|
|
86
97
|
});
|
|
98
|
+
if (token) {
|
|
99
|
+
const ht = token;
|
|
100
|
+
const originalWrite = res.write.bind(res);
|
|
101
|
+
const originalEnd = res.end.bind(res);
|
|
102
|
+
const chunks = [];
|
|
103
|
+
let intercepting = null;
|
|
104
|
+
const shouldIntercept = () => {
|
|
105
|
+
if (intercepting === null) {
|
|
106
|
+
intercepting = !res.headersSent && (0, import_node2.isInjectableHtml)(res.getHeader("content-type"));
|
|
107
|
+
}
|
|
108
|
+
return intercepting;
|
|
109
|
+
};
|
|
110
|
+
res.write = function(chunk, ...rest) {
|
|
111
|
+
if (!shouldIntercept()) return originalWrite(chunk, ...rest);
|
|
112
|
+
if (chunk) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
113
|
+
return true;
|
|
114
|
+
};
|
|
115
|
+
res.end = function(chunk, ...rest) {
|
|
116
|
+
try {
|
|
117
|
+
if (!shouldIntercept()) return originalEnd(chunk, ...rest);
|
|
118
|
+
if (chunk && typeof chunk !== "function") {
|
|
119
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
120
|
+
}
|
|
121
|
+
const body = (0, import_node2.injectHoneytokenLink)(Buffer.concat(chunks).toString("utf8"), ht.linkHtml);
|
|
122
|
+
const out = Buffer.from(body, "utf8");
|
|
123
|
+
if (!res.headersSent) res.setHeader("Content-Length", String(out.length));
|
|
124
|
+
return originalEnd(out);
|
|
125
|
+
} catch {
|
|
126
|
+
return originalEnd(chunk, ...rest);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
if (mode === "monitor") {
|
|
131
|
+
req.webdecoy = result.detection;
|
|
132
|
+
req.webdecoyEdge = result.edge;
|
|
133
|
+
req.webdecoyWouldBlock = !result.allowed;
|
|
134
|
+
return next();
|
|
135
|
+
}
|
|
87
136
|
if (!result.allowed && result.ruleResult) {
|
|
88
137
|
const rr = result.ruleResult;
|
|
89
138
|
if (rr.action === "THROTTLE") {
|
|
@@ -110,7 +159,7 @@ function webdecoy(config) {
|
|
|
110
159
|
req.webdecoyEdge = result.edge;
|
|
111
160
|
return next();
|
|
112
161
|
} else {
|
|
113
|
-
return onBlocked(req, res, result.detection);
|
|
162
|
+
return onBlocked(req, res, result.detection, next);
|
|
114
163
|
}
|
|
115
164
|
} catch (error) {
|
|
116
165
|
onError(req, res, error);
|
|
@@ -120,7 +169,7 @@ function webdecoy(config) {
|
|
|
120
169
|
}
|
|
121
170
|
|
|
122
171
|
// src/captcha.ts
|
|
123
|
-
var
|
|
172
|
+
var import_node3 = require("@webdecoy/node");
|
|
124
173
|
function getIP(req) {
|
|
125
174
|
const forwardedFor = req.headers["x-forwarded-for"];
|
|
126
175
|
if (forwardedFor) {
|
|
@@ -139,7 +188,7 @@ function normalizeQuery(query) {
|
|
|
139
188
|
return out;
|
|
140
189
|
}
|
|
141
190
|
function webdecoyCaptcha(options) {
|
|
142
|
-
const endpoints = (0,
|
|
191
|
+
const endpoints = (0, import_node3.createCaptchaEndpoints)(options);
|
|
143
192
|
return async (req, res, next) => {
|
|
144
193
|
const result = await endpoints.handle({
|
|
145
194
|
method: req.method,
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
// src/middleware.ts
|
|
2
2
|
import { WebDecoy } from "@webdecoy/node";
|
|
3
|
+
import {
|
|
4
|
+
siteHoneytoken,
|
|
5
|
+
injectHoneytokenLink,
|
|
6
|
+
isInjectableHtml,
|
|
7
|
+
tripwire
|
|
8
|
+
} from "@webdecoy/node";
|
|
3
9
|
function defaultGetIP(req) {
|
|
4
10
|
const forwardedFor = req.headers["x-forwarded-for"];
|
|
5
11
|
if (forwardedFor) {
|
|
@@ -12,7 +18,7 @@ function defaultGetIP(req) {
|
|
|
12
18
|
}
|
|
13
19
|
return req.ip || req.socket.remoteAddress || "127.0.0.1";
|
|
14
20
|
}
|
|
15
|
-
function defaultOnBlocked(req, res, detection) {
|
|
21
|
+
function defaultOnBlocked(req, res, detection, _next) {
|
|
16
22
|
res.status(403).json({
|
|
17
23
|
error: "Forbidden",
|
|
18
24
|
message: "Access denied by Web Decoy protection",
|
|
@@ -37,6 +43,16 @@ function webdecoy(config) {
|
|
|
37
43
|
const sdk = new WebDecoy(config);
|
|
38
44
|
const getIP2 = config.getIP || defaultGetIP;
|
|
39
45
|
const onBlocked = config.onBlocked || defaultOnBlocked;
|
|
46
|
+
const mode = config.mode ?? "monitor";
|
|
47
|
+
const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
|
|
48
|
+
let token = null;
|
|
49
|
+
if (honeytokenEnabled) {
|
|
50
|
+
void siteHoneytoken({ secret: config.apiKey }).then((t) => {
|
|
51
|
+
token = t;
|
|
52
|
+
sdk.addRule(tripwire({ paths: t.activePaths, includeDefaults: false }));
|
|
53
|
+
}).catch(() => {
|
|
54
|
+
});
|
|
55
|
+
}
|
|
40
56
|
const onError = config.onError || defaultOnError;
|
|
41
57
|
const skipPaths = config.skipPaths;
|
|
42
58
|
return async (req, res, next) => {
|
|
@@ -57,6 +73,44 @@ function webdecoy(config) {
|
|
|
57
73
|
skipLocalAnalysis: config.skipLocalAnalysis,
|
|
58
74
|
metadata: config.metadata
|
|
59
75
|
});
|
|
76
|
+
if (token) {
|
|
77
|
+
const ht = token;
|
|
78
|
+
const originalWrite = res.write.bind(res);
|
|
79
|
+
const originalEnd = res.end.bind(res);
|
|
80
|
+
const chunks = [];
|
|
81
|
+
let intercepting = null;
|
|
82
|
+
const shouldIntercept = () => {
|
|
83
|
+
if (intercepting === null) {
|
|
84
|
+
intercepting = !res.headersSent && isInjectableHtml(res.getHeader("content-type"));
|
|
85
|
+
}
|
|
86
|
+
return intercepting;
|
|
87
|
+
};
|
|
88
|
+
res.write = function(chunk, ...rest) {
|
|
89
|
+
if (!shouldIntercept()) return originalWrite(chunk, ...rest);
|
|
90
|
+
if (chunk) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
91
|
+
return true;
|
|
92
|
+
};
|
|
93
|
+
res.end = function(chunk, ...rest) {
|
|
94
|
+
try {
|
|
95
|
+
if (!shouldIntercept()) return originalEnd(chunk, ...rest);
|
|
96
|
+
if (chunk && typeof chunk !== "function") {
|
|
97
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
98
|
+
}
|
|
99
|
+
const body = injectHoneytokenLink(Buffer.concat(chunks).toString("utf8"), ht.linkHtml);
|
|
100
|
+
const out = Buffer.from(body, "utf8");
|
|
101
|
+
if (!res.headersSent) res.setHeader("Content-Length", String(out.length));
|
|
102
|
+
return originalEnd(out);
|
|
103
|
+
} catch {
|
|
104
|
+
return originalEnd(chunk, ...rest);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (mode === "monitor") {
|
|
109
|
+
req.webdecoy = result.detection;
|
|
110
|
+
req.webdecoyEdge = result.edge;
|
|
111
|
+
req.webdecoyWouldBlock = !result.allowed;
|
|
112
|
+
return next();
|
|
113
|
+
}
|
|
60
114
|
if (!result.allowed && result.ruleResult) {
|
|
61
115
|
const rr = result.ruleResult;
|
|
62
116
|
if (rr.action === "THROTTLE") {
|
|
@@ -83,7 +137,7 @@ function webdecoy(config) {
|
|
|
83
137
|
req.webdecoyEdge = result.edge;
|
|
84
138
|
return next();
|
|
85
139
|
} else {
|
|
86
|
-
return onBlocked(req, res, result.detection);
|
|
140
|
+
return onBlocked(req, res, result.detection, next);
|
|
87
141
|
}
|
|
88
142
|
} catch (error) {
|
|
89
143
|
onError(req, res, error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/express",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Web Decoy middleware for Express.js",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"url": "https://github.com/WebDecoy/node/issues"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@webdecoy/node": "^0.
|
|
43
|
+
"@webdecoy/node": "^0.8.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"express": "^4.18.0 || ^5.0.0"
|