Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Encrypt public_uuid at rest, resolved via an indexed prefix (#80929)

Alexander Polyankinc3dddcfcc6ce
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 0e6469a30c3..2bcdd8491f4 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -104,6 +104,7 @@
model-persistence
models
parameters
+ public-sharing
queries
query-processor
search
@@ -1783,7 +1784,8 @@
request
tiles
util}
- :model-imports #{:model/Card
+ :model-imports #{:model/Action
+ :model/Card
:model/Dashboard
:model/DashboardCard
:model/Document}}
diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/test_util.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/test_util.clj
index 9639ec66757..86c6f46aa76 100644
--- a/enterprise/backend/test/metabase_enterprise/semantic_search/test_util.clj
+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/test_util.clj
@@ -21,6 +21,7 @@
[metabase.search.config :as search.config]
[metabase.search.ingestion :as search.ingestion]
[metabase.test :as mt]
+ [metabase.test.initialize :as initialize]
[metabase.util :as u]
[metabase.util.json :as json]
[metabase.util.log :as log]
@@ -36,8 +37,12 @@
;; Purpose of this fixure is to block running tests if db-url is not set. That's true for enterprise app-db tests in CI.
(defn once-fixture
+ "Shared `:once` fixture for semantic-search tests. Skips the namespace when no pgvector URL is configured, and
+ initializes the application DB so tests that read Metabase content (e.g. `collection`) while indexing are
+ self-sufficient rather than depending on a CI partition-mate to have set the app DB up."
[f]
(when semantic.db.datasource/db-url
+ (initialize/initialize-if-needed! :db)
(f)))
(def default-test-db "my_test_db")
diff --git a/resources/migrations/058_update_migrations.yaml b/resources/migrations/058_update_migrations.yaml
index e1ece3e5c62..fcc7f957aef 100644
--- a/resources/migrations/058_update_migrations.yaml
+++ b/resources/migrations/058_update_migrations.yaml
@@ -1344,6 +1344,270 @@ databaseChangeLog:
- customChange:
class: metabase.app_db.custom_migrations.EncryptApiKeys
+ - changeSet:
+ id: v58.2026-08-25T00:00:02
+ author: ranquild
+ comment: Widen report_card.public_uuid so it can hold the encrypted value written by EncryptPublicUuids.
+ changes:
+ - modifyDataType:
+ tableName: report_card
+ columnName: public_uuid
+ newDataType: varchar(254)
+ rollback:
+ - modifyDataType:
+ tableName: report_card
+ columnName: public_uuid
+ newDataType: char(36)
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:03
+ author: ranquild
+ comment: Widen report_dashboard.public_uuid so it can hold the encrypted value written by EncryptPublicUuids.
+ changes:
+ - modifyDataType:
+ tableName: report_dashboard
+ columnName: public_uuid
+ newDataType: varchar(254)
+ rollback:
+ - modifyDataType:
+ tableName: report_dashboard
+ columnName: public_uuid
+ newDataType: char(36)
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:04
+ author: ranquild
+ comment: Widen action.public_uuid so it can hold the encrypted value written by EncryptPublicUuids.
+ changes:
+ - modifyDataType:
+ tableName: action
+ columnName: public_uuid
+ newDataType: varchar(254)
+ rollback:
+ - modifyDataType:
+ tableName: action
+ columnName: public_uuid
+ newDataType: char(36)
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:05
+ author: ranquild
+ comment: Widen document.public_uuid so it can hold the encrypted value written by EncryptPublicUuids.
+ changes:
+ - modifyDataType:
+ tableName: document
+ columnName: public_uuid
+ newDataType: varchar(254)
+ rollback:
+ - modifyDataType:
+ tableName: document
+ columnName: public_uuid
+ newDataType: char(36)
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:06
+ author: ranquild
+ comment: >-
+ Add report_card.public_uuid_prefix, a plaintext copy of the public_uuid's leading characters. public_uuid is
+ encrypted (non-deterministic), so it can't be matched with a SQL '='; this indexed prefix narrows a public-link
+ lookup to a handful of candidate rows that are then decrypted and compared in memory.
+ changes:
+ - addColumn:
+ tableName: report_card
+ columns:
+ - column:
+ name: public_uuid_prefix
+ type: varchar(36)
+ remarks: Plaintext leading characters of public_uuid, used as an indexed narrowing key for public-link lookups.
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:07
+ author: ranquild
+ comment: >-
+ Add report_dashboard.public_uuid_prefix, a plaintext copy of the public_uuid's leading characters. public_uuid is
+ encrypted (non-deterministic), so it can't be matched with a SQL '='; this indexed prefix narrows a public-link
+ lookup to a handful of candidate rows that are then decrypted and compared in memory.
+ changes:
+ - addColumn:
+ tableName: report_dashboard
+ columns:
+ - column:
+ name: public_uuid_prefix
+ type: varchar(36)
+ remarks: Plaintext leading characters of public_uuid, used as an indexed narrowing key for public-link lookups.
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:08
+ author: ranquild
+ comment: >-
+ Add action.public_uuid_prefix, a plaintext copy of the public_uuid's leading characters. public_uuid is
+ encrypted (non-deterministic), so it can't be matched with a SQL '='; this indexed prefix narrows a public-link
+ lookup to a handful of candidate rows that are then decrypted and compared in memory.
+ changes:
+ - addColumn:
+ tableName: action
+ columns:
+ - column:
+ name: public_uuid_prefix
+ type: varchar(36)
+ remarks: Plaintext leading characters of public_uuid, used as an indexed narrowing key for public-link lookups.
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:09
+ author: ranquild
+ comment: >-
+ Add document.public_uuid_prefix, a plaintext copy of the public_uuid's leading characters. public_uuid is
+ encrypted (non-deterministic), so it can't be matched with a SQL '='; this indexed prefix narrows a public-link
+ lookup to a handful of candidate rows that are then decrypted and compared in memory.
+ changes:
+ - addColumn:
+ tableName: document
+ columns:
+ - column:
+ name: public_uuid_prefix
+ type: varchar(36)
+ remarks: Plaintext leading characters of public_uuid, used as an indexed narrowing key for public-link lookups.
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:10
+ author: ranquild
+ comment: Add index on report_card.public_uuid_prefix for public-link lookups.
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ - indexExists:
+ tableName: report_card
+ indexName: idx_report_card_public_uuid_prefix
+ changes:
+ - createIndex:
+ tableName: report_card
+ indexName: idx_report_card_public_uuid_prefix
+ columns:
+ - column:
+ name: public_uuid_prefix
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:11
+ author: ranquild
+ comment: Add index on report_dashboard.public_uuid_prefix for public-link lookups.
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ - indexExists:
+ tableName: report_dashboard
+ indexName: idx_report_dashboard_public_uuid_prefix
+ changes:
+ - createIndex:
+ tableName: report_dashboard
+ indexName: idx_report_dashboard_public_uuid_prefix
+ columns:
+ - column:
+ name: public_uuid_prefix
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:12
+ author: ranquild
+ comment: Add index on action.public_uuid_prefix for public-link lookups.
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ - indexExists:
+ tableName: action
+ indexName: idx_action_public_uuid_prefix
+ changes:
+ - createIndex:
+ tableName: action
+ indexName: idx_action_public_uuid_prefix
+ columns:
+ - column:
+ name: public_uuid_prefix
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:13
+ author: ranquild
+ comment: Add index on document.public_uuid_prefix for public-link lookups.
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ - indexExists:
+ tableName: document
+ indexName: idx_document_public_uuid_prefix
+ changes:
+ - createIndex:
+ tableName: document
+ indexName: idx_document_public_uuid_prefix
+ columns:
+ - column:
+ name: public_uuid_prefix
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:14
+ author: ranquild
+ comment: >-
+ Backfill report_card.public_uuid_prefix from the leading 8 characters of the (still plaintext) public_uuid, so
+ existing public links keep resolving. Runs before EncryptPublicUuids, which then encrypts public_uuid
+ itself. 8 = public-sharing.core/public-uuid-prefix-length.
+ changes:
+ - sql: >-
+ UPDATE report_card SET public_uuid_prefix = SUBSTRING(public_uuid, 1, 8) WHERE public_uuid IS NOT NULL
+ rollback:
+ - sql: >-
+ UPDATE report_card SET public_uuid_prefix = NULL
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:15
+ author: ranquild
+ comment: >-
+ Backfill report_dashboard.public_uuid_prefix from the leading 8 characters of the (still plaintext) public_uuid, so
+ existing public links keep resolving. Runs before EncryptPublicUuids, which then encrypts public_uuid
+ itself. 8 = public-sharing.core/public-uuid-prefix-length.
+ changes:
+ - sql: >-
+ UPDATE report_dashboard SET public_uuid_prefix = SUBSTRING(public_uuid, 1, 8) WHERE public_uuid IS NOT NULL
+ rollback:
+ - sql: >-
+ UPDATE report_dashboard SET public_uuid_prefix = NULL
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:16
+ author: ranquild
+ comment: >-
+ Backfill action.public_uuid_prefix from the leading 8 characters of the (still plaintext) public_uuid, so
+ existing public links keep resolving. Runs before EncryptPublicUuids, which then encrypts public_uuid
+ itself. 8 = public-sharing.core/public-uuid-prefix-length.
+ changes:
+ - sql: >-
+ UPDATE action SET public_uuid_prefix = SUBSTRING(public_uuid, 1, 8) WHERE public_uuid IS NOT NULL
+ rollback:
+ - sql: >-
+ UPDATE action SET public_uuid_prefix = NULL
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:17
+ author: ranquild
+ comment: >-
+ Backfill document.public_uuid_prefix from the leading 8 characters of the (still plaintext) public_uuid, so
+ existing public links keep resolving. Runs before EncryptPublicUuids, which then encrypts public_uuid
+ itself. 8 = public-sharing.core/public-uuid-prefix-length.
+ changes:
+ - sql: >-
+ UPDATE document SET public_uuid_prefix = SUBSTRING(public_uuid, 1, 8) WHERE public_uuid IS NOT NULL
+ rollback:
+ - sql: >-
+ UPDATE document SET public_uuid_prefix = NULL
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:18
+ author: ranquild
+ comment: >-
+ Encrypt any plaintext public_uuid values at rest for cards, dashboards, actions, and documents, so the column
+ can be read strictly once MB_ENCRYPTION_SECRET_KEY is set. Otherwise an attacker with SQL write access could
+ forge a public link by inserting a chosen public_uuid; encrypting it means such a plaintext value is rejected on
+ the strict read. Already-encrypted values are left untouched.
+ changes:
+ - customChange:
+ class: metabase.app_db.custom_migrations.EncryptPublicUuids
+
# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<
########################################################################################################################
diff --git a/src/metabase/actions/models.clj b/src/metabase/actions/models.clj
index d501fd2d902..c1891c23940 100644
--- a/src/metabase/actions/models.clj
+++ b/src/metabase/actions/models.clj
@@ -7,6 +7,7 @@
[metabase.models.interface :as mi]
[metabase.models.serialization :as serdes]
[metabase.parameters.core :as parameters]
+ [metabase.public-sharing.core :as public-sharing]
[metabase.queries.models.query :as query]
[metabase.search.core :as search]
[metabase.util :as u]
@@ -53,6 +54,7 @@
(t2/deftransforms :model/Action
{:type mi/transform-keyword
+ :public_uuid mi/transform-encrypted-text
:parameter_mappings parameters/transform-parameter-mappings
:parameters parameters/transform-parameters
:visualization_settings transform-action-visualization-settings})
@@ -89,12 +91,12 @@
(t2/define-before-insert :model/Action
[{model-id :model_id, :as action}]
- (u/prog1 action
+ (u/prog1 (public-sharing/add-public-uuid-prefix action)
(check-model-is-not-a-saved-question model-id)))
(t2/define-before-update :model/Action
[{archived? :archived, id :id, model-id :model_id, :as changes}]
- (u/prog1 changes
+ (u/prog1 (public-sharing/add-public-uuid-prefix-if-changed changes)
(if archived?
(t2/delete! :model/DashboardCard :action_id id)
(check-model-is-not-a-saved-question model-id))))
@@ -108,7 +110,7 @@
(def ^:private action-columns
"The columns that are common to all Action types."
[:archived :created_at :creator_id :description :entity_id :made_public_by_id :model_id :name :parameter_mappings
- :parameters :public_uuid :type :updated_at :visualization_settings])
+ :parameters :public_uuid :public_uuid_prefix :type :updated_at :visualization_settings])
(mu/defn- type->model
"Returns the model from an action type.
@@ -464,7 +466,8 @@
(defmethod serdes/make-spec "Action" [_model-name opts]
{:copy [:archived :description :entity_id :name :public_uuid]
- :skip []
+ :skip [;; always re-derived from public_uuid on import
+ :public_uuid_prefix]
:transform {:created_at (serdes/date)
:type (serdes/kw)
:creator_id (serdes/fk :model/User)
diff --git a/src/metabase/app_db/custom_migrations.clj b/src/metabase/app_db/custom_migrations.clj
index b2257930a5c..ccfa7b53b05 100644
--- a/src/metabase/app_db/custom_migrations.clj
+++ b/src/metabase/app_db/custom_migrations.clj
@@ -2276,3 +2276,29 @@
:where [:= :id id]})))
(t2/reducible-query {:select [:id :key]
:from [:api_key]}))))
+
+(define-reversible-migration EncryptPublicUuids
+ (when (encryption/default-encryption-enabled?)
+ (doseq [table [:report_card :report_dashboard :action :document]]
+ (run! (fn [{:keys [id public_uuid]}]
+ (when (and (string? public_uuid)
+ (not (str/blank? public_uuid))
+ (not (encryption/possibly-encrypted-string? public_uuid)))
+ (t2/query {:update table
+ :set {:public_uuid (encryption/maybe-encrypt public_uuid)}
+ :where [:= :id id]})))
+ (t2/reducible-query {:select [:id :public_uuid]
+ :from [table]
+ :where [:!= :public_uuid nil]}))))
+ (when (encryption/default-encryption-enabled?)
+ (doseq [table [:report_card :report_dashboard :action :document]]
+ (run! (fn [{:keys [id public_uuid]}]
+ (when (and (string? public_uuid)
+ (not (str/blank? public_uuid))
+ (encryption/possibly-encrypted-string? public_uuid))
+ (t2/query {:update table
+ :set {:public_uuid (encryption/maybe-decrypt public_uuid)}
+ :where [:= :id id]})))
+ (t2/reducible-query {:select [:id :public_uuid]
+ :from [table]
+ :where [:!= :public_uuid nil]})))))
diff --git a/src/metabase/app_db/encryption.clj b/src/metabase/app_db/encryption.clj
index 73c8e7f4e31..0b73b58cffa 100644
--- a/src/metabase/app_db/encryption.clj
+++ b/src/metabase/app_db/encryption.clj
@@ -35,7 +35,11 @@
[:auth_identity :credentials]
[:exploration_query_result :chart_stats]
[:exploration_query_result :metric_description]
- [:exploration_query_result :chart_description]])
+ [:exploration_query_result :chart_description]
+ [:report_card :public_uuid]
+ [:report_dashboard :public_uuid]
+ [:action :public_uuid]
+ [:document :public_uuid]])
(def ^:private encrypted-bytes-columns
"`^bytes` columns encrypted at rest via `mi/transform-secret-value` (a strict `maybe-decrypt-bytes` on read). Unlike
diff --git a/src/metabase/dashboards/models/dashboard.clj b/src/metabase/dashboards/models/dashboard.clj
index 6bb52e18828..fd43867a230 100644
--- a/src/metabase/dashboards/models/dashboard.clj
+++ b/src/metabase/dashboards/models/dashboard.clj
@@ -69,6 +69,7 @@
(t2/deftransforms :model/Dashboard
{:parameters parameters/transform-parameters
+ :public_uuid mi/transform-encrypted-text
:embedding_params mi/transform-json})
(t2/define-before-delete :model/Dashboard
@@ -81,7 +82,7 @@
[dashboard]
(let [defaults {:parameters []}
dashboard (lib/normalize ::dashboards.schema/dashboard (merge defaults dashboard))]
- (u/prog1 dashboard
+ (u/prog1 (public-sharing/add-public-uuid-prefix dashboard)
(collection/check-allowed-content :model/Dashboard (:collection_id dashboard))
(params/assert-valid-parameters dashboard)
(collection/check-collection-namespace :model/Dashboard (:collection_id dashboard)))))
@@ -97,7 +98,9 @@
dashboard (lib/normalize ::dashboards.schema/dashboard dashboard)
changes (lib/normalize ::dashboards.schema/dashboard changes)]
(collection/check-allowed-content :model/Dashboard (:collection_id changes))
- (u/prog1 (maybe-populate-initially-published-at dashboard)
+ (u/prog1 (-> dashboard
+ maybe-populate-initially-published-at
+ public-sharing/add-public-uuid-prefix-if-changed)
(params/assert-valid-parameters dashboard)
(when (:parameters changes)
(queries/upsert-or-delete-parameter-cards-from-parameters! "dashboard" (:id dashboard) (:parameters dashboard)))
@@ -450,7 +453,9 @@
:skip [;; those stats are inherently local state
:view_count :last_viewed_at
;; this is deprecated
- :cache_ttl]
+ :cache_ttl
+ ;; always re-derived from public_uuid on import
+ :public_uuid_prefix]
:transform {:created_at (serdes/date)
:initially_published_at (serdes/date)
:collection_id (serdes/fk :model/Collection)
diff --git a/src/metabase/documents/models/document.clj b/src/metabase/documents/models/document.clj
index 962c5330ae6..d845d572f63 100644
--- a/src/metabase/documents/models/document.clj
+++ b/src/metabase/documents/models/document.clj
@@ -26,7 +26,8 @@
:model/Document)
(t2/deftransforms :model/Document
- {:document mi/transform-json})
+ {:document mi/transform-json
+ :public_uuid mi/transform-encrypted-text})
(doto :model/Document
(derive :metabase/model)
@@ -226,7 +227,7 @@
(defmethod serdes/make-spec "Document"
[_model-name _opts]
{:copy [:archived :archived_directly :content_type :entity_id :name :collection_position]
- :skip [:view_count :last_viewed_at :public_uuid :made_public_by_id]
+ :skip [:view_count :last_viewed_at :public_uuid :public_uuid_prefix :made_public_by_id]
:transform {:created_at (serdes/date)
:updated_at (serdes/date)
:document {:export-with-context export-document-content
@@ -295,8 +296,8 @@
(t2/define-before-insert :model/Document [model]
(collection/check-allowed-content :model/Document (:collection_id model))
- model)
+ (public-sharing/add-public-uuid-prefix model))
(t2/define-before-update :model/Document [model]
(collection/check-allowed-content :model/Document (:collection_id (t2/changes model)))
- model)
+ (public-sharing/add-public-uuid-prefix-if-changed model))
diff --git a/src/metabase/documents/revisions/impl.clj b/src/metabase/documents/revisions/impl.clj
index 6efd40fda6d..ca9a062230a 100644
--- a/src/metabase/documents/revisions/impl.clj
+++ b/src/metabase/documents/revisions/impl.clj
@@ -22,6 +22,7 @@
:collection_id
:collection_position
:public_uuid
+ :public_uuid_prefix
:made_public_by_id})
(defmethod revisions/serialize-instance :model/Document
diff --git a/src/metabase/embedding_rest/api/common.clj b/src/metabase/embedding_rest/api/common.clj
index b7b12eb1731..3ecbcb7778f 100644
--- a/src/metabase/embedding_rest/api/common.clj
+++ b/src/metabase/embedding_rest/api/common.clj
@@ -326,7 +326,7 @@
token-params (embed/get-in-unsigned-token-or-throw unsigned-token [:params])
resolved-embedding-params (or embedding-params
(t2/select-one-fn :embedding_params :model/Card :id card-id))]
- (-> (apply api.public/public-card :id card-id, constraints)
+ (-> (apply api.public/public-card card-id constraints)
api.public/combine-parameters-and-template-tags
(remove-token-parameters token-params)
(remove-locked-and-disabled-params resolved-embedding-params)
@@ -447,7 +447,7 @@
embedding-params (or embedding-params
(t2/select-one-fn :embedding_params :model/Dashboard, :id dashboard-id))
token-params (embed/get-in-unsigned-token-or-throw unsigned-token [:params])]
- (-> (apply api.public/public-dashboard :id dashboard-id constraints)
+ (-> (apply api.public/public-dashboard dashboard-id constraints)
(substitute-token-parameters-in-text token-params)
(remove-locked-parameters embedding-params)
(remove-token-parameters token-params)
diff --git a/src/metabase/public_sharing/core.clj b/src/metabase/public_sharing/core.clj
index 1c27b74d48e..cb7758ed147 100644
--- a/src/metabase/public_sharing/core.clj
+++ b/src/metabase/public_sharing/core.clj
@@ -1,6 +1,20 @@
(ns metabase.public-sharing.core
(:require
- [metabase.public-sharing.settings :as public-sharing.settings]))
+ [medley.core :as m]
+ [metabase.public-sharing.settings :as public-sharing.settings]
+ [toucan2.core :as t2]))
+
+(def public-uuid-prefix-length
+ "How many leading characters of a `public_uuid` are copied into the plaintext `public_uuid_prefix` column.
+
+ The full `public_uuid` is encrypted at rest with a non-deterministic cipher, so it can't be matched with a SQL `=`.
+ `public_uuid_prefix` is an indexed, unencrypted narrowing key: a lookup filters to the few rows sharing these leading
+ characters and then decrypts and compares the full uuid in memory. Eight hex characters keep that candidate set at
+ ~1 row well past our shared-entity counts while leaving the remaining ~96 bits of the token secret.
+
+ Changing this value requires a data migration to rewrite `public_uuid_prefix` for existing rows, or their public
+ links will stop resolving."
+ 8)
(defn remove-public-uuid-if-public-sharing-is-disabled
"If public sharing is *disabled* and `object` has a `:public_uuid`, remove it so people don't try to use it (since it
@@ -10,3 +24,59 @@
(not (public-sharing.settings/enable-public-sharing)))
(assoc object :public_uuid nil)
object))
+
+(defn public-uuid-prefix
+ "The plaintext lookup prefix for `public-uuid` — its first [[public-uuid-prefix-length]] characters — or nil when
+ there is no uuid."
+ [public-uuid]
+ (when public-uuid
+ (subs public-uuid 0 (min public-uuid-prefix-length (count public-uuid)))))
+
+(defn add-public-uuid-prefix
+ "Set `:public_uuid_prefix` from `:public_uuid`. Intended for a before-INSERT hook: a freshly inserted row always gets
+ a prefix matching its `:public_uuid` (nil when it has none). `:public_uuid` is still plaintext at this point — the
+ encrypting transform runs on the way to the database, after this hook."
+ [entity]
+ (assoc entity :public_uuid_prefix (public-uuid-prefix (:public_uuid entity))))
+
+(defn add-public-uuid-prefix-if-changed
+ "Re-derive `:public_uuid_prefix` from `:public_uuid`, but only when the update actually changes `:public_uuid`.
+ Intended for a before-UPDATE hook. Deriving it on every update is wrong: the instance's `:public_uuid` is nil'd out
+ by the post-select hook when public sharing is disabled, so an unrelated update would blank the prefix while the real
+ (encrypted) `public_uuid` column is left untouched, permanently breaking the public link. Whenever `:public_uuid` is
+ genuinely in the changes we re-derive the prefix, nil or not."
+ [entity]
+ (if (contains? (t2/changes entity) :public_uuid)
+ (assoc entity :public_uuid_prefix (public-uuid-prefix (:public_uuid entity)))
+ entity))
+
+(defn public-uuid->id
+ "Resolve a shared entity's public `uuid` to the id of the matching `model` row, or nil.
+
+ `public_uuid` is encrypted with a non-deterministic cipher, so it can't be matched with a SQL `=`. The lookup filters
+ by the plaintext `public_uuid_prefix` (an indexed copy of the uuid's first [[public-uuid-prefix-length]] characters)
+ to a small candidate set, then compares the model-decrypted `public_uuid` in memory. A value written outside the
+ model — e.g. a plaintext uuid forged via raw SQL — fails the strict decrypting read and errors out rather than
+ resolving."
+ [model uuid]
+ (when uuid
+ (some (fn [{:keys [id public_uuid]}]
+ (when (= uuid public_uuid)
+ id))
+ (t2/select [model :id :public_uuid]
+ :public_uuid_prefix (public-uuid-prefix uuid)
+ :archived false))))
+
+(defn public-uuid->model
+ "Resolve a shared entity's public `uuid` to the full matching `model` row, or nil. Like [[public-uuid->id]] but
+ returns the whole instance so callers needn't re-select it by id.
+
+ Selects full rows narrowed by the indexed `public_uuid_prefix` and compares the model-decrypted `public_uuid` in
+ memory (see [[public-uuid->id]] for why the prefix + in-memory `=` is needed). A value written outside the model —
+ e.g. a plaintext uuid forged via raw SQL — fails the strict decrypting read and errors out rather than resolving."
+ [model uuid]
+ (when uuid
+ (m/find-first #(= uuid (:public_uuid %))
+ (t2/select model
+ :public_uuid_prefix (public-uuid-prefix uuid)
+ :archived false))))
diff --git a/src/metabase/public_sharing_rest/api.clj b/src/metabase/public_sharing_rest/api.clj
index 1bde6012e7b..892c484de94 100644
--- a/src/metabase/public_sharing_rest/api.clj
+++ b/src/metabase/public_sharing_rest/api.clj
@@ -19,6 +19,7 @@
[metabase.parameters.dashboard :as parameters.dashboard]
[metabase.parameters.params :as params]
[metabase.parameters.schema :as parameters.schema]
+ [metabase.public-sharing.core :as public-sharing]
[metabase.public-sharing.validation :as public-sharing.validation]
[metabase.queries.core :as queries]
[metabase.query-processor.card :as qp.card]
@@ -99,18 +100,18 @@
(seq query) blank-dataset-query)))))))
(defn public-card
- "Return a public Card matching key-value `conditions`, removing all columns that should not be visible to the general
- public. Throws a 404 if the Card doesn't exist."
- [& conditions]
+ "Return the public Card with the given `card-id` (plus any extra key-value `conditions`), removing all columns that
+ should not be visible to the general public. Throws a 404 if the Card doesn't exist."
+ [card-id & conditions]
(binding [params/*ignore-current-user-perms-and-return-all-field-values* true]
(-> (api/check-404 (apply t2/select-one [:model/Card :id :dataset_query :description :display :name :parameters
:visualization_settings :card_schema]
- :archived false, conditions))
+ :id card-id, :archived false, conditions))
combine-parameters-and-template-tags
(t2/hydrate :param_fields)
remove-card-non-public-columns)))
-(defn- card-with-uuid [uuid] (public-card :public_uuid uuid))
+(defn- card-with-uuid [uuid] (public-card (public-sharing/public-uuid->id :model/Card uuid)))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
@@ -195,7 +196,7 @@
`StreamingResponse` object that should be returned as the result of an API endpoint."
[uuid export-format parameters & options]
(public-sharing.validation/check-public-sharing-enabled)
- (let [card (api/check-404 (t2/select-one :model/Card :public_uuid uuid, :archived false))]
+ (let [card (api/check-404 (public-sharing/public-uuid->model :model/Card uuid))]
(apply process-query-for-card-with-id card export-format parameters options)))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
@@ -269,13 +270,13 @@
(select-keys action-public-keys))))
(mu/defn public-dashboard :- ::dashboards.schema/dashboard
- "Return a public Dashboard matching key-value `conditions`, removing all columns that should not be visible to the
- general public. Throws a 404 if the Dashboard doesn't exist."
- [& conditions]
+ "Return the public Dashboard with the given `dashboard-id` (plus any extra key-value `conditions`), removing all
+ columns that should not be visible to the general public. Throws a 404 if the Dashboard doesn't exist."
+ [dashboard-id & conditions]
{:pre [(even? (count conditions))]}
(binding [params/*ignore-current-user-perms-and-return-all-field-values* true
params/*field-id-context* (atom params/empty-field-id-context)]
- (-> (api/check-404 (apply t2/select-one [:model/Dashboard :name :description :id :parameters :auto_apply_filters :width], :archived false, conditions))
+ (-> (api/check-404 (apply t2/select-one [:model/Dashboard :name :description :id :parameters :auto_apply_filters :width], :id dashboard-id, :archived false, conditions))
(t2/hydrate [:dashcards :card :series :dashcard/action] :tabs :param_fields)
api.dashboard/add-query-average-durations
(update :dashcards (fn [dashcards]
@@ -288,7 +289,7 @@
(remove-card-non-public-columns series))))
(m/update-existing :action public-action))))))))
-(defn- dashboard-with-uuid [uuid] (public-dashboard :public_uuid uuid))
+(defn- dashboard-with-uuid [uuid] (public-dashboard (public-sharing/public-uuid->id :model/Dashboard uuid)))
(defn- dashboard-with-uuid-for-param-values
"Light Dashboard fetch for the public param-value endpoints. Unlike [[dashboard-with-uuid]]/[[public-dashboard]] it
@@ -296,7 +297,7 @@
`dashboard-param-remapped-value` only need `:resolved-params`, which they hydrate themselves. Matches how the embed
param-value endpoints fetch the Dashboard."
[uuid]
- (api/check-404 (t2/select-one :model/Dashboard :public_uuid uuid, :archived false)))
+ (api/check-404 (public-sharing/public-uuid->model :model/Dashboard uuid)))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
@@ -367,7 +368,7 @@
[:ignore_cache {:optional true} [:maybe ms/BooleanValue]]]]
(public-sharing.validation/check-public-sharing-enabled)
(let [card (api/check-404 (t2/select-one :model/Card :id card-id :archived false))
- dashboard (api/check-404 (t2/select-one :model/Dashboard :public_uuid uuid, :archived false))
+ dashboard (api/check-404 (public-sharing/public-uuid->model :model/Dashboard uuid))
dashcard (api/check-404 (t2/select-one :model/DashboardCard :id dashcard-id))]
(process-query-for-dashcard
:dashboard dashboard
@@ -397,7 +398,7 @@
[:csv_include_bom {:optional true} [:maybe ms/BooleanValue]]]]
(public-sharing.validation/check-public-sharing-enabled)
(let [card (api/check-404 (t2/select-one :model/Card :id card-id :archived false))
- dashboard (api/check-404 (t2/select-one :model/Dashboard :public_uuid uuid, :archived false))
+ dashboard (api/check-404 (public-sharing/public-uuid->model :model/Dashboard uuid))
dashcard (api/check-404 (t2/select-one :model/DashboardCard :id dashcard-id))]
(u/prog1 (process-query-for-dashcard
:dashboard dashboard
@@ -421,7 +422,7 @@
{:keys [parameters]} :- [:map
[:parameters ::actions.schema/prefetch-parameter-values]]]
(public-sharing.validation/check-public-sharing-enabled)
- (let [dashboard-id (api/check-404 (t2/select-one-pk :model/Dashboard :public_uuid uuid :archived false))]
+ (let [dashboard-id (api/check-404 (public-sharing/public-uuid->id :model/Dashboard uuid))]
(api/check-404 (t2/select-one-pk :model/DashboardCard :id dashcard-id :dashboard_id dashboard-id))
(actions/fetch-values
(api/check-404 (actions/dashcard->action dashcard-id))
@@ -457,7 +458,7 @@
throttle-time (assoc :headers {"Retry-After" throttle-time}))
(do
(public-sharing.validation/check-public-sharing-enabled)
- (let [dashboard-id (api/check-404 (t2/select-one-pk :model/Dashboard :public_uuid uuid, :archived false))]
+ (let [dashboard-id (api/check-404 (public-sharing/public-uuid->id :model/Dashboard uuid))]
;; Run this query with full superuser perms. We don't want the various perms checks
;; failing because there are no current user perms; if this Dashcard is public
;; you're by definition allowed to run it without a perms check anyway
@@ -509,7 +510,7 @@
[{:keys [uuid]} :- [:map
[:uuid ms/UUIDString]]]
(public-sharing.validation/check-public-sharing-enabled)
- (let [action (api/check-404 (actions/select-action :public_uuid uuid :archived false))]
+ (let [action (api/check-404 (actions/select-action :id (public-sharing/public-uuid->id :model/Action uuid)))]
(actions/check-actions-enabled! action)
(public-action action)))
@@ -529,7 +530,7 @@
[:uuid ms/UUIDString]
[:param-key ms/NonBlankString]]]
(public-sharing.validation/check-public-sharing-enabled)
- (let [card (t2/select-one :model/Card :public_uuid uuid, :archived false)]
+ (let [card (public-sharing/public-uuid->model :model/Card uuid)]
(request/as-admin
(queries/card-param-values card param-key))))
@@ -544,7 +545,7 @@
[:param-key ms/NonBlankString]
[:query ms/NonBlankString]]]
(public-sharing.validation/check-public-sharing-enabled)
- (let [card (t2/select-one :model/Card :public_uuid uuid, :archived false)]
+ (let [card (public-sharing/public-uuid->model :model/Card uuid)]
(request/as-admin
(queries/card-param-values card param-key query))))
@@ -559,7 +560,7 @@
[:param-key ms/NonBlankString]]
{:keys [value]} :- [:map [:value :string]]]
(public-sharing.validation/check-public-sharing-enabled)
- (let [card (t2/select-one :model/Card :public_uuid uuid, :archived false)]
+ (let [card (public-sharing/public-uuid->model :model/Card uuid)]
(request/as-admin
(queries/card-param-remapped-value card param-key (codec/url-decode value)))))
@@ -649,7 +650,7 @@
[:ignore_cache {:optional true} [:maybe ms/BooleanValue]]]]
(public-sharing.validation/check-public-sharing-enabled)
(let [card (api/check-404 (t2/select-one :model/Card :id card-id :archived false))
- dashboard (api/check-404 (t2/select-one :model/Dashboard :public_uuid uuid, :archived false))
+ dashboard (api/check-404 (public-sharing/public-uuid->model :model/Dashboard uuid))
dashcard (api/check-404 (t2/select-one :model/DashboardCard :id dashcard-id))]
(process-query-for-dashcard
:dashboard dashboard
@@ -699,7 +700,7 @@
;; failing because there are no current user perms; if this Dashcard is public
;; you're by definition allowed to run it without a perms check anyway
(request/as-admin
- (let [action (api/check-404 (actions/select-action :public_uuid uuid :archived false))]
+ (let [action (api/check-404 (actions/select-action :id (public-sharing/public-uuid->id :model/Action uuid)))]
(actions/check-actions-enabled! action)
(analytics/track-event! :snowplow/action
{:event :action-executed
@@ -731,7 +732,7 @@
[:latField ::api.tiles/legacy-ref]
[:lonField ::api.tiles/legacy-ref]]]
(public-sharing.validation/check-public-sharing-enabled)
- (let [card (api/check-404 (t2/select-one :model/Card :public_uuid uuid, :archived false))]
+ (let [card (api/check-404 (public-sharing/public-uuid->model :model/Card uuid))]
(request/as-admin
(api.tiles/process-tiles-query-for-card card parameters zoom x y latField lonField))))
@@ -756,7 +757,7 @@
[:latField ::api.tiles/legacy-ref]
[:lonField ::api.tiles/legacy-ref]]]
(public-sharing.validation/check-public-sharing-enabled)
- (let [dashboard (api/check-404 (t2/select-one :model/Dashboard :public_uuid uuid, :archived false))
+ (let [dashboard (api/check-404 (public-sharing/public-uuid->model :model/Dashboard uuid))
dashcard (api/check-404 (t2/select-one :model/DashboardCard dashcard-id))
card (api/check-404 (t2/select-one :model/Card card-id))]
(request/as-admin
@@ -781,9 +782,9 @@
consistent experience with public dashboards. This also allows us to filter sensitive fields from all cards at
once before exposing them to unauthenticated users. The document and all cards must not be archived to be
accessible publicly."
- [& conditions]
- (let [document (-> (api/check-404 (apply t2/select-one [:model/Document :id :name :document :content_type :created_at :updated_at]
- :archived false, conditions))
+ [document-id]
+ (let [document (-> (api/check-404 (t2/select-one [:model/Document :id :name :document :content_type :created_at :updated_at]
+ :id document-id, :archived false))
;; Hydrate cards via Toucan batched hydration to avoid N+1 queries
(t2/hydrate :cards))
embedded-ids (set (prose-mirror/card-ids document))]
@@ -803,8 +804,7 @@
Returns the loaded `:model/Card` entity so the caller can thread it downstream without re-selecting it."
[uuid card-id]
- (let [document (api/check-404 (t2/select-one [:model/Document :id :document :content_type]
- :public_uuid uuid :archived false))]
+ (let [document (api/check-404 (t2/select-one [:model/Document :id :document :content_type] :id (public-sharing/public-uuid->id :model/Document uuid)))]
(api/check-404 (when (contains? (set (prose-mirror/card-ids document)) card-id)
(t2/select-one :model/Card :id card-id :document_id (:id document) :archived false)))))
@@ -821,7 +821,7 @@
[{:keys [uuid]} :- [:map
[:uuid ms/UUIDString]]]
(public-sharing.validation/check-public-sharing-enabled)
- (let [document (public-document :public_uuid uuid)]
+ (let [document (public-document (public-sharing/public-uuid->id :model/Document uuid))]
(events/publish-event! :event/document-read {:object-id (:id document), :user-id api/*current-user-id*})
document))
diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.clj
index 75adcf1ba57..a22fe2966bc 100644
--- a/src/metabase/queries/models/card.clj
+++ b/src/metabase/queries/models/card.clj
@@ -117,6 +117,7 @@
(t2/deftransforms :model/Card
{:dataset_query lib-be/transform-query
+ :public_uuid mi/transform-encrypted-text
:display mi/transform-keyword
:embedding_params mi/transform-json
:query_type mi/transform-keyword
@@ -820,7 +821,8 @@
(u/assoc-default :entity_id (u/generate-nano-id))
card.metadata/populate-result-metadata
pre-insert
- populate-query-fields)
+ populate-query-fields
+ public-sharing/add-public-uuid-prefix)
(collection/check-allowed-content (:type <>) (:collection_id <>))))
(t2/define-after-insert :model/Card
@@ -880,7 +882,8 @@
(populate-query-fields (contains? changes :dataset_query))
(clear-metabot-origin changes)
(pre-update changes)
- maybe-populate-initially-published-at)))
+ maybe-populate-initially-published-at
+ public-sharing/add-public-uuid-prefix-if-changed)))
;; Cards don't normally get deleted (they get archived instead) so this mostly affects tests
(t2/define-before-delete :model/Card
@@ -1421,7 +1424,9 @@
;; always re-derived from dataset_query by populate-query-fields on import
:table_id :source_card_id
;; instance-specific Metabot origin (which conversation/chart the card was saved from)
- :metabot_conversation_id :metabot_chart_id]
+ :metabot_conversation_id :metabot_chart_id
+ ;; always re-derived from public_uuid on import
+ :public_uuid_prefix]
:transform
{:created_at (serdes/date)
;; database_id is usually derivable from dataset_query, but must be kept when the query
diff --git a/src/metabase/revisions/impl/card.clj b/src/metabase/revisions/impl/card.clj
index 9f38c0a1fc3..79bbcbb837f 100644
--- a/src/metabase/revisions/impl/card.clj
+++ b/src/metabase/revisions/impl/card.clj
@@ -27,6 +27,7 @@
:metabot_chart_id
:metabot_conversation_id
:public_uuid
+ :public_uuid_prefix
:updated_at
:view_count})
diff --git a/src/metabase/revisions/impl/dashboard.clj b/src/metabase/revisions/impl/dashboard.clj
index 9f33c020471..2461de2afa4 100644
--- a/src/metabase/revisions/impl/dashboard.clj
+++ b/src/metabase/revisions/impl/dashboard.clj
@@ -19,7 +19,7 @@
;; > The position this Dashboard should appear in the Dashboards list,
;; lower-numbered positions appearing before higher numbered ones.
;; TODO: querying on stats we don't have any dashboard that has a position, maybe we could just drop it?
- :public_uuid :made_public_by_id :enable_embedding :embedding_params :embedding_type
+ :public_uuid :public_uuid_prefix :made_public_by_id :enable_embedding :embedding_params :embedding_type
:position :initially_published_at :view_count
:last_viewed_at})
diff --git a/test/metabase/documents/models/document_test.clj b/test/metabase/documents/models/document_test.clj
index 6e882b19e30..5087f2c738a 100644
--- a/test/metabase/documents/models/document_test.clj
+++ b/test/metabase/documents/models/document_test.clj
@@ -432,7 +432,7 @@
(let [spec (serdes/make-spec "Document" {})]
(is (= [:archived :archived_directly :content_type :entity_id :name :collection_position]
(:copy spec)))
- (is (= [:view_count :last_viewed_at :public_uuid :made_public_by_id] (:skip spec)))
+ (is (= [:view_count :last_viewed_at :public_uuid :public_uuid_prefix :made_public_by_id] (:skip spec)))
(is (contains? (:transform spec) :created_at))
(is (contains? (:transform spec) :document))
(is (contains? (:transform spec) :updated_at))
diff --git a/test/metabase/documents/revisions/impl_test.clj b/test/metabase/documents/revisions/impl_test.clj
index 50422c82650..7a284bbf7da 100644
--- a/test/metabase/documents/revisions/impl_test.clj
+++ b/test/metabase/documents/revisions/impl_test.clj
@@ -98,4 +98,5 @@
(is (contains? excluded-columns :updated_at))
(is (contains? excluded-columns :collection_id))
(is (contains? excluded-columns :collection_position))
- (is (= 10 (count excluded-columns)) "Should exclude exactly 10 metadata fields"))))
+ (is (contains? excluded-columns :public_uuid_prefix))
+ (is (= 11 (count excluded-columns)) "Should exclude exactly 11 metadata fields"))))
diff --git a/test/metabase/public_sharing_rest/api_test.clj b/test/metabase/public_sharing_rest/api_test.clj
index e4f34682619..f7629124a68 100644
--- a/test/metabase/public_sharing_rest/api_test.clj
+++ b/test/metabase/public_sharing_rest/api_test.clj
@@ -17,6 +17,7 @@
[metabase.permissions.models.permissions :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.public-sharing-rest.api :as api.public]
+ [metabase.public-sharing.core :as public-sharing]
[metabase.queries-rest.api.card-test :as api.card-test]
[metabase.query-processor.card-test :as qp.card-test]
[metabase.query-processor.middleware.process-userland-query-test :as process-userland-query-test]
@@ -27,6 +28,8 @@
[metabase.test.util :as tu]
[metabase.tiles.api-test :as tiles.api-test]
[metabase.util :as u]
+ [metabase.util.encryption :as encryption]
+ [metabase.util.encryption-test :as encryption-test]
[metabase.util.json :as json]
[metabase.warehouse-schema.models.field-values :as field-values]
[throttle.core :as throttle]
@@ -86,6 +89,114 @@
(fn [~binding]
~@body)))
+;;; ------------------------------------- public_uuid encryption + prefix -------------------------------------
+
+(def ^:private encryption-test-secret-key "public-uuid-encryption-test-key")
+
+(defn- raw-public-uuid
+ "Read the `public_uuid` column straight from the DB (raw ciphertext), bypassing the model's decrypting transform."
+ [model id]
+ (:public_uuid (t2/query-one {:select [:public_uuid] :from [(t2/table-name model)] :where [:= :id id]})))
+
+(defn- raw-public-uuid-prefix
+ [model id]
+ (:public_uuid_prefix (t2/query-one {:select [:public_uuid_prefix] :from [(t2/table-name model)] :where [:= :id id]})))
+
+(defn- set-raw-public-uuid!
+ "Forge a public link via raw SQL: write a plaintext `public_uuid` (and a matching prefix so the lookup would find it),
+ bypassing the model's encrypting transform."
+ [model id value]
+ (t2/query {:update (t2/table-name model)
+ :set {:public_uuid value
+ :public_uuid_prefix (public-sharing/public-uuid-prefix value)}
+ :where [:= :id id]}))
+
+(defn- assert-public-uuid-lifecycle!
+ "For an already-created UNSHARED `model` row `id` (no public_uuid), exercise share / unrelated-update-while-disabled /
+ unshare and assert `public_uuid` stays encrypted at rest while `public_uuid_prefix` always tracks it. Runs inside a
+ secret-key + public-sharing-enabled context."
+ [model id]
+ (testing "a row with no public_uuid has no prefix"
+ (is (nil? (raw-public-uuid model id)))
+ (is (nil? (raw-public-uuid-prefix model id))))
+ (let [uuid (str (random-uuid))]
+ (testing "sharing encrypts the uuid at rest and derives the prefix"
+ (t2/update! model id {:public_uuid uuid})
+ (let [raw (raw-public-uuid model id)]
+ (is (encryption/possibly-encrypted-string? raw) "public_uuid is stored as ciphertext")
+ (is (not= uuid raw) "public_uuid is not stored in plaintext")
+ (is (= uuid (encryption/maybe-decrypt raw)) "and decrypts back to the uuid"))
+ (is (= (subs uuid 0 public-sharing/public-uuid-prefix-length) (raw-public-uuid-prefix model id))
+ "prefix is the plaintext leading characters of the uuid")
+ (is (= id (public-sharing/public-uuid->id model uuid)) "resolves by uuid via the prefix lookup"))
+ (testing "an unrelated update while public sharing is disabled leaves uuid + prefix intact"
+ (mt/with-temporary-setting-values [enable-public-sharing false]
+ (t2/update! model id {:name "renamed while unshared"}))
+ (is (= uuid (encryption/maybe-decrypt (raw-public-uuid model id))) "public_uuid untouched")
+ (is (= (subs uuid 0 public-sharing/public-uuid-prefix-length) (raw-public-uuid-prefix model id))
+ "prefix untouched")
+ (is (= id (public-sharing/public-uuid->id model uuid)) "still resolves"))
+ (testing "unsharing clears uuid + prefix"
+ (t2/update! model id {:public_uuid nil})
+ (is (nil? (raw-public-uuid model id)))
+ (is (nil? (raw-public-uuid-prefix model id)))
+ (is (nil? (public-sharing/public-uuid->id model uuid)) "no longer resolves"))))
+
+(deftest card-public-uuid-encryption-lifecycle-test
+ (encryption-test/with-secret-key encryption-test-secret-key
+ (mt/with-temporary-setting-values [enable-public-sharing true]
+ (mt/with-temp [:model/Card {id :id} {}]
+ (assert-public-uuid-lifecycle! :model/Card id)))))
+
+(deftest dashboard-public-uuid-encryption-lifecycle-test
+ (encryption-test/with-secret-key encryption-test-secret-key
+ (mt/with-temporary-setting-values [enable-public-sharing true]
+ (mt/with-temp [:model/Dashboard {id :id} {}]
+ (assert-public-uuid-lifecycle! :model/Dashboard id)))))
+
+(deftest document-public-uuid-encryption-lifecycle-test
+ (encryption-test/with-secret-key encryption-test-secret-key
+ (mt/with-temporary-setting-values [enable-public-sharing true]
+ (mt/with-temp [:model/Document {id :id} {:name "Signature Doc"}]
+ (assert-public-uuid-lifecycle! :model/Document id)))))
+
+(deftest action-public-uuid-encryption-lifecycle-test
+ (encryption-test/with-secret-key encryption-test-secret-key
+ (mt/with-actions-enabled
+ (mt/with-temporary-setting-values [enable-public-sharing true]
+ (mt/with-actions [{action-id :action-id} {}]
+ ;; with-actions creates the action already shared; unshare it so the lifecycle starts from a clean slate
+ (t2/update! :model/Action action-id {:public_uuid nil})
+ (assert-public-uuid-lifecycle! :model/Action action-id))))))
+
+(deftest public-uuid-resolves-via-endpoint-test
+ (testing "GET /api/public/... resolves a shared entity by its uuid through the prefix lookup"
+ (encryption-test/with-secret-key encryption-test-secret-key
+ (mt/with-temporary-setting-values [enable-public-sharing true]
+ (with-temp-public-card [{uuid :public_uuid, card-id :id}]
+ (is (= card-id (:id (mt/client :get 200 (str "public/card/" uuid))))))
+ (with-temp-public-dashboard [{uuid :public_uuid, dashboard-id :id}]
+ (is (= dashboard-id (:id (mt/client :get 200 (str "public/dashboard/" uuid))))))))))
+
+(defn- assert-forged-plaintext-does-not-resolve!
+ [model id]
+ (let [uuid (str (random-uuid))]
+ (t2/update! model id {:public_uuid uuid})
+ (is (= id (public-sharing/public-uuid->id model uuid)) "a genuine encrypted public_uuid resolves")
+ (let [forged (str (random-uuid))]
+ (set-raw-public-uuid! model id forged)
+ (is (thrown? Exception (public-sharing/public-uuid->id model forged))
+ "a plaintext public_uuid forged via raw SQL fails the strict read instead of resolving")
+ (set-raw-public-uuid! model id nil))))
+
+(deftest forged-plaintext-public-uuid-does-not-resolve-test
+ (encryption-test/with-secret-key encryption-test-secret-key
+ (mt/with-temporary-setting-values [enable-public-sharing true]
+ (mt/with-temp [:model/Card {card-id :id} {}]
+ (assert-forged-plaintext-does-not-resolve! :model/Card card-id))
+ (mt/with-temp [:model/Dashboard {dash-id :id} {}]
+ (assert-forged-plaintext-does-not-resolve! :model/Dashboard dash-id)))))
+
(defn- venues-query
[]
{:database (mt/id)