Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

fix: resolve the voice-embedding notice on re-baseline, prefer a JSON error body over the session-expired heuristic, retry a stale sliced-upload ingest, and harden lock-file creation against planted symlinks

Murtaza Nasireb06de81f802
diff --git a/src/services/email.py b/src/services/email.py
index a85c8b69..166794a5 100644
--- a/src/services/email.py
+++ b/src/services/email.py
@@ -660,7 +660,9 @@ def send_transcription_failed_email(user, recording, error: str = None) -> bool:
# The worker's error text can carry an upstream URL or key, so it is
# summarised rather than forwarded in full.
- reason = (error or '').strip().splitlines()[0][:200] if error else ''
+ # next(iter(...), '') rather than [0]: a whitespace-only error is truthy
+ # but strips to nothing, and ''.splitlines() is an empty list.
+ reason = next(iter((error or '').strip().splitlines()), '')[:200]
reason_html = f"""
<p style="color: #6b7280; font-size: 14px; margin: 0 0 24px 0; padding: 12px 16px; background-color: #f8f9fa; border-left: 3px solid #d1d5db; border-radius: 4px;">{_h(reason)}</p>
""" if reason else ''
diff --git a/src/services/notifications.py b/src/services/notifications.py
index 6c70b3a5..be0043d4 100644
--- a/src/services/notifications.py
+++ b/src/services/notifications.py
@@ -38,6 +38,14 @@ def notify(kind, message_key, *, user_ids=None, admins=False, level='info',
if not user_ids:
return 0
+ # The link is rendered as an href. Only a same-origin path is accepted,
+ # so a future producer cannot put a javascript: or off-site URL into
+ # the one place users are told to click. Today's single producer is
+ # server-internal; this is for the next one.
+ if link is not None and not (link.startswith('/') and not link.startswith('//')):
+ logger.warning("Dropping non-relative notification link %r for %s", link, kind)
+ link = None
+
key = dedupe_key or kind
now = datetime.utcnow()
touched = 0
diff --git a/src/services/voice_embedding_check.py b/src/services/voice_embedding_check.py
index 03c3e977..48a38355 100644
--- a/src/services/voice_embedding_check.py
+++ b/src/services/voice_embedding_check.py
@@ -203,6 +203,10 @@ def check_voice_embeddings(app, force=False):
"""
if not embeddings_supported():
logger.debug('Active connector returns no speaker embeddings; skipping the canary')
+ # Whatever was wrong with the old backend's embeddings no longer
+ # applies: this one has none. Leaving the notice up would tell the
+ # admin to fix something that cannot be fixed from here.
+ _clear_notification(app)
return {'status': STATUS_UNKNOWN, 'supported': False,
'detail': 'the active connector does not return speaker embeddings'}
@@ -245,6 +249,10 @@ def check_voice_embeddings(app, force=False):
'detail': 'baseline recorded',
})
logger.info(f'Recorded a voice embedding baseline ({len(vector)} dimensions)')
+ # A fresh baseline is by definition a good state. This is also the
+ # path rebaseline() lands on, so without this the admin who did what
+ # the banner told them to do would keep seeing the warning.
+ _clear_notification(app)
return get_status(app)
stored = reference.get('embedding') or []
diff --git a/src/utils/database.py b/src/utils/database.py
index 490d38d0..d911954c 100644
--- a/src/utils/database.py
+++ b/src/utils/database.py
@@ -67,7 +67,10 @@ def migration_lock(engine, logger=None, timeout=120):
# host do not serialise against each other.
digest = hashlib.sha256(str(engine.url).encode()).hexdigest()[:16]
lock_path = os.path.join(tempfile.gettempdir(), f'speakr_migration_{digest}.lock')
- handle = open(lock_path, 'w')
+ # O_NOFOLLOW: the path is predictable and under a shared temp dir, so a
+ # symlink planted there must not be followed and truncated. 0o600 keeps
+ # other local users from pre-creating or reading it.
+ handle = os.fdopen(os.open(lock_path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600), 'r+')
deadline = time.monotonic() + timeout
while True:
try:
@@ -138,7 +141,10 @@ def acquire_singleton_lock(engine, name, logger=None):
lock_path = os.path.join(tempfile.gettempdir(), f'speakr_singleton_{digest}.lock')
handle = None
try:
- handle = open(lock_path, 'w')
+ # O_NOFOLLOW: the path is predictable and under a shared temp dir, so a
+ # symlink planted there must not be followed and truncated. 0o600 keeps
+ # other local users from pre-creating or reading it.
+ handle = os.fdopen(os.open(lock_path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600), 'r+')
fcntl.flock(handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
acquired = True
except BlockingIOError:
diff --git a/static/js/modules/db/sliced-file-upload.js b/static/js/modules/db/sliced-file-upload.js
index 29f107de..cfb3c7c1 100644
--- a/static/js/modules/db/sliced-file-upload.js
+++ b/static/js/modules/db/sliced-file-upload.js
@@ -49,6 +49,22 @@ const INCONCLUSIVE_STATUSES = new Set([408, 429, 502, 503, 504, 520, 521, 522, 5
const INGEST_POLL_MS = 3000;
const INGEST_WAIT_MS = 10 * 60 * 1000;
+// A `finalizing` session whose heartbeat is this stale has no ingest behind
+// it (the worker died, usually a container recreate). The server lets a new
+// finalize take it over after three missed beats, 90 s by default; this sits
+// past that so the takeover is attempted rather than waited out. If the
+// server's window is longer it answers 409, which is resumable, and the
+// loop simply keeps waiting. The server stays the authority either way.
+const STALE_INGEST_MS = 120 * 1000;
+
+// Session timestamps are naive UTC ISO strings with no zone suffix, which
+// Date.parse would read as local time. Pin them to UTC before comparing.
+function ageMs(isoTimestamp) {
+ if (!isoTimestamp) return 0;
+ const pinned = /[zZ]|[+-]\d\d:?\d\d$/.test(isoTimestamp) ? isoTimestamp : isoTimestamp + 'Z';
+ const t = Date.parse(pinned);
+ return Number.isFinite(t) ? Date.now() - t : 0;
+}
const MAX_FINALIZE_ATTEMPTS = 3;
const RESUME_KEY = 'speakr.slicedUploads';
@@ -389,11 +405,16 @@ async function settleUnfinishedFinalize(sessionId, formData, tokenRef, options,
}
if (status === null) throw firstError;
- if (status.status === 'finalizing') {
+ if (status.status === 'finalizing' && ageMs(status.last_seen_at) < STALE_INGEST_MS) {
+ // A live ingest is still stamping the row. Wait for it.
await sleep(INGEST_POLL_MS);
continue;
}
- if (!['recording', 'finalized'].includes(status.status)) throw firstError;
+ // Either the ingest finished, or it went quiet long enough that the
+ // server will hand the session to a fresh finalize. Fall through and
+ // try; a 409 means it was not ours to take yet. `finalizing` reaches
+ // here only when stale, so it is a legitimate state to retry from.
+ if (!['recording', 'finalized', 'finalizing'].includes(status.status)) throw firstError;
if (finalizeAttempts >= MAX_FINALIZE_ATTEMPTS) throw firstError;
finalizeAttempts += 1;
diff --git a/static/js/modules/db/sliced-file-upload.test.js b/static/js/modules/db/sliced-file-upload.test.js
index 55f7ddc5..61458b0a 100644
--- a/static/js/modules/db/sliced-file-upload.test.js
+++ b/static/js/modules/db/sliced-file-upload.test.js
@@ -422,6 +422,53 @@ describe('resuming an interrupted upload', () => {
expect(rememberedSessions()[`talk.mp4|${40 * MB}|0`]).toMatchObject({ sessionId: 'sess-1' });
});
+ it('takes over a finalizing session whose ingest went quiet instead of waiting ten minutes', async () => {
+ // Found in review: a container recreated mid-ingest leaves the session
+ // `finalizing` with nobody behind it. The server hands it to a fresh
+ // finalize after three missed heartbeats (90 s), but this loop only
+ // re-finalized once the status left `finalizing`, which a dead ingest
+ // never does on its own. The user waited the full INGEST_WAIT_MS.
+ vi.useFakeTimers();
+ const file = fakeFile(40 * MB);
+ // First finalize is cut off by the edge; the row then shows a
+ // heartbeat three minutes old, i.e. no ingest is stamping it.
+ finalizeResponse = [TIMED_OUT, INGESTED];
+ const stale = new Date(Date.now() - 3 * 60 * 1000).toISOString().replace('Z', '');
+ sessionStatus = { kind: 'sliced_upload', status: 'finalizing', chunk_count: 3,
+ upload_total_bytes: 40 * MB, last_seen_at: stale };
+
+ const upload = uploadFileInSlices(file, new Map(), {});
+ // Well under the ten-minute ceiling: it should retry on the first poll.
+ await vi.advanceTimersByTimeAsync(30 * 1000);
+
+ expect(await upload).toMatchObject({ id: 42 });
+ expect(finalizePosts()).toHaveLength(2);
+ });
+
+ it('keeps waiting while the ingest is still stamping the row', async () => {
+ // The converse: a fresh heartbeat means a live ingest, and re-finalizing
+ // would get a 409 for nothing. Same shape as the existing timeout test
+ // but with an explicit recent stamp, so a naive-UTC timestamp is not
+ // misread as local time and mistaken for stale.
+ vi.useFakeTimers();
+ finalizeResponse = TIMED_OUT;
+ // A getter, so every poll sees a stamp five seconds old relative to
+ // the (fake) clock at that moment, the way a live ingest's heartbeat
+ // would look. A static string would silently age past the stale
+ // threshold as the timers advance and turn this into the other test.
+ sessionStatus = { kind: 'sliced_upload', status: 'finalizing', chunk_count: 3,
+ upload_total_bytes: 40 * MB,
+ get last_seen_at() { return new Date(Date.now() - 5 * 1000).toISOString().replace('Z', ''); } };
+ const file = fakeFile(40 * MB);
+
+ const upload = uploadFileInSlices(file, new Map(), {});
+ const assertion = expect(upload).rejects.toThrow('HTTP 524');
+ await vi.advanceTimersByTimeAsync(11 * 60 * 1000);
+ await assertion;
+
+ expect(finalizePosts()).toHaveLength(1);
+ });
+
it('waits out an ingest that was already running when the file was added again', async () => {
vi.useFakeTimers();
const file = fakeFile(40 * MB);
diff --git a/static/js/modules/utils/response.js b/static/js/modules/utils/response.js
index 42cff0ec..b81ff6da 100644
--- a/static/js/modules/utils/response.js
+++ b/static/js/modules/utils/response.js
@@ -48,12 +48,16 @@ export async function readJsonResponse(response, t, fallbackKey = 'errors.reques
err.isCsrfRejection = true;
throw err;
}
+ // A JSON error body is proof this is the app answering, not the login
+ // page, so it wins over the status-code heuristics below. Otherwise a
+ // real 403 such as "you do not have permission to reprocess this
+ // recording" would be reported as an expired session.
+ if (data && data.error) {
+ throw new Error(data.error);
+ }
if (response.status === 401 || response.status === 403 || response.redirected) {
throw new Error(_t('errors.sessionExpiredReload'));
}
- if (data && data.error) {
- throw new Error(data.error); // A real JSON error from the app.
- }
if (response.status >= 500) {
throw new Error(_t('errors.serverErrorWithStatus', { status: response.status }));
}
diff --git a/static/js/modules/utils/response.test.js b/static/js/modules/utils/response.test.js
index 9008aece..3af65867 100644
--- a/static/js/modules/utils/response.test.js
+++ b/static/js/modules/utils/response.test.js
@@ -46,6 +46,21 @@ describe('readJsonResponse', () => {
.rejects.toThrow('No valid transcription available');
});
+ it('lets a real 403 from the app through instead of calling it an expired session', async () => {
+ // Found in review: reprocess and summary return a JSON 403 such as
+ // "You do not have permission to reprocess this recording". The
+ // status-code heuristic ran first and told the user their session
+ // had expired and to reload, which changes nothing.
+ const body = '{"error":"You do not have permission to reprocess this recording"}';
+ await expect(readJsonResponse(res({ ok: false, status: 403, body }), t))
+ .rejects.toThrow('You do not have permission to reprocess this recording');
+ });
+
+ it('still reads a 401 with a JSON body as the app message, not the login page', async () => {
+ await expect(readJsonResponse(res({ ok: false, status: 401, body: '{"error":"Token revoked"}' }), t))
+ .rejects.toThrow('Token revoked');
+ });
+
it('treats a 401 as an expired session', async () => {
await expect(readJsonResponse(res({ ok: false, status: 401, body: '' }), t))
.rejects.toThrow('errors.sessionExpiredReload');
diff --git a/tests/test_completion_email.py b/tests/test_completion_email.py
index 658a6d47..2dda58ec 100644
--- a/tests/test_completion_email.py
+++ b/tests/test_completion_email.py
@@ -349,3 +349,15 @@ def test_an_unreadable_logo_still_sends_a_valid_message():
parts = {p.get_content_type() for p in msg.walk()}
assert 'text/plain' in parts and 'text/html' in parts
assert 'image/png' not in parts
+
+
+def test_a_whitespace_only_error_still_sends_the_failure_email(user_factory, smtp_configured):
+ """Found in review: `(error or '').strip().splitlines()[0]` raised
+ IndexError on an error like "\\n" (truthy, strips to nothing), and the
+ worker's except swallowed it, so exactly the failure email was lost."""
+ user = user_factory(notify_email_on_completion=True)
+ with app.app_context(), patch.object(email_service, '_send_email', return_value=True) as send:
+ rec = _recording(user.id)
+ rec.id = 12
+ assert email_service.send_transcription_failed_email(user, rec, '\n \n') is True
+ send.assert_called_once()
diff --git a/tests/test_job_queue_singleton.py b/tests/test_job_queue_singleton.py
index c71b2ff2..0a7151fe 100644
--- a/tests/test_job_queue_singleton.py
+++ b/tests/test_job_queue_singleton.py
@@ -232,3 +232,31 @@ def test_the_election_never_opens_a_database_connection():
assert acquire_singleton_lock(
_EngineThatRefusesConnections(), "speakr.test_no_connection"
) is True
+
+
+def test_the_lock_file_does_not_follow_a_planted_symlink(tmp_path):
+ """The lock path is predictable and lives in the shared temp dir. On a
+ bare-metal host another local user could plant a symlink there pointing
+ at, say, the SQLite database; a plain open(..., 'w') would follow it and
+ truncate the target on the next startup. O_NOFOLLOW refuses the symlink."""
+ import hashlib, os, tempfile
+ from unittest.mock import patch
+ from src.utils import database as db_utils
+
+ class _Engine:
+ url = 'sqlite:///symlink-probe'
+ name = 'speakr.test_symlink'
+ digest = hashlib.sha256(f'{_Engine.url}|{name}'.encode()).hexdigest()[:16]
+
+ victim = tmp_path / 'precious.db'
+ victim.write_bytes(b'do not truncate me')
+ lock_path = tmp_path / f'speakr_singleton_{digest}.lock'
+ os.symlink(victim, lock_path)
+
+ with patch.object(tempfile, 'gettempdir', return_value=str(tmp_path)):
+ db_utils._held_singleton_locks.pop(name, None)
+ # Failing open is the documented behaviour when the lock cannot be
+ # taken; what matters here is that the victim file is untouched.
+ acquire_singleton_lock(_Engine(), name)
+
+ assert victim.read_bytes() == b'do not truncate me', 'the symlink was followed and the target truncated'
diff --git a/tests/test_label_urls.py b/tests/test_label_urls.py
index e43cfbef..ea01e3fe 100644
--- a/tests/test_label_urls.py
+++ b/tests/test_label_urls.py
@@ -147,7 +147,16 @@ def test_label_url_serves_the_spa_shell(owner):
resp = c.get(f"/label/{_PREFIX}shell")
assert resp.status_code == 200
# Same shell the '/' route serves — the label is resolved client-side.
- assert resp.data == c.get("/").data
+ #
+ # Compared with the CSRF meta token blanked out. Flask-WTF signs that
+ # token with a timed serializer, so its bytes embed the current second;
+ # two renders that straddle a second boundary differ there and nowhere
+ # else. That made a raw byte comparison flake in the full suite while
+ # passing every time this file ran alone.
+ import re
+ def _shell(body):
+ return re.sub(rb'(name="csrf-token"\s+content=")[^"]*(")', rb'\1\2', body)
+ assert _shell(resp.data) == _shell(c.get("/").data)
def test_label_url_accepts_spaces_and_mixed_case(owner):
diff --git a/tests/test_notifications.py b/tests/test_notifications.py
index dab5ef16..aac9246d 100644
--- a/tests/test_notifications.py
+++ b/tests/test_notifications.py
@@ -270,3 +270,28 @@ def test_a_bad_ids_payload_is_rejected(api):
client, login, users = api
login(users['admin1'])
assert client.post('/api/notifications/read', json={'ids': 'not-a-list'}).status_code == 400
+
+
+# --- the link is rendered as an href ------------------------------------
+
+ 'javascript:alert(1)',
+ 'https://evil.example/login',
+ '//evil.example/login', # protocol-relative, still off-site
+ 'data:text/html,<script>',
+])
+def test_a_link_that_is_not_a_same_origin_path_is_dropped(users, bad):
+ """The link is the one thing in a notice a user is told to click.
+ Only a same-origin path is accepted, so a future producer cannot put
+ a javascript: or off-site URL there."""
+ with app.app_context():
+ ns.notify('test.kind', 'notifications.test', user_ids=[users['admin1']], link=bad)
+ n = Notification.query.filter_by(user_id=users['admin1']).one()
+ assert n.link is None, f'{bad!r} was stored as a link'
+
+
+def test_a_same_origin_path_is_kept(users):
+ with app.app_context():
+ ns.notify('test.kind', 'notifications.test', user_ids=[users['admin1']], link='/admin?tab=voice')
+ n = Notification.query.filter_by(user_id=users['admin1']).one()
+ assert n.link == '/admin?tab=voice'
diff --git a/tests/test_voice_embedding_check.py b/tests/test_voice_embedding_check.py
index 919036a7..774655e8 100644
--- a/tests/test_voice_embedding_check.py
+++ b/tests/test_voice_embedding_check.py
@@ -423,3 +423,80 @@ def test_the_check_endpoint_forces_a_probe(admin_client):
assert resp.status_code == 200
assert resp.get_json()['status'] == vec.STATUS_OK
probe.assert_called_once()
+
+
+# --- the notification must follow the verdict -----------------------------
+#
+# Found in review: every path that ends in a known-good state has to clear
+# the admin notice, or the person who did what the banner told them to do
+# keeps seeing the banner.
+
+def _notice_active():
+ from src.models import Notification, KIND_VOICE_EMBEDDING_CHANGED
+ return Notification.query.filter_by(
+ kind=KIND_VOICE_EMBEDDING_CHANGED, resolved_at=None).count()
+
+
+def an_admin():
+ import os
+ from src.models import User, Notification
+ with app.app_context():
+ u = User(username=f'vec_adm_{os.getpid()}', email=f'vec_adm_{os.getpid()}@example.com',
+ password='x', is_admin=True)
+ db.session.add(u); db.session.commit(); uid = u.id
+ yield uid
+ with app.app_context():
+ Notification.query.filter_by(user_id=uid).delete()
+ User.query.filter_by(id=uid).delete(); db.session.commit()
+
+
+def test_rebaselining_clears_the_admin_notification(an_admin):
+ """rebaseline() deletes the reference and re-checks, landing on the
+ fresh-baseline path. That path used to return without resolving the
+ notice, so clicking the button the banner points at left the banner up."""
+ with app.app_context():
+ _baseline()
+ with _supported(), patch.object(vec, 'probe_backend', return_value=DIFFERENT), \
+ patch.object(vec, 'backend_fingerprint', return_value='backend-b'):
+ vec.check_voice_embeddings(app)
+ assert _notice_active() == 1, 'precondition: the change raised a notice'
+
+ with _supported(), patch.object(vec, 'probe_backend', return_value=DIFFERENT), \
+ patch.object(vec, 'backend_fingerprint', return_value='backend-b'):
+ result = vec.rebaseline(app)
+
+ assert result['status'] == vec.STATUS_OK
+ assert _notice_active() == 0, 'rebaselining left the warning in place'
+
+
+def test_a_new_clip_version_clears_a_stale_notification(an_admin):
+ with app.app_context():
+ _baseline()
+ with _supported(), patch.object(vec, 'probe_backend', return_value=DIFFERENT), \
+ patch.object(vec, 'backend_fingerprint', return_value='backend-b'):
+ vec.check_voice_embeddings(app)
+ stored = json.loads(SystemSetting.get_setting(vec.SETTING_KEY))
+ stored['clip_version'] = 'v0'
+ SystemSetting.set_setting(vec.SETTING_KEY, json.dumps(stored))
+
+ with _supported(), patch.object(vec, 'probe_backend', return_value=DIFFERENT), \
+ patch.object(vec, 'backend_fingerprint', return_value='backend-b'):
+ vec.check_voice_embeddings(app)
+ assert _notice_active() == 0
+
+
+def test_switching_to_a_connector_without_embeddings_clears_the_notification(an_admin):
+ """The old backend's embedding problem cannot be fixed from a backend
+ that has no embeddings; the notice would be telling the admin to act on
+ something that no longer exists."""
+ with app.app_context():
+ _baseline()
+ with _supported(), patch.object(vec, 'probe_backend', return_value=DIFFERENT), \
+ patch.object(vec, 'backend_fingerprint', return_value='backend-b'):
+ vec.check_voice_embeddings(app)
+ assert _notice_active() == 1
+
+ with patch.object(vec, 'embeddings_supported', return_value=False):
+ vec.check_voice_embeddings(app)
+ assert _notice_active() == 0