fix(auth): require a same-origin call on the loopback bridge callback#1257
Draft
deepme987 wants to merge 1 commit into
Draft
fix(auth): require a same-origin call on the loopback bridge callback#1257deepme987 wants to merge 1 commit into
deepme987 wants to merge 1 commit into
Conversation
The bridge's POST /callback accepts a signed-in Firebase user and injects
it into the app session, but its only check was that the request arrived on
the loopback socket. Every page in every browser on the machine also reaches
us from 127.0.0.1, so that check cannot distinguish our own bridge page from
any other page the user happens to have open. The handler read no Origin, no
Sec-Fetch-Site, and did not enforce a content-type.
Require positive proof the caller is the page we served — a same-origin JSON
fetch, which is exactly what the bridge page already sends:
- Content-Type: application/json. This is the load-bearing check. A
cross-site caller can only reach a loopback server without a CORS
preflight by staying inside the "simple" content-types (text/plain,
form-encoded, multipart). We serve no OPTIONS handler, so demanding JSON
forces a preflight that cannot succeed.
- Sec-Fetch-Site must be same-origin when present (a forbidden header — a
page cannot forge it).
- Origin must be our own loopback origin when present.
Fails closed when neither Origin nor Sec-Fetch-Site attests same-origin.
Tests drive the real server over raw node:http (undici strips Origin, so
fetch cannot express the case): a cross-site form POST, a JSON POST from a
foreign origin, and a POST with neither header are all rejected 403 and
complete no sign-in; the genuine same-origin call still returns 204 and
resolves. Removing the guard fails all three.
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The loopback sign-in bridge's
POST /callbackaccepts a signed-in Firebase user and injects it into the app session. Its only validation was that the request arrived on the loopback socket.That check can't do the job it looks like it's doing: every page in every browser on the machine also reaches the server from
127.0.0.1. So it cannot distinguish the bridge page we serve from any other page the user happens to have open. The handler read noOrigin, noSec-Fetch-Site, and never enforced a content-type —readJsonBodyparses the body regardless.This makes the endpoint require positive proof that the caller is the page we served: a same-origin JSON fetch, which is exactly what the bridge page already sends.
The checks
Content-Type: application/json— the load-bearing one. A cross-site caller can only reach a loopback server without a CORS preflight by staying inside the "simple" content-types (text/plain, form-encoded, multipart). We serve noOPTIONShandler, so demanding JSON forces a preflight that cannot succeed.Sec-Fetch-Site: same-originwhen present — a forbidden header, so a page cannot forge it.Originmust be our own loopback origin when present.Fails closed when neither
OriginnorSec-Fetch-Siteattests same-origin.No behaviour change for the real flow: the bridge page's
fetch('/callback', {method:'POST', headers:{'Content-Type':'application/json'}})already satisfies all three.Testing
Tests drive the real server over raw
node:http— undici stripsOrigin, sofetchcan't express these cases:text/plain)OriginnorSec-Fetch-SiteEach asserts the sign-in flow did not complete, not just the status code. Removing the guard fails all three. Full
src/main/authsuite green (41), typecheck + lint clean.Notes
Found while reviewing #1222. Independent of that PR — this hardens the existing bridge, which #1222 keeps as its fallback path.