Repository navigation
fix(api): return 403 instead of 500 for a malformed API key - #468
Conversation
Co-authored-by: polylane[bot] <277585245+polylane[bot]@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedBot user detected. To trigger a single review, invoke the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Deploying usesend with
|
| Latest commit: |
4bc0166
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://f5292bd1.usesend.pages.dev |
| Branch Preview URL: | https://polylane-autofix-1va4dcqf9oc.usesend.pages.dev |
| if (!clientId || !token) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
This branch replaces two error lines per malformed request with silence; one structured line keeps that traffic countable.
| if (!clientId || !token) { | |
| return null; | |
| } | |
| if (!clientId || !token) { | |
| logger.info( | |
| { hasClientId: Boolean(clientId), hasToken: Boolean(token) }, | |
| "Rejected API token outside the useSend key format", | |
| ); | |
| return null; | |
| } |
|
Tip Expected to resolve the linked issue. Linked issue: useSend public API returns HTTP 500 for a malformed Bearer token instead of 401 I read unsend in production. Each of the 708 Also considered · 3 refuted
Analysed against 1 Service and 1 repository
Polylane analysed Did this help? React 👍 or 👎 so the next review is sharper. |
Fixes: useSend public API returns HTTP 500 for a malformed Bearer token instead of 401
The public API replied with an internal server error when a caller sent a Bearer token that is not in the useSend key format, and logged each one as an internal failure. One automated client sends those requests about once a minute, roughly 1,270 a day. The malformed value now gets the 403 rejection that an unknown key already gets, and no longer raises an internal error.
Detection evidence
Affected:
acc_13ee50413001wypxc1ww20ftWhy this fix
Production telemetry pairs each 500 with the same Prisma validation error within milliseconds, and the invocation dump shows the lookup running with an undefined client id, so every observed event is this one defect. The same client polls about once every 68 seconds, roughly 1,270 requests a day, continuous since at least 17 September, so the earlier count of 201 was a query-limit floor. Keys the product generates always have three non-empty parts, so no working key is affected. The caller's credential remains a separate config item outside this repository, and it now receives a 403 instead of a 500.
Fix walkthrough
Watch the fix explained (2:27). Recorded for commit
4bc016625b3d76a424c4ecf8434af347036ea58a.Out of scope
Causal chain
apiKey.split('_')yields no second element when the token has no underscore, soclientIdis undefined;db.apiKey.findUnique({ where: { clientId: undefined } })has neitheridnorclientId, so Prisma raises a validation error before the query, which escapes the function because its try/catch starts after the call.Authorization: Bearer <value>where the value contains no underscore.findUniquewithclientIdfromapiKey.split('_').3 files changed (+160/-1)
apps/web/src/server/public-api/auth.api.test.ts: added, +81/-0apps/web/src/server/service/api-service.ts: modified, +5/-1apps/web/src/server/service/api-service.unit.test.ts: added, +74/-0Generated by Polylane. You can ask follow-ups by mentioning @polylane in a comment.