@webdecoy/express 0.7.0 → 0.8.1

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
@@ -26,6 +26,22 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
26
26
  * Watch what it would have done, then set `mode: 'enforce'`.
27
27
  */
28
28
  mode?: 'monitor' | 'enforce';
29
+ /**
30
+ * Inject a hidden honeytoken link into HTML responses, and arm the tripwire it
31
+ * points at. Defaults to **on** when an apiKey is present (#482).
32
+ *
33
+ * The SDK used to generate a honeytoken and ask the developer to embed it.
34
+ * Almost nobody did: `sdk_tripwire` had FOUR rows in production, ever, while
35
+ * the WordPress plugin — which injects the link itself — has real coverage.
36
+ *
37
+ * A trap hit is the only detection here that needs no score, no JavaScript, no
38
+ * fingerprint and no IP. It is also the only one that scores: honeypot signals
39
+ * are weighted 38% against user-agent's 1%, which is why every rule-less `sdk`
40
+ * detection ever recorded came out at 0.
41
+ *
42
+ * Set `false` to opt out, or place the link yourself for apps that stream.
43
+ */
44
+ honeytoken?: boolean;
29
45
  /**
30
46
  * Custom function to extract IP address from request
31
47
  * By default, uses req.ip or x-forwarded-for header
package/dist/index.d.ts CHANGED
@@ -26,6 +26,22 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
26
26
  * Watch what it would have done, then set `mode: 'enforce'`.
27
27
  */
28
28
  mode?: 'monitor' | 'enforce';
