Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/src/routes/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const storage = require('../storage');

const router = express.Router();

// Maximum age of a webhook event before it is rejected as a potential replay.
const WEBHOOK_EVENT_MAX_AGE_SECONDS = 300;

async function getCustomerEmail(customerId) {
const customer = await stripe.customers.retrieve(customerId);
return customer.email ? customer.email.toLowerCase() : null;
Expand All @@ -33,6 +36,14 @@ router.post('/stripe', async (req, res) => {
return res.status(400).json({ error: 'Invalid signature' });
}

// Reject stale events to mitigate replay attacks (an intercepted, validly
// signed event replayed later than when Stripe originally sent it).
const eventAgeSeconds = Math.floor(Date.now() / 1000) - event.created;
if (eventAgeSeconds > WEBHOOK_EVENT_MAX_AGE_SECONDS) {
console.error(`[webhook] Rejected stale event ${event.id} (age: ${eventAgeSeconds}s)`);
return res.status(400).json({ error: 'Event too old' });
}

// Reject events whose product isn't in our allowlist. Protects against
// cross-product bleed when multiple apps share a Stripe account.
try {
Expand Down