Skip to content

fix(auth): enable cross-site cookies and add CSRF protection - #17

Merged
davidraehles merged 2 commits into
fix-cross-site-auth-8949512291347004855from
claude/address-pr-review-comments-HXXTV
Feb 22, 2026
Merged

fix(auth): enable cross-site cookies and add CSRF protection#17
davidraehles merged 2 commits into
fix-cross-site-auth-8949512291347004855from
claude/address-pr-review-comments-HXXTV

Conversation

@davidraehles

Copy link
Copy Markdown
Collaborator

Address copilot bot review comments by implementing:

  • Enable SameSite=None cookies in production for cross-site auth (Vercel frontend with Railway backend)
  • Add CSRF validation using Origin/Referer header checking against ALLOWED_ORIGINS
  • Apply CSRF validation to register, login, and logout endpoints
  • Add trust proxy configuration to support deployments behind reverse proxies
  • Standardize on npm as package manager with packageManager field in package.json

Addresses concerns about CSRF vulnerability when using SameSite=None cookies by implementing explicit origin validation for state-changing endpoints.

https://claude.ai/code/session_01CEvBbvirScmX9qZbzt9NhV

Address copilot bot review comments by implementing:
- Enable SameSite=None cookies in production for cross-site auth (Vercel frontend with Railway backend)
- Add CSRF validation using Origin/Referer header checking against ALLOWED_ORIGINS
- Apply CSRF validation to register, login, and logout endpoints
- Add trust proxy configuration to support deployments behind reverse proxies
- Standardize on npm as package manager with packageManager field in package.json
Addresses concerns about CSRF vulnerability when using SameSite=None cookies
by implementing explicit origin validation for state-changing endpoints.
https://claude.ai/code/session_01CEvBbvirScmX9qZbzt9NhV
CopilotAI review requested due to automatic review settings February 22, 2026 09:16
@davidraehles
davidraehles merged commit b4c4cb2 into fix-cross-site-auth-8949512291347004855Feb 22, 2026

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates backend authentication to support cross-site cookie-based auth (e.g., Vercel ↔ Railway) by switching to SameSite=None in production and adding an Origin/Referer-based CSRF check on auth endpoints. It also adjusts Express proxy settings and pins the backend package manager.

Changes:

  • Enable SameSite=None cookies in production (and lax in development) for auth token cookies.
  • Add Origin/Referer allowlist validation for CSRF protection on /register, /login, and /logout.
  • Configure Express trust proxy and add packageManager metadata to backend package.json.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

FileDescription
backend/src/server.tsEnables trust proxy for reverse-proxy deployments; keeps CORS allowlist-based configuration.
backend/src/routes/auth.tsAdds Origin/Referer validation and updates cookie sameSite behavior for cross-site auth.
backend/package.jsonAdds packageManager field to standardize tooling metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +13 to +16
// Get allowed origins from environment
const allowedOrigins = process.env.ALLOWED_ORIGINS
? process.env.ALLOWED_ORIGINS.split(',').map(origin => origin.trim()).filter(origin => origin.length > 0)
: ['http://localhost:5173', 'http://localhost:3000'];

CopilotAIFeb 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowedOrigins is re-parsed here even though the server already builds the same list for CORS. Duplicating this parsing in multiple places risks the CORS allowlist and CSRF allowlist diverging over time; consider centralizing origin allowlist parsing in a shared module and importing it from both server and routes.

Copilot uses AI. Check for mistakes.
Comment on lines +21 to +22
function validateOrigin(req: express.Request, res: express.Response): boolean {
const origin = req.get('origin');

CopilotAIFeb 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validateOrigin takes res but doesn't use it. Dropping the unused parameter will make the function signature clearer and avoid implying that the response is modified during validation.

Copilot uses AI. Check for mistakes.
Comment on lines +25 to +33
// Check if origin header is present and in allowed list
if (origin) {
return allowedOrigins.some(allowed => origin === allowed || origin.startsWith(allowed));
}

// Fallback to referer header if origin is not present
if (referer) {
return allowedOrigins.some(allowed => referer.startsWith(allowed));
}

CopilotAIFeb 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validateOrigin uses origin.startsWith(allowed) / referer.startsWith(allowed) for allowlisting. This is bypassable (e.g., https://good.com.evil.com starts with https://good.com, and differing ports can also match). Parse the header as a URL and compare the normalized .origin against an exact allowlist (or implement explicit, boundary-checked wildcard subdomain matching if needed).

Copilot uses AI. Check for mistakes.
Comment on lines +47 to +50
// CSRF protection: Validate origin
if (!validateOrigin(req, res)) {
return res.status(403).json({ message: 'CSRF validation failed' });
}

CopilotAIFeb 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSRF validation is only applied to /register, /login, and /logout. With SameSite=None cookies in production, any cookie-authenticated state-changing endpoints (e.g., /api/notes POST/PUT/DELETE) remain CSRFable unless they also enforce an origin check or a CSRF token. Consider extracting this into middleware and applying it to all non-idempotent routes that accept cookie auth.

Copilot uses AI. Check for mistakes.
@davidraehles
davidraehles deleted the claude/address-pr-review-comments-HXXTV branch February 22, 2026 10:57
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@davidraehles@claude