29
+ /**
30
+ * Inject a hidden honeytoken link into HTML responses, and arm the tripwire it
31
+ * points at. Defaults to **on** when an apiKey is present (#482).
32
+ *
33
+ * The SDK used to generate a honeytoken and ask the developer to embed it.
34
+ * Almost nobody did: `sdk_tripwire` had FOUR rows in production, ever, while
35
+ * the WordPress plugin — which injects the link itself — has real coverage.
36
+ *
37
+ * A trap hit is the only detection here that needs no score, no JavaScript, no
38
+ * fingerprint and no IP. It is also the only one that scores: honeypot signals
39
+ * are weighted 38% against user-agent's 1%, which is why every rule-less `sdk`
40
+ * detection ever recorded came out at 0.
41
+ *
42
+ * Set `false` to opt out, or place the link yourself for apps that stream.
43
+ */
44
+ honeytoken?: boolean;
29
45
  /**
30
46
  * Custom function to extract IP address from request
31
47
  * By default, uses req.ip or x-forwarded-for header
package/dist/index.js CHANGED
@@ -27,6 +27,7 @@ module.exports = __toCommonJS(index_exports);
27
27
 
28
28
  // src/middleware.ts
29
29
  var import_node = require("@webdecoy/node");
30
+ var import_node2 = require("@webdecoy/node");
30
31
  function defaultGetIP(req) {
31
32
  const forwardedFor = req.headers["x-forwarded-for"];
32
33
  if (forwardedFor) {
@@ -65,6 +66,15 @@ function webdecoy(config) {
65
66
  const getIP2 = config.getIP || defaultGetIP;
66
67
  const onBlocked = config.onBlocked || defaultOnBlocked;
67
68
  const mode = config.mode ?? "monitor";
69
+ const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
70
+ let token = null;
71
+ if (honeytokenEnabled) {
72
+ void (0, import_node2.siteHoneytoken)({ secret: config.apiKey }).then((t) => {
73
+ token = t;
74
+ sdk.addRule((0, import_node2.tripwire)({ paths: t.activePaths, includeDefaults: false }));
75
+ }).catch(() => {
76
+ });
77
+ }
68
78
  const onError = config.onError || defaultOnError;
69
79
  const skipPaths = config.skipPaths;
70
80
  return async (req, res, next) => {
@@ -85,6 +95,40 @@ function webdecoy(config) {
85
95
  skipLocalAnalysis: config.skipLocalAnalysis,
86
96
  metadata: config.metadata
87
97
  });
98
+ if (token) {
99
+ const ht = token;
100
+ const originalWrite = res.write.bind(res);
101
+ const originalEnd = res.end.bind(res);
102
+ const chunks = [];
103
+ let intercepting = null;
104
+ const shouldIntercept = () => {
105
+ if (intercepting === null) {
106
+ const isHtml = (0, import_node2.isInjectableHtml)(res.getHeader("content-type"));
107
+ const lengthCommitted = res.headersSent && res.getHeader("content-length") !== void 0;
108
+ intercepting = isHtml && !lengthCommitted;
109
+ }
110
+ return intercepting;
111
+ };
112
+ res.write = function(chunk, ...rest) {
113
+ if (!shouldIntercept()) return originalWrite(chunk, ...rest);
114
+ if (chunk) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
115
+ return true;
116
+ };
117
+ res.end = function(chunk, ...rest) {
118
+ try {
119
+ if (!shouldIntercept()) return originalEnd(chunk, ...rest);
120
+ if (chunk && typeof chunk !== "function") {
121
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
122
+ }
123
+ const body = (0, import_node2.injectHoneytokenLink)(Buffer.concat(chunks).toString("utf8"), ht.linkHtml);
124
+ const out = Buffer.from(body, "utf8");
125
+ if (!res.headersSent) res.setHeader("Content-Length", String(out.length));
126
+ return originalEnd(out);
127
+ } catch {
128
+ return originalEnd(chunk, ...rest);
129
+ }
130
+ };
131
+ }
88
132
  if (mode === "monitor") {
89
133
  req.webdecoy = result.detection;
90
134
  req.webdecoyEdge = result.edge;
@@ -127,7 +171,7 @@ function webdecoy(config) {
127
171
  }
128
172
 
129
173
  // src/captcha.ts
130
- var import_node2 = require("@webdecoy/node");
174
+ var import_node3 = require("@webdecoy/node");
131
175
  function getIP(req) {
132
176
  const forwardedFor = req.headers["x-forwarded-for"];
133
177
  if (forwardedFor) {
@@ -146,7 +190,7 @@ function normalizeQuery(query) {
146
190
  return out;
147
191
  }
148
192
  function webdecoyCaptcha(options) {
149
- const endpoints = (0, import_node2.createCaptchaEndpoints)(options);
193
+ const endpoints = (0, import_node3.createCaptchaEndpoints)(options);
150
194
  return async (req, res, next) => {
151
195
  const result = await endpoints.handle({
152
196
  method: req.method,
package/dist/index.mjs CHANGED
@@ -1,5 +1,11 @@
1
1
  // src/middleware.ts
2
2
  import { WebDecoy } from "@webdecoy/node";
3
+ import {
4
+ siteHoneytoken,
5
+ injectHoneytokenLink,
6
+ isInjectableHtml,
7
+ tripwire
8
+ } from "@webdecoy/node";
3
9
  function defaultGetIP(req) {
4
10
  const forwardedFor = req.headers["x-forwarded-for"];
5
11
  if (forwardedFor) {
@@ -38,6 +44,15 @@ function webdecoy(config) {
38
44
  const getIP2 = config.getIP || defaultGetIP;
39
45
  const onBlocked = config.onBlocked || defaultOnBlocked;
40
46
  const mode = config.mode ?? "monitor";
47
+ const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
48
+ let token = null;
49
+ if (honeytokenEnabled) {
50
+ void siteHoneytoken({ secret: config.apiKey }).then((t) => {
51
+ token = t;
52
+ sdk.addRule(tripwire({ paths: t.activePaths, includeDefaults: false }));
53
+ }).catch(() => {
54
+ });
55
+ }
41
56
  const onError = config.onError || defaultOnError;
42
57
  const skipPaths = config.skipPaths;
43
58
  return async (req, res, next) => {
@@ -58,6 +73,40 @@ function webdecoy(config) {
58
73
  skipLocalAnalysis: config.skipLocalAnalysis,
59
74
  metadata: config.metadata
60
75
  });
76
+ if (token) {
77
+ const ht = token;
78
+ const originalWrite = res.write.bind(res);
79
+ const originalEnd = res.end.bind(res);
80
+ const chunks = [];
81
+ let intercepting = null;
82
+ const shouldIntercept = () => {
83
+ if (intercepting === null) {
84
+ const isHtml = isInjectableHtml(res.getHeader("content-type"));
85
+ const lengthCommitted = res.headersSent && res.getHeader("content-length") !== void 0;
86
+ intercepting = isHtml && !lengthCommitted;
87
+ }
88
+ return intercepting;
89
+ };
90
+ res.write = function(chunk, ...rest) {
91
+ if (!shouldIntercept()) return originalWrite(chunk, ...rest);
92
+ if (chunk) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
93
+ return true;
94
+ };
95
+ res.end = function(chunk, ...rest) {
96
+ try {
97
+ if (!shouldIntercept()) return originalEnd(chunk, ...rest);
98
+ if (chunk && typeof chunk !== "function") {
99
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
100
+ }
101
+ const body = injectHoneytokenLink(Buffer.concat(chunks).toString("utf8"), ht.linkHtml);
102
+ const out = Buffer.from(body, "utf8");
103
+ if (!res.headersSent) res.setHeader("Content-Length", String(out.length));
104
+ return originalEnd(out);
105
+ } catch {
106
+ return originalEnd(chunk, ...rest);
107
+ }
108
+ };
109
+ }
61
110
  if (mode === "monitor") {
62
111
  req.webdecoy = result.detection;
63
112
  req.webdecoyEdge = result.edge;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/express",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
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.7.0"
43
+ "@webdecoy/node": "^0.8.1"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "express": "^4.18.0 || ^5.0.0"