🐛 Scope components in share-link view-only bundles (#13118)

* 🐛 Scope share-link component reads to the allowed pages

A page-scoped share link exposed the primary file's components
map in get-view-only-bundle and authorized the file's components
fragment in get-file-fragment, leaking the stored shapes of
deleted components from pages the link excludes.

The share-link bundle now trims the primary file's components to
those referenced transitively from the allowed pages and emits them
inline; the membership path keeps the full map. The fragment scope
check no longer grants the components pointer to share links;
allowed page pointers remain readable.

AI-assisted-by: inkling-2-small-free

* 🐛 Tighten share-link bundle component retention

The share-link view-only bundle now carries only live components
referenced transitively from the allowed pages; deleted components
never travel (their stored shapes are the main vehicle of leaked
excluded-page content). This drops the reachability walk at deleted
references and removes the duplicated subtree helper in favor of the
canonical get-component-shapes.

Also adds membership retention and cycle regression tests, and drops
the private advisory identifiers from the new test comments.

AI-assisted-by: inkling-2-small-free

* ♻️ Apply the second review's fixes to the share-link fix

Ordering the namespace require so the format gate passes, renaming
the working bindings to what they hold, documenting the eager-realize
limitation at the realization site, sharing the same-file reference
predicate, and switching the fragment scope check to an allowlist of
the pages scope.

Test markers are anonymized; the walk-through-deleted contract and
the full-scope link get dedicated tests; pointer-mapped files assert
an inline referenced component and the empty-components form.

AI-assisted-by: inkling-2-small-free

* ♻️ Apply the third review's minor fixes to the share-link fix

Correct the comment about realization reuse, align the new test bindings,
and drop the tautological fixture assertions. Cover the fragment scope
for excluded pages, membership on pointer-mapped files, and the legacy
same-file reference form; make both permission memories state the
actual invariants.

AI-assisted-by: inkling-2-small-free

* 🐛 Scope the share-link library trim to the emitted bundle

The library trim walked the primary file's full components map, deleted
components included. A deleted component keeps its main-instance subtree
in :objects, so a library component instanced only inside those stored
shapes was retained in the linked libraries of the view-only bundle and
reached anonymous share-link viewers, metadata and stored shapes
included.

Walk the data the bundle emits instead: pages scoped to the link plus the
trimmed components map. Pages still come from the realized file, because
pointer-mapped pages only load in their own loader.

Covered by a regression test that instances two library components inside
a deleted primary component: only the library component the link's page
references survives the trim.

AI-assisted-by: deepseek-v4.1-flash
This commit is contained in:
Andrey Antukh 2026-11-20 12:06:02 +01:00 committed by GitHub
parent 3c24fb0e48
commit 212ee6ef48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 1266 additions and 18 deletions

View File

@ -28,6 +28,7 @@
- File/project role rows only count with a live team membership: the file/project permission queries require a matching `team_profile_rel` row, and leaving or removing a member deletes their team-scoped `file`/`project`/`team-project-profile-rel` rows (GHSA-v9r9-h77c-55m2).
- File/project/comment checks are implemented in the owning command namespaces, often via helpers imported from `files`, `teams`, or `projects`; do not bypass those helpers with direct DB lookups unless preserving their not-found semantics.
- Comment permission includes both logged-in state and the file/team comment policy. Shared viewer paths may pass `share-id`; preserve that path when changing comment queries. RPCs that accept `share-id` enforce the link's page scope before mutating (`share-link-pages` + `check-page-scope`): a thread or page outside the link raises `:not-found`, and that check runs before the ownership checks. `get-profiles-for-file-comments` filters its query by the same pages. `mark-all-threads-as-read` does not take `share-id`.
- Share-link view-only bundles are page-scoped for components too: `get-view-only-bundle` filters `pages`/`pages-index` to the link, trims the primary file's `:components` to live components referenced transitively from the allowed pages (deleted components never travel), and emits them inline; membership bundles keep the full primary components map. Linked libraries are trimmed to the components the emitted data references, and that walk reads the emitted data itself (scoped pages plus the trimmed components map) rather than the full realized file, so neither excluded pages nor deleted primary components — whose stored `:objects` may instance library components — retain a library component; library pages never travel either. `get-file-fragment` (`check-fragment-scope`) authorizes only fragments of the link's pages for share links (`:not-found` otherwise): no root-level pointer, components included, is ever authorized.
## Teams, projects, and invitations

View File

@ -16,6 +16,6 @@
- `app.main.ui.ui` parses the viewer's `file-id` query param with `uuid/parse*`, so a missing or malformed id arrives as nil. `viewer-page*` calls `check-file-id!` (same namespace) during render, before mounting `viewer*`: it raises `ex/raise :type :not-found :code :missing-file-id`, which the error boundary turns into the not-found page. Without the guard the `dv/initialize` assertion fails on the nil file-id and the user gets an internal-error page. Test: `frontend-tests.ui.viewer-test/check-file-id-raises-not-found-for-invalid-file-id`.
- Viewer initialization sets `:current-file-id`, `:current-share-id`, and `:viewer-local`, then fetches the view-only bundle. Comment threads are fetched only for logged-in users.
- Viewer bundle fetch sends the full supported feature set because anonymous shared viewers may not know team-enabled features.
- View-only bundles can contain pointer values in `:pages-index` and file data. Viewer resolves those fragments with `:get-file-fragment` before storing the bundle, through `app.main.features.pointer-map/resolve-pointer` (share-link scoped; the workspace and library loaders use `resolve-file`, which resolves the same two levels). Both fail loudly: a fragment that cannot be resolved rejects the stream and is logged with file id and fragment id instead of silently dropping the page.
- View-only bundles can contain pointer values in `:pages-index` and file data (membership bundles; share-link bundles carry components inline and deny their fragment). Viewer resolves those fragments with `:get-file-fragment` before storing the bundle, through `app.main.features.pointer-map/resolve-pointer` (share-link scoped; the workspace and library loaders use `resolve-file`, which resolves the same two levels). Both fail loudly: a fragment that cannot be resolved rejects the stream and is logged with file id and fragment id instead of silently dropping the page.
- `bundle-fetched` indexes pages and precomputes viewer frames/all-frames, stores libraries/users/thumbnails/permissions under `:viewer`, then navigates to frame id, query index, or auto-selected frame.
- Viewer zoom and interaction mode changes update both `:viewer-local` and the `:viewer` route query params. `update-zoom-querystring` guards with a query-param comparison (`not= current expected`) and uses `::rt/replace`; without that guard the load sequence (`bundle-fetched` → `zoom-to-fill` → `update-zoom-querystring` → `rt/nav`) re-runs on every navigation and crashes with React "maximum update depth exceeded" (2.18.0-RC5 regression, fixed in `31b73460c3`; regression test: `bundle-fetched-with-zoom-fill-url-does-not-navigate`).

View File

@ -256,12 +256,15 @@
(feat.fdata/decode-file-data cfg))]
(select-keys fragment (sm/keys schema:file-fragment)))))
(defn- check-fragment-scope!
(defn- check-fragment-scope
"Checks that the fragment is reachable from the pages authorized by
the share-link. Raises a :not-found exception if the fragment is not reachable."
the share-link. Raises a :not-found exception if the fragment is not reachable.
Only the fragments of the link's pages are authorized: root-level pointers
(such as the file's components) never pass."
[cfg file-id fragment-id pages]
(let [fdata (-> (bfc/get-file cfg file-id :read-only? true)
(get :data)
(select-keys [:pages-index])
(update :pages-index select-keys pages))]
(when-not (contains? (feat.fdata/get-used-pointer-ids fdata) fragment-id)
(ex/raise :type :not-found
@ -279,7 +282,7 @@
(let [perms (perms/get-file-read-permissions cfg profile-id file-id share-id)]
(check-read-permissions! perms)
(when (= :share-link (:type perms))
(check-fragment-scope! cfg file-id fragment-id (:pages perms)))
(check-fragment-scope cfg file-id fragment-id (:pages perms)))
(-> (get-file-fragment cfg file-id fragment-id)
(rph/with-http-cache long-cache-duration))))))

View File

@ -12,6 +12,7 @@
[app.common.features :as cfeat]
[app.common.files.helpers :as cfh]
[app.common.schema :as sm]
[app.common.types.file :as ctf]
[app.config :as cf]
[app.db :as db]
[app.features.fdata :as fdata]
@ -37,8 +38,19 @@
(update :pages (fn [pages] (filterv #(contains? allowed %) pages)))
(update :pages-index select-keys allowed)))
(defn- same-file-component-ref?
"Whether `shape` references a component of this same file. A reference
missing `:component-file` means a same-file reference."
[file-id shape]
(and (associative? shape)
(some? (:component-id shape))
(or (nil? (:component-file shape))
(= file-id (:component-file shape)))))
(defn- find-library-refs
"Return `{lib-id #{component-id}}` referenced by the given shapes."
"Return `{lib-id #{component-id}}` referenced by the given shapes.
Counterpart of `same-file-refs`, kept for the library side: this one
requires a non-nil `:component-file`; same-file references are none."
[shapes]
(reduce (fn [acc shape]
(if (and (map? shape)
@ -67,10 +79,12 @@
(cfh/get-children-with-self objects main-instance-id)))))
(defn- collect-used-library-components
"Walk the objects of the (already page-scoped) primary file data and of
the referenced library components themselves, and return a map of
`{lib-id #{component-id}}` with every library component the allowed
pages need. Follows nested references to a fixpoint so trimming never
"Walk the objects of the primary file data the view-only bundle emits,
and of the referenced library components themselves, and return a map
of `{lib-id #{component-id}}` with every library component the emitted
data needs. The caller passes that data: pages scoped to the link and
the already trimmed components map, so deleted components are never
walked. Follows nested references to a fixpoint so trimming never
drops a component that is only reached through another component."
[primary-data libs-by-id]
(let [root-shapes (concat (mapcat (comp vals :objects) (vals (:pages-index primary-data)))
@ -100,6 +114,48 @@
(assoc-in [:data :pages-index] {})
(update-in [:data :components] select-keys (get used-refs id #{}))))
(defn- same-file-refs
"Return the ids of this file's components referenced by `shapes`.
Counterpart of `find-library-refs`, which handles the library side.
A reference missing `:component-file` means a same-file reference;
library references are handled by `trim-library-data` and skipped."
[file-id shapes]
(into #{}
(keep (fn [shape]
(when (same-file-component-ref? file-id shape)
(:component-id shape))))
shapes))
(defn- referenced-component-ids
"Return the ids of the primary file's live components a share-link
viewer of `allowed` pages must receive: every live component referenced
transitively from those pages. A referenced component that is deleted
is neither emitted nor expanded; the bundle never carries stored
shapes of deleted components. References are traced through the full
realized data — a component's subtree may live on an excluded page —
but only the ids are returned; the emitted bundle carries the trimmed
map inline. Terminates on cycles."
[realized-file allowed]
(let [file-id (:id realized-file)
seed-shapes (->> (vals (select-keys (:pages-index (:data realized-file)) allowed))
(mapcat (comp vals :objects)))]
(loop [seen #{}
pending (vec (same-file-refs file-id seed-shapes))]
(if (empty? pending)
seen
(let [comp-id (peek pending)
pending' (pop pending)]
(if (contains? seen comp-id)
(recur seen pending')
(let [component (get-in realized-file [:data :components comp-id])
live? (and (some? component) (not (:deleted component)))
extra (when live?
(same-file-refs file-id
(ctf/get-component-shapes (:data realized-file)
component)))]
(recur (if live? (conj seen comp-id) seen)
(into pending' extra)))))))))
(defn obfuscate-email
"Obfuscate the `email` for share-link members so the viewer only sees a
partially redacted address. Accepts any string shape (including nil,
@ -153,9 +209,23 @@
(cfeat/check-client-features! (:features params))
(cfeat/check-file-features! (:features file)))
;; The share-link bundle carries the primary file's components
;; too: reuse the realization the library trim already needs and
;; keep the emitted map inline. The walk may cross onto excluded
;; pages (a retained component's subtree can live there), so the
;; whole file is realized on each share-link bundle request.
;; Pointer-map is operator-enabled and not active in production;
;; revisit if it ships.
realized-data
(when (= :share-link (:type perms))
(fdata/realize-pointers cfg file))
file (cond-> file
(= :share-link (:type perms))
(update :data remove-not-allowed-pages (:pages perms))
(-> (update :data remove-not-allowed-pages (:pages perms))
(assoc-in [:data :components]
(select-keys (:components (:data realized-data))
(referenced-component-ids realized-data (:pages perms)))))
:always
(update :data select-keys view-only-data-keys))
@ -166,15 +236,20 @@
(bfc/file->response)))))
;; A share link never grants pages of a linked library, so trim
;; each library to the components the allowed pages reference.
;; File data may hold lazy pointer-mapped pages/components, so
;; realize each file with its own loader before walking it.
;; Membership bundles keep the full libraries.
;; each library to the components the emitted bundle references:
;; pages are scoped to the link and the components map already
;; dropped deleted components, so neither of them seeds library
;; references. The pages must come from the realized file, not
;; from `file`: lazy pointer-mapped pages only load in their own
;; loader. Membership bundles keep the full libraries.
libs (if (= :share-link (:type perms))
(let [file' (fdata/realize-pointers cfg file)
libs' (mapv #(fdata/realize-pointers cfg %) libs)
libs-by-id (d/index-by :id libs')
used (collect-used-library-components (:data file') libs-by-id)]
(let [allowed-data (-> realized-data
(update :data remove-not-allowed-pages (:pages perms))
(assoc-in [:data :components]
(get-in file [:data :components])))
libs' (mapv #(fdata/realize-pointers cfg %) libs)
libs-by-id (d/index-by :id libs')
used (collect-used-library-components (:data allowed-data) libs-by-id)]
(mapv #(trim-library-data used %) libs'))
libs)

File diff suppressed because it is too large Load Diff