fix(auth): send an https redirect_uri so GitHub sign-in works

The OAuth app registers https://gh-stats.com/api/auth/github/callback, but
the app was sending the http:// form. GitHub compares redirect_uri to the
registered callback character for character, so it refused every attempt
with "The redirect_uri is not associated with this application" and showed
users a warning that the app might be redirecting them somewhere they were
not expecting. Sign-in has been failing for everyone, and it gates themes,
custom tags and achievements.

_derived_redirect_uri built the URL from X-Forwarded-Proto, falling back to
request.scheme. The cloudflared tunnel does not reliably forward that
header, so the scheme resolved to http. The comment above it claimed the
headers are set, which is what hid this.

Anything that is not localhost or 127.0.0.1 is https. Local dev keeps
working over plain http, pinned by a test.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BwMoSbmK77Ph7XaV5YnAdS
This commit is contained in:
shay
2026-08-23 09:38:26 -07:00
co-authored by Claude Opus 5
parent 12f6890f57
commit 107273a1c8
2 changed files with 31 additions and 1 deletions
+23
View File
@@ -1002,3 +1002,26 @@ def test_get_user_data_records_request_event(client, monkeypatch):
and r["endpoint"] == "/api/<u>/data"
for r in rows
)
def test_oauth_redirect_uri_is_https_in_production(client, monkeypatch):
"""GitHub matches redirect_uri against the registered callback character for
character. The tunnel can leave X-Forwarded-Proto unset, which made the app
send http:// and GitHub reject every sign-in with "The redirect_uri is not
associated with this application". Only a local dev host may be plaintext."""
monkeypatch.setattr(cfg, "GITHUB_OAUTH_REDIRECT_URI", "")
r = client.get("/api/auth/github/login?next=/",
headers={"Host": "gh-stats.com"})
assert r.status_code == 302
loc = r.headers["Location"]
assert "redirect_uri=https%3A%2F%2Fgh-stats.com%2Fapi%2Fauth%2Fgithub%2Fcallback" in loc, loc
assert "http%3A%2F%2Fgh-stats.com" not in loc
def test_oauth_redirect_uri_stays_http_on_localhost(client, monkeypatch):
"""Local development must keep working over plain http."""
monkeypatch.setattr(cfg, "GITHUB_OAUTH_REDIRECT_URI", "")
r = client.get("/api/auth/github/login?next=/",
headers={"Host": "localhost:5002"})
assert r.status_code == 302
assert "redirect_uri=http%3A%2F%2Flocalhost%3A5002" in r.headers["Location"]