@webdecoy/fastify 0.11.1 → 0.13.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 CHANGED
@@ -1,5 +1,5 @@
1
1
  import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify';
2
- import { EdgeVerdict, ProtectOptions, 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
  /**
@@ -37,15 +37,33 @@ interface WebDecoyPluginOptions extends ProtectOptions {
37
37
  */
38
38
  honeytoken?: boolean;
39
39
  /**
40
- * Custom function to extract IP address from request
41
- * By default, uses request.ip or x-forwarded-for header
40
+ * How much of the `X-Forwarded-For` chain to believe.
41
+ *
42
+ * Leave this unset and Fastify decides: `request.ip` already honours the
43
+ * server's own `trustProxy` option, which defaults to the socket address. Set
44
+ * it to override that for WebDecoy alone — a number of trusted hops,
45
+ * `'cloudflare'`, or CIDRs of your proxies.
46
+ *
47
+ * Fastify's default was already the safe one, so unlike the Express and
48
+ * Next.js adapters nothing changes here in 0.12.0. The option exists so all
49
+ * three adapters answer the question the same way.
50
+ */
51
+ trustProxy?: TrustedProxies;
52
+ /**
53
+ * Custom function to extract IP address from request.
54
+ *
55
+ * Overrides `trustProxy` entirely — you are choosing the address yourself.
42
56
  */
43
57
  getIP?: (req: FastifyRequest) => string;
44
58
  /**
45
- * Custom function to handle blocked requests
46
- * By default, returns 403 Forbidden
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.
47
65
  */
48
- onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection: any) => void;
66
+ onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection: SDKDetectionResponse, decision: ProtectResult) => void;
49
67
  /**
50
68
  * Custom function to handle errors
51
69
  * By default, logs error and allows request (fail open)
@@ -71,6 +89,13 @@ interface WebDecoyDetection {
71
89
  declare module 'fastify' {
72
90
  interface FastifyRequest {
73
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;
74
99
  /** What the edge validator said about this request. */
75
100
  webdecoyEdge?: EdgeVerdict;
76
101
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify';
2
- import { EdgeVerdict, ProtectOptions, 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
  /**
@@ -37,15 +37,33 @@ interface WebDecoyPluginOptions extends ProtectOptions {
37
37
  */
38
38
  honeytoken?: boolean;
39
39
  /**
40
- * Custom function to extract IP address from request
41
- * By default, uses request.ip or x-forwarded-for header
40
+ * How much of the `X-Forwarded-For` chain to believe.
41
+ *
42
+ * Leave this unset and Fastify decides: `request.ip` already honours the
43
+ * server's own `trustProxy` option, which defaults to the socket address. Set
44
+ * it to override that for WebDecoy alone — a number of trusted hops,
45
+ * `'cloudflare'`, or CIDRs of your proxies.
46
+ *
47
+ * Fastify's default was already the safe one, so unlike the Express and
48
+ * Next.js adapters nothing changes here in 0.12.0. The option exists so all
49
+ * three adapters answer the question the same way.
50
+ */
51
+ trustProxy?: TrustedProxies;
52
+ /**
53
+ * Custom function to extract IP address from request.
54
+ *
55
+ * Overrides `trustProxy` entirely — you are choosing the address yourself.
42
56
  */
43
57
  getIP?: (req: FastifyRequest) => string;
44
58
  /**
45
- * Custom function to handle blocked requests
46
- * By default, returns 403 Forbidden
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.
47
65
  */
48
- onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection: any) => void;
66
+ onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection: SDKDetectionResponse, decision: ProtectResult) => void;
49
67
  /**
50
68
  * Custom function to handle errors
51
69
  * By default, logs error and allows request (fail open)
@@ -71,6 +89,13 @@ interface WebDecoyDetection {
71
89
  declare module 'fastify' {
72
90
  interface FastifyRequest {
73
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;
74
99
  /** What the edge validator said about this request. */
75
100
  webdecoyEdge?: EdgeVerdict;
76
101
  }
package/dist/index.js CHANGED
@@ -39,8 +39,16 @@ module.exports = __toCommonJS(index_exports);
39
39
  // src/plugin.ts
40
40
  var import_fastify_plugin = __toESM(require("fastify-plugin"));
41
41
  var import_node = require("@webdecoy/node");
