The tell: login guard, id-only lookup
builds on: Trust boundaries and untrusted input, What the agent cannot see
Tell: the route has `requireLogin`, and the query filters on `req.params.id` alone. Nothing ties the invoice to `req.user`. BETTER: `where: { id: req.params.id, orgId: req.user.orgId }`. Another org's id then finds nothing and gets 404 rather than 403, which also hides that the invoice exists. Later cost: anyone with an account can walk the ids and download every invoice, with no error and nothing odd in the logs.
// TypeScript
--- a/src/routes/exports.ts
+++ b/src/routes/exports.ts
@@ -12,0 +13,8 @@
+r.get("/invoices/:id/pdf", requireLogin,
+ async (req, res) => {
+ const inv = await db.invoice.findFirst({
+ where: { id: req.params.id },
+ });
+ if (!inv) return res.sendStatus(404);
+ res.type("pdf").send(await toPdf(inv));
+ });