Repository navigation
Api logic building - #23
Conversation
…into API-LogicBuilding
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 0 minutes and 13 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Pull request overview
Updates the Express CSRF middleware to add token normalization, safer parsing of Origin/Referer, and additional logging to diagnose mismatches for API requests guarded by csrfMiddleware.
Changes:
- Refactors unsafe-method detection into a shared constant and introduces token normalization before comparison.
- Replaces the prior CSRF token comparison helper with a local
timingSafeEqual-based compare. - Adds origin parsing/validation handling and warning logs on CSRF validation failures.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const rawOrigin = req.headers.origin || req.headers.referer; | ||
|
|
||
| if (rawOrigin) { | ||
| try { | ||
| const originUrl = new URL(rawOrigin as string).origin; | ||
|
|
||
| if (!ALLOWED_ORIGINS.includes(originUrl)) { | ||
| return next(new UnauthorizedError('Invalid origin')); | ||
| } | ||
| } catch { | ||
| return next(new UnauthorizedError('Invalid origin format')); | ||
| } |
There was a problem hiding this comment.
The middleware no longer rejects unsafe requests when both Origin and Referer headers are missing (it simply skips origin validation if rawOrigin is falsy). This is a security regression compared to the previous behavior and weakens CSRF defenses for user-agents/clients that omit these headers. Consider restoring a hard-fail for unsafe methods when neither header is present (or making the exception explicit and gated by trusted internal clients).
| const rawOrigin = req.headers.origin || req.headers.referer; | |
| if (rawOrigin) { | |
| try { | |
| const originUrl = new URL(rawOrigin as string).origin; | |
| if (!ALLOWED_ORIGINS.includes(originUrl)) { | |
| return next(new UnauthorizedError('Invalid origin')); | |
| } | |
| } catch { | |
| return next(new UnauthorizedError('Invalid origin format')); | |
| } | |
| const originHeader = Array.isArray(req.headers.origin) ? req.headers.origin[0] : req.headers.origin; | |
| const refererHeader = Array.isArray(req.headers.referer) ? req.headers.referer[0] : req.headers.referer; | |
| const rawOrigin = originHeader || refererHeader; | |
| if (!rawOrigin) { | |
| return next(new UnauthorizedError('Origin or Referer header required')); | |
| } | |
| try { | |
| const originUrl = new URL(rawOrigin).origin; | |
| if (!ALLOWED_ORIGINS.includes(originUrl)) { | |
| return next(new UnauthorizedError('Invalid origin')); | |
| } | |
| } catch { | |
| return next(new UnauthorizedError('Invalid origin format')); |
| if (!isValid) { | ||
| logger.warn(`'CSRF DEBUG', | ||
| header: ${csrfHeader}, | ||
| cookie: ${csrfCookieRaw}, | ||
| normalizedHeader: ${csrfToken}, | ||
| normalizedCookie: ${csrfCookie}, | ||
| headerLength: ${csrfToken.length}, | ||
| cookieLength: ${csrfCookie.length}, | ||
| `); |
There was a problem hiding this comment.
logger.warn logs raw and normalized CSRF token values from both the header and cookie. CSRF tokens are secrets and can be replayed; writing them to logs is a sensitive data exposure risk (and warn level is likely to be retained in production). Remove token values from logs (at most log non-sensitive metadata like lengths or a one-way hash) and ensure any debug logging is gated behind a non-production flag and uses an appropriate log level.
| const safeCompare = (a: string, b: string): boolean => { | ||
| const aBuf = Buffer.from(a); | ||
| const bBuf = Buffer.from(b); | ||
|
|
||
| if (aBuf.length !== bBuf.length) return false; | ||
|
|
||
| return crypto.timingSafeEqual(aBuf, bBuf); | ||
| }; |
There was a problem hiding this comment.
safeCompare duplicates the existing compareHashTokens helper in @utils/tokens.js (both do a length check and crypto.timingSafeEqual). Duplicating this logic increases maintenance cost and leaves the exported util unused after this change. Prefer reusing the shared helper (or moving/renaming so there is a single canonical implementation).
No description provided.