@webdecoy/fastify 0.13.0 → 0.15.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.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 honeytokenEnabled = (options.honeytoken ?? true) && Boolean(options.apiKey);
82
- let token = null;
83
- if (honeytokenEnabled) {
84
- try {
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 = {};
@@ -125,26 +108,13 @@ async function webdecoyPluginImpl(fastify, options) {
125
108
  req.webdecoyEdge = result.edge;
126
109
  return;
127
110
  }
128
- if (!result.allowed && result.ruleResult) {
129
- const rr = result.ruleResult;
130
- if (rr.action === "THROTTLE") {
131
- const retryAfter = rr.metadata?.retryAfter ?? 60;
132
- reply.header("Retry-After", String(retryAfter));
133
- reply.status(429).send({
134
- error: "Too Many Requests",
135
- message: rr.reason || "Rate limit exceeded",
136
- retry_after: retryAfter
137
- });
138
- return;
139
- }
140
- if (rr.action === "DENY") {
141
- reply.status(403).send({
142
- error: "Forbidden",
143
- message: rr.reason || "Access denied by rule",
144
- rule: rr.rule
145
- });
146
- 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);
147
115
  }
116
+ reply.status(block.status).send(block.body);
117
+ return;
148
118
  }
149
119
  if (result.allowed) {
150
120
  req.webdecoy = result.detection;
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 honeytokenEnabled = (options.honeytoken ?? true) && Boolean(options.apiKey);
52
- let token = null;
53
- if (honeytokenEnabled) {
54
- try {
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) => {
@@ -95,26 +79,13 @@ async function webdecoyPluginImpl(fastify, options) {
95
79
  req.webdecoyEdge = result.edge;
96
80
  return;
97
81
  }
98
- if (!result.allowed && result.ruleResult) {
99
- const rr = result.ruleResult;
100
- if (rr.action === "THROTTLE") {
101
- const retryAfter = rr.metadata?.retryAfter ?? 60;
102
- reply.header("Retry-After", String(retryAfter));
103
- reply.status(429).send({
104
- error: "Too Many Requests",
105
- message: rr.reason || "Rate limit exceeded",
106
- retry_after: retryAfter
107
- });
108
- return;
109
- }
110
- if (rr.action === "DENY") {
111
- reply.status(403).send({
112
- error: "Forbidden",
113
- message: rr.reason || "Access denied by rule",
114
- rule: rr.rule
115
- });
116
- return;
82
+ const block = ruleBlockResponse(result);
83
+ if (block) {
84
+ for (const [name, value] of Object.entries(block.headers)) {
85
+ reply.header(name, value);
117
86
  }
87
+ reply.status(block.status).send(block.body);
88
+ return;
118
89
  }
119
90
  if (result.allowed) {
120
91
  req.webdecoy = result.detection;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/fastify",
3
- "version": "0.13.0",
3
+ "version": "0.15.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.13.0",
43
+ "@webdecoy/node": "^0.15.0",
44
44
  "fastify-plugin": "^4.5.1"
45
45
  },
46
46
  "peerDependencies": {