eslint-plugin-webhook-safety 0.0.0-stage → 0.1.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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +72 -2
- package/dist/index.cjs +491 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +489 -0
- package/dist/index.js.map +1 -0
- package/docs/rules/bullmq-require-job-id.md +40 -0
- package/docs/rules/require-idempotency.md +53 -0
- package/docs/rules/require-raw-body.md +61 -0
- package/docs/rules/require-signature-verification.md +49 -0
- package/package.json +79 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-10-22)
|
|
4
|
+
|
|
5
|
+
- Rules: `require-raw-body`, `require-signature-verification`, `require-idempotency`, `bullmq-require-job-id`.
|
|
6
|
+
- Configs: `recommended`, `strict`, `legacy-recommended`, `legacy-strict`.
|
|
7
|
+
- Tested on 8 open-source apps (Stripe/BullMQ users). False positives fixed before release:
|
|
8
|
+
- `require-signature-verification` ignores authenticated endpoints (e.g. CRUD APIs that manage webhook subscriptions).
|
|
9
|
+
- `require-idempotency` ignores verify-and-return wrappers that return the event to the caller.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Krrish Kumar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,73 @@
|
|
|
1
|
-
#
|
|
1
|
+
# eslint-plugin-webhook-safety
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/Krrishkumar125/eslint-plugin-webhook-safety/actions/workflows/ci.yml)
|
|
4
|
+
|
|
5
|
+
ESLint rules that catch the Stripe webhook bugs that only show up in production:
|
|
6
|
+
|
|
7
|
+
- the body was parsed before the signature check, so every event fails;
|
|
8
|
+
- the signature is never checked;
|
|
9
|
+
- duplicate deliveries are processed twice;
|
|
10
|
+
- retries enqueue the same BullMQ job twice.
|
|
11
|
+
|
|
12
|
+
The static half of [webhook-crash-lab](https://github.com/Krrishkumar125/webhook-crash-lab).
|
|
13
|
+
This plugin finds the bug in your editor, and crash-lab proves the fix against a running server.
|
|
14
|
+
|
|
15
|
+
## 60-second start
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm i -D eslint eslint-plugin-webhook-safety
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
// eslint.config.js
|
|
23
|
+
import webhookSafety from 'eslint-plugin-webhook-safety';
|
|
24
|
+
|
|
25
|
+
export default [
|
|
26
|
+
webhookSafety.configs.recommended, // or .strict
|
|
27
|
+
];
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
// server.js
|
|
32
|
+
app.use(express.json());
|
|
33
|
+
app.post('/webhooks/stripe', async (req, res) => {
|
|
34
|
+
const event = stripe.webhooks.constructEvent(req.body, sig, secret);
|
|
35
|
+
await emailQueue.add('receipt', { to: event.data.object.customer_email });
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```text
|
|
40
|
+
$ npx eslint server.js
|
|
41
|
+
3:1 error `app.use(express.json())` on line 1 runs before this webhook route and parses the body ... webhook-safety/require-raw-body
|
|
42
|
+
4:17 warning Stripe retries and duplicates webhook deliveries, but this handler never uses `event.id` ... webhook-safety/require-idempotency
|
|
43
|
+
5:9 warning Webhook deliveries are retried, so this queue.add() will enqueue the same work twice ... webhook-safety/bullmq-require-job-id
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Rules
|
|
47
|
+
|
|
48
|
+
| Rule | What it catches | recommended | strict | 💡 |
|
|
49
|
+
| ------------------------------------------------------------------------------ | -------------------------------------------------------------------------- | ----------- | ------ | --- |
|
|
50
|
+
| [require-raw-body](docs/rules/require-raw-body.md) | `express.json()` / `req.json()` / `JSON.stringify` before `constructEvent` | error | error | ✅ |
|
|
51
|
+
| [require-signature-verification](docs/rules/require-signature-verification.md) | webhook route or Next.js `POST` with no signature check | error | error | |
|
|
52
|
+
| [require-idempotency](docs/rules/require-idempotency.md) | `constructEvent` result whose `event.id` is never used for dedupe | warn | error | |
|
|
53
|
+
| [bullmq-require-job-id](docs/rules/bullmq-require-job-id.md) | `queue.add()` in a webhook handler without `jobId` | warn | error | ✅ |
|
|
54
|
+
|
|
55
|
+
The rules are tuned for **low false positives**. When in doubt (custom middleware, the event passed to a
|
|
56
|
+
helper, options passed as a variable) they stay quiet. Each rule doc lists exactly what triggers it.
|
|
57
|
+
|
|
58
|
+
Supports Express, Hono, Fastify-style routers and the Next.js App Router, in JavaScript and TypeScript
|
|
59
|
+
(use `typescript-eslint`'s parser as usual).
|
|
60
|
+
|
|
61
|
+
### Legacy `.eslintrc` (ESLint 8)
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{ "extends": ["plugin:webhook-safety/legacy-recommended"] }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Requirements
|
|
68
|
+
|
|
69
|
+
ESLint ≥ 8.57 (flat config) and Node ≥ 20.
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
MIT © Krrish Kumar
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/utils.ts
|
|
2
|
+
var REPO = "https://github.com/Krrishkumar125/eslint-plugin-webhook-safety";
|
|
3
|
+
var docsUrl = (name) => `${REPO}/blob/main/docs/rules/${name}.md`;
|
|
4
|
+
var WEBHOOK_PATH = /webhook|\/hooks?(?:\/|$)/i;
|
|
5
|
+
var CONSTRUCT_EVENT = /* @__PURE__ */ new Set(["constructEvent", "constructEventAsync"]);
|
|
6
|
+
var ROUTE_METHODS = /* @__PURE__ */ new Set(["post", "put", "all"]);
|
|
7
|
+
function isFunction(node) {
|
|
8
|
+
return !!node && (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression");
|
|
9
|
+
}
|
|
10
|
+
function walk(root, visit) {
|
|
11
|
+
const stack = [root];
|
|
12
|
+
while (stack.length) {
|
|
13
|
+
const node = stack.pop();
|
|
14
|
+
visit(node);
|
|
15
|
+
for (const key of Object.keys(node)) {
|
|
16
|
+
if (key === "parent") continue;
|
|
17
|
+
const value = node[key];
|
|
18
|
+
const children = Array.isArray(value) ? value : [value];
|
|
19
|
+
for (const child of children) {
|
|
20
|
+
if (child && typeof child === "object" && typeof child.type === "string") {
|
|
21
|
+
stack.push(child);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function calleeName(node) {
|
|
28
|
+
const callee = node.callee;
|
|
29
|
+
if (callee.type === "Identifier") return callee.name;
|
|
30
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier") {
|
|
31
|
+
return callee.property.name;
|
|
32
|
+
}
|
|
33
|
+
return void 0;
|
|
34
|
+
}
|
|
35
|
+
function memberCall(node) {
|
|
36
|
+
if (node.type !== "CallExpression") return void 0;
|
|
37
|
+
const callee = node.callee;
|
|
38
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.object.type === "Identifier" && callee.property.type === "Identifier") {
|
|
39
|
+
return [callee.object.name, callee.property.name];
|
|
40
|
+
}
|
|
41
|
+
return void 0;
|
|
42
|
+
}
|
|
43
|
+
var PARSER_OWNERS = /* @__PURE__ */ new Set(["express", "bodyParser", "bodyparser"]);
|
|
44
|
+
function isJsonParser(node) {
|
|
45
|
+
const m = memberCall(node);
|
|
46
|
+
return !!m && PARSER_OWNERS.has(m[0]) && m[1] === "json";
|
|
47
|
+
}
|
|
48
|
+
function isRawParser(node) {
|
|
49
|
+
const m = memberCall(node);
|
|
50
|
+
return !!m && PARSER_OWNERS.has(m[0]) && (m[1] === "raw" || m[1] === "text");
|
|
51
|
+
}
|
|
52
|
+
function isBodyParser(node) {
|
|
53
|
+
const m = memberCall(node);
|
|
54
|
+
return !!m && PARSER_OWNERS.has(m[0]) && ["json", "raw", "text", "urlencoded"].includes(m[1]);
|
|
55
|
+
}
|
|
56
|
+
function isConstructEvent(node) {
|
|
57
|
+
if (node.type !== "CallExpression") return false;
|
|
58
|
+
const name = calleeName(node);
|
|
59
|
+
return !!name && CONSTRUCT_EVENT.has(name);
|
|
60
|
+
}
|
|
61
|
+
function findAll(root, test) {
|
|
62
|
+
const out = [];
|
|
63
|
+
walk(root, (n) => {
|
|
64
|
+
if (test(n)) out.push(n);
|
|
65
|
+
});
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
function enclosingFunction(node) {
|
|
69
|
+
let cur = node.parent;
|
|
70
|
+
while (cur) {
|
|
71
|
+
if (isFunction(cur)) return cur;
|
|
72
|
+
cur = cur.parent;
|
|
73
|
+
}
|
|
74
|
+
return void 0;
|
|
75
|
+
}
|
|
76
|
+
function literalString(node) {
|
|
77
|
+
if (!node) return void 0;
|
|
78
|
+
if (node.type === "Literal" && typeof node.value === "string") return node.value;
|
|
79
|
+
if (node.type === "TemplateLiteral" && node.expressions.length === 0) {
|
|
80
|
+
return _nullishCoalesce(_optionalChain([node, 'access', _ => _.quasis, 'access', _2 => _2[0], 'optionalAccess', _3 => _3.value, 'access', _4 => _4.cooked]), () => ( void 0));
|
|
81
|
+
}
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
function asRoute(node) {
|
|
85
|
+
if (node.type !== "CallExpression") return void 0;
|
|
86
|
+
const callee = node.callee;
|
|
87
|
+
if (callee.type !== "MemberExpression" || callee.computed || callee.property.type !== "Identifier") {
|
|
88
|
+
return void 0;
|
|
89
|
+
}
|
|
90
|
+
if (!ROUTE_METHODS.has(callee.property.name)) return void 0;
|
|
91
|
+
const args = node.arguments;
|
|
92
|
+
const path = literalString(args[0]);
|
|
93
|
+
const handler = args[args.length - 1];
|
|
94
|
+
if (path === void 0 || args.length < 2 || !isFunction(handler)) return void 0;
|
|
95
|
+
return {
|
|
96
|
+
call: node,
|
|
97
|
+
owner: callee.object.type === "Identifier" ? callee.object.name : void 0,
|
|
98
|
+
path,
|
|
99
|
+
middlewares: args.slice(1, -1),
|
|
100
|
+
handler
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function isNextPostHandler(fn) {
|
|
104
|
+
const parent = fn.parent;
|
|
105
|
+
if (fn.type === "FunctionDeclaration") {
|
|
106
|
+
return _optionalChain([fn, 'access', _5 => _5.id, 'optionalAccess', _6 => _6.name]) === "POST" && _optionalChain([parent, 'optionalAccess', _7 => _7.type]) === "ExportNamedDeclaration";
|
|
107
|
+
}
|
|
108
|
+
if (_optionalChain([parent, 'optionalAccess', _8 => _8.type]) === "VariableDeclarator") {
|
|
109
|
+
return parent.id.type === "Identifier" && parent.id.name === "POST" && _optionalChain([parent, 'access', _9 => _9.parent, 'optionalAccess', _10 => _10.parent, 'optionalAccess', _11 => _11.type]) === "ExportNamedDeclaration";
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
function isWebhookFile(context) {
|
|
114
|
+
return WEBHOOK_PATH.test(context.filename.replace(/\\/g, "/"));
|
|
115
|
+
}
|
|
116
|
+
function eventBinding(call) {
|
|
117
|
+
let cur = call.parent;
|
|
118
|
+
if (_optionalChain([cur, 'optionalAccess', _12 => _12.type]) === "AwaitExpression") cur = cur.parent;
|
|
119
|
+
let target;
|
|
120
|
+
if (_optionalChain([cur, 'optionalAccess', _13 => _13.type]) === "VariableDeclarator") target = cur.id;
|
|
121
|
+
else if (_optionalChain([cur, 'optionalAccess', _14 => _14.type]) === "AssignmentExpression") target = cur.left;
|
|
122
|
+
if (!target) return void 0;
|
|
123
|
+
if (target.type === "Identifier") return { kind: "identifier", name: target.name };
|
|
124
|
+
if (target.type === "ObjectPattern") {
|
|
125
|
+
const hasId = target.properties.some(
|
|
126
|
+
(p) => p.type === "RestElement" || p.key.type === "Identifier" && p.key.name === "id" || p.key.type === "Literal" && p.key.value === "id"
|
|
127
|
+
);
|
|
128
|
+
return { kind: "pattern", hasId };
|
|
129
|
+
}
|
|
130
|
+
return void 0;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/rules/bullmq-require-job-id.ts
|
|
134
|
+
var QUEUE_NAME = /queue$/i;
|
|
135
|
+
var rule = {
|
|
136
|
+
meta: {
|
|
137
|
+
type: "problem",
|
|
138
|
+
hasSuggestions: true,
|
|
139
|
+
docs: {
|
|
140
|
+
description: "Require a jobId (or deduplication id) when a webhook handler adds a BullMQ job, so retried deliveries do not enqueue duplicates",
|
|
141
|
+
recommended: true,
|
|
142
|
+
url: docsUrl("bullmq-require-job-id")
|
|
143
|
+
},
|
|
144
|
+
schema: [],
|
|
145
|
+
messages: {
|
|
146
|
+
missing: "Webhook deliveries are retried, so this queue.add() will enqueue the same work twice. Pass a stable jobId such as the event id.",
|
|
147
|
+
addJobId: "Add { jobId: {{name}}.id }"
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
create(context) {
|
|
151
|
+
const webhookFile = isWebhookFile(context);
|
|
152
|
+
const queues = /* @__PURE__ */ new Set();
|
|
153
|
+
function isWebhookFunction(fn) {
|
|
154
|
+
const parent = fn.parent;
|
|
155
|
+
if (parent) {
|
|
156
|
+
const route = asRoute(parent);
|
|
157
|
+
if (route && route.handler === fn && WEBHOOK_PATH.test(route.path)) return true;
|
|
158
|
+
}
|
|
159
|
+
if (webhookFile && isNextPostHandler(fn)) return true;
|
|
160
|
+
return findAll(fn, isConstructEvent).some((c) => enclosingFunction(c) === fn);
|
|
161
|
+
}
|
|
162
|
+
function eventName(fn) {
|
|
163
|
+
for (const c of findAll(fn, isConstructEvent)) {
|
|
164
|
+
const b = eventBinding(c);
|
|
165
|
+
if (_optionalChain([b, 'optionalAccess', _15 => _15.kind]) === "identifier") return b.name;
|
|
166
|
+
}
|
|
167
|
+
return void 0;
|
|
168
|
+
}
|
|
169
|
+
function isQueue(obj) {
|
|
170
|
+
if (obj.type === "Identifier") return queues.has(obj.name) || QUEUE_NAME.test(obj.name);
|
|
171
|
+
return obj.type === "MemberExpression" && !obj.computed && obj.property.type === "Identifier" && QUEUE_NAME.test(obj.property.name);
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
VariableDeclarator(node) {
|
|
175
|
+
if (node.id.type === "Identifier" && _optionalChain([node, 'access', _16 => _16.init, 'optionalAccess', _17 => _17.type]) === "NewExpression" && node.init.callee.type === "Identifier" && node.init.callee.name === "Queue") {
|
|
176
|
+
queues.add(node.id.name);
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
CallExpression(node) {
|
|
180
|
+
const callee = node.callee;
|
|
181
|
+
if (callee.type !== "MemberExpression" || callee.computed || callee.property.type !== "Identifier" || callee.property.name !== "add" || node.arguments.length < 2 || node.arguments.length > 3 || !isQueue(callee.object)) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const opts = node.arguments[2];
|
|
185
|
+
if (opts && opts.type !== "ObjectExpression") return;
|
|
186
|
+
if (_optionalChain([opts, 'optionalAccess', _18 => _18.type]) === "ObjectExpression" && opts.properties.some(
|
|
187
|
+
(p) => p.type === "SpreadElement" || p.key.type === "Identifier" && ["jobId", "deduplication"].includes(p.key.name)
|
|
188
|
+
)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
let fn = enclosingFunction(node);
|
|
192
|
+
let webhookFn;
|
|
193
|
+
while (fn) {
|
|
194
|
+
if (isWebhookFunction(fn)) {
|
|
195
|
+
webhookFn = fn;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
fn = enclosingFunction(fn);
|
|
199
|
+
}
|
|
200
|
+
if (!webhookFn) return;
|
|
201
|
+
const name = eventName(webhookFn);
|
|
202
|
+
const suggest = [];
|
|
203
|
+
if (name) {
|
|
204
|
+
const prop = `jobId: ${name}.id`;
|
|
205
|
+
suggest.push({
|
|
206
|
+
messageId: "addJobId",
|
|
207
|
+
data: { name },
|
|
208
|
+
fix: (fixer) => {
|
|
209
|
+
if (!opts)
|
|
210
|
+
return fixer.insertTextAfter(node.arguments[1], `, { ${prop} }`);
|
|
211
|
+
const obj = opts;
|
|
212
|
+
const last = obj.properties[obj.properties.length - 1];
|
|
213
|
+
return last ? fixer.insertTextAfter(last, `, ${prop}`) : fixer.replaceText(obj, `{ ${prop} }`);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
context.report({ node, messageId: "missing", suggest });
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var bullmq_require_job_id_default = rule;
|
|
223
|
+
|
|
224
|
+
// src/rules/require-idempotency.ts
|
|
225
|
+
var DEFAULT_HELPERS = /idempot|dedup|processed|alreadyHandled|seenEvent/i;
|
|
226
|
+
var rule2 = {
|
|
227
|
+
meta: {
|
|
228
|
+
type: "suggestion",
|
|
229
|
+
docs: {
|
|
230
|
+
description: "Require a duplicate-event check (using event.id) before a webhook handler performs side effects",
|
|
231
|
+
recommended: true,
|
|
232
|
+
url: docsUrl("require-idempotency")
|
|
233
|
+
},
|
|
234
|
+
schema: [
|
|
235
|
+
{
|
|
236
|
+
type: "object",
|
|
237
|
+
properties: { idempotencyFunctions: { type: "array", items: { type: "string" } } },
|
|
238
|
+
additionalProperties: false
|
|
239
|
+
}
|
|
240
|
+
],
|
|
241
|
+
messages: {
|
|
242
|
+
missing: "Stripe retries and duplicates webhook deliveries, but this handler never uses `{{name}}.id`. Record processed event ids (unique insert) and skip duplicates before side effects."
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
create(context) {
|
|
246
|
+
const helpers = new Set(
|
|
247
|
+
_nullishCoalesce(_optionalChain([context, 'access', _19 => _19.options, 'access', _20 => _20[0], 'optionalAccess', _21 => _21.idempotencyFunctions]), () => ( []))
|
|
248
|
+
);
|
|
249
|
+
return {
|
|
250
|
+
CallExpression(node) {
|
|
251
|
+
if (!isConstructEvent(node)) return;
|
|
252
|
+
const binding = eventBinding(node);
|
|
253
|
+
if (!binding) return;
|
|
254
|
+
if (binding.kind === "pattern") {
|
|
255
|
+
if (!binding.hasId)
|
|
256
|
+
context.report({ node, messageId: "missing", data: { name: "event" } });
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const fn = enclosingFunction(node);
|
|
260
|
+
if (!fn) return;
|
|
261
|
+
const name = binding.name;
|
|
262
|
+
let handled = false;
|
|
263
|
+
walk(fn, (n) => {
|
|
264
|
+
if (handled) return;
|
|
265
|
+
if (n.type === "MemberExpression" && n.object.type === "Identifier" && n.object.name === name && !n.computed && n.property.type === "Identifier" && n.property.name === "id") {
|
|
266
|
+
handled = true;
|
|
267
|
+
} else if (n.type === "ReturnStatement" && _optionalChain([n, 'access', _22 => _22.argument, 'optionalAccess', _23 => _23.type]) === "Identifier" && n.argument.name === name || n.type === "ArrowFunctionExpression" && n.body.type === "Identifier" && n.body.name === name) {
|
|
268
|
+
handled = true;
|
|
269
|
+
} else if (n.type === "CallExpression") {
|
|
270
|
+
const callee = calleeName(n);
|
|
271
|
+
if (callee && (helpers.has(callee) || DEFAULT_HELPERS.test(callee))) handled = true;
|
|
272
|
+
const isConsole = n.callee.type === "MemberExpression" && n.callee.object.type === "Identifier" && n.callee.object.name === "console";
|
|
273
|
+
if (!isConsole && n.arguments.some((a) => a.type === "Identifier" && a.name === name)) {
|
|
274
|
+
handled = true;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
if (!handled) context.report({ node, messageId: "missing", data: { name } });
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
var require_idempotency_default = rule2;
|
|
284
|
+
|
|
285
|
+
// src/rules/require-raw-body.ts
|
|
286
|
+
var rule3 = {
|
|
287
|
+
meta: {
|
|
288
|
+
type: "problem",
|
|
289
|
+
hasSuggestions: true,
|
|
290
|
+
docs: {
|
|
291
|
+
description: "Require the raw request body for Stripe signature verification (no JSON parsing before constructEvent)",
|
|
292
|
+
recommended: true,
|
|
293
|
+
url: docsUrl("require-raw-body")
|
|
294
|
+
},
|
|
295
|
+
schema: [],
|
|
296
|
+
messages: {
|
|
297
|
+
jsonMiddleware: "express.json() parses the body before the Stripe signature check, so constructEvent will always fail. Use express.raw() on this route.",
|
|
298
|
+
globalJson: "`{{owner}}.use(express.json())` on line {{line}} runs before this webhook route and parses the body, so constructEvent will fail. Register the webhook route first or give it express.raw().",
|
|
299
|
+
parsedJson: "The body is parsed with .json() before the Stripe signature check. Read the raw body with .text() and pass it to constructEvent.",
|
|
300
|
+
stringified: "JSON.stringify() of a parsed body is not byte-identical to what Stripe signed. Pass the raw body string or Buffer.",
|
|
301
|
+
useRaw: "Replace with express.raw({ type: 'application/json' })",
|
|
302
|
+
useText: "Read the body with .text() instead"
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
create(context) {
|
|
306
|
+
const globalParsers = [];
|
|
307
|
+
function handlerVerifies(fn) {
|
|
308
|
+
return findAll(fn, isConstructEvent).length > 0;
|
|
309
|
+
}
|
|
310
|
+
return {
|
|
311
|
+
CallExpression(node) {
|
|
312
|
+
const n = node;
|
|
313
|
+
const callee = n.callee;
|
|
314
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "use" && callee.object.type === "Identifier") {
|
|
315
|
+
const args = n.arguments;
|
|
316
|
+
const first = args[0];
|
|
317
|
+
const pathless = !(_optionalChain([first, 'optionalAccess', _24 => _24.type]) === "Literal" && first.value !== "/");
|
|
318
|
+
if (pathless && args.some(isJsonParser)) {
|
|
319
|
+
globalParsers.push({ owner: callee.object.name, node: n });
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const route = asRoute(n);
|
|
323
|
+
if (route && handlerVerifies(route.handler)) {
|
|
324
|
+
const inlineJson = route.middlewares.filter(isJsonParser);
|
|
325
|
+
for (const mw of inlineJson) {
|
|
326
|
+
context.report({
|
|
327
|
+
node: mw,
|
|
328
|
+
messageId: "jsonMiddleware",
|
|
329
|
+
suggest: [
|
|
330
|
+
{
|
|
331
|
+
messageId: "useRaw",
|
|
332
|
+
fix: (fixer) => fixer.replaceText(mw, "express.raw({ type: 'application/json' })")
|
|
333
|
+
}
|
|
334
|
+
]
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
if (!inlineJson.length && !route.middlewares.some(isRawParser)) {
|
|
338
|
+
const earlier = globalParsers.find(
|
|
339
|
+
(p) => p.owner === route.owner && (_nullishCoalesce(_optionalChain([p, 'access', _25 => _25.node, 'access', _26 => _26.range, 'optionalAccess', _27 => _27[0]]), () => ( 0))) < (_nullishCoalesce(_optionalChain([n, 'access', _28 => _28.range, 'optionalAccess', _29 => _29[0]]), () => ( 0)))
|
|
340
|
+
);
|
|
341
|
+
if (earlier) {
|
|
342
|
+
context.report({
|
|
343
|
+
node: n,
|
|
344
|
+
messageId: "globalJson",
|
|
345
|
+
data: { owner: earlier.owner, line: String(_optionalChain([earlier, 'access', _30 => _30.node, 'access', _31 => _31.loc, 'optionalAccess', _32 => _32.start, 'access', _33 => _33.line])) }
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (isConstructEvent(n)) {
|
|
351
|
+
const payload = n.arguments[0];
|
|
352
|
+
if (_optionalChain([payload, 'optionalAccess', _34 => _34.type]) === "CallExpression" && payload.callee.type === "MemberExpression" && payload.callee.object.type === "Identifier" && payload.callee.object.name === "JSON" && payload.callee.property.type === "Identifier" && payload.callee.property.name === "stringify") {
|
|
353
|
+
context.report({ node: payload, messageId: "stringified" });
|
|
354
|
+
}
|
|
355
|
+
const fn = enclosingFunction(n);
|
|
356
|
+
if (!fn) return;
|
|
357
|
+
const params = new Set(
|
|
358
|
+
fn.params.flatMap((p) => p.type === "Identifier" ? [p.name] : [])
|
|
359
|
+
);
|
|
360
|
+
const isRequest = (obj) => obj.type === "Identifier" && params.has(obj.name) || obj.type === "MemberExpression" && obj.object.type === "Identifier" && params.has(obj.object.name) && obj.property.type === "Identifier" && obj.property.name === "req";
|
|
361
|
+
walkSameFunction(fn, (c) => {
|
|
362
|
+
if (c.type === "CallExpression" && c.arguments.length === 0 && c.callee.type === "MemberExpression" && !c.callee.computed && c.callee.property.type === "Identifier" && c.callee.property.name === "json" && isRequest(c.callee.object) && (_nullishCoalesce(_optionalChain([c, 'access', _35 => _35.range, 'optionalAccess', _36 => _36[0]]), () => ( 0))) < (_nullishCoalesce(_optionalChain([n, 'access', _37 => _37.range, 'optionalAccess', _38 => _38[0]]), () => ( 0)))) {
|
|
363
|
+
const prop = c.callee.property;
|
|
364
|
+
context.report({
|
|
365
|
+
node: c,
|
|
366
|
+
messageId: "parsedJson",
|
|
367
|
+
suggest: [
|
|
368
|
+
{ messageId: "useText", fix: (fixer) => fixer.replaceText(prop, "text") }
|
|
369
|
+
]
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
function walkSameFunction(fn, visit) {
|
|
379
|
+
const body = fn.body;
|
|
380
|
+
const stack = [body];
|
|
381
|
+
while (stack.length) {
|
|
382
|
+
const node = stack.pop();
|
|
383
|
+
visit(node);
|
|
384
|
+
for (const key of Object.keys(node)) {
|
|
385
|
+
if (key === "parent") continue;
|
|
386
|
+
const value = node[key];
|
|
387
|
+
for (const child of Array.isArray(value) ? value : [value]) {
|
|
388
|
+
if (child && typeof child === "object" && typeof child.type === "string") {
|
|
389
|
+
if (!isFunction(child)) stack.push(child);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
var require_raw_body_default = rule3;
|
|
396
|
+
|
|
397
|
+
// src/rules/require-signature-verification.ts
|
|
398
|
+
var DEFAULT_VERIFY = /verif|signature|constructEvent|timingSafeEqual|unwrap/i;
|
|
399
|
+
var AUTH_CALL = /auth|session|apiKey|permission|currentUser|requireUser/i;
|
|
400
|
+
var rule4 = {
|
|
401
|
+
meta: {
|
|
402
|
+
type: "problem",
|
|
403
|
+
docs: {
|
|
404
|
+
description: "Require webhook handlers to verify the request signature",
|
|
405
|
+
recommended: true,
|
|
406
|
+
url: docsUrl("require-signature-verification")
|
|
407
|
+
},
|
|
408
|
+
schema: [
|
|
409
|
+
{
|
|
410
|
+
type: "object",
|
|
411
|
+
properties: { verifyFunctions: { type: "array", items: { type: "string" } } },
|
|
412
|
+
additionalProperties: false
|
|
413
|
+
}
|
|
414
|
+
],
|
|
415
|
+
messages: {
|
|
416
|
+
missing: "This webhook handler never verifies the request signature, so anyone can forge events. Call stripe.webhooks.constructEvent() (or your provider\u2019s verify function) before trusting the body."
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
create(context) {
|
|
420
|
+
const extra = new Set(
|
|
421
|
+
_nullishCoalesce(_optionalChain([context, 'access', _39 => _39.options, 'access', _40 => _40[0], 'optionalAccess', _41 => _41.verifyFunctions]), () => ( []))
|
|
422
|
+
);
|
|
423
|
+
const webhookFile = isWebhookFile(context);
|
|
424
|
+
function verifies(fn) {
|
|
425
|
+
let found = false;
|
|
426
|
+
walk(fn, (n) => {
|
|
427
|
+
if (found || n.type !== "CallExpression" && n.type !== "NewExpression") return;
|
|
428
|
+
const name = calleeName(n);
|
|
429
|
+
if (name && (extra.has(name) || DEFAULT_VERIFY.test(name) || AUTH_CALL.test(name)))
|
|
430
|
+
found = true;
|
|
431
|
+
});
|
|
432
|
+
return found;
|
|
433
|
+
}
|
|
434
|
+
function check(fn) {
|
|
435
|
+
if (!verifies(fn)) context.report({ node: fn, messageId: "missing" });
|
|
436
|
+
}
|
|
437
|
+
return {
|
|
438
|
+
CallExpression(node) {
|
|
439
|
+
const route = asRoute(node);
|
|
440
|
+
if (!route || !WEBHOOK_PATH.test(route.path)) return;
|
|
441
|
+
if (route.middlewares.some((m) => !isBodyParser(m))) return;
|
|
442
|
+
check(route.handler);
|
|
443
|
+
},
|
|
444
|
+
"FunctionDeclaration, FunctionExpression, ArrowFunctionExpression"(node) {
|
|
445
|
+
if (webhookFile && isNextPostHandler(node)) check(node);
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
var require_signature_verification_default = rule4;
|
|
451
|
+
|
|
452
|
+
// src/index.ts
|
|
453
|
+
var rules = {
|
|
454
|
+
"require-raw-body": require_raw_body_default,
|
|
455
|
+
"require-signature-verification": require_signature_verification_default,
|
|
456
|
+
"require-idempotency": require_idempotency_default,
|
|
457
|
+
"bullmq-require-job-id": bullmq_require_job_id_default
|
|
458
|
+
};
|
|
459
|
+
var recommendedRules = {
|
|
460
|
+
"webhook-safety/require-raw-body": "error",
|
|
461
|
+
"webhook-safety/require-signature-verification": "error",
|
|
462
|
+
"webhook-safety/require-idempotency": "warn",
|
|
463
|
+
"webhook-safety/bullmq-require-job-id": "warn"
|
|
464
|
+
};
|
|
465
|
+
var strictRules = Object.fromEntries(
|
|
466
|
+
Object.keys(recommendedRules).map((k) => [k, "error"])
|
|
467
|
+
);
|
|
468
|
+
var plugin = {
|
|
469
|
+
meta: { name: "eslint-plugin-webhook-safety", version: "0.1.0" },
|
|
470
|
+
rules,
|
|
471
|
+
configs: {}
|
|
472
|
+
};
|
|
473
|
+
plugin.configs.recommended = {
|
|
474
|
+
name: "webhook-safety/recommended",
|
|
475
|
+
plugins: { "webhook-safety": plugin },
|
|
476
|
+
rules: recommendedRules
|
|
477
|
+
};
|
|
478
|
+
plugin.configs.strict = {
|
|
479
|
+
name: "webhook-safety/strict",
|
|
480
|
+
plugins: { "webhook-safety": plugin },
|
|
481
|
+
rules: strictRules
|
|
482
|
+
};
|
|
483
|
+
plugin.configs["legacy-recommended"] = { plugins: ["webhook-safety"], rules: recommendedRules };
|
|
484
|
+
plugin.configs["legacy-strict"] = { plugins: ["webhook-safety"], rules: strictRules };
|
|
485
|
+
var index_default = plugin;
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
exports.default = index_default;
|
|
489
|
+
|
|
490
|
+
module.exports = exports.default;
|
|
491
|
+
//# sourceMappingURL=index.cjs.map
|