Repository navigation
feat: share an alert with a temporary public link - #731
Conversation
Add an alert_shares table and /shares routes: an org user creates, lists
and cancels links to an alert of their organization; anyone holding the
link reads a whitelisted view of the alert on /shares/public/{token},
with short-lived image URLs. Only the SHA-256 of the 256-bit token is
stored. Links expire after 48 h; expired or cancelled links answer 410,
unknown ones 404.
Shares cascade on alert deletion and follow the surviving alert on
merge. Share tokens are scrubbed from Sentry events.
Closes #720
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #731 +/- ##
==========================================
+ Coverage 95.17% 95.68% +0.50%
==========================================
Files 60 65 +5
Lines 3402 3685 +283
==========================================
+ Hits 3238 3526 +288
+ Misses 164 159 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…-44507f # Conflicts: # src/app/api/api_v1/endpoints/detections.py
MateoLostanlen
left a comment
There was a problem hiding this comment.
Hi @Acruve15, thanks for the PR! The migration is chained correctly now, thanks for the quick fix.
One question before I approve: the Sentry scrub only cleans request.url. If an error happens on the public route, I think Sentry frame locals still hold the ASGI scope (path, raw_path) with the token. Can you confirm? If so, dropping the frame vars for /shares/public/ events in scrub_share_token would cover it, here or in a follow-up PR.
(nit) Uvicorn access logs also show the token. A small logging.Filter on uvicorn.access would hide it.
fe51
left a comment
There was a problem hiding this comment.
hi @Acruve15 !
Great work, thanks a lot! One product point: the recipient only gets the 10 latest frames per sequence. Ideally they'd see what the sharer sees, and be able to browse a sequence the way the platform does: load more frames and use sampling to get a spread from the start of the fire to now.
Would it be feasible to add something like:
GET /shares/public/{token}/sequences/{sequence_ref}/detections?sampling=&limit=&offset=&desc=
It would reuse DetectionCRUD.fetch_by_sequence (same sampling, X-Sampled-Total headers and presigned URLs as /sequences/{id}/detections), after checking that the sequence belongs to the shared alert. The open question is sequence_ref. You could expose the sequence id, which is harmless once scoped to the share. Or you could use a reference that only makes sense within the share, to keep the "no ids" whitelist. The sequence's position in the list won't work as that reference, because the order changes as last_seen_at moves.
It would probably also make the frontend side simpler. Since the endpoint would have the same parameters and response shape as /sequences/{id}/detections, the public page could reuse the existing sequence viewer components instead of building a separate view for a fixed list of frames.
If that's too much for this PR, no problem: just raise _MAX_DETECTIONS_PER_SEQUENCE to 20, and we can do the browsing in a follow-up.
Happy to discuss it, otherwise, the PR is fine for a first iteration !
fe51
left a comment
There was a problem hiding this comment.
Following up on my comment above: on second thought, I think matching how the platform loads detections is needed rather than optional. Otherwise the sharer and the recipient see different frames, and the frontend has to build a separate view instead of reusing the existing sequence components.
Requesting changes for that point only, everything else looks good to me.
|
hi @fe51 @MateoLostanlen, agreed, done in c84d127 😊 Any other comments or ready to approve?
The main link keeps a 10-frame preview per sequence. It skips continuity rows. The browse route keeps them, like the platform. Small ask: could you leave future comments as a PR review with inline comments on the code? Much easier for me to follow and resolve 🙇 Thanks! |
|
@MateoLostanlen I still need your approval please 🥲 |
Closes #720, part of pyronear/roadmap#31.
What it does
An operator can send an alert to someone with no account (a dispatcher, a mayor) through a link that opens without logging in, stops working after 48 h, and can be cancelled.
alert_shares: alert, source organization, creator, SHA-256 of the token,created_at,expires_at,revoked_at. Only the hash is stored, so a link cannot be shown again after creation.POST /shares {alert_id}returnsshare_id, the token (once) andexpires_atGET /shares?alert_id=lists the organization's links with astatus(active,expired,revoked)DELETE /shares/{share_id}cancels (soft revoke, idempotent)services/shares.py:GET /shares/public/{token}: alert time and fire position, and per sequence its id, the camera name and position, azimuths, and the 10 latest real detections as a preview. No organization, labels, device IPs, livestream or occlusion masks.GET /shares/public/{token}/sequences/{sequence_id}/detections: browse a sequence the way the platform does, with the samelimit,offset,desc,sampling,with_cropandX-Sampled-*headers asGET /sequences/{sequence_id}/detections(both callfetch_detection_page), continuity rows included, so the sharer and the recipient see the same frames. 404 unless the sequence belongs to the shared alert. Detections carryid,created_at,recorded_at,bbox,url,crop_url, the platform's field names, so the existing sequence viewer can read them; camera, pose and bucket keys stay out.secrets.token_urlsafe(32)) make enumeration infeasible, so the distinction leaks nothing and lets the public page say "expired" rather than "invalid".SHARE_URL_EXPIRATION) on the camera organization's bucket. The lifetime is fixed so the existing per-bucket URL cache keeps working.Cache-Control: no-storeon the public response.Alert lifecycle
Alerts are deleted in three places (
DELETE /alerts/{id}, the bulk delete in_merge_alerts, andrefresh_alert_statewhen an alert loses its last sequence), and none of them knew about other tables. So:alert_shares.alert_idisON DELETE CASCADEat DB level, which also covers the bulk delete._merge_alertsrepoints shares to the surviving alert before deleting the absorbed one, so a link sent just before a merge keeps working.Next steps: sharing with another organization (pyronear/roadmap#33)
This PR is meant as the first case of the model #33 would extend, not a parallel mechanism. Proposal for the #33 discussion: reference, not copy.
camera.organization_id. Each of those routes would need loosening.target_organization_id(andtoken_hashnullable, with a CHECK that exactly one recipient is set, or an explicitrecipient_kind).GET /shares/received) that reusebuild_shared_alert, so it gets the same whitelist. Existing alert, sequence and detection routes stay untouched.Nothing for #33 is built here; the table name, the source-organization column and the service split are what keep that path open.
Known limits
ShareTokenFilteronuvicorn.access) and in Sentry events (scrub_share_tokenmasks the URL, drops the Referer and drops frame locals). Other places that see the raw path, such as a reverse proxy's logs, still record it.unmatchor a relabel), its links go with it and answer 404. Only merges repoint.Tests
tests/endpoints/test_shares.py: create, list, cancel across roles and organizations; public route for valid, unknown, expired, revoked and freshly cancelled links; payload whitelist; continuity frames skipped in the preview; browsing route scoped to the shared alert, and returning the same frames andX-Sampled-*headers as the platform route across paging and sampling; cascade on alert deletion; repoint on merge.test_security.py, Sentry scrub and access log filter intest_main.py.upgrade,downgrade -1,upgraderound trip OK,alembic checkreports no drift. ruff and ty clean.