- All dependencies to current releases (97 known vulnerabilities -> 0), Python 3.14, uv pinned, requirements-test.txt, lxml/dateutil pinned - CSRF tokens on admin forms, logout and IndieAuth consent - IndieAuth client and crosspost link-card fetches via safe_get - FLASK_DEBUG/FLASK_ENV default off/production; image sets them - Foreign keys enforced on pooled connections; migration 018 removes orphans - Rate limits shared across processes (rate_limits, migration 019) - datetime.utcnow() and sqlite3 default adapters replaced - compose.yaml fixed; duplicate dev_auth.py and test.ini removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
2.7 KiB
Python
81 lines
2.7 KiB
Python
"""
|
|
CSRF protection on admin and consent forms (v2.11.0)
|
|
"""
|
|
|
|
import re
|
|
|
|
import pytest
|
|
|
|
from starpunk import create_app
|
|
from starpunk.auth import create_session
|
|
|
|
|
|
@pytest.fixture
|
|
def app(tmp_path):
|
|
return create_app({
|
|
"TESTING": True, "CSRF_ENABLED": True, # the real check, unlike other tests
|
|
"SITE_URL": "http://localhost:5000", "ADMIN_ME": "https://test.example.com",
|
|
"SESSION_SECRET": "x", "DATA_PATH": tmp_path, "NOTES_PATH": tmp_path / "notes",
|
|
"DATABASE_PATH": tmp_path / "starpunk.db",
|
|
})
|
|
|
|
|
|
@pytest.fixture
|
|
def admin(app):
|
|
client = app.test_client()
|
|
with app.test_request_context():
|
|
client.set_cookie("starpunk_session", create_session(app.config["ADMIN_ME"]))
|
|
return client
|
|
|
|
|
|
def token_from(page: bytes) -> str:
|
|
return re.search(rb'name="csrf_token" value="([0-9a-f]{64})"', page).group(1).decode()
|
|
|
|
|
|
def test_forms_carry_a_token(admin):
|
|
assert token_from(admin.get("/admin/new").data)
|
|
|
|
|
|
def test_post_without_token_is_refused(app, admin):
|
|
response = admin.post("/admin/new", data={"content": "Forged"})
|
|
assert response.status_code == 400
|
|
from starpunk.notes import list_notes
|
|
with app.app_context():
|
|
assert list_notes() == []
|
|
|
|
|
|
def test_post_with_wrong_token_is_refused(admin):
|
|
assert admin.post("/admin/new", data={"content": "x", "csrf_token": "0" * 64}).status_code == 400
|
|
|
|
|
|
def test_post_with_token_works(app, admin):
|
|
token = token_from(admin.get("/admin/new").data)
|
|
response = admin.post("/admin/new", data={"content": "Real", "csrf_token": token, "published": "on"})
|
|
assert response.status_code == 302
|
|
from starpunk.notes import list_notes
|
|
with app.app_context():
|
|
assert [n.content.strip() for n in list_notes()] == ["Real"]
|
|
|
|
|
|
def test_token_changes_with_the_session(app):
|
|
tokens = []
|
|
for _ in range(2):
|
|
client = app.test_client()
|
|
with app.test_request_context():
|
|
client.set_cookie("starpunk_session", create_session(app.config["ADMIN_ME"]))
|
|
tokens.append(token_from(client.get("/admin/new").data))
|
|
assert tokens[0] != tokens[1]
|
|
|
|
|
|
def test_logout_and_consent_are_protected(admin):
|
|
assert admin.post("/auth/logout").status_code == 400
|
|
assert admin.post("/auth/authorize/approve", data={"client_id": "https://app.example/"}).status_code == 400
|
|
|
|
|
|
def test_app_endpoints_are_not_affected(app):
|
|
client = app.test_client()
|
|
# Bearer-token and public endpoints answer normally, without a CSRF token
|
|
assert client.post("/micropub", data={"h": "entry", "content": "x"}).status_code == 401
|
|
assert client.post("/auth/token", data={"grant_type": "authorization_code"}).status_code == 400
|
|
assert client.post("/webmention", data={}).status_code == 400
|