@webdecoy/express 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
@@ -45,36 +45,20 @@ function defaultOnBlocked(req, res, detection, _next) {
45
45
  function defaultOnError(req, res, error) {
46
46
  console.error("[WebDecoy] Middleware error:", error);
47
47
  }
48
- function shouldSkipPath(path, skipPaths) {
49
- if (!skipPaths || skipPaths.length === 0) {
50
- return false;
51
- }
52
- return skipPaths.some((pattern) => {
53
- if (typeof pattern === "string") {
54
- return path === pattern || path.startsWith(pattern);
55
- }
56
- return pattern.test(path);
57
- });
58
- }
59
48
  function webdecoy(config) {
60
49
  const sdk = new import_node.WebDecoy(config);
61
50
  const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
62
51
  const onBlocked = config.onBlocked || defaultOnBlocked;
63
52
  const mode = config.mode ?? "monitor";
64
- const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
65
- let token = null;
66
- if (honeytokenEnabled) {
67
- void (0, import_node2.siteHoneytoken)({ secret: config.apiKey }).then((t) => {
68
- token = t;
69
- sdk.addRule((0, import_node2.tripwire)({ paths: t.activePaths, includeDefaults: false }));
70
- }).catch(() => {
71
- });
72
- }
53
+ const getToken = (0, import_node2.armSiteHoneytoken)(sdk, {
54
+ apiKey: config.apiKey,
55
+ enabled: config.honeytoken
56
+ });
73
57
  const onError = config.onError || defaultOnError;
74
58
  const skipPaths = config.skipPaths;
75
59
  return async (req, res, next) => {
76
60
  try {
77
- if (shouldSkipPath(req.path, skipPaths)) {
61
+ if ((0, import_node2.shouldSkipPath)(req.path, skipPaths)) {
78
62
  return next();
79
63
  }
80
64
  const metadata = {
@@ -93,8 +77,8 @@ function webdecoy(config) {
93
77
  skipLocalAnalysis: config.skipLocalAnalysis,
94
78
  metadata: config.metadata
95
79
  });
96
- if (token) {
97
- const ht = token;
80
+ const ht = getToken();
81
+ if (ht) {
98
82
  const originalWrite = res.write.bind(res);
99
83
  const originalEnd = res.end.bind(res);
100
84
  const chunks = [];
@@ -134,26 +118,13 @@ function webdecoy(config) {
134
118
  req.webdecoyWouldBlock = !result.allowed;
135
119
  return next();
136
120
  }
137
- if (!result.allowed && result.ruleResult) {
138
- const rr = result.ruleResult;
139
- if (rr.action === "THROTTLE") {
140
- const retryAfter = rr.metadata?.retryAfter ?? 60;
141
- res.setHeader("Retry-After", String(retryAfter));
142
- res.status(429).json({
143
- error: "Too Many Requests",
144
- message: rr.reason || "Rate limit exceeded",
145
- retry_after: retryAfter
146
- });
147
- return;
148
- }
149
- if (rr.action === "DENY") {
150
- res.status(403).json({
151
- error: "Forbidden",
152
- message: rr.reason || "Access denied by rule",
153
- rule: rr.rule
154
- });
155
- return;
121
+ const block = (0, import_node2.ruleBlockResponse)(result);
122
+ if (block) {
123
+ for (const [name, value] of Object.entries(block.headers)) {
124
+ res.setHeader(name, value);
156
125
  }
126
+ res.status(block.status).json(block.body);
127
+ return;
157
128
  }
158
129
  if (result.allowed) {
159
130
  req.webdecoy = result.detection;
package/dist/index.mjs CHANGED
@@ -1,12 +1,13 @@
1
1
  // src/middleware.ts
2
2
  import { WebDecoy } from "@webdecoy/node";
3
3
  import {
4
- siteHoneytoken,
5
4
  injectHoneytokenLink,
6
5
  isInjectableHtml,
7
- tripwire,
8
6
  resolveClientIp,
9
- normalizeIp
7
+ normalizeIp,
8
+ shouldSkipPath,
9
+ ruleBlockResponse,
10
+ armSiteHoneytoken
10
11
  } from "@webdecoy/node";
11
12
  function resolveIP(req, trustProxy) {
12
13
  const peer = req.socket?.remoteAddress;
@@ -25,31 +26,15 @@ function defaultOnBlocked(req, res, detection, _next) {
25
26
  function defaultOnError(req, res, error) {
26
27
  console.error("[WebDecoy] Middleware error:", error);
27
28
  }
28
- function shouldSkipPath(path, skipPaths) {
29
- if (!skipPaths || skipPaths.length === 0) {
30
- return false;
31
- }
32
- return skipPaths.some((pattern) => {
33
- if (typeof pattern === "string") {
34
- return path === pattern || path.startsWith(pattern);
35
- }
36
- return pattern.test(path);
37
- });
38
- }
39
29
  function webdecoy(config) {
40
30
  const sdk = new WebDecoy(config);
41
31
  const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
42
32
  const onBlocked = config.onBlocked || defaultOnBlocked;
43
33
  const mode = config.mode ?? "monitor";
44
- const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
45
- let token = null;
46
- if (honeytokenEnabled) {
47
- void siteHoneytoken({ secret: config.apiKey }).then((t) => {
48
- token = t;
49
- sdk.addRule(tripwire({ paths: t.activePaths, includeDefaults: false }));
50
- }).catch(() => {
51
- });
52
- }
34
+ const getToken = armSiteHoneytoken(sdk, {
35
+ apiKey: config.apiKey,
36
+ enabled: config.honeytoken
37
+ });
53
38
  const onError = config.onError || defaultOnError;
54
39
  const skipPaths = config.skipPaths;
55
40
  return async (req, res, next) => {
@@ -73,8 +58,8 @@ function webdecoy(config) {
73
58
  skipLocalAnalysis: config.skipLocalAnalysis,
74
59
  metadata: config.metadata
75
60
  });
76
- if (token) {
77
- const ht = token;
61
+ const ht = getToken();
62
+ if (ht) {
78
63
  const originalWrite = res.write.bind(res);
79
64
  const originalEnd = res.end.bind(res);
80
65
  const chunks = [];
@@ -114,26 +99,13 @@ function webdecoy(config) {
114
99
  req.webdecoyWouldBlock = !result.allowed;
115
100
  return next();
116
101
  }
117
- if (!result.allowed && result.ruleResult) {
118
- const rr = result.ruleResult;
119
- if (rr.action === "THROTTLE") {
120
- const retryAfter = rr.metadata?.retryAfter ?? 60;
121
- res.setHeader("Retry-After", String(retryAfter));
122
- res.status(429).json({
123
- error: "Too Many Requests",
124
- message: rr.reason || "Rate limit exceeded",
125
- retry_after: retryAfter
126
- });
127
- return;
128
- }
129
- if (rr.action === "DENY") {
130
- res.status(403).json({
131
- error: "Forbidden",
132
- message: rr.reason || "Access denied by rule",
133
- rule: rr.rule
134
- });
135
- return;
102
+ const block = ruleBlockResponse(result);
103
+ if (block) {
104
+ for (const [name, value] of Object.entries(block.headers)) {
105
+ res.setHeader(name, value);
136
106
  }
107
+ res.status(block.status).json(block.body);
108
+ return;
137
109
  }
138
110
  if (result.allowed) {
139
111
  req.webdecoy = result.detection;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/express",
3
- "version": "0.13.0",
3
+ "version": "0.15.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.13.0"
43
+ "@webdecoy/node": "^0.15.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "express": "^4.18.0 || ^5.0.0"