Skip to content

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

Python

Validate tag ownership and group membership in PATCH /api/v1/recordings/batch add_tag_ids and remove_tag_ids paths to close IDOR where any authenticated user could attach arbitrary tags including admin-curated or other users' private tags to their own recordings

Murtaza Nasir1cdb1ff90f93
diff --git a/src/api/api_v1.py b/src/api/api_v1.py
index b6c07ca7..7a9d9cd6 100644
--- a/src/api/api_v1.py
+++ b/src/api/api_v1.py
@@ -2553,9 +2553,27 @@ def batch_update_recordings():
# or a valid folder id the caller has access to).
recording.folder_id = target_folder_id
- # Handle tag additions
+ # Handle tag additions.
+ # Authorize each tag_id: personal tags must be owned, group
+ # tags require caller membership in the tag's group. Without
+ # this check, the batch endpoint was an IDOR that let any
+ # user attach arbitrary tag_ids (including admin-curated or
+ # other users' private tags) to their own recordings.
if 'add_tag_ids' in updates:
+ from src.models.organization import Tag, GroupMembership
for tag_id in updates['add_tag_ids']:
+ tag = db.session.get(Tag, tag_id)
+ if tag is None:
+ continue # silently skip unknown ids
+ if tag.group_id is None:
+ if tag.user_id != current_user.id:
+ continue # not yours
+ else:
+ is_member = GroupMembership.query.filter_by(
+ user_id=current_user.id, group_id=tag.group_id
+ ).first() is not None
+ if not is_member:
+ continue # not a member of the tag's group
existing = RecordingTag.query.filter_by(
recording_id=recording_id,
tag_id=tag_id
@@ -2571,9 +2589,25 @@ def batch_update_recordings():
)
db.session.add(recording_tag)
- # Handle tag removals
+ # Handle tag removals. Removing an unauthorised tag from
+ # one's own recording isn't an IDOR per se (you're only
+ # editing your own RecordingTag rows) but symmetry keeps
+ # the contract simple.
if 'remove_tag_ids' in updates:
+ from src.models.organization import Tag, GroupMembership
for tag_id in updates['remove_tag_ids']:
+ tag = db.session.get(Tag, tag_id)
+ if tag is None:
+ continue
+ if tag.group_id is None:
+ if tag.user_id != current_user.id:
+ continue
+ else:
+ is_member = GroupMembership.query.filter_by(
+ user_id=current_user.id, group_id=tag.group_id
+ ).first() is not None
+ if not is_member:
+ continue
RecordingTag.query.filter_by(
recording_id=recording_id,
tag_id=tag_id
diff --git a/tests/test_api_v1_batch_tag_authz.py b/tests/test_api_v1_batch_tag_authz.py
new file mode 100644
index 00000000..f8243da9
--- /dev/null
+++ b/tests/test_api_v1_batch_tag_authz.py
@@ -0,0 +1,211 @@
+"""Regression tests for the batch tag-update IDOR fix.
+
+PATCH /api/v1/recordings/batch with add_tag_ids / remove_tag_ids used to
+trust the tag_ids blindly. An authenticated attacker could attach any
+tag_id (including admin-curated or another user's private tags) to
+their own recordings. The fix mirrors the folder-validation pattern:
+personal tags must be owned by the caller, group tags require caller
+membership in the tag's group.
+"""
+import os
+import sys
+import uuid
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from src.app import app, db
+from src.models import User, Recording
+from src.models.organization import Tag, Group, GroupMembership, RecordingTag
+
+app.config["WTF_CSRF_ENABLED"] = False
+
+
+def _setup_user(prefix):
+ suffix = uuid.uuid4().hex[:8]
+ user = User(
+ username=f"{prefix}_{suffix}",
+ email=f"{prefix}_{suffix}@local.test",
+ password="x",
+ )
+ db.session.add(user)
+ db.session.commit()
+ return user
+
+
+def _login(client, user):
+ with client.session_transaction() as sess:
+ sess["_user_id"] = str(user.id)
+ sess["_fresh"] = True
+
+
+def _make_recording(user_id, title='r'):
+ rec = Recording(
+ user_id=user_id,
+ title=title,
+ audio_path='/tmp/x.mp3',
+ status='COMPLETED',
+ )
+ db.session.add(rec)
+ db.session.commit()
+ return rec
+
+
+def test_batch_update_rejects_adding_other_users_personal_tag():
+ """Attacker cannot attach a victim's personal tag to their own
+ recording via the batch endpoint. The tag is silently skipped;
+ other operations in the same batch still proceed."""
+ with app.app_context():
+ victim = _setup_user('v_tag_victim')
+ attacker = _setup_user('v_tag_attacker')
+ victim_tag = Tag(
+ name='secret-personal-tag',
+ user_id=victim.id,
+ group_id=None,
+ )
+ db.session.add(victim_tag)
+ db.session.commit()
+ attacker_rec = _make_recording(attacker.id, 'attacker rec')
+
+ client = app.test_client()
+ _login(client, attacker)
+ resp = client.patch(
+ '/api/v1/recordings/batch',
+ json={
+ 'recording_ids': [attacker_rec.id],
+ 'updates': {'add_tag_ids': [victim_tag.id]},
+ },
+ )
+ assert resp.status_code == 200
+
+ # The RecordingTag row must NOT have been created.
+ rt = RecordingTag.query.filter_by(
+ recording_id=attacker_rec.id, tag_id=victim_tag.id
+ ).first()
+ assert rt is None, (
+ 'Attacker attached victim_tag to their own recording via batch '
+ 'add_tag_ids. IDOR fix missing.'
+ )
+
+ db.session.delete(attacker_rec)
+ db.session.delete(victim_tag)
+ db.session.delete(victim)
+ db.session.delete(attacker)
+ db.session.commit()
+
+
+def test_batch_update_rejects_adding_group_tag_when_not_member():
+ """Group-scoped tags require caller membership in the tag's group.
+ A non-member can't attach a group tag to their own recordings."""
+ with app.app_context():
+ owner = _setup_user('grp_owner')
+ outsider = _setup_user('grp_outsider')
+ group = Group(name=f'g_{uuid.uuid4().hex[:6]}')
+ db.session.add(group)
+ db.session.commit()
+ # Owner is a member; outsider is not.
+ db.session.add(GroupMembership(user_id=owner.id, group_id=group.id, role='admin'))
+ db.session.commit()
+ group_tag = Tag(name='group-tag', user_id=owner.id, group_id=group.id)
+ db.session.add(group_tag)
+ db.session.commit()
+ outsider_rec = _make_recording(outsider.id, 'outsider rec')
+
+ client = app.test_client()
+ _login(client, outsider)
+ resp = client.patch(
+ '/api/v1/recordings/batch',
+ json={
+ 'recording_ids': [outsider_rec.id],
+ 'updates': {'add_tag_ids': [group_tag.id]},
+ },
+ )
+ assert resp.status_code == 200
+ rt = RecordingTag.query.filter_by(
+ recording_id=outsider_rec.id, tag_id=group_tag.id
+ ).first()
+ assert rt is None, 'Non-member attached a group tag via batch IDOR'
+
+ db.session.delete(outsider_rec)
+ db.session.delete(group_tag)
+ for m in GroupMembership.query.filter_by(group_id=group.id).all():
+ db.session.delete(m)
+ db.session.delete(group)
+ db.session.delete(owner)
+ db.session.delete(outsider)
+ db.session.commit()
+
+
+def test_batch_update_accepts_owned_personal_tag():
+ """The legitimate path still works: a user can attach their own
+ personal tag via the batch endpoint."""
+ with app.app_context():
+ user = _setup_user('legit_tag')
+ my_tag = Tag(name='mine', user_id=user.id, group_id=None)
+ db.session.add(my_tag)
+ db.session.commit()
+ rec = _make_recording(user.id, 'r')
+
+ client = app.test_client()
+ _login(client, user)
+ resp = client.patch(
+ '/api/v1/recordings/batch',
+ json={
+ 'recording_ids': [rec.id],
+ 'updates': {'add_tag_ids': [my_tag.id]},
+ },
+ )
+ assert resp.status_code == 200
+ rt = RecordingTag.query.filter_by(
+ recording_id=rec.id, tag_id=my_tag.id
+ ).first()
+ assert rt is not None, 'Owner could not attach their own tag'
+
+ db.session.delete(rt)
+ db.session.delete(rec)
+ db.session.delete(my_tag)
+ db.session.delete(user)
+ db.session.commit()
+
+
+def test_batch_update_accepts_group_tag_when_member():
+ """Group-scoped tags: members can attach them."""
+ with app.app_context():
+ owner = _setup_user('grp_owner_m')
+ member = _setup_user('grp_member')
+ group = Group(name=f'g_{uuid.uuid4().hex[:6]}')
+ db.session.add(group)
+ db.session.commit()
+ db.session.add_all([
+ GroupMembership(user_id=owner.id, group_id=group.id, role='admin'),
+ GroupMembership(user_id=member.id, group_id=group.id, role='member'),
+ ])
+ db.session.commit()
+ group_tag = Tag(name='gt', user_id=owner.id, group_id=group.id)
+ db.session.add(group_tag)
+ db.session.commit()
+ member_rec = _make_recording(member.id, 'r')
+
+ client = app.test_client()
+ _login(client, member)
+ resp = client.patch(
+ '/api/v1/recordings/batch',
+ json={
+ 'recording_ids': [member_rec.id],
+ 'updates': {'add_tag_ids': [group_tag.id]},
+ },
+ )
+ assert resp.status_code == 200
+ rt = RecordingTag.query.filter_by(
+ recording_id=member_rec.id, tag_id=group_tag.id
+ ).first()
+ assert rt is not None
+
+ db.session.delete(rt)
+ db.session.delete(member_rec)
+ db.session.delete(group_tag)
+ for m in GroupMembership.query.filter_by(group_id=group.id).all():
+ db.session.delete(m)
+ db.session.delete(group)
+ db.session.delete(owner)
+ db.session.delete(member)
+ db.session.commit()