@webdecoy/fastify 0.12.0 → 0.14.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 +15 -4
- package/dist/index.d.ts +15 -4
- package/dist/index.js +17 -42
- package/dist/index.mjs +20 -44
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify';
|
|
2
|
-
import { EdgeVerdict, ProtectOptions, TrustedProxies, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
2
|
+
import { ProtectResult, EdgeVerdict, ProtectOptions, TrustedProxies, SDKDetectionResponse, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
3
|
export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -56,10 +56,14 @@ interface WebDecoyPluginOptions extends ProtectOptions {
|
|
|
56
56
|
*/
|
|
57
57
|
getIP?: (req: FastifyRequest) => string;
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
59
|
+
* Called when a request would be blocked.
|
|
60
|
+
*
|
|
61
|
+
* `detection` is the detection response, as before. `decision` is the full
|
|
62
|
+
* typed verdict — `conclusion`, every rule's outcome including the ones that
|
|
63
|
+
* dry-ran or never ran, and `deniedBy('tripwire')` — for handlers that need to
|
|
64
|
+
* know WHY rather than just THAT.
|
|
61
65
|
*/
|
|
62
|
-
onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection:
|
|
66
|
+
onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection: SDKDetectionResponse, decision: ProtectResult) => void;
|
|
63
67
|
/**
|
|
64
68
|
* Custom function to handle errors
|
|
65
69
|
* By default, logs error and allows request (fail open)
|
|
@@ -85,6 +89,13 @@ interface WebDecoyDetection {
|
|
|
85
89
|
declare module 'fastify' {
|
|
86
90
|
interface FastifyRequest {
|
|
87
91
|
webdecoy?: WebDecoyDetection;
|
|
92
|
+
/**
|
|
93
|
+
* The full typed verdict: `conclusion`, every rule's outcome including the
|
|
94
|
+
* ones that dry-ran or never ran, and `deniedBy()`. Populated in monitor
|
|
95
|
+
* mode too, which is where it matters — that is the only place a verdict
|
|
96
|
+
* surfaces when nothing is blocked.
|
|
97
|
+
*/
|
|
98
|
+
webdecoyDecision?: ProtectResult;
|
|
88
99
|
/** What the edge validator said about this request. */
|
|
89
100
|
webdecoyEdge?: EdgeVerdict;
|
|
90
101
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify';
|
|
2
|
-
import { EdgeVerdict, ProtectOptions, TrustedProxies, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
2
|
+
import { ProtectResult, EdgeVerdict, ProtectOptions, TrustedProxies, SDKDetectionResponse, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
3
|
export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -56,10 +56,14 @@ interface WebDecoyPluginOptions extends ProtectOptions {
|
|
|
56
56
|
*/
|
|
57
57
|
getIP?: (req: FastifyRequest) => string;
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
59
|
+
* Called when a request would be blocked.
|
|
60
|
+
*
|
|
61
|
+
* `detection` is the detection response, as before. `decision` is the full
|
|
62
|
+
* typed verdict — `conclusion`, every rule's outcome including the ones that
|
|
63
|
+
* dry-ran or never ran, and `deniedBy('tripwire')` — for handlers that need to
|
|
64
|
+
* know WHY rather than just THAT.
|
|
61
65
|
*/
|
|
62
|
-
onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection:
|
|
66
|
+
onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection: SDKDetectionResponse, decision: ProtectResult) => void;
|
|
63
67
|
/**
|
|
64
68
|
* Custom function to handle errors
|
|
65
69
|
* By default, logs error and allows request (fail open)
|
|
@@ -85,6 +89,13 @@ interface WebDecoyDetection {
|
|
|
85
89
|
declare module 'fastify' {
|
|
86
90
|
interface FastifyRequest {
|
|
87
91
|
webdecoy?: WebDecoyDetection;
|
|
92
|
+
/**
|
|
93
|
+
* The full typed verdict: `conclusion`, every rule's outcome including the
|
|
94
|
+
* ones that dry-ran or never ran, and `deniedBy()`. Populated in monitor
|
|
95
|
+
* mode too, which is where it matters — that is the only place a verdict
|
|
96
|
+
* surfaces when nothing is blocked.
|
|
97
|
+
*/
|
|
98
|
+
webdecoyDecision?: ProtectResult;
|
|
88
99
|
/** What the edge validator said about this request. */
|
|
89
100
|
webdecoyEdge?: EdgeVerdict;
|
|
90
101
|
}
|
package/dist/index.js
CHANGED
|
@@ -60,17 +60,6 @@ function defaultOnBlocked(req, reply, detection) {
|
|
|
60
60
|
function defaultOnError(req, reply, error) {
|
|
61
61
|
req.log.error({ err: error }, "[WebDecoy] Plugin error");
|
|
62
62
|
}
|
|
63
|
-
function shouldSkipPath(path, skipPaths) {
|
|
64
|
-
if (!skipPaths || skipPaths.length === 0) {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
return skipPaths.some((pattern) => {
|
|
68
|
-
if (typeof pattern === "string") {
|
|
69
|
-
return path === pattern || path.startsWith(pattern);
|
|
70
|
-
}
|
|
71
|
-
return pattern.test(path);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
63
|
async function webdecoyPluginImpl(fastify, options) {
|
|
75
64
|
const sdk = new import_node.WebDecoy(options);
|
|
76
65
|
const getIP = options.getIP || ((req) => resolveIP(req, options.trustProxy));
|
|
@@ -78,21 +67,15 @@ async function webdecoyPluginImpl(fastify, options) {
|
|
|
78
67
|
const mode = options.mode ?? "monitor";
|
|
79
68
|
const onError = options.onError || defaultOnError;
|
|
80
69
|
const skipPaths = options.skipPaths;
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
token = await (0, import_node.siteHoneytoken)({ secret: options.apiKey });
|
|
86
|
-
sdk.addRule((0, import_node.tripwire)({ paths: token.activePaths, includeDefaults: false }));
|
|
87
|
-
} catch {
|
|
88
|
-
token = null;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
70
|
+
const token = await (0, import_node.deriveAndArm)(sdk, {
|
|
71
|
+
apiKey: options.apiKey,
|
|
72
|
+
enabled: options.honeytoken
|
|
73
|
+
});
|
|
91
74
|
fastify.decorateRequest("webdecoy", null);
|
|
92
75
|
fastify.decorateRequest("webdecoyEdge", null);
|
|
93
76
|
fastify.addHook("preHandler", async (req, reply) => {
|
|
94
77
|
try {
|
|
95
|
-
if (shouldSkipPath(req.url, skipPaths)) {
|
|
78
|
+
if ((0, import_node.shouldSkipPath)(req.url, skipPaths)) {
|
|
96
79
|
return;
|
|
97
80
|
}
|
|
98
81
|
const headers = {};
|
|
@@ -109,6 +92,9 @@ async function webdecoyPluginImpl(fastify, options) {
|
|
|
109
92
|
ip: getIP(req),
|
|
110
93
|
user_agent: req.headers["user-agent"],
|
|
111
94
|
headers,
|
|
95
|
+
// The query is where injection payloads live, and it is not part of
|
|
96
|
+
// the routed path, so attackSignatures() cannot see it otherwise.
|
|
97
|
+
query: req.url.includes("?") ? req.url.slice(req.url.indexOf("?") + 1) : void 0,
|
|
112
98
|
timestamp: Date.now()
|
|
113
99
|
};
|
|
114
100
|
const result = await sdk.protect(metadata, {
|
|
@@ -118,35 +104,24 @@ async function webdecoyPluginImpl(fastify, options) {
|
|
|
118
104
|
});
|
|
119
105
|
if (mode === "monitor") {
|
|
120
106
|
req.webdecoy = result.detection;
|
|
107
|
+
req.webdecoyDecision = result;
|
|
121
108
|
req.webdecoyEdge = result.edge;
|
|
122
109
|
return;
|
|
123
110
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
reply.header("Retry-After", String(retryAfter));
|
|
129
|
-
reply.status(429).send({
|
|
130
|
-
error: "Too Many Requests",
|
|
131
|
-
message: rr.reason || "Rate limit exceeded",
|
|
132
|
-
retry_after: retryAfter
|
|
133
|
-
});
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (rr.action === "DENY") {
|
|
137
|
-
reply.status(403).send({
|
|
138
|
-
error: "Forbidden",
|
|
139
|
-
message: rr.reason || "Access denied by rule",
|
|
140
|
-
rule: rr.rule
|
|
141
|
-
});
|
|
142
|
-
return;
|
|
111
|
+
const block = (0, import_node.ruleBlockResponse)(result);
|
|
112
|
+
if (block) {
|
|
113
|
+
for (const [name, value] of Object.entries(block.headers)) {
|
|
114
|
+
reply.header(name, value);
|
|
143
115
|
}
|
|
116
|
+
reply.status(block.status).send(block.body);
|
|
117
|
+
return;
|
|
144
118
|
}
|
|
145
119
|
if (result.allowed) {
|
|
146
120
|
req.webdecoy = result.detection;
|
|
121
|
+
req.webdecoyDecision = result;
|
|
147
122
|
req.webdecoyEdge = result.edge;
|
|
148
123
|
} else {
|
|
149
|
-
onBlocked(req, reply, result.detection);
|
|
124
|
+
onBlocked(req, reply, result.detection, result);
|
|
150
125
|
}
|
|
151
126
|
} catch (error) {
|
|
152
127
|
onError(req, reply, error);
|
package/dist/index.mjs
CHANGED
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
import fp from "fastify-plugin";
|
|
3
3
|
import {
|
|
4
4
|
WebDecoy,
|
|
5
|
-
siteHoneytoken,
|
|
6
5
|
injectHoneytokenLink,
|
|
7
6
|
isInjectableHtml,
|
|
8
|
-
tripwire,
|
|
9
7
|
resolveClientIp,
|
|
10
|
-
normalizeIp
|
|
8
|
+
normalizeIp,
|
|
9
|
+
shouldSkipPath,
|
|
10
|
+
ruleBlockResponse,
|
|
11
|
+
deriveAndArm
|
|
11
12
|
} from "@webdecoy/node";
|
|
12
13
|
function resolveIP(req, trustProxy) {
|
|
13
14
|
const peer = req.socket?.remoteAddress;
|
|
@@ -30,17 +31,6 @@ function defaultOnBlocked(req, reply, detection) {
|
|
|
30
31
|
function defaultOnError(req, reply, error) {
|
|
31
32
|
req.log.error({ err: error }, "[WebDecoy] Plugin error");
|
|
32
33
|
}
|
|
33
|
-
function shouldSkipPath(path, skipPaths) {
|
|
34
|
-
if (!skipPaths || skipPaths.length === 0) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
return skipPaths.some((pattern) => {
|
|
38
|
-
if (typeof pattern === "string") {
|
|
39
|
-
return path === pattern || path.startsWith(pattern);
|
|
40
|
-
}
|
|
41
|
-
return pattern.test(path);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
34
|
async function webdecoyPluginImpl(fastify, options) {
|
|
45
35
|
const sdk = new WebDecoy(options);
|
|
46
36
|
const getIP = options.getIP || ((req) => resolveIP(req, options.trustProxy));
|
|
@@ -48,16 +38,10 @@ async function webdecoyPluginImpl(fastify, options) {
|
|
|
48
38
|
const mode = options.mode ?? "monitor";
|
|
49
39
|
const onError = options.onError || defaultOnError;
|
|
50
40
|
const skipPaths = options.skipPaths;
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
token = await siteHoneytoken({ secret: options.apiKey });
|
|
56
|
-
sdk.addRule(tripwire({ paths: token.activePaths, includeDefaults: false }));
|
|
57
|
-
} catch {
|
|
58
|
-
token = null;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
41
|
+
const token = await deriveAndArm(sdk, {
|
|
42
|
+
apiKey: options.apiKey,
|
|
43
|
+
enabled: options.honeytoken
|
|
44
|
+
});
|
|
61
45
|
fastify.decorateRequest("webdecoy", null);
|
|
62
46
|
fastify.decorateRequest("webdecoyEdge", null);
|
|
63
47
|
fastify.addHook("preHandler", async (req, reply) => {
|
|
@@ -79,6 +63,9 @@ async function webdecoyPluginImpl(fastify, options) {
|
|
|
79
63
|
ip: getIP(req),
|
|
80
64
|
user_agent: req.headers["user-agent"],
|
|
81
65
|
headers,
|
|
66
|
+
// The query is where injection payloads live, and it is not part of
|
|
67
|
+
// the routed path, so attackSignatures() cannot see it otherwise.
|
|
68
|
+
query: req.url.includes("?") ? req.url.slice(req.url.indexOf("?") + 1) : void 0,
|
|
82
69
|
timestamp: Date.now()
|
|
83
70
|
};
|
|
84
71
|
const result = await sdk.protect(metadata, {
|
|
@@ -88,35 +75,24 @@ async function webdecoyPluginImpl(fastify, options) {
|
|
|
88
75
|
});
|
|
89
76
|
if (mode === "monitor") {
|
|
90
77
|
req.webdecoy = result.detection;
|
|
78
|
+
req.webdecoyDecision = result;
|
|
91
79
|
req.webdecoyEdge = result.edge;
|
|
92
80
|
return;
|
|
93
81
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
reply.header("Retry-After", String(retryAfter));
|
|
99
|
-
reply.status(429).send({
|
|
100
|
-
error: "Too Many Requests",
|
|
101
|
-
message: rr.reason || "Rate limit exceeded",
|
|
102
|
-
retry_after: retryAfter
|
|
103
|
-
});
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
if (rr.action === "DENY") {
|
|
107
|
-
reply.status(403).send({
|
|
108
|
-
error: "Forbidden",
|
|
109
|
-
message: rr.reason || "Access denied by rule",
|
|
110
|
-
rule: rr.rule
|
|
111
|
-
});
|
|
112
|
-
return;
|
|
82
|
+
const block = ruleBlockResponse(result);
|
|
83
|
+
if (block) {
|
|
84
|
+
for (const [name, value] of Object.entries(block.headers)) {
|
|
85
|
+
reply.header(name, value);
|
|
113
86
|
}
|
|
87
|
+
reply.status(block.status).send(block.body);
|
|
88
|
+
return;
|
|
114
89
|
}
|
|
115
90
|
if (result.allowed) {
|
|
116
91
|
req.webdecoy = result.detection;
|
|
92
|
+
req.webdecoyDecision = result;
|
|
117
93
|
req.webdecoyEdge = result.edge;
|
|
118
94
|
} else {
|
|
119
|
-
onBlocked(req, reply, result.detection);
|
|
95
|
+
onBlocked(req, reply, result.detection, result);
|
|
120
96
|
}
|
|
121
97
|
} catch (error) {
|
|
122
98
|
onError(req, reply, error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/fastify",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Web Decoy plugin for Fastify",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
19
19
|
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
20
20
|
"test": "jest --passWithNoTests",
|
|
21
|
-
"lint": "eslint src --max-warnings
|
|
21
|
+
"lint": "eslint src --max-warnings 0",
|
|
22
22
|
"clean": "rm -rf dist"
|
|
23
23
|
},
|
|
24
24
|
"keywords": [
|
|
@@ -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.14.0",
|
|
44
44
|
"fastify-plugin": "^4.5.1"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|