eslint-plugin-webhook-safety

ESLint rules for Stripe webhook handlers: raw body, signature checks, idempotenc

This is a fork of Krrishkumar125/eslint-plugin-webhook-safety, kept for reference and contributions. It is not an original project.

eslint-plugin-webhook-safety

CI
npm
downloads

ESLint rules that catch the Stripe webhook bugs that only show up in production:

The static half of webhook-crash-lab.
This plugin finds the bug in your editor, and crash-lab proves the fix against a running server.

60-second start

npm i -D eslint eslint-plugin-webhook-safety
// eslint.config.js
import webhookSafety from 'eslint-plugin-webhook-safety';

export default [
  webhookSafety.configs.recommended, // or .strict
];
// server.js
app.use(express.json());
app.post('/webhooks/stripe', async (req, res) => {
  const event = stripe.webhooks.constructEvent(req.body, sig, secret);
  await emailQueue.add('receipt', { to: event.data.object.customer_email });
});
$ npx eslint server.js
  2:1  error    `app.use(express.json())` on line 1 runs before this webhook route and parses the body ...  webhook-safety/require-raw-body
  4:9  warning  Consider a duplicate-event check: Stripe can deliver `event` more than once ...                 webhook-safety/require-idempotency
  4:9  warning  Webhook deliveries are retried, so this queue.add() will enqueue the same work twice ...      webhook-safety/bullmq-require-job-id

Rules

Rule What it catches recommended strict 💡
require-raw-body express.json() / req.json() / JSON.stringify before constructEvent error error ✅
require-signature-verification webhook route or Next.js POST with no signature check error error
require-idempotency DB/queue/email/payment write in the handler with no event.id dedupe warn error
bullmq-require-job-id queue.add() in a webhook handler without jobId warn error ✅

The rules are tuned for low false positives. When in doubt (custom middleware, the event passed to a
helper, options passed as a variable) they stay quiet. Each rule doc lists exactly what triggers it.

Supports Express, Hono, Fastify-style routers and the Next.js App Router, in JavaScript and TypeScript.

TypeScript

ESLint only lints .ts files that some config object matches, so pair the plugin with
typescript-eslint (or at least its parser):

// eslint.config.js
import tseslint from 'typescript-eslint';
import webhookSafety from 'eslint-plugin-webhook-safety';

export default [...tseslint.configs.recommended, webhookSafety.configs.recommended];

Legacy .eslintrc (ESLint 8)

{ "extends": ["plugin:webhook-safety/legacy-recommended"] }

Requirements

ESLint 8.57+, 9 or 10 (flat config, or .eslintrc via the legacy configs) and Node ≥ 20. Every release is
smoke-tested from the packed tarball against ESLint 8.57 (flat and legacy), 9, 10 and @typescript-eslint/parser.

Sibling packages

Other tools by the same author:

License

MIT © Krrish Kumar