Repository navigation
Api logic building #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,36 +2,72 @@ import crypto from 'crypto'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { ALLOWED_ORIGINS } from '@constants/globalConts.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { UnauthorizedError } from '@middleware/error/index.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { NextFunction, Request, Response } from 'express'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { compareHashTokens } from '@utils/tokens.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import logger from '@utils/logger.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const csrfMiddleware = (req: Request, _res: Response, next: NextFunction) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const unsafeMethods = ['POST', 'PUT', 'PATCH', 'DELETE']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!unsafeMethods.includes(req.method)) return next(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const unsafeMethods = ['POST', 'PUT', 'PATCH', 'DELETE']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const normalizeToken = (val: string): string => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return decodeURIComponent(val).replace(/^"|"$/g, '').trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return val.replace(/^"|"$/g, '').trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const origin = req.headers.origin || req.headers.referer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!origin) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new UnauthorizedError('Missing Origin or Referer header'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const csrfMiddleware = (req: Request, _res: Response, next: NextFunction) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!unsafeMethods.includes(req.method)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return next(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const originUrl = new URL(origin as string).origin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!ALLOWED_ORIGINS.includes(originUrl)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new UnauthorizedError('Invalid origin'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+42
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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')); |
Copilot
AI
Apr 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
safeCompareduplicates the existingcompareHashTokenshelper in@utils/tokens.js(both do a length check andcrypto.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).