Fix Helmet's default no-referrer policy breaking /api/gmaps/key - #32
Conversation
Helmet defaults to 'Referrer-Policy: no-referrer', which strips the
Referer header from every outgoing request the page makes — including
the same-origin fetch('/api/gmaps/key') call. Combined with browsers
not reliably sending an Origin header on simple same-origin GET fetches,
the backend's same-origin check in weatherProxyController.js always had
nothing to compare against, so it 403'd unconditionally — even from the
real page, regardless of BASE_URL correctness.
Set referrerPolicy to 'strict-origin-when-cross-origin' (the browser's
own default absent any CSP framework), which sends the full Referer on
same-origin requests while still only sending the bare origin
cross-origin. Verified locally: a same-origin Referer now correctly
authorizes the request, while no Referer/Origin still correctly 403s.
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 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 |
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_4cf631e7-bb06-4f84-a923-27c41727ec2f) |
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Root cause
The user reported
GET /api/gmaps/keyreturning403 {"error":"Forbidden"}even when loaded from the real deployed page (confirmed via DevTools Network tab), not just via direct URL navigation. TheirBASE_URLwas already correctly configured.The actual bug: Helmet defaults to
Referrer-Policy: no-referrerwhen not explicitly configured. This tells the browser to omit theRefererheader on every outgoing request from the page — including the same-originfetch('/api/gmaps/key')call inapp.js. Combined with browsers not reliably sending anOriginheader on simple same-originGETfetches,weatherProxyController.js's same-origin check had nothing to compare against and always returned 403 — completely independent of whetherBASE_URLwas correctly set (fixed in #30) or a scheme was present.This explains why the 403 persisted after the #30 fix: that PR fixed the comparison logic, but the browser was never sending the header being compared in the first place.
Fix
Set
referrerPolicy: { policy: 'strict-origin-when-cross-origin' }in the Helmet config — this is the browser's own default in the absence of any policy, and sends the fullRefereron same-origin requests while still only sending the bare origin cross-origin (no privacy regression for third-party sites).Verification
Ran the app locally with
BASE_URL=slackbot-weather-production.up.railway.app(matching the user's real value):Referer: https://slackbot-weather-production.up.railway.app/→200 {"key":"test123"}✅ (previously 403)Referer/Originat all → still403 {"error":"Forbidden"}✅ (security boundary intact)Test plan
npm test— 119/119 passingGenerated by Claude Code
Note
Low Risk
Single Helmet header tweak aligned with common browser defaults; tightens referrers only relative to no-referrer while preserving the existing 403 when neither Origin nor Referer is sent.
Overview
Helmet was sending
Referrer-Policy: no-referrerby default, so browsers droppedRefereron same-originfetch('/api/gmaps/key'). The endpoint’s same-origin guard often has noOriginon simple GETs and falls back toReferer, so legitimate page loads kept returning 403 even withBASE_URLconfigured correctly.The Helmet config now sets
referrerPolicy: { policy: 'strict-origin-when-cross-origin' }, restoring full referrers on same-origin requests while keeping origin-only referrers cross-origin. Requests with neither header should still be rejected.Reviewed by Cursor Bugbot for commit c53fa2b. Configure here.
Summary by cubic
Fixes 403 responses from /api/gmaps/key by setting
helmet’s referrer policy to allow the Referer header on same-origin requests. Restores the same-origin check without reducing cross-origin privacy.referrerPolicy: { policy: 'strict-origin-when-cross-origin' }inhelmet.Written for commit c53fa2b. Summary will update on new commits.