penpot/backend/test/backend_tests/rpc_viewer_test.clj
Andrey Antukh e23ca02aa2
✨ Reference the file data storage object with a foreign key (#12769)
* ✨ Reference the file data storage object with a foreign key

The file data pointer lived in `file_data.metadata.storage-ref-id`, a JSON
key. The storage GC parsed it to find live references, and nothing stopped it
from physically deleting an object a live row still pointed at: a failure in
that scan lost the blob.

Migration 0159 moves the pointer to a nullable `storage_object_id` column on
`file_data`, backfills it from the metadata key, removes the key from the
rows it backfills, indexes the column and rebuilds the foreign key to
`storage_object(id)` as `NO ACTION DEFERRABLE INITIALLY IMMEDIATE`.
`INITIALLY IMMEDIATE` is deliberate: the GC catches the violation inside the
chunk transaction, while a deferred check would fire at commit, outside it,
and the sweep would never move past the blocked chunk.

The column is the only source of truth. The `storage-ref-id` entry is gone
from the file metadata schema, and the single point that serializes the
metadata drops it, so no write can resurrect it. `upsert!` does not query
the row: the pointer of the row being replaced travels in its params, and
the object is touched before the row is written. Fragments and snapshots
pass nil because a modified pointer always gets a new id; the offload passes
its own row; `insert-file!` drops it, since an INSERT never replaces a
persisted object.

`update-file!` takes the file map read from the database plus the fields to
change and merges them, so the pointer comes from the read map and no caller
can lose it by building a partial map. A map without `:data` raises
`:missing-file-data`: it is not `assert`, which is compiled out in the
deployed binary. The params cannot override the pointer or retarget the
write with `:id`/`:file-id`.

`bfc/file->response` strips the pointer from every response (`get-file`,
`create-file`, `duplicate-file`, the view-only bundle, the team access
request, the comments read, the library summary, the snapshot read and the
debug download). A `file-data` object is authentication-only, so a leaked id
would let any logged-in profile fetch the blob from `/assets/by-id`.
`get-file-fragment` resolves through the file data backend, so a fragment on
cold storage no longer hands nil to `blob/decode`, and answers only the keys
of its result schema.

The storage GC reference scan, the restore union and `objects-gc` read the
column. The offload reads `SELECT fd.*`, so its rows go through a
`decode-row` that decodes jsonb metadata and drops a JSON null, which the row
schema rejected; a row without a readable object raises
`:missing-storage-object` with the file and row ids. A chunk blocked by the
foreign key (SQLState 23503) is a real anomaly, since the reference scan
should have frozen the object: it stays an error with its own hint and the
chunk is retried on the next run until the reference is fixed.

The release is a coordinated restart, not a rolling deploy, and not
rollback-safe for the rows it writes: the previous code writes and scans the
metadata pointer, so a key it writes misses the backfill and removing it
loses the reference. `operations/2.20.0-file-data-storage-object-id.md`
builds the column, the index and the foreign key online and runs the
backfill with every instance stopped, then marks the step as applied; a
fresh or small instance does the whole job in the migration. The runbook
keeps the validation scan out of the stopped window and adds a lock timeout
and retry to the online statements.

Tests cover the foreign key shape, the backfill and its corrupt and orphan
guards, the rebuild of a missing index and of a wrong, non-deferrable or
deferred constraint, the deletion refused by a live reference, the metadata
cleanup, the offload row normalizations, the touched objects of the unshare
and the restore, the response stripping on every call site and the chunk
blocked by the foreign key. The backend memories and the developer guide
document the pointer, the GC behavior and the two migration paths.

Closes #12768

AI-assisted-by: deepseek-v4.1-flash
AI-assisted-by: inkling-2-small-free

* 🐛 Hold the file row lock across the migration write

`get-file` applies the pending migrations in memory and, when it finds
any, persists the result. That write read the row without a lock and
`update-file!` is an unconditional UPDATE, so a save that committed in
between was overwritten: the saved content, `revn` and `modified-at`
went back, the next save from that client failed with `:revn-conflict`
and the object the save left became an orphan the storage GC cannot
revisit once it is frozen.

The read now runs in a transaction of its own (`get-file` calls
`get-file*` inside `db/tx-run!` and accepts a cfg map only), so the
`file` row lock covers the re-check and the write. `get-file*` reads
the row once (`sql:get-file` plus `resolve-applied-migrations`) and
completes it with `attach-file-data`; only when a migration is pending
it reads the file again under `FOR UPDATE`, re-checks `need-migration?`
on that fresh map and persists it (`migrate-file`). The common case
stays at one row read and one migrations resolution, the migration case
keeps a single decoded copy in the heap, and a row that changed in
between is not overwritten. A migration found on the unlocked read is
applied in memory only, for the next read to persist.

`migrate-file` returns the reattached map when it persists, with the
`:storage-object-id` of the row the write left, and the realized map
when it does not; `:realize?` is what returns the read fully plain.
`fdata/upsert!` answers `{:written ... :storage-object-id ...}` instead
of a boolean, `update-file!` propagates it and the migrating read puts
it back in the map it returns, so a caller that persists again touches
the current object instead of the replaced one. `insert-file!` keeps
nil: an INSERT never replaces a persisted object.

The write also compares the stored pointer with the caller's value
(`IS NOT DISTINCT FROM`) and raises `:stale-file-data-pointer` when it
affects no row. Every writer holds the `file` row lock, so a pointer
that no longer matches is a protocol violation, not a race to retry.
The expected pointer is a separate argument because the storage branch
replaces `:storage-object-id` before the upsert, and the check lives in
`upsert-in-database`: the `legacy-db` branches do not use that SQL and
their count can legitimately be zero.

The save, snapshot and restore-backup reads take `FOR UPDATE` instead
of `FOR SHARE`: they inherit the lock, and the migration branch asks
for the exclusive one in the same transaction, so a shared lock that
upgrades deadlocks two concurrent readers. A data read rejects
`:lock-for-share?` for the same reason; only row-only reads accept it.
`sql:get-file-without-data` is a plain select now, not a CTE, which
makes locking legal without loading the data. Realization has a single
owner: `attach-file-data` only resolves and decodes, and `get-file*`
realizes once per path. The applied migrations no longer travel in the
`get-file-with-summary` response, cache hit and cache miss alike.

`embed-assets` reads each library without `:realize?`, so its
`:components` is an unloaded pointer map; `absorb-assets` walks it with
`vals`, `PointerMap.seq` calls `load!` and that raised with no loader
bound, so every export with `:merge-libraries` of a library with
components failed. The absorb runs under the library's own loader now.

The in-place binfile import (`import-file-and-overwrite*`,
`::overwrite`, `::bfc/file-id`) had no product caller and cannot carry
the previous `storage_object_id`, so overwriting a storage-backed row
would leave its object unreclaimed. `import-files!` rejects a cfg with
`::bfc/file-id` (`:not-supported`) and the helpers only that path used
go with it (`invalidate-thumbnails`, `*reference-file*`, the
`::overwrite` id branch of `index-object` and of `save-file!`, the
`on-conflict-do-nothing?` flag of the media insert).

Tests: a new `file-migration-read-test` pins the single read, the
locked re-read, the concurrent save interleaving, the double migration
and the read-only path; the upsert tests cover the return shape and the
stale pointer; the SQL recorder they share lives in
`backend-tests.helpers`, and the save, snapshot, restore and summary
sequences follow the new reads. The backend memories document the read
contract and the pointer.

AI-assisted-by: deepseek-v4.1-flash

* 🐛 Harden the get-file guard and skip its read-only transaction

The cfg guard of `get-file` was an `assert`, which `app.config` elides
when `:backend-asserts` is off, so a deployed binary accepted a bare
pool or connection while the docstring promised it raises. It throws
`IllegalArgumentException` now, the class the option guards of the same
namespace already use, and the existing test expects it instead of
`AssertionError`.

A read with `:read-only?` applies the pending migrations in memory and
writes nothing, so the transaction the migration write needs only costs
two round trips there. `get-file` runs it with `db/run!` instead: with
a cfg that carries a connection it still joins the caller's
transaction, and only the reads that would have opened their own skip
it. A lock option needs a read-write transaction from the caller,
because PostgreSQL rejects row locks in a read-only one; the docstring
states that.

Tests: the guard test expects the new class; a cfg marked
`::db/read-only` reads a pending migration without persisting it (the
config flag that fills that key is legacy, but the key is set by hand);
and a read-only read is pinned as not opening a transaction while the
same call without the option does. The backend memory documents the
split and the guard.

AI-assisted-by: deepseek-v4.1-flash
2026-11-14 19:27:18 +01:00

948 lines
42 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns backend-tests.rpc-viewer-test
(:require
[app.common.types.shape :as cts]
[app.common.uuid :as uuid]
[app.db :as db]
[app.rpc :as-alias rpc]
[app.rpc.commands.viewer :as viewer]
[backend-tests.helpers :as th]
[clojure.test :as t]
[datoteka.fs :as fs]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(def ^:private safe-library-envelope-keys
"Top-level keys a trimmed library may expose next to `:data`. Anything
else is design content leaking through the envelope (see F3)."
#{:backend :comment-thread-seqn :created-at :features :has-media-trimmed
:id :is-indirect :is-shared :migrations :modified-at :name :project-id
:revn :synced-at :team-id :vern :version})
(defn- update-file!
[& {:keys [profile-id file-id changes]}]
(let [out (th/command! {::th/type :update-file
::rpc/profile-id profile-id
:id file-id
:session-id (uuid/random)
:revn 0
:vern 0
:changes changes})]
(t/is (nil? (:error out)))
(:result out)))
(t/deftest obfuscate-email-happy-path
(t/is (= "a****@****.com" (viewer/obfuscate-email "alice@example.com")))
(t/is (= "a****@****.example.com" (viewer/obfuscate-email "alice@sub.example.com")))
(t/is (= "****@****.com" (viewer/obfuscate-email "bob@bar.com"))))
(t/deftest obfuscate-email-handles-domain-without-dot
;; `localhost`-style domains have no `.`; the previous implementation produced
;; a dangling-dot output like "a****@****." — now the trailing `.` is only
;; emitted when there actually is a TLD segment to append.
(t/is (= "a****@****" (viewer/obfuscate-email "alice@localhost")))
(t/is (= "****@****" (viewer/obfuscate-email "x@y"))))
(t/deftest obfuscate-email-handles-malformed-input
;; These shapes must not throw — `obfuscate-email` runs while building the
;; view-only bundle for share-link viewers and an NPE here aborts the whole
;; RPC response. The previous implementation called `clojure.string/split`
;; on `nil` for the `no-@` case, raising NullPointerException.
(t/is (= "****@****" (viewer/obfuscate-email nil)))
(t/is (= "****@****" (viewer/obfuscate-email "")))
(t/is (= "r***@****" (viewer/obfuscate-email "root"))) ; no `@`, count > 3
(t/is (= "****@****" (viewer/obfuscate-email "bob")))) ; no `@`, count <= 3
(t/deftest retrieve-bundle
(let [prof (th/create-profile* 1 {:is-active true})
prof2 (th/create-profile* 2 {:is-active true})
team-id (:default-team-id prof)
proj-id (:default-project-id prof)
file (th/create-file* 1 {:profile-id (:id prof)
:project-id proj-id
:is-shared false})
share-id (atom nil)]
(t/testing "authenticated with page-id"
(let [data {::th/type :get-view-only-bundle
::rpc/profile-id (:id prof)
:file-id (:id file)
:page-id (get-in file [:data :pages 0])
:components-v2 true}
out (th/command! data)]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(let [result (:result out)]
(t/is (contains? result :share-links))
(t/is (contains? result :permissions))
(t/is (contains? result :libraries))
(t/is (contains? result :file))
(t/is (contains? result :project)))))
(t/testing "generate share token"
(let [data {::th/type :create-share-link
::rpc/profile-id (:id prof)
:file-id (:id file)
:pages #{(get-in file [:data :pages 0])}
:who-comment "team"
:who-inspect "all"}
out (th/command! data)]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(let [result (:result out)]
(t/is (uuid? (:id result)))
(reset! share-id (:id result)))))
(t/testing "not authenticated with page-id"
(let [data {::th/type :get-view-only-bundle
::rpc/profile-id (:id prof2)
:file-id (:id file)
:page-id (get-in file [:data :pages 0])
:components-v2 true}
out (th/command! data)]
;; (th/print-result! out)
(let [error (:error out)
error-data (ex-data error)]
(t/is (th/ex-info? error))
(t/is (= (:type error-data) :not-found))
(t/is (= (:code error-data) :object-not-found)))))
(t/testing "authenticated with token & profile"
(let [data {::th/type :get-view-only-bundle
::rpc/profile-id (:id prof2)
:share-id @share-id
:file-id (:id file)
:page-id (get-in file [:data :pages 0])
:components-v2 true}
out (th/command! data)]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(let [result (:result out)]
(t/is (contains? result :file))
(t/is (contains? result :project)))))
(t/testing "authenticated with token"
(let [data {::th/type :get-view-only-bundle
:share-id @share-id
:file-id (:id file)
:page-id (get-in file [:data :pages 0])
:components-v2 true}
out (th/command! data)]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(let [result (:result out)]
(t/is (contains? result :file))
(t/is (contains? result :project)))))))
(t/deftest share-link-token-disclosure
(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})
page-a (get-in file [:data :pages 0])
page-b (uuid/random)
;; Add a second page to the file
_ (th/command! {::th/type :update-file
::rpc/profile-id (:id owner)
:id (:id file)
:session-id (uuid/random)
:revn 0
:vern 0
:changes [{:type :add-page
:id page-b
:page {:id page-b
:name "Page B"
:options {}
:objects {}}}]})
;; Create Link A: restrictive (no pages, team-only comments/inspect)
link-a (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id file)
:pages #{}
:who-comment "team"
:who-inspect "team"})
link-a-id (get-in link-a [:result :id])
;; Create Link B: permissive (all pages, all can comment/inspect)
link-b (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id file)
:pages #{page-a page-b}
:who-comment "all"
:who-inspect "all"})
link-b-id (get-in link-b [:result :id])]
(t/testing "restrictive share-link holder cannot see other share-link tokens"
(let [out (th/command! {::th/type :get-view-only-bundle
:share-id link-a-id
:file-id (:id file)})
err (:error out)
result (:result out)
share-links (:share-links result)]
;; Should not error
(t/is (nil? err))
;; Should only see the share-link used for authentication
(t/is (= 1 (count share-links)))
(t/is (= link-a-id (:id (first share-links))))
;; Should NOT see Link B's token
(t/is (not (some #(= link-b-id (:id %)) share-links)))))
(t/testing "team member still sees all share-links"
(let [out (th/command! {::th/type :get-view-only-bundle
::rpc/profile-id (:id owner)
:file-id (:id file)})
err (:error out)
result (:result out)
share-links (:share-links result)]
;; Should not error
(t/is (nil? err))
;; Team member should see both share-links
(t/is (= 2 (count share-links)))
(t/is (some #(= link-a-id (:id %)) share-links))
(t/is (some #(= link-b-id (:id %)) share-links))))
(t/testing "share-link viewer does not query sibling share-links"
;; Direct regression test for the predicate-pushdown invariant:
;; on the share-link path the bundle must resolve the caller's
;; row with a composite (id, file-id) single-row lookup and must
;; never run the full {:file-id} query that would load sibling
;; tokens into the backend process. The with-redefs spies only
;; record and delegate, following the instrumentation style used
;; elsewhere in this suite (e.g. auth-ldap-test).
(let [share-queries (atom [])
share-gets (atom [])
orig-query @#'db/query
orig-get* @#'db/get*]
(with-redefs [db/query (fn [conn table params & opts]
(when (= :share-link table)
(swap! share-queries conj params))
(apply orig-query conn table params opts))
db/get* (fn [conn table params & opts]
(when (= :share-link table)
(swap! share-gets conj params))
(apply orig-get* conn table params opts))]
(let [out (th/command! {::th/type :get-view-only-bundle
:share-id link-a-id
:file-id (:id file)})
result (:result out)]
(t/is (nil? (:error out)))
(t/is (= 1 (count (:share-links result))))
(t/is (= link-a-id (:id (first (:share-links result)))))
;; No full-file sibling query ran on this path.
(t/is (empty? @share-queries))
;; Only composite single-row lookups ran (the permission
;; check plus the bundle itself, same predicate in both).
(t/is (seq @share-gets))
(t/is (every? #(= {:id link-a-id :file-id (:id file)} %)
@share-gets))))))
(t/testing "cross-file share-id replay fails closed"
(let [other-file (th/create-file* 2 {:profile-id (:id owner)
:project-id proj-id
:is-shared false})
out (th/command! {::th/type :get-view-only-bundle
:share-id link-a-id
:file-id (:id other-file)})
error (:error out)
error-data (ex-data error)]
(t/is (th/ex-info? error))
(t/is (= :not-found (:type error-data)))
(t/is (= :object-not-found (:code error-data)))))))
(t/deftest trim-library-data-unit
(let [lib-id (uuid/random)
comp-a (uuid/random)
comp-b (uuid/random)
comp-c (uuid/random)
page-id (uuid/random)
mk-shape (fn [id comp] {:id id :type :rect :component-id comp :component-file lib-id})
shape-a (mk-shape (uuid/random) comp-a)
primary {:pages [page-id]
:pages-index {page-id {:id page-id :objects {(:id shape-a) shape-a}}}
:components {}}
lib {:id lib-id
:synced-at "now"
:data {:id lib-id
:options {}
:pages [page-id]
:pages-index {page-id {:id page-id}}
:components {comp-a {:id comp-a :name "a" :objects {}}
comp-b {:id comp-b :name "b" :objects {}}}}}]
(t/testing "keeps referenced components and drops library pages"
(let [used (#'viewer/collect-used-library-components primary {lib-id lib})
trimmed (#'viewer/trim-library-data used lib)]
(t/is (= #{comp-a} (get used lib-id)))
(t/is (= #{comp-a} (set (keys (get-in trimmed [:data :components])))))
(t/is (= [] (get-in trimmed [:data :pages])))
(t/is (= {} (get-in trimmed [:data :pages-index])))
(t/is (= lib-id (:id trimmed)))
(t/is (= "now" (:synced-at trimmed)))))
(t/testing "no references keeps no components"
(let [trimmed (#'viewer/trim-library-data {} lib)]
(t/is (= {} (get-in trimmed [:data :components])))
(t/is (= [] (get-in trimmed [:data :pages])))))
(t/testing "follows nested component references through the main instance"
;; Stored components carry no `:objects`; nested references resolve
;; through the main-instance subtree on the library page.
(let [main-used (uuid/random)
main-nested (uuid/random)
child (assoc (mk-shape (uuid/random) comp-c) :parent-id main-used)
lib2 (assoc lib :data
{:id lib-id
:options {}
:pages [page-id]
:pages-index {page-id {:id page-id
:objects {main-used {:id main-used
:type :frame
:shapes [(:id child)]}
(:id child) child
main-nested {:id main-nested
:type :frame}}}}
:components {comp-a {:id comp-a
:name "a"
:main-instance-id main-used
:main-instance-page page-id}
comp-b {:id comp-b
:name "b"}
comp-c {:id comp-c
:name "c"
:main-instance-id main-nested
:main-instance-page page-id}}})
used (#'viewer/collect-used-library-components primary {lib-id lib2})
trimmed (#'viewer/trim-library-data used lib2)]
(t/is (= #{comp-a comp-c} (get used lib-id)))
(t/is (= #{comp-a comp-c} (set (keys (get-in trimmed [:data :components])))))
(t/is (not (contains? (get-in trimmed [:data :components]) comp-b)))))))
(t/deftest share-link-bundle-trims-linked-libraries
(let [owner (th/create-profile* 1 {:is-active true})
stranger (th/create-profile* 2 {: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})
;; A second page that only exists inside the library; the main
;; file never references it, so no share link on the main file
;; should ever expose it.
canary (uuid/random)
_ (th/command! {::th/type :update-file
::rpc/profile-id (:id owner)
:id (:id lib)
:session-id (uuid/random)
:revn 0
:vern 0
:changes [{:type :add-page
:id canary
:page {:id canary
:name "Private canary"
:options {}
:objects {}}}]})
file (th/create-file* 2 {:profile-id (:id owner)
:project-id proj-id
:is-shared false})
_ (th/link-file-to-library* {:file-id (:id file)
:library-id (:id lib)})
slink (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id file)
:pages #{(get-in file [:data :pages 0])}
:who-comment "team"
:who-inspect "all"})
slink-id (get-in slink [:result :id])]
(t/testing "control: non-member cannot read the library directly"
(let [out (th/command! {::th/type :get-file
::rpc/profile-id (:id stranger)
:id (:id lib)
:components-v2 true})
error-data (ex-data (:error out))]
(t/is (th/ex-info? (:error out)))
(t/is (= :not-found (:type error-data)))))
(t/testing "anonymous bundle omits unreferenced library pages"
(let [out (th/command! {::th/type :get-view-only-bundle
:share-id slink-id
:file-id (:id file)})
result (:result out)
lib-pages (into #{} (mapcat #(get-in % [:data :pages] []))
(:libraries result))]
(t/is (nil? (:error out)))
(t/is (not (contains? lib-pages canary)))
(t/is (every? #(= #{:id :options :pages :pages-index :components}
(set (keys (:data %))))
(:libraries result)))
(t/is (every? #(every? safe-library-envelope-keys
(keys (dissoc % :data)))
(:libraries result)))))))
(t/deftest share-link-bundle-library-edge-cases
(let [owner (th/create-profile* 1 {:is-active true})
proj-id (:default-project-id owner)
add-canary (fn [file n]
(let [canary (uuid/random)]
(th/command! {::th/type :update-file
::rpc/profile-id (:id owner)
:id (:id file)
:session-id (uuid/random)
:revn 0
:vern 0
:changes [{:type :add-page
:id canary
:page {:id canary
:name n
:options {}
:objects {}}}]})
canary))
lib2 (th/create-file* 1 {:profile-id (:id owner)
:project-id proj-id
:is-shared true})
canary2 (add-canary lib2 "Private canary 2")
lib1 (th/create-file* 2 {:profile-id (:id owner)
:project-id proj-id
:is-shared true})
canary1 (add-canary lib1 "Private canary 1")
_ (th/link-file-to-library* {:file-id (:id lib1)
:library-id (:id lib2)})
file (th/create-file* 3 {:profile-id (:id owner)
:project-id proj-id
:is-shared false})
_ (th/link-file-to-library* {:file-id (:id file)
:library-id (:id lib1)})
full (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id file)
:pages #{(get-in file [:data :pages 0])}
:who-comment "team"
:who-inspect "all"})
full-id (get-in full [:result :id])
empty (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id file)
:pages #{}
:who-comment "team"
:who-inspect "team"})
empty-id (get-in empty [:result :id])
bundle (fn [& {:as params}]
(th/command! (merge {::th/type :get-view-only-bundle
:file-id (:id file)}
params)))
lib-pages (fn [result]
(into #{} (mapcat #(get-in % [:data :pages] []))
(:libraries result)))]
(t/testing "indirect libraries are trimmed too"
(let [out (bundle :share-id full-id)
result (:result out)
pages (lib-pages result)]
(t/is (nil? (:error out)))
(t/is (= 2 (count (:libraries result))))
(t/is (not (contains? pages canary1)))
(t/is (not (contains? pages canary2)))))
(t/testing "empty-pages link exposes no library content"
(let [out (bundle :share-id empty-id)
result (:result out)]
(t/is (nil? (:error out)))
(t/is (every? #(= {} (get-in % [:data :components])) (:libraries result)))
(t/is (every? #(= [] (get-in % [:data :pages])) (:libraries result)))))
(t/testing "member bundle keeps full libraries"
(let [out (bundle ::rpc/profile-id (:id owner))
result (:result out)
pages (lib-pages result)]
(t/is (nil? (:error out)))
(t/is (contains? pages canary1))
(t/is (contains? pages canary2))))
(t/testing "file without libraries returns no libraries"
(let [plain (th/create-file* 4 {:profile-id (:id owner)
:project-id proj-id
:is-shared false})
link (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id plain)
:pages #{(get-in plain [:data :pages 0])}
:who-comment "team"
:who-inspect "all"})
out (th/command! {::th/type :get-view-only-bundle
:share-id (get-in link [:result :id])
:file-id (:id plain)})]
(t/is (nil? (:error out)))
(t/is (= [] (:libraries (:result out))))))))
(t/deftest share-link-bundle-keeps-used-library-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-page (first (get-in lib [:data :pages]))
c-nested (uuid/random)
c-used (uuid/random)
c-unused (uuid/random)
n-main (uuid/random)
u-main (uuid/random)
u-child (uuid/random)
x-main (uuid/random)
inst (uuid/random)
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)))
;; Nested component, referenced only through c-used.
_ (update-file!
:profile-id (:id owner)
:file-id (:id lib)
:changes [{:type :add-obj
:page-id lib-page
:id n-main
:parent-id uuid/zero
:frame-id uuid/zero
:components-v2 true
:obj (frame n-main uuid/zero uuid/zero
{:main-instance true
:component-root true
:component-file (:id lib)
:component-id c-nested})}
{:type :add-component
:path ""
:name "nested"
:main-instance-id n-main
:main-instance-page lib-page
:id c-nested
:anotation nil}])
;; Used component, instancing the nested one.
_ (update-file!
:profile-id (:id owner)
:file-id (:id lib)
:changes [{:type :add-obj
:page-id lib-page
:id u-main
:parent-id uuid/zero
:frame-id uuid/zero
:components-v2 true
:obj (frame u-main uuid/zero uuid/zero
{:main-instance true
:component-root true
:component-file (:id lib)
:component-id c-used})}
{:type :add-obj
:page-id lib-page
:id u-child
:parent-id u-main
:frame-id u-main
:components-v2 true
:obj (frame u-child u-main u-main
{:main-instance false
:component-root true
:component-file (:id lib)
:component-id c-nested})}
{:type :add-component
:path ""
:name "used"
:main-instance-id u-main
:main-instance-page lib-page
:id c-used
:anotation nil}])
;; Unreferenced component, never instanced anywhere.
_ (update-file!
:profile-id (:id owner)
:file-id (:id lib)
:changes [{:type :add-obj
:page-id lib-page
:id x-main
:parent-id uuid/zero
:frame-id uuid/zero
:components-v2 true
:obj (frame x-main uuid/zero uuid/zero
{:main-instance true
:component-root true
:component-file (:id lib)
:component-id c-unused})}
{:type :add-component
:path ""
:name "unused"
:main-instance-id x-main
:main-instance-page lib-page
:id c-unused
:anotation nil}])
file (th/create-file* 2 {:profile-id (:id owner)
:project-id proj-id
:is-shared false})
main-page (first (get-in file [:data :pages]))
;; Instance the used component on the main file.
_ (update-file!
:profile-id (:id owner)
:file-id (:id file)
:changes [{:type :add-obj
:page-id main-page
:id inst
:parent-id uuid/zero
:frame-id uuid/zero
:components-v2 true
:obj (frame inst uuid/zero uuid/zero
{:main-instance false
:component-root true
:component-file (:id lib)
:component-id c-used})}])
_ (th/link-file-to-library* {:file-id (:id file)
:library-id (:id lib)})
slink (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id file)
:pages #{main-page}
:who-comment "team"
:who-inspect "all"})
slink-id (get-in slink [:result :id])]
(t/testing "anonymous bundle keeps referenced components and drops the rest"
(let [out (th/command! {::th/type :get-view-only-bundle
:share-id slink-id
:file-id (:id file)})
result (:result out)
libs (:libraries result)]
(t/is (nil? (:error out)))
(t/is (= 1 (count libs)))
(t/is (= #{c-used c-nested}
(set (keys (get-in (first libs) [:data :components])))))
(t/is (= [] (get-in (first libs) [:data :pages])))))))
(t/deftest share-link-bundle-drops-components-from-disallowed-pages
(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-page (first (get-in lib [:data :pages]))
comp (uuid/random)
main (uuid/random)
_ (update-file!
:profile-id (:id owner)
:file-id (:id lib)
:changes [{:type :add-obj
:page-id lib-page
:id main
:parent-id uuid/zero
:frame-id uuid/zero
:components-v2 true
:obj (cts/setup-shape
{:id main
:name "Board"
:frame-id uuid/zero
:parent-id uuid/zero
:type :frame
:main-instance true
:component-root true
:component-file (:id lib)
:component-id comp})}
{:type :add-component
:path ""
:name "Board"
:main-instance-id main
:main-instance-page lib-page
:id comp
:anotation nil}])
file (th/create-file* 2 {:profile-id (:id owner)
:project-id proj-id
:is-shared false})
page-a (first (get-in file [:data :pages]))
page-b (uuid/random)
inst (uuid/random)
;; Second page, outside the share link scope.
_ (update-file!
:profile-id (:id owner)
:file-id (:id file)
:changes [{:type :add-page
:id page-b
:page {:id page-b
:name "Hidden"
:options {}
:objects {}}}])
;; Instance the library component only on the disallowed page.
_ (update-file!
:profile-id (:id owner)
:file-id (:id file)
:changes [{:type :add-obj
:page-id page-b
:id inst
:parent-id uuid/zero
:frame-id uuid/zero
:components-v2 true
:obj (cts/setup-shape
{:id inst
:name "Board"
:frame-id uuid/zero
:parent-id uuid/zero
:type :frame
:main-instance false
:component-root true
:component-file (:id lib)
:component-id comp})}])
_ (th/link-file-to-library* {:file-id (:id file)
:library-id (:id lib)})
slink (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id file)
:pages #{page-a}
:who-comment "team"
:who-inspect "all"})]
(t/testing "references from disallowed pages seed nothing"
(let [out (th/command! {::th/type :get-view-only-bundle
:share-id (get-in slink [:result :id])
:file-id (:id file)})
result (:result out)
libs (:libraries result)]
(t/is (nil? (:error out)))
(t/is (= 1 (count libs)))
(t/is (= {} (get-in (first libs) [:data :components])))
(t/is (= [] (get-in (first libs) [:data :pages])))))))
(t/deftest share-link-bundle-keeps-cross-library-nested-components
(let [owner (th/create-profile* 1 {:is-active true})
proj-id (:default-project-id owner)
lib2 (th/create-file* 1 {:profile-id (:id owner)
:project-id proj-id
:is-shared true})
lib2-page (first (get-in lib2 [:data :pages]))
comp-b (uuid/random)
b-main (uuid/random)
_ (update-file!
:profile-id (:id owner)
:file-id (:id lib2)
:changes [{:type :add-obj
:page-id lib2-page
:id b-main
:parent-id uuid/zero
:frame-id uuid/zero
:components-v2 true
:obj (cts/setup-shape
{:id b-main
:name "Leaf"
:frame-id uuid/zero
:parent-id uuid/zero
:type :frame
:main-instance true
:component-root true
:component-file (:id lib2)
:component-id comp-b})}
{:type :add-component
:path ""
:name "leaf"
:main-instance-id b-main
:main-instance-page lib2-page
:id comp-b
:anotation nil}])
lib1 (th/create-file* 2 {:profile-id (:id owner)
:project-id proj-id
:is-shared true})
lib1-page (first (get-in lib1 [:data :pages]))
_ (th/link-file-to-library* {:file-id (:id lib1)
:library-id (:id lib2)})
comp-a (uuid/random)
a-main (uuid/random)
a-child (uuid/random)
;; Component whose main instance embeds an instance from lib2.
_ (update-file!
:profile-id (:id owner)
:file-id (:id lib1)
:changes [{:type :add-obj
:page-id lib1-page
:id a-main
:parent-id uuid/zero
:frame-id uuid/zero
:components-v2 true
:obj (cts/setup-shape
{:id a-main
:name "Wrapper"
:frame-id uuid/zero
:parent-id uuid/zero
:type :frame
:main-instance true
:component-root true
:component-file (:id lib1)
:component-id comp-a})}
{:type :add-obj
:page-id lib1-page
:id a-child
:parent-id a-main
:frame-id a-main
:components-v2 true
:obj (cts/setup-shape
{:id a-child
:name "Leaf"
:frame-id a-main
:parent-id a-main
:type :frame
:main-instance false
:component-root true
:component-file (:id lib2)
:component-id comp-b})}
{:type :add-component
:path ""
:name "wrapper"
:main-instance-id a-main
:main-instance-page lib1-page
:id comp-a
:anotation nil}])
file (th/create-file* 3 {:profile-id (:id owner)
:project-id proj-id
:is-shared false})
main-page (first (get-in file [:data :pages]))
inst (uuid/random)
_ (update-file!
:profile-id (:id owner)
:file-id (:id file)
:changes [{:type :add-obj
:page-id main-page
:id inst
:parent-id uuid/zero
:frame-id uuid/zero
:components-v2 true
:obj (cts/setup-shape
{:id inst
:name "Wrapper"
:frame-id uuid/zero
:parent-id uuid/zero
:type :frame
:main-instance false
:component-root true
:component-file (:id lib1)
:component-id comp-a})}])
_ (th/link-file-to-library* {:file-id (:id file)
:library-id (:id lib1)})
slink (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id file)
:pages #{main-page}
:who-comment "team"
:who-inspect "all"})]
(t/testing "nested references resolve across libraries"
(let [out (th/command! {::th/type :get-view-only-bundle
:share-id (get-in slink [:result :id])
:file-id (:id file)})
result (:result out)
by-id (into {} (map (juxt :id identity)) (:libraries result))]
(t/is (nil? (:error out)))
(t/is (= 2 (count (:libraries result))))
(t/is (= #{comp-a}
(set (keys (get-in by-id [(:id lib1) :data :components])))))
(t/is (= #{comp-b}
(set (keys (get-in by-id [(:id lib2) :data :components])))))
(t/is (= [] (get-in by-id [(:id lib2) :data :pages])))))))
(t/deftest view-only-bundle-does-not-expose-the-storage-pointer
;; Both the file and its libraries are moved to cold storage, so their file
;; maps carry the internal pointer unless the response strips it.
(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})
lib (th/create-file* 2 {:profile-id (:id owner)
:project-id proj-id
:is-shared true})
page-id (get-in file [:data :pages 0])
_ (th/link-file-to-library* {:file-id (:id file)
:library-id (:id lib)})
slink (th/command! {::th/type :create-share-link
::rpc/profile-id (:id owner)
:file-id (:id file)
:pages #{page-id}
:who-comment "team"
:who-inspect "all"})
slink-id (get-in slink [:result :id])]
(th/run-task! :offload-file-data {:file-id (:id file)})
(th/run-task! :offload-file-data {:file-id (:id lib)})
(let [out (th/command! {::th/type :get-view-only-bundle
:share-id slink-id
:file-id (:id file)})
result (:result out)]
(t/is (nil? (:error out)))
(t/is (seq (:libraries result)))
(t/is (not (contains? (:file result) :storage-object-id)))
(t/is (every? #(not (contains? % :storage-object-id))
(:libraries result))))))