Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Fix shared-audio endpoint returning 500 instead of 404 for unknown public_id
Murtaza Nasirb6fedb5d5912
diff --git a/src/api/shares.py b/src/api/shares.pyindex 010dca1f..d875058d 100644--- a/src/api/shares.py+++ b/src/api/shares.py@@ -12,6 +12,7 @@ import reimport jsonfrom flask import Blueprint, render_template, request, redirect, url_for, jsonify, send_file, current_appfrom flask_login import login_required, current_user+from werkzeug.exceptions import HTTPExceptionfrom src.database import dbfrom src.models import Recording, Share, InternalShare, SharedRecordingState, User, TranscriptChunk, ShareAuditLog@@ -160,6 +161,10 @@ def get_shared_audio(public_id):current_app.logger.error(f"Audio file missing from server: {recording.audio_path}")return jsonify({'error': 'Audio file missing from server'}), 404return send_file(recording.audio_path, conditional=True)+ except HTTPException:+ # Let first_or_404() (and any other HTTP error) keep its real status —+ # an unknown public_id must stay a 404, not be masked as a 500.+ raiseexcept Exception as e:current_app.logger.error(f"Error serving shared audio for public_id {public_id}: {e}", exc_info=True)return jsonify({'error': 'An unexpected error occurred.'}), 500diff --git a/tests/test_shares_authz.py b/tests/test_shares_authz.pyindex dc6f83e9..cf5a7df4 100644--- a/tests/test_shares_authz.py+++ b/tests/test_shares_authz.py@@ -233,18 +233,16 @@ def test_public_view_revoked_public_id_returns_404():# Public shared-audio endpoint# =============================================================================-def test_shared_audio_nonexistent_public_id_returns_500():- """REAL BEHAVIOUR (and a minor bug): an unknown public_id on the audio- endpoint returns 500, not 404.-- get_shared_audio() wraps first_or_404() in a broad ``except Exception`` that- catches the werkzeug NotFound (404) it raises and re-reports it as a generic- 500. No data leaks (still no audio served), but the status is wrong. This- test pins the current behaviour; if the route is fixed to let the 404- propagate, update this to expect 404.+def test_shared_audio_nonexistent_public_id_returns_404():+ """An unknown public_id on the audio endpoint must be a 404, not a 500.++ get_shared_audio() now re-raises HTTPException so first_or_404()'s 404+ propagates instead of being masked as a generic 500 by the broad+ ``except Exception``. No audio is served either way; this asserts the+ correct status (matches the sibling HTML view /share/<public_id>)."""resp = _anon_client().get("/share/audio/nope-not-a-real-id")- assert resp.status_code == 500+ assert resp.status_code == 404def test_shared_audio_valid_share_missing_file_returns_404():