Make /api/gmaps/key origin check tolerant of scheme-less BASE_URL - #30
Conversation
The Origin/Referer check previously used raw string prefix matching against BASE_URL, so a BASE_URL set without a scheme (e.g. 'example.up.railway.app' instead of 'https://example.up.railway.app') would never match a browser's 'https://example.up.railway.app' Origin header, causing every request to 403 — this is exactly what happened on the live Railway deployment. Now parses both BASE_URL and the incoming Origin/Referer into hostnames via the URL constructor (defaulting to https:// when no scheme is present) and compares hostnames instead of raw strings, so scheme mismatches and trailing slashes no longer break the check. Added backend/__tests__/controllers/weatherProxyController.test.js covering: no key configured, no BASE_URL (dev mode), matching origin, scheme-less BASE_URL, Referer fallback, trailing slash, and 403 cases.
|
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_ff9b37f1-9bed-4626-bf49-fc3d68f69d44) |
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Summary
Root-caused the 403 on
GET /api/gmaps/keyon the live Railway deployment:BASE_URLwas set toslackbot-weather-production.up.railway.app(nohttps://scheme). The old check did raw string prefix matching:A browser's
Origin: https://slackbot-weather-production.up.railway.appheader does not start with the bare hostnameslackbot-weather-production.up.railway.app, so every request was rejected — the map page couldn't load a key at all.Fix
weatherProxyController.jsnow parses bothBASE_URLand the incomingOrigin/Refererinto hostnames via theURLconstructor (defaulting tohttps://when no scheme is present) and compares hostnames instead of raw strings. This makes the check tolerant of:BASE_URL(the actual bug hit in production)BASE_URLTests
Added
backend/__tests__/controllers/weatherProxyController.test.js(8 new tests) covering: no key configured, noBASE_URL(dev mode passthrough), matching origin, scheme-lessBASE_URL(regression test for the exact production bug), Referer fallback, trailing slash, and both 403 cases (mismatched host, missing headers).Test plan
npm test— 119/119 passing (111 existing + 8 new)BASE_URL=slackbot-weather-production.up.railway.app(no scheme, matching the real deployment's misconfigured value) and confirmedcurl -H "Origin: https://slackbot-weather-production.up.railway.app"now returns 200 with the key, while a mismatched origin still returns 403Note
The user should still fix their Railway
BASE_URLenv var to include thehttps://scheme going forward (also used byslackService.js's "View Live Map" Slack link) — this PR makes the code resilient to that misconfiguration but doesn't replace fixing the env var.Generated by Claude Code
Note
Medium Risk
Touches access control for exposing the Google Maps API key; behavior is tightened to hostname equality but is slightly more permissive for scheme-less BASE_URL misconfiguration.
Overview
Fixes 403 on
GET /api/gmaps/keywhenBASE_URLis set without a scheme (e.g. bare Railway hostname), which broke prefix matching against browserOrigin: https://...headers.getGoogleMapsKeynow normalizesBASE_URL,Origin, andRefererto lowercase hostnames (defaulting missing schemes tohttps://) instead ofstartsWithon raw strings. Same-origin still uses Origin or Referer; dev mode with noBASE_URLis unchanged.Adds
weatherProxyController.test.jswith eight cases: missing key, dev passthrough, matching origin, scheme-lessBASE_URL, Referer fallback, trailing slash, and two 403 paths.Reviewed by Cursor Bugbot for commit b001116. Configure here.
Summary by cubic
Fixes 403s on GET /api/gmaps/key by comparing hostnames instead of raw strings, making the origin check tolerant of a scheme-less BASE_URL. Restores key loading on the Railway deployment.
Written for commit b001116. Summary will update on new commits.