@webdecoy/fastify 0.9.0 → 0.11.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
@@ -13,12 +13,29 @@ interface WebDecoyPluginOptions extends ProtectOptions {
13
13
  * MONITOR IS THE DEFAULT ON PURPOSE, and this changed in 0.7.0. Before,
14
14
  * installing this with an API key began returning 403 to any request whose
15
15
  * server-side score cleared `threatScoreThreshold` (default 80), and there was
16
- * no supported way to watch first. Found by installing it on a live site that
17
- * takes payments, where the homepage returned Forbidden on the first request.
16
+ * no supported way to watch first — which is a good way to take down a site on
17
+ * the first install.
18
18
  *
19
19
  * Watch what it would have done, then set `mode: 'enforce'`.
20
20
  */
21
21
  mode?: 'monitor' | 'enforce';
22
+ /**
23
+ * Inject a hidden honeytoken link into HTML responses, and arm the tripwire it
24
+ * points at. Defaults to **on** when an apiKey is present.
25
+ *
26
+ * The SDK used to generate a honeytoken and ask the developer to embed it.
27
+ * Almost nobody does, and an unplaced link is a trap nothing can walk into.
28
+ *
29
+ * A trap hit is the only detection here that needs no score, no JavaScript, no
30
+ * fingerprint and no IP. It is also the only one that scores: honeypot signals
31
+ * carry the highest weight in the threat score, while a User-Agent — all a
32
+ * rule-less install can otherwise report — carries nearly none.
33
+ *
34
+ * Applies to buffered responses — `reply.send(html)`, `@fastify/view`, and
35
+ * anything else that hands Fastify a string or Buffer. Streamed replies are
36
+ * left untouched and logged once; see the `onSend` hook for why.
37
+ */
38
+ honeytoken?: boolean;
22
39
  /**
23
40
  * Custom function to extract IP address from request
24
41
  * By default, uses request.ip or x-forwarded-for header
@@ -54,7 +71,7 @@ interface WebDecoyDetection {
54
71
  declare module 'fastify' {
55
72
  interface FastifyRequest {
56
73
  webdecoy?: WebDecoyDetection;
57
- /** What the edge validator said about this request (#481). */
74
+ /** What the edge validator said about this request. */
58
75
  webdecoyEdge?: EdgeVerdict;
59
76
  }
60
77
  }
package/dist/index.d.ts CHANGED
@@ -13,12 +13,29 @@ interface WebDecoyPluginOptions extends ProtectOptions {
13
13
  * MONITOR IS THE DEFAULT ON PURPOSE, and this changed in 0.7.0. Before,
14
14
  * installing this with an API key began returning 403 to any request whose
15
15
  * server-side score cleared `threatScoreThreshold` (default 80), and there was
16
- * no supported way to watch first. Found by installing it on a live site that
17
- * takes payments, where the homepage returned Forbidden on the first request.
16
+ * no supported way to watch first — which is a good way to take down a site on
17
+ * the first install.
18
18
  *
19
19
  * Watch what it would have done, then set `mode: 'enforce'`.
20
20
  */
21
21
  mode?: 'monitor' | 'enforce';
22
+ /**
23
+ * Inject a hidden honeytoken link into HTML responses, and arm the tripwire it
24
+ * points at. Defaults to **on** when an apiKey is present.
25
+ *
26
+ * The SDK used to generate a honeytoken and ask the developer to embed it.
27
+ * Almost nobody does, and an unplaced link is a trap nothing can walk into.
28
+ *
29
+ * A trap hit is the only detection here that needs no score, no JavaScript, no
30
+ * fingerprint and no IP. It is also the only one that scores: honeypot signals
31
+ * carry the highest weight in the threat score, while a User-Agent — all a
32
+ * rule-less install can otherwise report — carries nearly none.
33
+ *
34
+ * Applies to buffered responses — `reply.send(html)`, `@fastify/view`, and
35
+ * anything else that hands Fastify a string or Buffer. Streamed replies are
36
+ * left untouched and logged once; see the `onSend` hook for why.
37
+ */
38
+ honeytoken?: boolean;
22
39
  /**
23
40
  * Custom function to extract IP address from request
24
41
  * By default, uses request.ip or x-forwarded-for header
@@ -54,7 +71,7 @@ interface WebDecoyDetection {
54
71
  declare module 'fastify' {
55
72
  interface FastifyRequest {
56
73
  webdecoy?: WebDecoyDetection;
57
- /** What the edge validator said about this request (#481). */
74
+ /** What the edge validator said about this request. */
58
75
  webdecoyEdge?: EdgeVerdict;
59
76
  }
60
77
  }
