Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Share target refuses a POST from another site (#435)
Murtaza Nasir875e9dbffcc0
diff --git a/src/api/recordings.py b/src/api/recordings.pyindex 224ef556..8974c6d9 100644--- a/src/api/recordings.py+++ b/src/api/recordings.py@@ -2122,7 +2122,11 @@ def share_target():CSRF is exempted at app startup (the share sheet has no way toround-trip a CSRF token). Authentication still happens via thesession cookie carried by the browser; unauthenticated visitors hit- @login_required and are bounced to the login page.+ @login_required and are bounced to the login page. A POST that a+ browser marks as sent from another site (``Sec-Fetch-Site``) is+ refused (#435): a share from the operating system's share sheet is+ marked ``none``, and a page elsewhere could otherwise submit a forged+ share with the signed-in user's cookies.Implementation: save the file to ``UPLOAD_FOLDER``, create a Recordingrow applying the same user / folder / tag defaults as ``upload_file``@@ -2135,6 +2139,12 @@ def share_target():# Some platforms pre-flight with GET. Just land the user on the SPA.return redirect(url_for('recordings.index'))+ # Browsers without Fetch Metadata send no header and are let through.+ if request.headers.get('Sec-Fetch-Site', '').lower() in ('cross-site', 'same-site'):+ current_app.logger.warning(+ f"share-target POST from another site refused for user {current_user.id}")+ return redirect(url_for('recordings.index') + '?share_target_error=cross_site')+shared = request.files.get('shared_audio') or request.files.get('file')if not shared or not getattr(shared, 'filename', ''):return redirect(url_for('recordings.index') + '?share_target_error=missing_file')diff --git a/tests/test_share_target.py b/tests/test_share_target.pyindex 8cbce053..7cae5a8e 100644--- a/tests/test_share_target.py+++ b/tests/test_share_target.py@@ -18,6 +18,8 @@ import uuidfrom pathlib import Pathfrom unittest.mock import patch+import pytest+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))from src.app import app, db@@ -210,6 +212,37 @@ def test_share_target_is_csrf_exempt():f"share_target view should be CSRF-exempt; current exempts: {exempt_set}"[email protected]("site", ["cross-site", "same-site"])+def test_share_target_refuses_a_post_from_another_site(site):+ """A forged share from another site carries the user's cookies (#435)."""+ with app.app_context():+ user = _setup_user(f"share_xsite_{site[:5]}")+ client = app.test_client()+ _login(client, user)+ data = {"shared_audio": (io.BytesIO(b"\x00" * 512), "a.mp3"), "text": "<b>x</b>"}+ resp = client.post("/share-target", data=data, headers={"Sec-Fetch-Site": site},+ content_type="multipart/form-data", follow_redirects=False)+ assert resp.status_code == 302+ assert "share_target_error=cross_site" in resp.headers.get("Location", "")+ assert Recording.query.filter_by(user_id=user.id).count() == 0+ db.session.delete(user)+ db.session.commit()++[email protected]("site", ["none", "same-origin"])+def test_share_target_accepts_the_share_sheet(site):+ """The operating system's share sheet is marked "none"."""+ with app.app_context():+ user = _setup_user(f"share_ok_{site[:4]}")+ client = app.test_client()+ _login(client, user)+ resp = client.post("/share-target", data={}, headers={"Sec-Fetch-Site": site},+ follow_redirects=False)+ assert "share_target_error=missing_file" in resp.headers.get("Location", "")+ db.session.delete(user)+ db.session.commit()++def teardown_module(module):with app.app_context():for u in User.query.filter(User.username.like("share_%")).all():