diff --git a/.serena/memories/backend/auth-permissions-product-domains.md b/.serena/memories/backend/auth-permissions-product-domains.md index d49d2c4bff..941358c65a 100644 --- a/.serena/memories/backend/auth-permissions-product-domains.md +++ b/.serena/memories/backend/auth-permissions-product-domains.md @@ -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 diff --git a/.serena/memories/frontend/dashboard-viewer-subtleties.md b/.serena/memories/frontend/dashboard-viewer-subtleties.md index 148f5307e8..3a666e2484 100644 --- a/.serena/memories/frontend/dashboard-viewer-subtleties.md +++ b/.serena/memories/frontend/dashboard-viewer-subtleties.md @@ -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`). \ No newline at end of file diff --git a/backend/src/app/rpc/commands/files.clj b/backend/src/app/rpc/commands/files.clj index ba2428f576..80f22bf07f 100644 --- a/backend/src/app/rpc/commands/files.clj +++ b/backend/src/app/rpc/commands/files.clj @@ -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)))))) diff --git a/backend/src/app/rpc/commands/viewer.clj b/backend/src/app/rpc/commands/viewer.clj index d757f4018d..c2dd861d10 100644 --- a/backend/src/app/rpc/commands/viewer.clj +++ b/backend/src/app/rpc/commands/viewer.clj @@ -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) diff --git a/backend/test/backend_tests/rpc_viewer_test.clj b/backend/test/backend_tests/rpc_viewer_test.clj index 58e568ef02..b4bbfef999 100644 --- a/backend/test/backend_tests/rpc_viewer_test.clj +++ b/backend/test/backend_tests/rpc_viewer_test.clj @@ -6,12 +6,16 @@ (ns backend-tests.rpc-viewer-test (:require + [app.binfile.common :as bfc] [app.common.types.shape :as cts] [app.common.uuid :as uuid] [app.db :as db] + [app.features.fdata :as fdata] [app.rpc :as-alias rpc] [app.rpc.commands.viewer :as viewer] + [app.util.pointer-map :as pmap] [backend-tests.helpers :as th] + [clojure.string :as str] [clojure.test :as t] [datoteka.fs :as fs])) @@ -945,3 +949,1168 @@ (t/is (not (contains? (:file result) :storage-object-id))) (t/is (every? #(not (contains? % :storage-object-id)) (:libraries result)))))) + +(t/deftest share-link-bundle-omits-unreferenced-deleted-component + ;; Regression test: a page-A-only share link received the primary + ;; file's full :components map, leaking the stored shapes of a deleted + ;; component whose main instance lived on the excluded page B. + (let [owner (th/create-profile* 1 {:is-active true}) + proj-id (:default-project-id owner) + file (th/create-file* 1 {:profile-id (:id owner) + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + page-b (uuid/random) + shape (uuid/random) + comp (uuid/random) + marker "SIICA-SYNTHETIC-DELETED" + + _ (update-file! :profile-id (:id owner) + :file-id file-id + :changes [{:type :add-page + :id page-b + :page {:id page-b + :name "Hidden" + :options {} + :objects {}}} + {:type :add-obj + :page-id page-b + :id shape + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id shape + :name marker + :frame-id uuid/zero + :parent-id uuid/zero + :type :rect})} + {:type :add-component + :path "" + :name "retired" + :main-instance-id shape + :main-instance-page page-b + :id comp + :anotation nil} + {:type :del-component + :id comp}]) + + slink (th/command! {::th/type :create-share-link + ::rpc/profile-id (:id owner) + :file-id file-id + :pages #{page-a} + :who-comment "team" + :who-inspect "all"}) + slink-id (get-in slink [:result :id])] + + (t/testing "share-link viewer does not receive the deleted component" + (let [out (th/command! {::th/type :get-view-only-bundle + :share-id slink-id + :file-id file-id}) + result (:result out)] + (t/is (nil? (:error out))) + (t/is (not (contains? (get-in result [:file :data :components]) comp))) + (t/is (= {} (get-in result [:file :data :components]))) + (t/is (not (str/includes? (pr-str out) marker))))) + + (t/testing "the excluded page is still absent from the bundle" + (let [out (th/command! {::th/type :get-view-only-bundle + :share-id slink-id + :file-id file-id}) + result (:result out)] + (t/is (= [page-a] (get-in result [:file :data :pages]))) + (t/is (not (contains? (get-in result [:file :data :pages-index]) page-b))))) + + (t/testing "team member still receives the full components map" + (let [out (th/command! {::th/type :get-file + ::rpc/profile-id (:id owner) + :id file-id}) + comps (get-in out [:result :data :components])] + (t/is (nil? (:error out))) + (t/is (contains? comps comp)))) + + (t/testing "team member bundle keeps the full primary components map" + (let [out (th/command! {::th/type :get-view-only-bundle + ::rpc/profile-id (:id owner) + :file-id file-id}) + result (:result out) + comps (get-in result [:file :data :components])] + (t/is (nil? (:error out))) + ;; The membership bundle carries lazy values; resolve the + ;; components pointer with the file's own loader before checking. + (fdata/with-pointers th/*system* file-id + (t/is (contains? comps comp))))))) + +(t/deftest share-link-bundle-keeps-referenced-components-and-nested-refs + ;; Companion to the regression test above: the trim must keep + ;; everything an allowed page actually renders. Component X is + ;; referenced from page A but its main instance lives on the excluded + ;; page B; X's subtree contains an instance of Y. A trim that does not + ;; follow the fixpoint drops Y and breaks rendering. W is unreferenced + ;; anywhere and must not be kept. + (let [owner (th/create-profile* 1 {:is-active true}) + proj-id (:default-project-id owner) + file (th/create-file* 1 {:profile-id (:id owner) + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + page-b (uuid/random) + + x-main (uuid/random) + y-main (uuid/random) + w-main (uuid/random) + x-child (uuid/random) + inst (uuid/random) + comp-x (uuid/random) + comp-y (uuid/random) + comp-w (uuid/random) + + mk-frame (fn [id parent-id frame-id] + (cts/setup-shape {:id id + :name (str id) + :frame-id frame-id + :parent-id parent-id + :type :frame})) + + _ (update-file! :profile-id (:id owner) + :file-id file-id + :changes [{:type :add-page + :id page-b + :page {:id page-b + :name "Hidden" + :options {} + :objects {}}} + ;; X's main instance on the excluded page, + ;; containing an instance of Y. + {:type :add-obj + :page-id page-b + :id x-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (assoc (mk-frame x-main uuid/zero uuid/zero) + :main-instance true + :component-root true + :component-file file-id + :component-id comp-x)} + {:type :add-obj + :page-id page-b + :id x-child + :parent-id x-main + :frame-id x-main + :components-v2 true + :obj (cts/setup-shape + {:id x-child + :name "nested" + :frame-id x-main + :parent-id x-main + :type :frame + :component-file file-id + :component-id comp-y})} + ;; Y's main instance on the allowed page. + {:type :add-obj + :page-id page-a + :id y-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (assoc (mk-frame y-main uuid/zero uuid/zero) + :main-instance true + :component-root true + :component-file file-id + :component-id comp-y)} + ;; W's main instance on the excluded page; + ;; referenced nowhere. + {:type :add-obj + :page-id page-b + :id w-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (assoc (mk-frame w-main uuid/zero uuid/zero) + :main-instance true + :component-root true + :component-file file-id + :component-id comp-w)} + {:type :add-component + :path "" + :name "wrapper" + :main-instance-id x-main + :main-instance-page page-b + :id comp-x + :anotation nil} + {:type :add-component + :path "" + :name "leaf" + :main-instance-id y-main + :main-instance-page page-a + :id comp-y + :anotation nil} + {:type :add-component + :path "" + :name "unreferenced" + :main-instance-id w-main + :main-instance-page page-b + :id comp-w + :anotation nil} + ;; A-page instance of X: the only reference + ;; reachable from the allowed pages. + {:type :add-obj + :page-id page-a + :id inst + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id inst + :name "instance" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :component-file file-id + :component-id comp-x})}]) + + slink (th/command! {::th/type :create-share-link + ::rpc/profile-id (:id owner) + :file-id file-id + :pages #{page-a} + :who-comment "team" + :who-inspect "all"}) + slink-id (get-in slink [:result :id])] + + (t/testing "bundle keeps X (referenced from A) and Y (nested ref reached from X)" + (let [out (th/command! {::th/type :get-view-only-bundle + :share-id slink-id + :file-id file-id}) + result (:result out) + comps (get-in result [:file :data :components])] + (t/is (nil? (:error out))) + (t/is (contains? comps comp-x)) + (t/is (contains? comps comp-y)) + (t/is (not (contains? comps comp-w))))))) + +(t/deftest share-link-fragment-scope-blocks-components-pointer + ;; Regression test: the share-link scope check for get-file-fragment + ;; let the file's components fragment through because + ;; get-used-pointer-ids collects top-level pointers indiscriminately. + (let [owner (th/create-profile* 1 {:is-active true}) + owner-id (:id owner) + proj-id (:default-project-id owner) + file (th/create-file* 1 {:profile-id owner-id + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + page-b (uuid/random) + marker "SIICA-SYNTHETIC-FRAGMENT" + shape (uuid/random) + comp (uuid/random) + + _ (update-file! :profile-id owner-id + :file-id file-id + :changes [{:type :add-page + :id page-b + :page {:id page-b + :name "Excluded" + :options {} + :objects {}}} + {:type :add-obj + :page-id page-a + :id shape + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id shape + :name marker + :frame-id uuid/zero + :parent-id uuid/zero + :type :rect})} + {:type :add-component + :path "" + :name "secret" + :main-instance-id shape + :main-instance-page page-a + :id comp + :anotation nil} + {:type :del-component + :id comp}]) + + ;; Enable pointer-map on the file, the way a migration would: + ;; wrap the pages and components in pointers and persist them. + _ (db/tx-run! th/*system* + (fn [cfg] + (fdata/with-tracked-pointers cfg file-id + (bfc/update-file! cfg + (-> (bfc/get-file cfg file-id) + (fdata/enable-pointer-map)))))) + + comp-ptr-id (db/tx-run! th/*system* + (fn [cfg] + (pmap/get-id (:components (:data (bfc/get-file cfg file-id)))))) + page-ptr-id (db/tx-run! th/*system* + (fn [cfg] + (pmap/get-id (get (:pages-index (:data (bfc/get-file cfg file-id))) + page-a)))) + page-b-ptr-id (db/tx-run! th/*system* + (fn [cfg] + (pmap/get-id (get (:pages-index (:data (bfc/get-file cfg file-id))) + page-b)))) + + stranger (th/create-profile* 2 {:is-active true}) + + slink (th/command! {::th/type :create-share-link + ::rpc/profile-id owner-id + :file-id file-id + :pages #{page-a} + :who-comment "team" + :who-inspect "all"}) + slink-id (get-in slink [:result :id])] + + (t/testing "stranger with a page-A-only share link cannot read the components fragment" + (let [out (th/command! {::th/type :get-file-fragment + ::rpc/profile-id (:id stranger) + :share-id slink-id + :file-id file-id + :fragment-id comp-ptr-id}) + err (:error out) + edata (ex-data err)] + (t/is (th/ex-info? err)) + (t/is (= :not-found (:type edata))))) + + (t/testing "the fragment of a page outside the link is denied" + (let [out (th/command! {::th/type :get-file-fragment + ::rpc/profile-id (:id stranger) + :share-id slink-id + :file-id file-id + :fragment-id page-b-ptr-id}) + err (:error out) + edata (ex-data err)] + (t/is (th/ex-info? err)) + (t/is (= :not-found (:type edata))))) + + (t/testing "the page fragment of an allowed page remains available" + (let [out (th/command! {::th/type :get-file-fragment + ::rpc/profile-id (:id stranger) + :share-id slink-id + :file-id file-id + :fragment-id page-ptr-id})] + (t/is (nil? (:error out))) + (t/is (= page-ptr-id (get-in out [:result :id]))))))) + +(t/deftest share-link-bundle-omits-unreferenced-deleted-component-when-pointer-mapped + ;; Same defect as the regression test above, on a pointer-mapped + ;; file: the trim emits the referenced subset inline so the served + ;; bundle never carries (nor references) the full components map. + (let [owner (th/create-profile* 1 {:is-active true}) + proj-id (:default-project-id owner) + file (th/create-file* 1 {:profile-id (:id owner) + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + page-b (uuid/random) + shape (uuid/random) + comp (uuid/random) + live-main (uuid/random) + live-inst (uuid/random) + comp-live (uuid/random) + marker "SIICA-SYNTHETIC-DELETED-PM" + + _ (update-file! :profile-id (:id owner) + :file-id file-id + :changes [{:type :add-page + :id page-b + :page {:id page-b + :name "Hidden" + :options {} + :objects {}}} + {:type :add-obj + :page-id page-b + :id shape + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id shape + :name marker + :frame-id uuid/zero + :parent-id uuid/zero + :type :rect})} + {:type :add-component + :path "" + :name "retired" + :main-instance-id shape + :main-instance-page page-b + :id comp + :anotation nil} + {:type :del-component + :id comp} + ;; A live component referenced from + ;; the allowed page must still reach + ;; the bundle inline. + {:type :add-obj + :page-id page-a + :id live-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id live-main + :name "live" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :main-instance true + :component-root true + :component-file file-id + :component-id comp-live})} + {:type :add-component + :path "" + :name "live" + :main-instance-id live-main + :main-instance-page page-a + :id comp-live + :anotation nil} + {:type :add-obj + :page-id page-a + :id live-inst + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id live-inst + :name "live-instance" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :component-file file-id + :component-id comp-live})}]) + + _ (db/tx-run! th/*system* + (fn [cfg] + (fdata/with-tracked-pointers cfg file-id + (bfc/update-file! cfg + (-> (bfc/get-file cfg file-id) + (fdata/enable-pointer-map)))))) + + slink (th/command! {::th/type :create-share-link + ::rpc/profile-id (:id owner) + :file-id file-id + :pages #{page-a} + :who-comment "team" + :who-inspect "all"}) + slink-id (get-in slink [:result :id])] + + (t/testing "bundle emits components inline as a plain map without the deleted component" + (let [out (th/command! {::th/type :get-view-only-bundle + :share-id slink-id + :file-id file-id}) + result (:result out) + comps (get-in result [:file :data :components])] + (t/is (nil? (:error out))) + (t/is (not (pmap/pointer-map? comps))) + (t/is (not (contains? comps comp))) + (t/is (not (str/includes? (pr-str out) marker))) + (t/is (contains? comps comp-live)))))) + +(t/deftest share-link-bundle-omits-referenced-deleted-component + ;; The share-link bundle never carries a component marked as deleted, + ;; even when the allowed page instances it: its stored shapes may + ;; hold content of excluded pages. Instances render from the flat + ;; page graph; the bundle needs no deleted components at all. + (let [owner (th/create-profile* 1 {:is-active true}) + proj-id (:default-project-id owner) + file (th/create-file* 1 {:profile-id (:id owner) + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + page-b (uuid/random) + + main (uuid/random) + inst (uuid/random) + comp (uuid/random) + marker "SIICA-SYNTHETIC-REFERENCED" + + _ (update-file! :profile-id (:id owner) + :file-id file-id + :changes [{:type :add-page + :id page-b + :page {:id page-b + :name "Hidden" + :options {} + :objects {}}} + {:type :add-obj + :page-id page-b + :id main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id main + :name marker + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :main-instance true + :component-root true + :component-file file-id + :component-id comp})} + {:type :add-component + :path "" + :name "retired" + :main-instance-id main + :main-instance-page page-b + :id comp + :anotation nil} + {:type :del-component + :id comp} + ;; An instance of the deleted + ;; component on the allowed page. + {:type :add-obj + :page-id page-a + :id inst + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id inst + :name "instance" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :component-file file-id + :component-id comp})}]) + + slink (th/command! {::th/type :create-share-link + ::rpc/profile-id (:id owner) + :file-id file-id + :pages #{page-a} + :who-comment "team" + :who-inspect "all"}) + slink-id (get-in slink [:result :id])] + + (t/testing "the share bundle omits the referenced deleted component" + (let [out (th/command! {::th/type :get-view-only-bundle + :share-id slink-id + :file-id file-id}) + result (:result out) + comps (get-in result [:file :data :components])] + (t/is (nil? (:error out))) + (t/is (not (contains? comps comp))) + (t/is (not (str/includes? (pr-str out) marker))))))) + +(t/deftest share-link-bundle-keeps-mutually-referencing-components + ;; Components may reference each other (X contains an instance of Y + ;; and Y an instance of X). The reachability fixpoint must terminate + ;; and both components must reach the bundle. + (let [owner (th/create-profile* 1 {:is-active true}) + proj-id (:default-project-id owner) + file (th/create-file* 1 {:profile-id (:id owner) + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + page-b (uuid/random) + + x-main (uuid/random) + x-child (uuid/random) + y-main (uuid/random) + y-child (uuid/random) + inst (uuid/random) + comp-x (uuid/random) + comp-y (uuid/random) + + mk-frame (fn [id parent-id frame-id extra] + (cts/setup-shape (merge {:id id + :name (str id) + :frame-id frame-id + :parent-id parent-id + :type :frame} + extra))) + + _ (update-file! :profile-id (:id owner) + :file-id file-id + :changes [{:type :add-page + :id page-b + :page {:id page-b + :name "Hidden" + :options {} + :objects {}}} + ;; X on the excluded page, containing Y. + {:type :add-obj + :page-id page-b + :id x-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (mk-frame x-main uuid/zero uuid/zero + {:main-instance true + :component-root true + :component-file file-id + :component-id comp-x})} + {:type :add-obj + :page-id page-b + :id x-child + :parent-id x-main + :frame-id x-main + :components-v2 true + :obj (mk-frame x-child x-main x-main + {:component-file file-id + :component-id comp-y})} + ;; Y on the allowed page, containing X. + {:type :add-obj + :page-id page-a + :id y-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (mk-frame y-main uuid/zero uuid/zero + {:main-instance true + :component-root true + :component-file file-id + :component-id comp-y})} + {:type :add-obj + :page-id page-a + :id y-child + :parent-id y-main + :frame-id y-main + :components-v2 true + :obj (mk-frame y-child y-main y-main + {:component-file file-id + :component-id comp-x})} + {:type :add-component + :path "" + :name "cross-a" + :main-instance-id x-main + :main-instance-page page-b + :id comp-x + :anotation nil} + {:type :add-component + :path "" + :name "cross-b" + :main-instance-id y-main + :main-instance-page page-a + :id comp-y + :anotation nil} + ;; An instance of X on the allowed page. + {:type :add-obj + :page-id page-a + :id inst + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (mk-frame inst uuid/zero uuid/zero + {:component-file file-id + :component-id comp-x})}]) + + slink (th/command! {::th/type :create-share-link + ::rpc/profile-id (:id owner) + :file-id file-id + :pages #{page-a} + :who-comment "team" + :who-inspect "all"}) + slink-id (get-in slink [:result :id])] + + (t/testing "bundle terminates and keeps both mutually referencing components" + (let [out (th/command! {::th/type :get-view-only-bundle + :share-id slink-id + :file-id file-id}) + result (:result out) + comps (get-in result [:file :data :components])] + (t/is (nil? (:error out))) + (t/is (contains? comps comp-x)) + (t/is (contains? comps comp-y)))))) + +(t/deftest share-link-bundle-does-not-expand-through-deleted-component + ;; A deleted component stores the shapes of its subtree. A live + ;; component referenced only from within those stored shapes is out + ;; of scope: the reachability walk does not expand through deleted + ;; components, so it stays out of the share-link bundle. + (let [owner (th/create-profile* 1 {:is-active true}) + proj-id (:default-project-id owner) + file (th/create-file* 1 {:profile-id (:id owner) + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + page-b (uuid/random) + + e-main (uuid/random) + main (uuid/random) + nested (uuid/random) + inst (uuid/random) + comp-d (uuid/random) + comp-e (uuid/random) + + _ (update-file! :profile-id (:id owner) + :file-id file-id + :changes [{:type :add-page + :id page-b + :page {:id page-b + :name "Hidden" + :options {} + :objects {}}} + ;; E: live component, main instance on + ;; the excluded page; referenced only + ;; inside deleted D's stored shapes. + {:type :add-obj + :page-id page-b + :id e-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id e-main + :name "live-leaf" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :main-instance true + :component-root true + :component-file file-id + :component-id comp-e})} + ;; D: deleted component with an + ;; instance of E inside. + {:type :add-obj + :page-id page-b + :id main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id main + :name "retired" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :main-instance true + :component-root true + :component-file file-id + :component-id comp-d})} + {:type :add-obj + :page-id page-b + :id nested + :parent-id main + :frame-id main + :components-v2 true + :obj (cts/setup-shape + {:id nested + :name "leaf-instance" + :frame-id main + :parent-id main + :type :frame + :component-file file-id + :component-id comp-e})} + {:type :add-component + :path "" + :name "retired" + :main-instance-id main + :main-instance-page page-b + :id comp-d + :anotation nil} + {:type :add-component + :path "" + :name "leaf" + :main-instance-id e-main + :main-instance-page page-b + :id comp-e + :anotation nil} + {:type :del-component + :id comp-d} + ;; An instance of D on the allowed page. + {:type :add-obj + :page-id page-a + :id inst + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id inst + :name "instance" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :component-file file-id + :component-id comp-d})}]) + + slink (th/command! {::th/type :create-share-link + ::rpc/profile-id (:id owner) + :file-id file-id + :pages #{page-a} + :who-comment "team" + :who-inspect "all"}) + slink-id (get-in slink [:result :id])] + + (t/testing "live E, referenced only through deleted D, stays out" + (let [out (th/command! {::th/type :get-view-only-bundle + :share-id slink-id + :file-id file-id}) + result (:result out) + comps (get-in result [:file :data :components])] + (t/is (nil? (:error out))) + (t/is (not (contains? comps comp-d))) + (t/is (not (contains? comps comp-e))))))) + +(t/deftest share-link-bundle-drops-library-components-of-deleted-primary-component + ;; A deleted primary component keeps its main-instance subtree in + ;; `:objects`. A library component instanced only inside those stored + ;; shapes is out of scope: the library trim walks the data the bundle + ;; emits, whose `:components` map no longer carries deleted components. + (let [owner (th/create-profile* 1 {:is-active true}) + proj-id (:default-project-id owner) + + lib (th/create-file* 1 {:profile-id (:id owner) + :project-id proj-id + :is-shared true}) + lib-id (:id lib) + lib-page (first (get-in lib [:data :pages])) + + l-control (uuid/random) + l-live (uuid/random) + l-retired (uuid/random) + lc-main (uuid/random) + ll-main (uuid/random) + lr-main (uuid/random) + marker "SIICA-LIB-RETIRED-REF" + + frame (fn [id parent frame-id extra] + (cts/setup-shape + (merge {:id id + :name "Board" + :frame-id frame-id + :parent-id parent + :type :frame} + extra))) + + _ (update-file! + :profile-id (:id owner) + :file-id lib-id + :changes [{:type :add-obj + :page-id lib-page + :id lc-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (frame lc-main uuid/zero uuid/zero + {:main-instance true + :component-root true + :component-file lib-id + :component-id l-control})} + {:type :add-obj + :page-id lib-page + :id ll-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (frame ll-main uuid/zero uuid/zero + {:main-instance true + :component-root true + :component-file lib-id + :component-id l-live})} + {:type :add-obj + :page-id lib-page + :id lr-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (frame lr-main uuid/zero uuid/zero + {:main-instance true + :component-root true + :component-file lib-id + :component-id l-retired})} + {:type :add-component + :path "" + :name "control" + :main-instance-id lc-main + :main-instance-page lib-page + :id l-control + :anotation nil} + {:type :add-component + :path "" + :name "live" + :main-instance-id ll-main + :main-instance-page lib-page + :id l-live + :anotation nil} + {:type :add-component + :path "" + :name marker + :main-instance-id lr-main + :main-instance-page lib-page + :id l-retired + :anotation nil} + ;; A retired library component keeps its + ;; stored shapes; no link may receive them. + {:type :del-component + :id l-retired}]) + + file (th/create-file* 2 {:profile-id (:id owner) + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + page-b (uuid/random) + + c-deleted (uuid/random) + d-main (uuid/random) + d-live (uuid/random) + d-retired (uuid/random) + inst (uuid/random) + + _ (update-file! + :profile-id (:id owner) + :file-id file-id + :changes [{:type :add-page + :id page-b + :page {:id page-b + :name "Hidden" + :options {} + :objects {}}} + ;; The deleted component's stored subtree + ;; instances both library components. + {:type :add-obj + :page-id page-b + :id d-main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (frame d-main uuid/zero uuid/zero + {:main-instance true + :component-root true + :component-file file-id + :component-id c-deleted})} + {:type :add-obj + :page-id page-b + :id d-live + :parent-id d-main + :frame-id d-main + :components-v2 true + :obj (frame d-live d-main d-main + {:component-root true + :component-file lib-id + :component-id l-live})} + {:type :add-obj + :page-id page-b + :id d-retired + :parent-id d-main + :frame-id d-main + :components-v2 true + :obj (frame d-retired d-main d-main + {:component-root true + :component-file lib-id + :component-id l-retired})} + {:type :add-component + :path "" + :name "retired" + :main-instance-id d-main + :main-instance-page page-b + :id c-deleted + :anotation nil} + {:type :del-component + :id c-deleted} + ;; The only library reference the link grants. + {:type :add-obj + :page-id page-a + :id inst + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (frame inst uuid/zero uuid/zero + {:component-root true + :component-file lib-id + :component-id l-control})}]) + + _ (th/link-file-to-library* {:file-id file-id + :library-id lib-id}) + + slink (th/command! {::th/type :create-share-link + ::rpc/profile-id (:id owner) + :file-id file-id + :pages #{page-a} + :who-comment "team" + :who-inspect "all"}) + slink-id (get-in slink [:result :id])] + + (t/testing "the trim keeps only the library component the link references" + (let [out (th/command! {::th/type :get-view-only-bundle + :share-id slink-id + :file-id file-id}) + result (:result out) + libs (:libraries result) + comps (get-in (first libs) [:data :components])] + (t/is (nil? (:error out))) + (t/is (= 1 (count libs))) + (t/is (= #{l-control} (set (keys comps)))) + (t/is (not (contains? comps l-live))) + (t/is (not (contains? comps l-retired))) + (t/is (not (str/includes? (pr-str out) marker))))))) + +(t/deftest share-link-bundle-full-scope-link-still-lists-pages + ;; A link granting every page is the common "public prototype" case: + ;; pages are listed, instances render, and even there a deleted + ;; component does not travel. + (let [owner (th/create-profile* 1 {:is-active true}) + proj-id (:default-project-id owner) + file (th/create-file* 1 {:profile-id (:id owner) + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + page-b (uuid/random) + main (uuid/random) + inst (uuid/random) + comp (uuid/random) + + _ (update-file! :profile-id (:id owner) + :file-id file-id + :changes [{:type :add-page + :id page-b + :page {:id page-b + :name "Second" + :options {} + :objects {}}} + {:type :add-obj + :page-id page-b + :id main + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id main + :name "retired" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :main-instance true + :component-root true + :component-file file-id + :component-id comp})} + {:type :add-component + :path "" + :name "retired" + :main-instance-id main + :main-instance-page page-b + :id comp + :anotation nil} + {:type :del-component + :id comp} + {:type :add-obj + :page-id page-a + :id inst + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id inst + :name "instance" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :component-file file-id + :component-id comp})}]) + + slink (th/command! {::th/type :create-share-link + ::rpc/profile-id (:id owner) + :file-id file-id + :pages #{page-a page-b} + :who-comment "team" + :who-inspect "all"}) + slink-id (get-in slink [:result :id])] + + (t/testing "full-scope link lists both pages; the deleted component does not travel" + (let [out (th/command! {::th/type :get-view-only-bundle + :share-id slink-id + :file-id file-id}) + result (:result out) + data (:file result)] + (t/is (nil? (:error out))) + (t/is (= #{page-a page-b} (set (get-in data [:data :pages])))) + (t/is (contains? (get-in data [:data :pages-index page-a :objects]) inst)) + (t/is (not (contains? (get-in data [:data :components]) comp))))))) + +(t/deftest membership-bundle-with-pointer-map-keeps-components-fragment + ;; The share-link restrictions must not leak into the membership path: + ;; a member still receives the components pointer and can resolve it. + (let [owner (th/create-profile* 1 {:is-active true}) + owner-id (:id owner) + proj-id (:default-project-id owner) + file (th/create-file* 1 {:profile-id owner-id + :project-id proj-id + :is-shared false}) + file-id (:id file) + page-a (first (get-in file [:data :pages])) + shape (uuid/random) + comp (uuid/random) + + _ (update-file! :profile-id owner-id + :file-id file-id + :changes [{:type :add-obj + :page-id page-a + :id shape + :parent-id uuid/zero + :frame-id uuid/zero + :components-v2 true + :obj (cts/setup-shape + {:id shape + :name "live" + :frame-id uuid/zero + :parent-id uuid/zero + :type :frame + :main-instance true + :component-root true + :component-file file-id + :component-id comp})} + {:type :add-component + :path "" + :name "live" + :main-instance-id shape + :main-instance-page page-a + :id comp + :anotation nil}]) + + _ (db/tx-run! th/*system* + (fn [cfg] + (fdata/with-tracked-pointers cfg file-id + (bfc/update-file! cfg + (-> (bfc/get-file cfg file-id) + (fdata/enable-pointer-map)))))) + + comp-ptr-id (db/tx-run! th/*system* + (fn [cfg] + (pmap/get-id (:components (:data (bfc/get-file cfg file-id))))))] + + (t/testing "membership bundle carries the components pointer" + (let [out (th/command! {::th/type :get-view-only-bundle + ::rpc/profile-id owner-id + :file-id file-id}) + comps (get-in out [:result :file :data :components])] + (t/is (nil? (:error out))) + (t/is (pmap/pointer-map? comps)))) + + (t/testing "a member can resolve the components fragment" + (let [out (th/command! {::th/type :get-file-fragment + ::rpc/profile-id owner-id + :file-id file-id + :fragment-id comp-ptr-id})] + (t/is (nil? (:error out))) + (t/is (= comp-ptr-id (get-in out [:result :id]))))))) + +(t/deftest same-file-component-ref-legacy-shapes + ;; Legacy files omit `:component-file` on same-file references; the + ;; shared predicate must treat that as a same-file reference and still + ;; reject library shapes and plain shapes. + (let [file-id (uuid/random) + other-id (uuid/random) + legacy {:component-id (uuid/random)} + own {:component-id (uuid/random) :component-file file-id} + library {:component-id (uuid/random) :component-file other-id} + plain {:name "not-a-component"}] + + (t/is (true? (#'viewer/same-file-component-ref? file-id legacy))) + (t/is (true? (#'viewer/same-file-component-ref? file-id own))) + (t/is (false? (#'viewer/same-file-component-ref? file-id library))) + (t/is (false? (#'viewer/same-file-component-ref? file-id plain)))))