@webdecoy/express 0.7.0 → 0.8.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
@@ -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,38 @@ 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
+ intercepting = !res.headersSent && (0, import_node2.isInjectableHtml)(res.getHeader("content-type"));
107
+ }
108
+ return intercepting;
109
+ };
110
+ res.write = function(chunk, ...rest) {
111
+ if (!shouldIntercept()) return originalWrite(chunk, ...rest);
112
+ if (chunk) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
113
+ return true;
114
+ };
115
+ res.end = function(chunk, ...rest) {
116
+ try {
117
+ if (!shouldIntercept()) return originalEnd(chunk, ...rest);
118
+ if (chunk && typeof chunk !== "function") {
119
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
120
+ }
121
+ const body = (0, import_node2.injectHoneytokenLink)(Buffer.concat(chunks).toString("utf8"), ht.linkHtml);
122
+ const out = Buffer.from(body, "utf8");
123
+ if (!res.headersSent) res.setHeader("Content-Length", String(out.length));
124
+ return originalEnd(out);
125
+ } catch {
126
+ return originalEnd(chunk, ...rest);
127
+ }
128
+ };
129
+ }
88
130
  if (mode === "monitor") {
89
131
  req.webdecoy = result.detection;
90
132
  req.webdecoyEdge = result.edge;
@@ -127,7 +169,7 @@ function webdecoy(config) {
127
169
  }
128
170
 
129
171
  // src/captcha.ts
130
- var import_node2 = require("@webdecoy/node");
172
+ var import_node3 = require("@webdecoy/node");
131
173
  function getIP(req) {
132
174
  const forwardedFor = req.headers["x-forwarded-for"];
133
175
  if (forwardedFor) {
@@ -146,7 +188,7 @@ function normalizeQuery(query) {
146
188
  return out;
147
189
  }
148
190
  function webdecoyCaptcha(options) {
149
- const endpoints = (0, import_node2.createCaptchaEndpoints)(options);
191
+ const endpoints = (0, import_node3.createCaptchaEndpoints)(options);
150
192
  return async (req, res, next) => {
151
193
  const result = await endpoints.handle({
152
194
  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,38 @@ 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
+ intercepting = !res.headersSent && isInjectableHtml(res.getHeader("content-type"));
85
+ }
86
+ return intercepting;
87
+ };
88
+ res.write = function(chunk, ...rest) {
89
+ if (!shouldIntercept()) return originalWrite(chunk, ...rest);
90
+ if (chunk) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
91
+ return true;
92
+ };
93
+ res.end = function(chunk, ...rest) {
94
+ try {
95
+ if (!shouldIntercept()) return originalEnd(chunk, ...rest);
96
+ if (chunk && typeof chunk !== "function") {
97
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
98
+ }
99
+ const body = injectHoneytokenLink(Buffer.concat(chunks).toString("utf8"), ht.linkHtml);
100
+ const out = Buffer.from(body, "utf8");
101
+ if (!res.headersSent) res.setHeader("Content-Length", String(out.length));
102
+ return originalEnd(out);
103
+ } catch {
104
+ return originalEnd(chunk, ...rest);
105
+ }
106
+ };
107
+ }
61
108
  if (mode === "monitor") {
62
109
  req.webdecoy = result.detection;
63
110
  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.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.7.0"
43
+ "@webdecoy/node": "^0.8.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "express": "^4.18.0 || ^5.0.0"