diff --git a/generator/src/api.py b/generator/src/api.py index 2a2710c..f8d1657 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -575,7 +575,6 @@ def enroll_endpoint(): @app.route("/api//settings", methods=["GET"]) @rate_limited("read") -@require_edit_token def get_settings(username: str): s = db.get_settings(username) if s is None: @@ -591,7 +590,8 @@ def get_settings(username: str): @app.route("/api//settings", methods=["PATCH"]) @rate_limited("mutate") -@require_edit_token +@auth.require_same_origin +@auth.require_github_owner def patch_settings(username: str): current = db.get_settings(username) if current is None: diff --git a/generator/tests/test_api.py b/generator/tests/test_api.py index f75274d..c364133 100644 --- a/generator/tests/test_api.py +++ b/generator/tests/test_api.py @@ -2,6 +2,7 @@ import os, tempfile, pytest from unittest.mock import patch from src import db as dbmod from src import api as apimod +from src import config as cfg @pytest.fixture @@ -13,8 +14,11 @@ def client(monkeypatch): monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db")) monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db")) 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.config["TESTING"] = True + app.config["SESSION_COOKIE_SECURE"] = False with app.test_client() as c: yield c @@ -54,34 +58,62 @@ def test_enrolled_user_with_built_widget_returns_ready(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( "/api/alice/settings", json={"theme": "light"}, - headers={"Authorization": f"Bearer {token}"}, + headers={"Origin": "https://gh-stats.com"}, ) assert r.status_code == 200 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"}) + with client.session_transaction() as s: + s["gh_login"] = "alice" r = client.patch("/api/alice/settings", json={"theme": "light"}) - assert r.status_code == 401 - # Settings must not be mutated when the token check fails. + assert r.status_code == 403 + # Settings must not be mutated when the origin check fails. 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"}) - r = client.patch( - "/api/alice/settings", - json={"theme": "light"}, - headers={"Authorization": "Bearer not-the-real-token"}, - ) + r = client.patch("/api/alice/settings", json={"theme": "light"}, + headers={"Origin": "https://gh-stats.com"}) 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): token = dbmod.enroll("alice", {"theme": "dark"})["edit_token"] headers = {"Authorization": f"Bearer {token}"}