feat(sharing): M8-13 receive-side envelope unpack — grant-key cache, real routes, ghost sweep (#223) #228

Merged
amiryahaya merged 1 commit from feat/m8-13-receive-envelope into main 2026-11-18 13:17:28 +01:00
Owner

Implements M8-13 (audit #214): the receive side of sharing never consumed the key envelope — recipients could not unwrap anything shared to them. First M4 follow-on to the M8-3 key provisioning (#227).

What was broken (ghost contract)

  • list_shared_with_me/list_my_shares hit routes that do not exist (/shares/incoming|outgoing — the server serves /received|/created)
  • The Share model (item_id/owner_*/status/signature) had no server counterpart — every list deserialization would fail
  • POST /shares/{id}/respond (accept/decline) has no server route; PUT /shares/{id} likewise (real: PATCH /permission + PATCH /expiry)
  • decrypt_share_folder_key (sharing_service.rs:268-284) was dead — the envelope sat opaque

What lands

  • Server: /received + /created projections gain ResourceName (batched per-type lookup — recipients need the name for the UI)
  • Desktop Rust: real routes + ShareDto (server projection); on incoming listing, each encrypted_key envelope is unpacked, the grant key decapsulated with the M8-3-provisioned KEM keys, and cached by resource id (new grant_key_cache table, AES-GCM under the account master key). Corrupt envelopes skip that share; the listing survives
  • folder_keys::resolve_folder_key consults the cache FIRST (a recipient can only open the wrap made FOR them); download_file consults the cached DEK for received file shares before the owner-wrap path
  • Envelope-hash guard: the server re-wraps Shares.EncryptedKey on folder-key rotation, so the cache is keyed on SHA-256(envelope) — revoke→rotate→re-grant re-decapsulates instead of serving a stale key (review I3)
  • Account switch clears the cache (M8-3 drop_stale_cache); undecryptable rows (stale/cross-account) fall through with a warning instead of erroring
  • Ghost sweep: accept/decline commands + TS buttons/store/mocks removed (7.3 lifecycle is future work); Share type → server projection; dead ShareStatus/StatusBadge removed

Review: dual-pass per repo rules — ponytail (cache-guard churn, cross-account fall-through, TestApi dedupe — all fixed) + full correctness pass (I1/I2 were already fixed in-tree; I3 caught the rotation staleness — fixed with the envelope hash; M1 expiry-clearing now always PATCHes; M2 SK strings zeroized; M3/M4 mock/type/test residue cleaned; M5 server pre-query breadth + M6 cache-warming-at-unlock noted as follow-ups).

Tests: 3 new Rust receive-path tests vs in-process HTTP stub (envelope consumed + cached; real folder_keys::decapsulate round trip with NO network — cache-before-network pinned; file-share DEK cache; corrupt-envelope survival). Gates: cargo 85/85, clippy clean, tsc clean, vitest 1282 green, server coverage 87.82% ≥ 80% (pre-push hook).

Part of #222/#223 M4 scope; receive-side unblocked by M8-3 (#227).

Implements **M8-13** (audit #214): the receive side of sharing never consumed the key envelope — recipients could not unwrap anything shared to them. First M4 follow-on to the M8-3 key provisioning (#227). **What was broken (ghost contract)** - `list_shared_with_me`/`list_my_shares` hit routes that do not exist (`/shares/incoming|outgoing` — the server serves `/received|/created`) - The `Share` model (item_id/owner_*/status/signature) had no server counterpart — every list deserialization would fail - `POST /shares/{id}/respond` (accept/decline) has no server route; `PUT /shares/{id}` likewise (real: PATCH `/permission` + PATCH `/expiry`) - `decrypt_share_folder_key` (sharing_service.rs:268-284) was dead — the envelope sat opaque **What lands** - Server: `/received` + `/created` projections gain `ResourceName` (batched per-type lookup — recipients need the name for the UI) - Desktop Rust: real routes + `ShareDto` (server projection); on incoming listing, each `encrypted_key` envelope is unpacked, the grant key decapsulated with the M8-3-provisioned KEM keys, and cached by resource id (new `grant_key_cache` table, AES-GCM under the account master key). Corrupt envelopes skip that share; the listing survives - `folder_keys::resolve_folder_key` consults the cache FIRST (a recipient can only open the wrap made FOR them); `download_file` consults the cached DEK for received file shares before the owner-wrap path - Envelope-hash guard: the server re-wraps `Shares.EncryptedKey` on folder-key rotation, so the cache is keyed on SHA-256(envelope) — revoke→rotate→re-grant re-decapsulates instead of serving a stale key (review I3) - Account switch clears the cache (M8-3 `drop_stale_cache`); undecryptable rows (stale/cross-account) fall through with a warning instead of erroring - Ghost sweep: accept/decline commands + TS buttons/store/mocks removed (7.3 lifecycle is future work); `Share` type → server projection; dead `ShareStatus`/`StatusBadge` removed **Review**: dual-pass per repo rules — ponytail (cache-guard churn, cross-account fall-through, TestApi dedupe — all fixed) + full correctness pass (I1/I2 were already fixed in-tree; I3 caught the rotation staleness — fixed with the envelope hash; M1 expiry-clearing now always PATCHes; M2 SK strings zeroized; M3/M4 mock/type/test residue cleaned; M5 server pre-query breadth + M6 cache-warming-at-unlock noted as follow-ups). **Tests**: 3 new Rust receive-path tests vs in-process HTTP stub (envelope consumed + cached; real `folder_keys::decapsulate` round trip with NO network — cache-before-network pinned; file-share DEK cache; corrupt-envelope survival). Gates: cargo 85/85, clippy clean, tsc clean, vitest 1282 green, server coverage 87.82% ≥ 80% (pre-push hook). Part of #222/#223 M4 scope; receive-side unblocked by M8-3 (#227).
feat(sharing): M8-13 receive-side envelope unpack — grant-key cache, real routes, ghost sweep (#223)
Some checks failed
API CI / migrations (pull_request) Successful in 5m57s
API CI / build-and-test (pull_request) Successful in 9m20s
Security CI / security-scan (pull_request) Failing after 16s
Desktop CI / lint-and-test (pull_request) Successful in 13m24s
18695eef9f
The receive side of sharing never consumed the key envelope (audit #214
M8-13): list_shared_with_me hit ghost routes (/shares/incoming|outgoing —
the server serves /received|/created), the Share model had no server
counterpart (every list deserialization would fail), and the envelope
base64(JSON{kem_ciphertext, wrapped_kek}) sat opaque in encrypted_key.

- Server: /received + /created projections gain ResourceName (batched
  per-type lookup — recipients need the name for the share list UI).
- Rust: list_shared_with_me → GET /shares/received (paged items), unpack
  each envelope with the M8-3-provisioned KEM keys, cache the decapsulated
  grant key by resource id (new grant_key_cache table, encrypted under the
  account master key). Corrupt envelopes skip that share with a warning —
  the listing survives. list_my_shares → /shares/created.
- folder_keys::resolve_folder_key consults the cache FIRST (a recipient
  can only open the wrap made FOR them — the folder DTO's string columns
  wrap the owner's key). download_file consults the DEK cache for received
  file shares before the owner-wrap path.
- Ghost sweep: accept_share/decline_share commands + /respond calls (no
  such server route — task 7.3 lifecycle), dead decrypt_share_folder_key,
  PUT /shares/{id} → real PATCH /permission + PATCH /expiry.
- TS: Share type → server projection; SharedWithMe/MyShares pages drop
  ghost status badges + accept/decline buttons; store + mocks + tests
  updated (14 ghost tests removed).

Tests: 3 new Rust receive-path tests (envelope consumed + cached, real
folder_keys::decapsulate round trip with no network, file-share DEK
cache, corrupt-envelope survival); cargo 85/85, clippy clean, tsc clean,
vitest 1289 green.
amiryahaya force-pushed feat/m8-13-receive-envelope from 18695eef9f
Some checks failed
API CI / migrations (pull_request) Successful in 5m57s
API CI / build-and-test (pull_request) Successful in 9m20s
Security CI / security-scan (pull_request) Failing after 16s
Desktop CI / lint-and-test (pull_request) Successful in 13m24s
to 3d3b097bfe
All checks were successful
Security CI / security-scan (pull_request) Successful in 3m9s
API CI / migrations (pull_request) Successful in 4m16s
API CI / build-and-test (pull_request) Successful in 6m41s
Desktop CI / lint-and-test (pull_request) Successful in 13m29s
2026-11-18 13:02:44 +01:00
Compare
Sign in to join this conversation.
No description provided.