package/dist/index.js CHANGED
@@ -70,6 +70,16 @@ async function webdecoyPluginImpl(fastify, options) {
70
70
  const mode = options.mode ?? "monitor";
71
71
  const onError = options.onError || defaultOnError;
72
72
  const skipPaths = options.skipPaths;
73
+ const honeytokenEnabled = (options.honeytoken ?? true) && Boolean(options.apiKey);
74
+ let token = null;
75
+ if (honeytokenEnabled) {
76
+ try {
77
+ token = await (0, import_node.siteHoneytoken)({ secret: options.apiKey });
78
+ sdk.addRule((0, import_node.tripwire)({ paths: token.activePaths, includeDefaults: false }));
79
+ } catch {
80
+ token = null;
81
+ }
82
+ }
73
83
  fastify.decorateRequest("webdecoy", null);
74
84
  fastify.decorateRequest("webdecoyEdge", null);
75
85
  fastify.addHook("preHandler", async (req, reply) => {
@@ -134,6 +144,29 @@ async function webdecoyPluginImpl(fastify, options) {
134
144
  onError(req, reply, error);
135
145
  }
136
146
  });
147
+ if (token) {
148
+ const ht = token;
149
+ let warnedAboutStream = false;
150
+ fastify.addHook("onSend", async (_req, reply, payload) => {
151
+ try {
152
+ if (!(0, import_node.isInjectableHtml)(reply.getHeader("content-type"))) return payload;
153
+ if (payload === null || typeof payload === "object") {
154
+ if (!warnedAboutStream) {
155
+ warnedAboutStream = true;
156
+ fastify.log.warn(
157
+ `[WebDecoy] HTML is being streamed, so the honeytoken link was not injected. Render to a string, or embed the link yourself: <a href="${ht.primaryPath}" aria-hidden="true" tabindex="-1" rel="nofollow noindex" style="position:absolute;left:-9999px">.</a>`
158
+ );
159
+ }
160
+ return payload;
161
+ }
162
+ if (typeof payload !== "string" && !Buffer.isBuffer(payload)) return payload;
163
+ const html = Buffer.isBuffer(payload) ? payload.toString("utf8") : payload;
164
+ return (0, import_node.injectHoneytokenLink)(html, ht.linkHtml);
165
+ } catch {
166
+ return payload;
167
+ }
168
+ });
169
+ }
137
170
  }
138
171
  var webdecoyPlugin = (0, import_fastify_plugin.default)(webdecoyPluginImpl, {
139
172
  fastify: ">=4.0.0",
package/dist/index.mjs CHANGED
@@ -1,6 +1,12 @@
1
1
  // src/plugin.ts
2
2
  import fp from "fastify-plugin";
3
- import { WebDecoy } from "@webdecoy/node";
3
+ import {
4
+ WebDecoy,
5
+ siteHoneytoken,
6
+ injectHoneytokenLink,
7
+ isInjectableHtml,
8
+ tripwire
9
+ } from "@webdecoy/node";
4
10
  function defaultGetIP(req) {
5
11
  return req.ip || "127.0.0.1";
6
12
  }
@@ -32,6 +38,16 @@ async function webdecoyPluginImpl(fastify, options) {
32
38
  const mode = options.mode ?? "monitor";
33
39
  const onError = options.onError || defaultOnError;
34
40
  const skipPaths = options.skipPaths;
41
+ const honeytokenEnabled = (options.honeytoken ?? true) && Boolean(options.apiKey);
42
+ let token = null;
43
+ if (honeytokenEnabled) {
44
+ try {
45
+ token = await siteHoneytoken({ secret: options.apiKey });
46
+ sdk.addRule(tripwire({ paths: token.activePaths, includeDefaults: false }));
47
+ } catch {
48
+ token = null;
49
+ }
50
+ }
35
51
  fastify.decorateRequest("webdecoy", null);
36
52
  fastify.decorateRequest("webdecoyEdge", null);
37
53
  fastify.addHook("preHandler", async (req, reply) => {
@@ -96,6 +112,29 @@ async function webdecoyPluginImpl(fastify, options) {
96
112
  onError(req, reply, error);
97
113
  }
98
114
  });
115
+ if (token) {
116
+ const ht = token;
117
+ let warnedAboutStream = false;
118
+ fastify.addHook("onSend", async (_req, reply, payload) => {
119
+ try {
120
+ if (!isInjectableHtml(reply.getHeader("content-type"))) return payload;
121
+ if (payload === null || typeof payload === "object") {
122
+ if (!warnedAboutStream) {
123
+ warnedAboutStream = true;
124
+ fastify.log.warn(
125
+ `[WebDecoy] HTML is being streamed, so the honeytoken link was not injected. Render to a string, or embed the link yourself: <a href="${ht.primaryPath}" aria-hidden="true" tabindex="-1" rel="nofollow noindex" style="position:absolute;left:-9999px">.</a>`
126
+ );
127
+ }
128
+ return payload;
129
+ }
130
+ if (typeof payload !== "string" && !Buffer.isBuffer(payload)) return payload;
131
+ const html = Buffer.isBuffer(payload) ? payload.toString("utf8") : payload;
132
+ return injectHoneytokenLink(html, ht.linkHtml);
133
+ } catch {
134
+ return payload;
135
+ }
136
+ });
137
+ }
99
138
  }
100
139
  var webdecoyPlugin = fp(webdecoyPluginImpl, {
101
140
  fastify: ">=4.0.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/fastify",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "description": "Web Decoy plugin for Fastify",
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.9.0",
43
+ "@webdecoy/node": "^0.11.0",
44
44
  "fastify-plugin": "^4.5.1"
45
45
  },
46
46
  "peerDependencies": {