GreedyBear icon indicating copy to clipboard operation
GreedyBear copied to clipboard

Same-second share requests can return a token owned by another user

Open ABHIGYA0205 opened this issue 1 month ago • 2 comments

Bug

ShareTokenViewSet.share() signs the raw feed query parameters without including the requesting user or a per-share unique value. Django’s timestamped signing produces the same token for identical parameters generated in the same second.

The view then calls ShareToken.objects.get_or_create() using only the token hash. Since that hash is unique, a second user’s request reuses the first user’s row; the user value in defaults is not applied to an existing row.

The second user still receives the consume and revoke URLs, but the token remains owned by the first user. It does not appear in the second user’s token list, and the second user’s revoke request is rejected by the owner check. The first user can revoke the link returned to the second user.

Reproduction

  1. Freeze django.core.signing.time.time to one timestamp.
  2. Authenticate as user A and user B.
  3. Have both users POST identical parameters to /api/feeds/share during that same second.
  4. Observe that both responses contain the same token, but only one token row exists and it belongs to user A.
  5. Observe that user B cannot find the token in their token list and receives 403 when attempting to revoke it.

I verified the same-second token collision using the locked Django 5.2.18 signing code and verified that Django ORM get_or_create() retains the first owner for a unique-hash row. I have not run this through GreedyBear’s full API/PostgreSQL stack.

Expected behavior

Each caller should receive a token whose ownership and revoke behavior match the response, or the API should explicitly support shared ownership rather than returning an unusable per-user revoke URL.

Impact

This requires identical feed parameters to be shared within the same one-second signing window. It does not expose additional feed data, but can misattribute the token and its audit reason, prevent the second user from managing it, and let the first user revoke the link returned to the second user.

Suggested approach

Make newly created share tokens owner-specific while keeping the signed feed parameters unchanged. For example, sign with a salt derived from SHARE_TOKEN_SALT and the creator’s user ID. When consuming or revoking a token, look up its ShareToken row by hash and verify the signature using that row’s owner-specific salt.

This should give different users distinct tokens for identical feed parameters, while retaining same-user deduplication if that behavior is intentional. Add a regression test with two authenticated users and a fixed signing time; assert that they get separate token records and each can manage only their own token. Preserve verification of existing tokens until their current 30-day expiry, if existing share links must remain valid.

ABHIGYA0205 avatar Oct 29 '26 05:10 ABHIGYA0205

Hey @ABHIGYA0205 ! Thanks for bringing this up. Pre-existing links don't have to remain valid. We'll roll out this change with the next major, so its expected that existing tokens might be invalidated.

You want to work on it?

regulartim avatar Oct 29 '26 12:10 regulartim

Yes, I’ll take this on. I’ll work through the fix, add regression tests for the cross-user case, and submit a PR

ABHIGYA0205 avatar Oct 29 '26 13:10 ABHIGYA0205