42
- function defaultGetIP(req) {
43
- return req.ip || "127.0.0.1";
42
+ function resolveIP(req, trustProxy) {
43
+ const peer = req.socket?.remoteAddress;
44
+ if (trustProxy === void 0) {
45
+ return (0, import_node.normalizeIp)(req.ip) ?? (0, import_node.normalizeIp)(peer) ?? "127.0.0.1";
46
+ }
47
+ return (0, import_node.resolveClientIp)({
48
+ headers: req.headers,
49
+ peer,
50
+ trustProxy
51
+ }) ?? (0, import_node.normalizeIp)(peer) ?? "127.0.0.1";
44
52
  }
45
53
  function defaultOnBlocked(req, reply, detection) {
46
54
  reply.status(403).send({
@@ -65,7 +73,7 @@ function shouldSkipPath(path, skipPaths) {
65
73
  }
66
74
  async function webdecoyPluginImpl(fastify, options) {
67
75
  const sdk = new import_node.WebDecoy(options);
68
- const getIP = options.getIP || defaultGetIP;
76
+ const getIP = options.getIP || ((req) => resolveIP(req, options.trustProxy));
69
77
  const onBlocked = options.onBlocked || defaultOnBlocked;
70
78
  const mode = options.mode ?? "monitor";
71
79
  const onError = options.onError || defaultOnError;
@@ -101,6 +109,9 @@ async function webdecoyPluginImpl(fastify, options) {
101
109
  ip: getIP(req),
102
110
  user_agent: req.headers["user-agent"],
103
111
  headers,
112
+ // The query is where injection payloads live, and it is not part of
113
+ // the routed path, so attackSignatures() cannot see it otherwise.
114
+ query: req.url.includes("?") ? req.url.slice(req.url.indexOf("?") + 1) : void 0,
104
115
  timestamp: Date.now()
105
116
  };
106
117
  const result = await sdk.protect(metadata, {
@@ -110,6 +121,7 @@ async function webdecoyPluginImpl(fastify, options) {
110
121
  });
111
122
  if (mode === "monitor") {
112
123
  req.webdecoy = result.detection;
124
+ req.webdecoyDecision = result;
113
125
  req.webdecoyEdge = result.edge;
114
126
  return;
115
127
  }
@@ -136,9 +148,10 @@ async function webdecoyPluginImpl(fastify, options) {
136
148
  }
137
149
  if (result.allowed) {
138
150
  req.webdecoy = result.detection;
151
+ req.webdecoyDecision = result;
139
152
  req.webdecoyEdge = result.edge;
140
153
  } else {
141
- onBlocked(req, reply, result.detection);
154
+ onBlocked(req, reply, result.detection, result);
142
155
  }
143
156
  } catch (error) {
144
157
  onError(req, reply, error);
package/dist/index.mjs CHANGED
@@ -5,10 +5,20 @@ import {
5
5
  siteHoneytoken,
6
6
  injectHoneytokenLink,
7
7
  isInjectableHtml,
8
- tripwire
8
+ tripwire,
9
+ resolveClientIp,
10
+ normalizeIp
9
11
  } from "@webdecoy/node";
10
- function defaultGetIP(req) {
11
- return req.ip || "127.0.0.1";
12
+ function resolveIP(req, trustProxy) {
13
+ const peer = req.socket?.remoteAddress;
14
+ if (trustProxy === void 0) {
15
+ return normalizeIp(req.ip) ?? normalizeIp(peer) ?? "127.0.0.1";
16
+ }
17
+ return resolveClientIp({
18
+ headers: req.headers,
19
+ peer,
20
+ trustProxy
21
+ }) ?? normalizeIp(peer) ?? "127.0.0.1";
12
22
  }
13
23
  function defaultOnBlocked(req, reply, detection) {
14
24
  reply.status(403).send({
@@ -33,7 +43,7 @@ function shouldSkipPath(path, skipPaths) {
33
43
  }
34
44
  async function webdecoyPluginImpl(fastify, options) {
35
45
  const sdk = new WebDecoy(options);
36
- const getIP = options.getIP || defaultGetIP;
46
+ const getIP = options.getIP || ((req) => resolveIP(req, options.trustProxy));
37
47
  const onBlocked = options.onBlocked || defaultOnBlocked;
38
48
  const mode = options.mode ?? "monitor";
39
49
  const onError = options.onError || defaultOnError;
@@ -69,6 +79,9 @@ async function webdecoyPluginImpl(fastify, options) {
69
79
  ip: getIP(req),
70
80
  user_agent: req.headers["user-agent"],
71
81
  headers,
82
+ // The query is where injection payloads live, and it is not part of
83
+ // the routed path, so attackSignatures() cannot see it otherwise.
84
+ query: req.url.includes("?") ? req.url.slice(req.url.indexOf("?") + 1) : void 0,
72
85
  timestamp: Date.now()
73
86
  };
74
87
  const result = await sdk.protect(metadata, {
@@ -78,6 +91,7 @@ async function webdecoyPluginImpl(fastify, options) {
78
91
  });
79
92
  if (mode === "monitor") {
80
93
  req.webdecoy = result.detection;
94
+ req.webdecoyDecision = result;
81
95
  req.webdecoyEdge = result.edge;
82
96
  return;
83
97
  }
@@ -104,9 +118,10 @@ async function webdecoyPluginImpl(fastify, options) {
104
118
  }
105
119
  if (result.allowed) {
106
120
  req.webdecoy = result.detection;
121
+ req.webdecoyDecision = result;
107
122
  req.webdecoyEdge = result.edge;
108
123
  } else {
109
- onBlocked(req, reply, result.detection);
124
+ onBlocked(req, reply, result.detection, result);
110
125
  }
111
126
  } catch (error) {
112
127
  onError(req, reply, error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/fastify",
3
- "version": "0.11.1",
3
+ "version": "0.13.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/**/*.ts",
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.11.1",
43
+ "@webdecoy/node": "^0.13.0",
44
44
  "fastify-plugin": "^4.5.1"
45
45
  },
46
46
  "peerDependencies": {
@@ -49,9 +49,6 @@
49
49
  "devDependencies": {
50
50
  "@types/jest": "^29.5.11",
51
51
  "@types/node": "^20.11.0",
52
- "@typescript-eslint/eslint-plugin": "^6.18.1",
53
- "@typescript-eslint/parser": "^6.18.1",
54
- "eslint": "^8.56.0",
55
52
  "fastify": "^4.25.0",
56
53
  "jest": "^29.7.0",
57
54
  "ts-jest": "^29.1.1",