feat(api): PATCH /settings uses OAuth session + same-origin

This commit is contained in:
shay
2026-04-19 18:59:46 -04:00
parent eb4b68bf0d
commit 4ee71e0b86
2 changed files with 45 additions and 13 deletions
+2 -2
View File
@@ -575,7 +575,6 @@ def enroll_endpoint():
@app.route("/api/<username>/settings", methods=["GET"]) @app.route("/api/<username>/settings", methods=["GET"])
@rate_limited("read") @rate_limited("read")
@require_edit_token
def get_settings(username: str): def get_settings(username: str):
s = db.get_settings(username) s = db.get_settings(username)
if s is None: if s is None:
@@ -591,7 +590,8 @@ def get_settings(username: str):
@app.route("/api/<username>/settings", methods=["PATCH"]) @app.route("/api/<username>/settings", methods=["PATCH"])
@rate_limited("mutate") @rate_limited("mutate")
@require_edit_token @auth.require_same_origin
@auth.require_github_owner
def patch_settings(username: str): def patch_settings(username: str):
current = db.get_settings(username) current = db.get_settings(username)
if current is None: if current is None:
+43 -11
View File
@@ -2,6 +2,7 @@ import os, tempfile, pytest
from unittest.mock import patch from unittest.mock import patch
from src import db as dbmod from src import db as dbmod
from src import api as apimod from src import api as apimod
from src import config as cfg
@pytest.fixture @pytest.fixture
@@ -13,8 +14,11 @@ def client(monkeypatch):
monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db")) monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db"))
monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db")) monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db"))
dbmod.init_dbs() dbmod.init_dbs()
# Configure OAuth fixture with allowed origins and insecure cookies for testing.
monkeypatch.setattr(cfg, "ALLOWED_ORIGINS", ("https://gh-stats.com",))
app = apimod.app app = apimod.app
app.config["TESTING"] = True app.config["TESTING"] = True
app.config["SESSION_COOKIE_SECURE"] = False
with app.test_client() as c: with app.test_client() as c:
yield c yield c
@@ -54,34 +58,62 @@ def test_enrolled_user_with_built_widget_returns_ready(client):
def test_settings_patch_enqueues_rebuild(client): def test_settings_patch_enqueues_rebuild(client):
token = dbmod.enroll("alice", {"theme": "dark"})["edit_token"] dbmod.enroll("alice", {"theme": "dark"})
with client.session_transaction() as s:
s["gh_login"] = "alice"
r = client.patch( r = client.patch(
"/api/alice/settings", "/api/alice/settings",
json={"theme": "light"}, json={"theme": "light"},
headers={"Authorization": f"Bearer {token}"}, headers={"Origin": "https://gh-stats.com"},
) )
assert r.status_code == 200 assert r.status_code == 200
assert dbmod.get_settings("alice")["settings"]["theme"] == "light" assert dbmod.get_settings("alice")["settings"]["theme"] == "light"
def test_settings_patch_without_token_is_unauthorized(client): def test_settings_patch_without_origin_is_unauthorized(client):
dbmod.enroll("alice", {"theme": "dark"}) dbmod.enroll("alice", {"theme": "dark"})
with client.session_transaction() as s:
s["gh_login"] = "alice"
r = client.patch("/api/alice/settings", json={"theme": "light"}) r = client.patch("/api/alice/settings", json={"theme": "light"})
assert r.status_code == 401 assert r.status_code == 403
# Settings must not be mutated when the token check fails. # Settings must not be mutated when the origin check fails.
assert dbmod.get_settings("alice")["settings"]["theme"] == "dark" assert dbmod.get_settings("alice")["settings"]["theme"] == "dark"
def test_settings_patch_with_wrong_token_is_unauthorized(client): def test_patch_settings_without_session_is_401(client):
dbmod.enroll("alice", {"theme": "dark"}) dbmod.enroll("alice", {"theme": "dark"})
r = client.patch( r = client.patch("/api/alice/settings", json={"theme": "light"},
"/api/alice/settings", headers={"Origin": "https://gh-stats.com"})
json={"theme": "light"},
headers={"Authorization": "Bearer not-the-real-token"},
)
assert r.status_code == 401 assert r.status_code == 401
def test_patch_settings_wrong_login_is_403(client):
dbmod.enroll("alice", {"theme": "dark"})
with client.session_transaction() as s:
s["gh_login"] = "bob"
r = client.patch("/api/alice/settings", json={"theme": "light"},
headers={"Origin": "https://gh-stats.com"})
assert r.status_code == 403
def test_patch_settings_matching_login_succeeds(client):
dbmod.enroll("alice", {"theme": "dark"})
with client.session_transaction() as s:
s["gh_login"] = "alice"
r = client.patch("/api/alice/settings", json={"theme": "light"},
headers={"Origin": "https://gh-stats.com"})
assert r.status_code == 200
def test_patch_settings_bad_origin_is_403(client):
dbmod.enroll("alice", {"theme": "dark"})
with client.session_transaction() as s:
s["gh_login"] = "alice"
r = client.patch("/api/alice/settings", json={"theme": "light"},
headers={"Origin": "https://evil.example"})
assert r.status_code == 403
def test_refresh_is_one_shot(client): def test_refresh_is_one_shot(client):
token = dbmod.enroll("alice", {"theme": "dark"})["edit_token"] token = dbmod.enroll("alice", {"theme": "dark"})["edit_token"]
headers = {"Authorization": f"Bearer {token}"} headers = {"Authorization": f"Bearer {token}"}