Skip to content

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

Improve encrypted app-db column handling (#80785)

Alexander Polyankin55d7df2c9c35
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index dfb5cec7521..0e6469a30c3 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -355,10 +355,7 @@
connection-pool
task
util}
- :model-imports #{:model/Field
- :model/QueryCache
- :model/Secret
- :model/Setting}}
+ :model-imports #{:model/Field :model/QueryCache :model/Setting}}
appearance
{:team "UX West"
@@ -2140,8 +2137,7 @@
secrets
{:team "Graphy"
:api #{metabase.secrets.core}
- :uses #{api driver models premium-features util}
- :model-exports #{:model/Secret}}
+ :uses #{api driver models premium-features util}}
segments
{:team "Graphy"
diff --git a/enterprise/backend/test/metabase_enterprise/mfa/verification_test.clj b/enterprise/backend/test/metabase_enterprise/mfa/verification_test.clj
index a9e4964d65d..3f8e4ed2271 100644
--- a/enterprise/backend/test/metabase_enterprise/mfa/verification_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/mfa/verification_test.clj
@@ -163,7 +163,7 @@
(deftest key-rollover-test
(testing "an enrollment written under key A verifies after the column is rotated to key B"
- ;; The full `rotate-encryption-key` walk over `encrypted-json-columns` (which includes
+ ;; The full `rotate-encryption-key` walk over `encrypted-columns` (which includes
;; [:auth_identity :credentials]) is exercised by metabase.cmd.rotate-encryption-key-test;
;; this covers the MFA-specific end of it: rotate this row the way the command does —
;; decrypt raw value with the old key, re-encrypt with the new — then verify a login code
@@ -180,7 +180,7 @@
(let [ai-id (t2/select-one-fn :id :auth_identity :user_id user-id :provider "totp")
raw (t2/select-one-fn :credentials :auth_identity :id ai-id)
plaintext (encryption-test/with-secret-key k1
- (encryption/maybe-decrypt raw))
+ (encryption/maybe-decrypt-accepting-plaintext raw))
rotated (encryption-test/with-secret-key k2
(encryption/maybe-encrypt plaintext))]
(is (encryption/possibly-encrypted-string? raw) "sanity: stored under key A as ciphertext")
diff --git a/resources/migrations/058_update_migrations.yaml b/resources/migrations/058_update_migrations.yaml
index 449bc0959c7..e1ece3e5c62 100644
--- a/resources/migrations/058_update_migrations.yaml
+++ b/resources/migrations/058_update_migrations.yaml
@@ -1321,6 +1321,29 @@ databaseChangeLog:
tableName: data_permissions
constraintName: idx_unique_data_permissions
+ - changeSet:
+ id: v58.2026-08-25T00:00:00
+ author: ranquild
+ comment: >-
+ Encrypt any plaintext auth_identity.credentials rows written by the v58 password/SSO backfill (raw SQL that
+ bypassed encryption), so the credentials column can be read strictly once MB_ENCRYPTION_SECRET_KEY is set.
+ changes:
+ - customChange:
+ class: metabase.app_db.custom_migrations.EncryptAuthIdentityCredentials
+
+ - changeSet:
+ id: v58.2026-08-25T00:00:01
+ author: ranquild
+ comment: >-
+ Encrypt any plaintext api_key.key bcrypt hashes at rest, so the column can be read strictly once
+ MB_ENCRYPTION_SECRET_KEY is set. Otherwise an attacker with SQL write access could inject the bcrypt hash of a
+ key they know (bcrypt is keyless) and authenticate as any user; encrypting the hash means such a plaintext value
+ is rejected on the strict read. Already-encrypted values are left untouched. On rollback the values are
+ decrypted back to the plaintext hash, so a downgrade to code that reads the column directly can still verify keys.
+ changes:
+ - customChange:
+ class: metabase.app_db.custom_migrations.EncryptApiKeys
+
# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<
########################################################################################################################
diff --git a/src/metabase/api_keys/models/api_key.clj b/src/metabase/api_keys/models/api_key.clj
index 5e48b254493..cecf02dd8ee 100644
--- a/src/metabase/api_keys/models/api_key.clj
+++ b/src/metabase/api_keys/models/api_key.clj
@@ -57,7 +57,8 @@
(derive :hook/timestamped?))
(t2/deftransforms :model/ApiKey
- {:scope mi/transform-keyword})
+ {:scope mi/transform-keyword
+ :key mi/transform-encrypted-text})
(mu/defn- expose :- :string
^String [s :- [:or ::u.secret/secret :string]]
diff --git a/src/metabase/app_db/custom_migrations.clj b/src/metabase/app_db/custom_migrations.clj
index edab549a126..b2257930a5c 100644
--- a/src/metabase/app_db/custom_migrations.clj
+++ b/src/metabase/app_db/custom_migrations.clj
@@ -135,17 +135,17 @@
(comp encryption/maybe-encrypt json-in))
(defn- encrypted-json-out
- "Should mirror [[metabase.models.interface/encrypted-json-out]]"
+ "Lenient deserialize of an encrypted-json column that tolerates plaintext at rest, for reading legacy rows during
+ migrations. Mirrors [[metabase.models.interface/encrypted-json-in]]'s inverse from before that read became strict."
[v]
- (let [decrypted (encryption/maybe-decrypt v)]
- (try
- (json/decode+kw decrypted)
- (catch Throwable e
- (if (or (encryption/possibly-encrypted-string? decrypted)
- (encryption/possibly-encrypted-bytes? decrypted))
- (log/errorf "Could not decrypt encrypted field! Have you forgot to set MB_ENCRYPTION_SECRET_KEY?: %s" (ex-message e))
- (log/errorf "Error parsing JSON: %s" (ex-message e))) ; same message as in `json-out`
- v))))
+ (try
+ (json/decode+kw (encryption/maybe-decrypt-accepting-plaintext v))
+ (catch Throwable e
+ (if (or (encryption/possibly-encrypted-string? v)
+ (encryption/possibly-encrypted-bytes? v))
+ (log/errorf "Could not decrypt encrypted field! Have you forgot to set MB_ENCRYPTION_SECRET_KEY?: %s" (ex-message e))
+ (log/errorf "Error parsing JSON: %s" (ex-message e))) ; same message as in `json-out`
+ v)))
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | MIGRATIONS |
@@ -1406,7 +1406,7 @@
(defn- raw-setting-value [key]
(some-> (t2/query-one {:select [:value], :from :setting, :where [:= :key key]})
:value
- encryption/maybe-decrypt))
+ encryption/maybe-decrypt-accepting-plaintext))
(define-reversible-migration MigrateUploadsSettings
(do (when (some-> (raw-setting-value "uploads-enabled") parse-boolean)
@@ -2242,3 +2242,37 @@
(define-reversible-migration MigrateLlmProviderSettings
(llm-providers/migrate-up!)
(llm-providers/migrate-down!))
+
+(define-migration EncryptAuthIdentityCredentials
+ (when (encryption/default-encryption-enabled?)
+ (run! (fn [{:keys [id credentials]}]
+ (when (and (string? credentials)
+ (not (str/blank? credentials))
+ (not (encryption/possibly-encrypted-string? credentials)))
+ (t2/query {:update :auth_identity
+ :set {:credentials (encryption/maybe-encrypt credentials)}
+ :where [:= :id id]})))
+ (t2/reducible-query {:select [:id :credentials]
+ :from [:auth_identity]}))))
+
+(define-reversible-migration EncryptApiKeys
+ (when (encryption/default-encryption-enabled?)
+ (run! (fn [{:keys [id] k :key}]
+ (when (and (string? k)
+ (not (str/blank? k))
+ (not (encryption/possibly-encrypted-string? k)))
+ (t2/query {:update :api_key
+ :set {:key (encryption/maybe-encrypt k)}
+ :where [:= :id id]})))
+ (t2/reducible-query {:select [:id :key]
+ :from [:api_key]})))
+ (when (encryption/default-encryption-enabled?)
+ (run! (fn [{:keys [id] k :key}]
+ (when (and (string? k)
+ (not (str/blank? k))
+ (encryption/possibly-encrypted-string? k))
+ (t2/query {:update :api_key
+ :set {:key (encryption/maybe-decrypt k)}
+ :where [:= :id id]})))
+ (t2/reducible-query {:select [:id :key]
+ :from [:api_key]}))))
diff --git a/src/metabase/app_db/custom_migrations/llm_providers.clj b/src/metabase/app_db/custom_migrations/llm_providers.clj
index e3101d19734..4ea6b3457b1 100644
--- a/src/metabase/app_db/custom_migrations/llm_providers.clj
+++ b/src/metabase/app_db/custom_migrations/llm_providers.clj
@@ -94,7 +94,7 @@
[setting-key]
(some-> (t2/query-one {:select [:value] :from :setting :where [:= :key setting-key]})
:value
- encryption/maybe-decrypt))
+ encryption/maybe-decrypt-accepting-plaintext))
(defn- non-blank
[value]
diff --git a/src/metabase/app_db/encryption.clj b/src/metabase/app_db/encryption.clj
index 92ed4719705..73c8e7f4e31 100644
--- a/src/metabase/app_db/encryption.clj
+++ b/src/metabase/app_db/encryption.clj
@@ -5,22 +5,45 @@
[metabase.util.i18n :refer [trs]]
[metabase.util.log :as log]
[metabase.util.string :as string]
- [toucan2.core :as t2]))
+ [toucan2.core :as t2])
+ (:import
+ (java.sql Blob)))
(set! *warn-on-reflection* true)
-;; All columns encrypted via `mi/transform-encrypted-json`. The on-disk format of such a column is
-;; `encrypt(json-string)`, so rotating the key only requires decrypting the raw value with the current key and
-;; re-encrypting the resulting string. We list raw table names (not models) so this also works for enterprise models
-;; that aren't loaded in every edition.
-(def ^:private encrypted-json-columns
+(defn- blob->bytes [^Blob b]
+ (.getBytes ^Blob b 0 (.length ^Blob b)))
+
+(defn- maybe-blob->bytes
+ "Normalize a raw `secret.value` read to a byte array: some drivers return a JDBC `Blob`, others a byte array."
+ [v]
+ (cond-> v
+ (instance? Blob v) blob->bytes))
+
+;; All columns whose whole value is encrypted at rest (via `mi/transform-encrypted-json`, or the encrypted-text/EDN
+;; transforms in explorations). The on-disk format is `encrypt(string)`, so rotating the key only requires decrypting
+;; the raw value with the current key and re-encrypting the resulting string. We list raw table names (not models) so
+;; this also works for enterprise models that aren't loaded in every edition.
+(def ^:private encrypted-string-columns
[[:metabase_database :details]
[:metabase_database :settings]
[:metabase_database :write_data_details]
[:metabase_database :admin_details]
[:core_user :settings]
[:channel :details]
- [:auth_identity :credentials]])
+ [:api_key :key]
+ [:auth_identity :credentials]
+ [:exploration_query_result :chart_stats]
+ [:exploration_query_result :metric_description]
+ [:exploration_query_result :chart_description]])
+
+(def ^:private encrypted-bytes-columns
+ "`^bytes` columns encrypted at rest via `mi/transform-secret-value` (a strict `maybe-decrypt-bytes` on read). Unlike
+ the string/JSON columns above these hold raw bytes, so encryption and key rotation must decrypt-and-re-encrypt them as
+ bytes. Any such column omitted here would keep whatever plaintext it held before a key was set, and the strict read
+ would then reject it."
+ [[:secret :value]
+ [:stored_result :result_data]])
;; Older versions of dump-to-h2 and key rotation only processed `metabase_database.details` (plus settings and
;; secrets), skipping every other encrypted JSON column. A dump or rotation from such a version left the skipped
@@ -41,12 +64,20 @@
[]
(let [raw (t2/select-one-fn :value :setting :key "encryption-check")]
(cond
- (or (nil? raw) (= raw "unencrypted")) :unknown
- (string/valid-uuid? (encryption/maybe-decrypt raw)) :valid
- :else :invalid)))
+ (or (nil? raw) (= raw "unencrypted"))
+ :unknown
-(defn- reencrypt-encrypted-json-column!
- "Re-encrypt `column` for every row in `table` using `encrypt-str-fn`. See `encrypted-json-columns`.
+ (and (encryption/possibly-encrypted-string? raw)
+ (try (string/valid-uuid? (encryption/maybe-decrypt-accepting-plaintext raw))
+ (catch Throwable _ false)))
+ :valid
+
+ :else
+ :invalid)))
+
+(defn- reencrypt-encrypted-column!
+ "Re-encrypt `column` for every row in `table` using `encrypt-str-fn`. See `encrypted-string-columns`. Streams the
+ rows so a large column does not have to be held in memory all at once.
When `clear-undecryptable?` is true, a value that cannot be decrypted with the current key is reset to an empty
JSON object (with a warning) instead of aborting. Only pass true when the current key is known to be correct for
@@ -54,18 +85,36 @@
key (see `clearable-when-undecryptable`): such values are equally unreadable at runtime, so clearing them loses
nothing that was usable."
[conn table column encrypt-str-fn clear-undecryptable?]
- (doseq [{:keys [id value]} (t2/select [table :id [column :value]])]
- (when (some? value)
- (let [decrypted (encryption/maybe-decrypt value)]
- (if (encryption/possibly-encrypted-string? decrypted)
- (if clear-undecryptable?
- (do
- (log/warnf "Can't decrypt %s.%s for id %s with MB_ENCRYPTION_SECRET_KEY even though the key is correct for this database; resetting the value to {}. It was likely written with a different key and has been unreadable at runtime."
- (name table) (name column) id)
- (t2/update! :conn conn table {:id id} {column (encrypt-str-fn "{}")}))
- (throw (ex-info (trs "Can''t decrypt app db with MB_ENCRYPTION_SECRET_KEY")
- {:table table, :id id, :column column})))
- (t2/update! :conn conn table {:id id} {column (encrypt-str-fn decrypted)}))))))
+ (run! (fn [{:keys [id value]}]
+ (when (some? value)
+ (let [decrypted (try
+ (encryption/maybe-decrypt-accepting-plaintext value)
+ (catch Throwable e
+ (if clear-undecryptable?
+ (do
+ (log/warnf "Can't decrypt %s.%s for id %s with MB_ENCRYPTION_SECRET_KEY even though the key is correct for this database; resetting the value to {}. It was likely written with a different key and has been unreadable at runtime."
+ (name table) (name column) id)
+ "{}")
+ (throw (ex-info (trs "Can''t decrypt app db with MB_ENCRYPTION_SECRET_KEY")
+ {:table table, :id id, :column column} e)))))]
+ (t2/update! :conn conn table {:id id} {column (encrypt-str-fn decrypted)}))))
+ (t2/reducible-select [table :id [column :value]])))
+
+(defn- reencrypt-encrypted-bytes-column!
+ "Re-encrypt a `^bytes` `column` for every row in `table` using `encrypt-bytes-fn`. See `encrypted-bytes-columns`.
+ Streams the rows so a large column (e.g. `stored_result.result_data`) does not have to be held in memory all at
+ once. A value that cannot be decrypted with the current key aborts rather than being re-encrypted: re-encrypting it
+ would produce `encrypt_new(encrypt_old(x))`, permanently unrecoverable."
+ [conn table column encrypt-bytes-fn]
+ (run! (fn [{:keys [id value]}]
+ (when (some? value)
+ (let [decrypted (try
+ (encryption/maybe-decrypt-bytes-accepting-plaintext (maybe-blob->bytes value))
+ (catch Throwable e
+ (throw (ex-info (trs "Can''t decrypt app db with MB_ENCRYPTION_SECRET_KEY")
+ {:table table, :id id, :column column} e))))]
+ (t2/update! :conn conn table {:id id} {column (encrypt-bytes-fn decrypted)}))))
+ (t2/reducible-select [table :id [column :value]])))
(defn- do-encryption
"Encrypt or decrypts the db using the current `MB_ENCRYPTION_SECRET_KEY` to read data.
@@ -79,10 +128,10 @@
(when (= check-status :invalid)
(throw (ex-info (trs "Database was encrypted with a different key than the MB_ENCRYPTION_SECRET_KEY environment contains")
{})))
- (doseq [[table column] encrypted-json-columns]
- (reencrypt-encrypted-json-column! conn table column encrypt-str-fn
- (and (= check-status :valid)
- (contains? clearable-when-undecryptable [table column])))))
+ (doseq [[table column] encrypted-string-columns]
+ (reencrypt-encrypted-column! conn table column encrypt-str-fn
+ (and (= check-status :valid)
+ (contains? clearable-when-undecryptable [table column])))))
(doseq [[key value] (t2/select-fn->fn :key :value :model/Setting)]
(case key
"settings-last-updated" (let [current-timestamp-as-string-honeysql (h2x/cast (if (= db-type :mysql) :char :text)
@@ -92,16 +141,8 @@
(t2/update! :conn conn :setting
{:key key}
{:value (encrypt-str-fn value)})))
- ;; update all secret values according to the new encryption key
- ;; fortunately, we don't need to fetch the latest secret instance per ID, as we would need to in order to update
- ;; a secret value through the regular database save API path; instead, ALL secret values in the app DB (regardless
- ;; of whether they are the "current version" or not), should be updated with the new key
- (doseq [[id value] (t2/select-pk->fn :value :model/Secret)]
- (when (encryption/possibly-encrypted-string? value)
- (throw (ex-info (trs "Can''t decrypt secret value with MB_ENCRYPTION_SECRET_KEY") {:secret-id id})))
- (t2/update! :conn conn :secret
- {:id id}
- {:value (encrypt-bytes-fn value)}))
+ (doseq [[table column] encrypted-bytes-columns]
+ (reencrypt-encrypted-bytes-column! conn table column encrypt-bytes-fn))
(t2/delete! :conn conn :model/QueryCache))))
(defn encrypt-db
diff --git a/src/metabase/app_db/setup.clj b/src/metabase/app_db/setup.clj
index 25f80d4146c..e5ba6591641 100644
--- a/src/metabase/app_db/setup.clj
+++ b/src/metabase/app_db/setup.clj
@@ -206,7 +206,9 @@
(do
(when-not (encryption/default-encryption-enabled?)
(throw (ex-info "Database is encrypted but the MB_ENCRYPTION_SECRET_KEY environment variable was NOT set" {})))
- (when-not (string/valid-uuid? (encryption/maybe-decrypt raw))
+ (when-not (and (encryption/possibly-encrypted-string? raw)
+ (u/ignore-exceptions
+ (string/valid-uuid? (encryption/maybe-decrypt-accepting-plaintext raw))))
(throw (ex-info "Database was encrypted with a different key than the MB_ENCRYPTION_SECRET_KEY environment contains" {})))
(log/debug "Database encrypted and MB_ENCRYPTION_SECRET_KEY correctly configured"))
(if (encryption/default-encryption-enabled?)
diff --git a/src/metabase/auth_identity/models/auth_identity.clj b/src/metabase/auth_identity/models/auth_identity.clj
index 5acb2c55330..91360b4b135 100644
--- a/src/metabase/auth_identity/models/auth_identity.clj
+++ b/src/metabase/auth_identity/models/auth_identity.clj
@@ -38,8 +38,7 @@
;; `credentials` is encrypted at rest (whole column) so secrets stored in it — e.g. the TOTP
;; shared secret — survive `rotate-encryption-key`, which re-encrypts whole columns and cannot
;; reach fields nested inside JSON. The column is listed in
-;; `metabase.app-db.encryption/encrypted-json-columns`. Rows written before encryption was
-;; introduced are plaintext JSON; `maybe-decrypt` passes them through unchanged.
+;; `metabase.app-db.encryption/encrypted-string-columns`.
(t2/deftransforms :model/AuthIdentity
{:credentials {:in mi/encrypted-json-in
:out (comp parse-credentials-timestamps-out mi/encrypted-json-out)}
diff --git a/src/metabase/explorations/models/exploration_query_result.clj b/src/metabase/explorations/models/exploration_query_result.clj
index 5a27e9409e1..50b6329d426 100644
--- a/src/metabase/explorations/models/exploration_query_result.clj
+++ b/src/metabase/explorations/models/exploration_query_result.clj
@@ -1,6 +1,7 @@
(ns metabase.explorations.models.exploration-query-result
(:require
[clojure.edn :as edn]
+ [metabase.models.interface :as mi]
[metabase.util.encryption :as encryption]
[metabase.util.log :as log]
[methodical.core :as methodical]
@@ -37,13 +38,8 @@
(log/warn e "Failed to parse an exploration_query_result EDN column; returning nil")
nil))))
-(def ^:private transform-encrypted-text
- {:in encryption/maybe-encrypt
- :out encryption/maybe-decrypt})
-
(def ^:private transform-encrypted-edn
- "[[transform-encrypted-text]] over a value serialized as EDN. `maybe-decrypt` passes plaintext
- through unchanged, so rows written before a column was encrypted keep reading with no migration."
+ "[[metabase.models.interface/transform-encrypted-text]] over a value serialized as EDN."
{:in (comp encryption/maybe-encrypt edn-in)
:out (comp edn-out encryption/maybe-decrypt)})
@@ -54,8 +50,8 @@
;; result rows (see [[metabase.interestingness.chart.categorical]]).
(t2/deftransforms :model/ExplorationQueryResult
{:chart_stats transform-encrypted-edn
- :metric_description transform-encrypted-text
- :chart_description transform-encrypted-text})
+ :metric_description mi/transform-encrypted-text
+ :chart_description mi/transform-encrypted-text})
(defn stored-results
"Resolve the cached stored_result for an exploration_query_id via the EQR FK. Returns the
diff --git a/src/metabase/models/interface.clj b/src/metabase/models/interface.clj
index 8522a035e59..21809cb4e5d 100644
--- a/src/metabase/models/interface.clj
+++ b/src/metabase/models/interface.clj
@@ -306,27 +306,35 @@
(comp encryption/maybe-encrypt json-in))
(defn encrypted-json-out
- "Deserialize encrypted json."
+ "Deserialize encrypted json, requiring the value to be encrypted when `MB_ENCRYPTION_SECRET_KEY` is set (see
+ [[encryption/maybe-decrypt]]): a plaintext value at rest is rejected. A value that decrypts (or, with no key set,
+ passes through) but is not valid JSON is logged and returned as-is rather than crashing the read."
[v]
(let [decrypted (encryption/maybe-decrypt v)]
(try
- (json/decode+kw decrypted)
+ (some-> decrypted json/decode+kw)
(catch Throwable e
(if (or (encryption/possibly-encrypted-string? decrypted)
(encryption/possibly-encrypted-bytes? decrypted))
(log/error "Could not decrypt encrypted field! Have you forgot to set MB_ENCRYPTION_SECRET_KEY?")
- (log/errorf "Error parsing JSON: %s" (ex-message e))) ; same message as in `json-out`
+ (log/errorf "Error parsing JSON: %s" (ex-message e)))
v))))
-;; cache the decryption/JSON parsing because it's somewhat slow (~500µs vs ~100µs on a *fast* computer)
-;; cache the decrypted JSON for one hour
-(def ^:private cached-encrypted-json-out (memoize/ttl encrypted-json-out :ttl/threshold (* 60 60 1000)))
+(def ^:private cached-encrypted-json-out
+ (memoize/ttl encrypted-json-out :ttl/threshold (* 60 60 1000)))
(def transform-encrypted-json
- "Transform for encrypted json."
+ "Encrypted-json transform. When `MB_ENCRYPTION_SECRET_KEY` is set, a plaintext value at rest is rejected on read."
{:in encrypted-json-in
:out cached-encrypted-json-out})
+(def transform-encrypted-text
+ "Whole-column encrypted text transform. When `MB_ENCRYPTION_SECRET_KEY` is set, a plaintext value at rest is rejected
+ on read (see [[encryption/maybe-decrypt]]) — a value written outside the encrypting path cannot stand in for a
+ properly encrypted one."
+ {:in encryption/maybe-encrypt
+ :out encryption/maybe-decrypt})
+
;;; TODO (Cam 10/27/25) -- this stuff should be moved into a different module instead of the general models interface,
;;; either `queries` or a new module along with [[metabase.models.visualization-settings]].
(mr/def ::viz-settings-ref
@@ -479,9 +487,9 @@
v))
(def transform-secret-value
- "Transform for secret value."
+ "Transform for secret value. When `MB_ENCRYPTION_SECRET_KEY` is set, a plaintext value at rest is rejected on read."
{:in (comp encryption/maybe-encrypt-bytes codecs/to-bytes)
- :out (comp encryption/maybe-decrypt maybe-blob->bytes)})
+ :out (comp encryption/maybe-decrypt-bytes maybe-blob->bytes)})
#_(defn decompress
"Decompress `compressed-bytes`."
diff --git a/src/metabase/server/middleware/session.clj b/src/metabase/server/middleware/session.clj
index 41a4cce0b7f..cf70c296c2b 100644
--- a/src/metabase/server/middleware/session.clj
+++ b/src/metabase/server/middleware/session.clj
@@ -34,6 +34,8 @@
[metabase.session.core :as session]
[metabase.settings.core :as setting]
[metabase.tracing.core :as tracing]
+ [metabase.util :as u]
+ [metabase.util.encryption :as encryption]
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.i18n :as i18n]
[metabase.util.log :as log]
@@ -234,11 +236,16 @@
[]
(u.password/verify-password api-key-that-should-never-match "" hash-that-should-never-match))
-(defn- matching-api-key? [{:keys [api-key] :as _user-data} passed-api-key]
- ;; if we get an API key, check the hash against the passed value. If not, don't reveal info via a timing attack - do
- ;; a useless hash, *then* return `false`.
- (if api-key
- (u.password/verify-password passed-api-key "" api-key)
+(defn- matching-api-key?
+ "Whether `passed-api-key` matches the hash stored in `user-data`. The stored bcrypt hash is encrypted at rest and this
+ path reads the raw column (bypassing the model's decrypting transform), so it is decrypted before the bcrypt compare;
+ a value that is not valid ciphertext — e.g. a plaintext hash injected via direct SQL — decrypts to nil and is
+ rejected rather than trusted. With no usable hash we still compute a useless hash so the two cases can't be told apart
+ by timing."
+ [{:keys [api-key] :as _user-data} passed-api-key]
+ (if-let [stored-hash (when api-key
+ (u/ignore-exceptions (encryption/maybe-decrypt api-key)))]
+ (u.password/verify-password passed-api-key "" stored-hash)
(do-useless-hash)))
(mu/defn- current-user-info-for-api-key :- [:maybe ::request.schema/current-user-info]
diff --git a/src/metabase/settings/models/setting.clj b/src/metabase/settings/models/setting.clj
index 0f58aa8f776..043e4fd902d 100644
--- a/src/metabase/settings/models/setting.clj
+++ b/src/metabase/settings/models/setting.clj
@@ -1739,7 +1739,7 @@
[:in :key (map setting-name settings)]
;; these are *definitely* decrypted already, let's not bother looking
[:not [:in :value ["true" "false"]]]]})
- :let [decrypted-v (encryption/maybe-decrypt v)]
+ :let [decrypted-v (encryption/maybe-decrypt-accepting-plaintext v)]
:when (not= decrypted-v v)]
(t2/update! :setting :key k {:value decrypted-v}))))))
@@ -1766,9 +1766,19 @@
[setting]
(maybe-encrypt setting))
+(defn- decrypt-setting-value-on-read
+ "Decrypt a Setting `value` on read, tolerating an undecryptable value by returning it unchanged so a stale row (e.g.
+ left by a botched key change) degrades gracefully at the reader instead of throwing on every read. The strict throw
+ from [[encryption/maybe-decrypt-accepting-plaintext]] still applies where it matters — the key-rotation path, which
+ does not swallow it."
+ [value]
+ (try
+ (encryption/maybe-decrypt-accepting-plaintext value)
+ (catch Throwable _ value)))
+
(t2/define-after-select :model/Setting
[setting]
;; Don't do any automatic handling of the "encryption-check" special setting used by mdb.encryption
(if (= "encryption-check" (:key setting))
setting
- (update setting :value encryption/maybe-decrypt)))
+ (update setting :value decrypt-setting-value-on-read)))
diff --git a/src/metabase/util/encryption.clj b/src/metabase/util/encryption.clj
index b6fd065a646..0b411cfd8ff 100644
--- a/src/metabase/util/encryption.clj
+++ b/src/metabase/util/encryption.clj
@@ -226,38 +226,63 @@
(codec/base64-decode s))]
(possibly-encrypted-bytes? b))))
-(defn maybe-decrypt
- "If `MB_ENCRYPTION_SECRET_KEY` is set and `v` is encrypted, decrypt `v`; otherwise return `s` as-is. Attempts to check
- whether `v` is an encrypted String, in which case the decrypted String is returned, or whether `v` is encrypted bytes,
- in which case the decrypted bytes are returned."
- {:arglists '([secret-key? s])}
- [& args]
- ;; secret-key as an argument so that tests can pass it directly without using `with-redefs` to run in parallel
- (let [[secret-key v] (if (and (bytes? (first args)) (string? (second args)))
- args
- (cons default-secret-key args))
- log-error-fn (fn [kind ^Throwable e]
- (log/warnf "Cannot decrypt encrypted %s. Have you changed or forgot to set MB_ENCRYPTION_SECRET_KEY? %s"
- kind
- (ex-message e)))]
- (cond (nil? secret-key)
- v
+(defn maybe-decrypt-accepting-plaintext
+ "Plaintext-tolerant decrypt of a String `s`. If `MB_ENCRYPTION_SECRET_KEY` is set and `s` is encrypted, decrypt it;
+ a value that is stored as plaintext is returned as-is. A value that *looks* encrypted but cannot be decrypted with
+ the current key (wrong key, tampering, corruption) throws — it must not be silently returned, or a re-encrypting
+ caller would double-encrypt it into something permanently unrecoverable. This differs from the strict
+ [[maybe-decrypt]] only in tolerating genuine plaintext; use it for values that may legitimately be plaintext at rest
+ (rows written before encryption was enabled, key rotation, settings, and migration reads).
+
+ `secret-key` is accepted as an argument so tests can pass it directly instead of using `with-redefs` to run in
+ parallel."
+ (^String [^String s] (maybe-decrypt-accepting-plaintext default-secret-key s))
+ (^String [secret-key ^String s]
+ (cond
+ (nil? secret-key) s
+ (possibly-encrypted-string? s) (decrypt secret-key s)
+ :else s)))
- (possibly-encrypted-string? v)
- (try
- (decrypt secret-key v)
- (catch Throwable e
- ;; if we can't decrypt `v`, but it *is* probably encrypted, log a warning
- (log-error-fn "String" e)
- v))
+(defn maybe-decrypt-bytes-accepting-plaintext
+ "Plaintext-tolerant counterpart to [[maybe-decrypt-accepting-plaintext]] for a byte array `b` (e.g. secret values):
+ an encrypted value is decrypted, a plaintext value is returned as-is, and a value that looks encrypted but cannot be
+ decrypted with the current key throws rather than being returned (returning it would let a re-encrypting caller
+ double-encrypt it into something permanently unrecoverable)."
+ (^bytes [^bytes b] (maybe-decrypt-bytes-accepting-plaintext default-secret-key b))
+ (^bytes [secret-key ^bytes b]
+ (cond
+ (nil? secret-key) b
+ (possibly-encrypted-bytes? b) (decrypt-bytes secret-key b)
+ :else b)))
- (possibly-encrypted-bytes? v)
- (try
- (decrypt-bytes secret-key v)
- (catch Throwable e
- ;; if we can't decrypt `v`, but it *is* probably encrypted, log a warning
- (log-error-fn "bytes" e)
- v))
+(defn maybe-decrypt
+ "Strict decrypt of a String `s`. When `MB_ENCRYPTION_SECRET_KEY` is set, `s` must be an encrypted value that decrypts
+ with the current key: a value that is not encrypted throws (it was written outside the encrypting path — a plaintext
+ value cannot stand in for an encrypted one), and a value that is encrypted but cannot be decrypted with the current
+ key (wrong key, tampering, or corruption) also throws rather than being trusted. When no key is set, `s` is returned
+ as-is (there is no key to decrypt with). For values that may legitimately be plaintext at rest, use
+ [[maybe-decrypt-accepting-plaintext]].
- :else
- v)))
+ `secret-key` is accepted as an argument so tests can pass it directly instead of using `with-redefs` to run in
+ parallel."
+ (^String [^String s] (maybe-decrypt default-secret-key s))
+ (^String [secret-key ^String s]
+ (cond
+ (nil? secret-key) s
+ (nil? s) s
+ (possibly-encrypted-string? s) (decrypt secret-key s)
+ :else (throw (ex-info "Expected an encrypted value but the stored value is not encrypted."
+ {:type ::not-encrypted})))))
+
+(defn maybe-decrypt-bytes
+ "Strict counterpart to [[maybe-decrypt-bytes-accepting-plaintext]] for a byte array `b`: a value that is not encrypted,
+ or that cannot be decrypted with the current key, throws rather than being trusted. When no key is set, `b` is returned
+ as-is."
+ (^bytes [^bytes b] (maybe-decrypt-bytes default-secret-key b))
+ (^bytes [secret-key ^bytes b]
+ (cond
+ (nil? secret-key) b
+ (nil? b) b
+ (possibly-encrypted-bytes? b) (decrypt-bytes secret-key b)
+ :else (throw (ex-info "Expected an encrypted value but the stored value is not encrypted."
+ {:type ::not-encrypted})))))
diff --git a/test/metabase/app_db/custom_migrations/llm_providers_test.clj b/test/metabase/app_db/custom_migrations/llm_providers_test.clj
index f5f551e9a93..888b09370f6 100644
--- a/test/metabase/app_db/custom_migrations/llm_providers_test.clj
+++ b/test/metabase/app_db/custom_migrations/llm_providers_test.clj
@@ -13,7 +13,7 @@
(defn- llm-setting-values
[setting-keys]
(into {}
- (map (fn [{:keys [key value]}] [key (encryption/maybe-decrypt value)]))
+ (map (fn [{:keys [key value]}] [key (encryption/maybe-decrypt-accepting-plaintext value)]))
(t2/query {:select [:key :value] :from :setting :where [:in :key setting-keys]})))
(defn- insert-llm-settings!
@@ -184,7 +184,7 @@
"llm-metabot-provider"]]}))]
(testing "the API key is sensitive, so it comes back as ciphertext"
(is (not= "sk-ant-stored" (get raw "llm-anthropic-api-key")))
- (is (= "sk-ant-stored" (encryption/maybe-decrypt (get raw "llm-anthropic-api-key")))))
+ (is (= "sk-ant-stored" (encryption/maybe-decrypt-accepting-plaintext (get raw "llm-anthropic-api-key")))))
(testing "settings declared :encryption :no come back as the plaintext they were declared to hold"
(is (= "https://self-hosted.example" (get raw "llm-anthropic-api-base-url")))
(is (= "anthropic/claude-opus-4-1" (get raw "llm-metabot-provider")))))))))
diff --git a/test/metabase/app_db/custom_migrations_test.clj b/test/metabase/app_db/custom_migrations_test.clj
index e5701f6eb68..a9b7e817a15 100644
--- a/test/metabase/app_db/custom_migrations_test.clj
+++ b/test/metabase/app_db/custom_migrations_test.clj
@@ -1977,8 +1977,8 @@
(is (not-empty settings-after))
(is (every? encryption/possibly-encrypted-string?
(map :value settings-after)))
- (is (= (set (map #(update % :value encryption/maybe-decrypt) settings-before))
- (set (map #(update % :value encryption/maybe-decrypt) settings-after))))))))))))
+ (is (= (set (map #(update % :value encryption/maybe-decrypt-accepting-plaintext) settings-before))
+ (set (map #(update % :value encryption/maybe-decrypt-accepting-plaintext) settings-after))))))))))))
(deftest ^:mb/old-migrations-test migrate-uploads-settings-test-2
(testing "MigrateUploadsSettings with invalid settings state (missing uploads-database-id) doesn't fail."
@@ -2701,6 +2701,60 @@
(is (= {"_@foo" "bang"}
(json/decode (t2/select-one-fn :login_attributes :core_user :id other-user-id))))))))
+(deftest encrypt-auth-identity-credentials-test
+ (testing "v58.2026-08-25T00:00:00 : plaintext auth_identity.credentials rows are encrypted, encrypted rows untouched"
+ (encryption-test/with-secret-key "encrypt-creds-test-key-1234"
+ (impl/test-migrations ["v58.2026-08-25T00:00:00"] [migrate!]
+ (let [user-id (:id (new-instance-with-default :core_user))
+ ins-cred (fn [provider creds-str]
+ (t2/insert-returning-pk! :auth_identity {:user_id user-id
+ :provider provider
+ :credentials creds-str
+ :created_at :%now
+ :updated_at :%now}))
+ plain-id (ins-cred "password" (json/encode {:password_hash "h" :password_salt "s"}))
+ enc-str (encryption/maybe-encrypt (json/encode {:password_hash "h2" :password_salt "s2"}))
+ enc-id (ins-cred "google" enc-str)
+ raw-cred (fn [id] (t2/select-one-fn :credentials :auth_identity :id id))]
+ (is (not (encryption/possibly-encrypted-string? (raw-cred plain-id))))
+ (migrate!)
+ (testing "plaintext credentials are encrypted and decrypt to the original value"
+ (is (encryption/possibly-encrypted-string? (raw-cred plain-id)))
+ (is (= {:password_hash "h" :password_salt "s"}
+ (json/decode+kw (encryption/maybe-decrypt-accepting-plaintext (raw-cred plain-id))))))
+ (testing "already-encrypted credentials row is left unchanged"
+ (is (= enc-str (raw-cred enc-id)))))))))
+
+(deftest encrypt-api-keys-test
+ (testing "v58.2026-08-25T00:00:01 : plaintext api_key.key hashes are encrypted, encrypted rows untouched"
+ (encryption-test/with-secret-key "encrypt-api-keys-test-key-1234"
+ (impl/test-migrations ["v58.2026-08-25T00:00:01"] [migrate!]
+ (let [user-id (:id (new-instance-with-default :core_user {:entity_id (u/generate-nano-id)}))
+ ins-key (fn [prefix key-str]
+ (:id (new-instance-with-default :api_key {:name (str "k-" prefix)
+ :user_id user-id
+ :creator_id user-id
+ :updated_by_id user-id
+ :key key-str
+ :key_prefix prefix})))
+ plain-id (ins-key "mb_plai" "plaintext-bcrypt-hash")
+ enc-str (encryption/maybe-encrypt "another-bcrypt-hash")
+ enc-id (ins-key "mb_encr" enc-str)
+ raw-key (fn [id] (t2/select-one-fn :key :api_key :id id))]
+ (is (not (encryption/possibly-encrypted-string? (raw-key plain-id))))
+ (migrate!)
+ (testing "plaintext key is encrypted and decrypts to the original hash"
+ (is (encryption/possibly-encrypted-string? (raw-key plain-id)))
+ (is (= "plaintext-bcrypt-hash"
+ (encryption/maybe-decrypt-accepting-plaintext (raw-key plain-id)))))
+ (testing "already-encrypted key row is left unchanged"
+ (is (= enc-str (raw-key enc-id))))
+ (testing "rollback decrypts keys back to the plaintext hash so downgraded code can still verify them"
+ (migrate! :down 57)
+ (is (not (encryption/possibly-encrypted-string? (raw-key plain-id))))
+ (is (= "plaintext-bcrypt-hash" (raw-key plain-id)))
+ (is (= "another-bcrypt-hash" (raw-key enc-id)))))))))
+
(deftest backfill-transform-target-db-id-test
(testing "v59.2026-01-31T12:01:23 : backfill target_db_id from target and source JSON"
(impl/test-migrations ["v59.2026-01-31T12:01:23"] [migrate!]
diff --git a/test/metabase/auth_identity/models/auth_identity_test.clj b/test/metabase/auth_identity/models/auth_identity_test.clj
index f0884e68662..60857fc619c 100644
--- a/test/metabase/auth_identity/models/auth_identity_test.clj
+++ b/test/metabase/auth_identity/models/auth_identity_test.clj
@@ -136,14 +136,26 @@
(get-in (t2/select-one :model/AuthIdentity :user_id user-id :provider "google")
[:credentials :secret]))))))))
-(deftest credentials-plaintext-rows-still-readable-test
- (testing "rows written before encryption (plain JSON in the column) still read as maps"
+(deftest credentials-reject-plaintext-when-key-set-test
+ (testing "with a key set, encrypted credentials read back but a plaintext value written directly via SQL is rejected"
(encryption-test/with-secret-key "key-for-auth-identity-test-2"
+ (mt/with-temp [:model/User {user-id :id}]
+ (t2/insert! :model/AuthIdentity {:user_id user-id :provider "google" :credentials {:secret "legit"}})
+ (is (= "legit"
+ (get-in (t2/select-one :model/AuthIdentity :user_id user-id :provider "google")
+ [:credentials :secret])))
+ (t2/update! :auth_identity {:user_id user-id :provider "google"}
+ {:credentials "{\"secret\":\"injected\"}"})
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"not encrypted"
+ (t2/select-one :model/AuthIdentity :user_id user-id :provider "google")))))))
+
+(deftest credentials-plaintext-allowed-without-key-test
+ (testing "with no key set there is nothing to sign with, so plaintext credentials read back as-is"
+ (encryption-test/with-secret-key nil
(mt/with-temp [:model/User {user-id :id}]
(t2/insert! :model/AuthIdentity {:user_id user-id :provider "google" :credentials {}})
- ;; simulate a legacy plaintext row by writing raw JSON straight to the table
(t2/update! :auth_identity {:user_id user-id :provider "google"}
- {:credentials "{\"secret\":\"legacy-plain\"}"})
- (is (= "legacy-plain"
+ {:credentials "{\"secret\":\"plain\"}"})
+ (is (= "plain"
(get-in (t2/select-one :model/AuthIdentity :user_id user-id :provider "google")
[:credentials :secret])))))))
diff --git a/test/metabase/cmd/dump_to_h2_test.clj b/test/metabase/cmd/dump_to_h2_test.clj
index 2988fb02985..4754adb23e0 100644
--- a/test/metabase/cmd/dump_to_h2_test.clj
+++ b/test/metabase/cmd/dump_to_h2_test.clj
@@ -5,6 +5,7 @@
[clojure.test :refer :all]
[metabase.app-db.connection :as mdb.connection]
[metabase.app-db.core :as mdb]
+ [metabase.app-db.encryption :as mdb.encryption]
[metabase.app-db.test-util :as mdb.test-util]
[metabase.cmd.copy :as copy]
[metabase.cmd.copy.h2 :as copy.h2]
@@ -74,6 +75,10 @@
(binding [copy/*copy-h2-database-details* true]
(load-from-h2/load-from-h2! h2-fixture-db-file)
(encryption-test/with-secret-key "89ulvIGoiYw6mNELuOoEZphQafnF/zYe+3vT+v70D1A="
+ ;; the fixture was loaded unencrypted; encrypt it under the key first (as startup's
+ ;; check-encryption does) so no encrypted column is left plaintext for the strict model reads
+ ;; the update and dump below trigger
+ (mdb.encryption/encrypt-db driver/*driver* (:data-source mdb.connection/*application-db*) nil)
(t2/insert! :model/Setting {:key "my-site-admin", :value "baz"})
(t2/update! :model/Database 1 {:details {:db "/tmp/test.db"}})
(dump-to-h2/dump-to-h2! h2-file-plaintext {:dump-plaintext? true})
diff --git a/test/metabase/cmd/rotate_encryption_key_test.clj b/test/metabase/cmd/rotate_encryption_key_test.clj
index 2b409329ec1..1d8ab4818c8 100644
--- a/test/metabase/cmd/rotate_encryption_key_test.clj
+++ b/test/metabase/cmd/rotate_encryption_key_test.clj
@@ -4,6 +4,7 @@
[clojure.test :refer :all]
[metabase.app-db.connection :as mdb.connection]
[metabase.app-db.core :as mdb]
+ [metabase.app-db.encryption :as mdb.encryption]
[metabase.cmd.copy :as copy]
[metabase.cmd.core :as cmd]
[metabase.cmd.dump-to-h2-test :as dump-to-h2-test]
@@ -97,6 +98,7 @@
(reset! secret-id-unenc (u/the-id secret)))
(encryption-test/with-secret-key k1
(t2/insert! :model/Setting {:key "k1crypted", :value "encrypted with k1"})
+ (mdb.encryption/encrypt-db driver/*driver* data-source nil)
(t2/update! :model/Database 1 {:details {:db "/tmp/test.db"}})
;; other encrypted-json columns that must also be re-encrypted on rotation
(t2/update! :model/Database 1 {:settings {:database-enable-actions true}})
@@ -133,12 +135,14 @@
(is (mt/secret-value-equals? secret-val (t2/select-one-fn :value :model/Secret :id @secret-id-unenc)))))
(testing "but not with old key"
(encryption-test/with-secret-key k1
+ ;; the Setting after-select is lenient: an undecryptable value comes back as-is (the raw ciphertext)
+ ;; rather than throwing, so the reader degrades gracefully instead of failing on every settings read
(is (not= "unencrypted value" (t2/select-one-fn :value :model/Setting :key "nocrypt")))
- (is (not= "{\"db\":\"/tmp/test.db\"}" (t2/select-one-fn :details :model/Database :id 1)))
- (is (not= {:database-enable-actions true} (t2/select-one-fn :settings :model/Database :id 1)))
- (is (not= {:locale "en"} (t2/select-one-fn :settings :model/User :id @user-id)))
- (is (not (mt/secret-value-equals? secret-val
- (t2/select-one-fn :value :model/Secret :id @secret-id-unenc)))))))
+ (is (thrown? clojure.lang.ExceptionInfo (t2/select-one-fn :details :model/Database :id 1)))
+ (is (thrown? clojure.lang.ExceptionInfo (t2/select-one-fn :settings :model/Database :id 1)))
+ (is (thrown? clojure.lang.ExceptionInfo (t2/select-one-fn :settings :model/User :id @user-id)))
+ (is (thrown? clojure.lang.ExceptionInfo
+ (t2/select-one-fn :value :model/Secret :id @secret-id-unenc))))))
(testing "full rollback when a database details looks encrypted with a different key than the current one"
(encryption-test/with-secret-key k3
(let [db (first (t2/insert-returning-instances! :model/Database {:name "k3", :engine :mysql, :details {:db "/tmp/k3.db"}}))]
@@ -153,7 +157,7 @@
#"Can't decrypt app db with MB_ENCRYPTION_SECRET_KEY"
(rotate-encryption-key! k3))))
(encryption-test/with-secret-key k3
- (is (not= {:db "/tmp/k2.db"} (t2/select-one-fn :details :model/Database :name "k2")))
+ (is (thrown? clojure.lang.ExceptionInfo (t2/select-one-fn :details :model/Database :name "k2")))
(is (= {:db "/tmp/k3.db"} (t2/select-one-fn :details :model/Database :name "k3")))))
(testing "rotate-encryption-key! to nil decrypts the encrypted keys"
(t2/update! :model/Database 1 {:details {:db "/tmp/test.db"}})
diff --git a/test/metabase/explorations/models/exploration_query_result_test.clj b/test/metabase/explorations/models/exploration_query_result_test.clj
index a41cd0d997c..3212f6c75c0 100644
--- a/test/metabase/explorations/models/exploration_query_result_test.clj
+++ b/test/metabase/explorations/models/exploration_query_result_test.clj
@@ -81,13 +81,24 @@
(is (= stats-with-warehouse-values
(t2/select-one-fn :chart_stats :model/ExplorationQueryResult :id id))))))))
-(deftest chart-stats-reads-pre-encryption-plaintext-test
- (testing "rows written before this column was encrypted keep reading — `maybe-decrypt` passes
- plaintext through, so no migration is needed"
- (let [id (query-result-row! nil)]
- (t2/query-one {:update :exploration_query_result
- :set {:chart_stats (pr-str stats-with-warehouse-values)}
- :where [:= :id id]})
- (encryption-test/with-secret-key "chart-stats-encryption-test-key"
+(deftest chart-stats-rejects-plaintext-when-key-set-test
+ (testing "with a key set, encrypted chart_stats reads back but a plaintext value written directly via SQL is rejected"
+ (encryption-test/with-secret-key "chart-stats-encryption-test-key"
+ (let [id (query-result-row! stats-with-warehouse-values)]
+ (is (= stats-with-warehouse-values
+ (t2/select-one-fn :chart_stats :model/ExplorationQueryResult :id id)))
+ (t2/query-one {:update :exploration_query_result
+ :set {:chart_stats (pr-str stats-with-warehouse-values)}
+ :where [:= :id id]})
+ (is (thrown? clojure.lang.ExceptionInfo
+ (t2/select-one-fn :chart_stats :model/ExplorationQueryResult :id id)))))))
+
+(deftest chart-stats-plaintext-allowed-without-key-test
+ (testing "with no key set there is nothing to decrypt with, so plaintext chart_stats reads back as-is"
+ (encryption-test/with-secret-key nil
+ (let [id (query-result-row! nil)]
+ (t2/query-one {:update :exploration_query_result
+ :set {:chart_stats (pr-str stats-with-warehouse-values)}
+ :where [:= :id id]})
(is (= stats-with-warehouse-values
(t2/select-one-fn :chart_stats :model/ExplorationQueryResult :id id)))))))
diff --git a/test/metabase/server/middleware/session_test.clj b/test/metabase/server/middleware/session_test.clj
index 43c6cabe387..bc797dd82f7 100644
--- a/test/metabase/server/middleware/session_test.clj
+++ b/test/metabase/server/middleware/session_test.clj
@@ -15,8 +15,11 @@
[metabase.session.core :as session]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
+ [metabase.util.encryption :as encryption]
+ [metabase.util.encryption-test :as encryption-test]
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.i18n :as i18n]
+ [metabase.util.password :as u.password]
[metabase.util.secret :as u.secret]
[ring.mock.request :as ring.mock]
[toucan2.core :as t2]))
@@ -134,6 +137,35 @@
:embedding/auth-method "api-key"})
(#'mw.session/merge-current-user-info req))))))))))
+(deftest api-key-hash-encrypted-at-rest-test
+ (encryption-test/with-secret-key "0B9cD6++AME+A7/oR7Y2xvPRHX3cHA2z7w+LbObd/9Y="
+ (mt/with-temp [:model/ApiKey {api-key-id :id} {:name "Encrypted API Key"
+ :user_id (mt/user->id :lucky)
+ :creator_id (mt/user->id :lucky)
+ :updated_by_id (mt/user->id :lucky)
+ ::api-key/unhashed-key (u.secret/secret "mb_encrypted123")}]
+ (testing "the stored bcrypt hash is encrypted at rest"
+ ;; select from the raw table to bypass the model's decrypting :out transform
+ (let [raw (t2/select-one-fn :key :api_key :id api-key-id)]
+ (is (encryption/possibly-encrypted-string? raw)
+ "raw column value should be ciphertext")
+ (is (not (encryption/possibly-encrypted-string? (encryption/maybe-decrypt raw)))
+ "it should decrypt to the (plaintext) bcrypt hash")))
+ (testing "a valid key still authenticates (middleware decrypts the raw hash before the bcrypt compare)"
+ (is (= (mt/user->id :lucky)
+ (:metabase-user-id (#'mw.session/merge-current-user-info {:headers {"x-api-key" "mb_encrypted123"}})))))
+ (testing "a plaintext bcrypt hash injected via direct SQL is rejected, even though it is a correct hash of the key"
+ (t2/query {:update :api_key
+ :set {:key (u.password/hash-bcrypt "mb_encrypted123")}
+ :where [:= :id api-key-id]})
+ (is (nil? (:metabase-user-id (#'mw.session/merge-current-user-info {:headers {"x-api-key" "mb_encrypted123"}})))
+ "strict decrypt rejects the unencrypted hash")
+ ;; restore a properly-encrypted hash so `with-temp` cleanup (whose before-delete reads the row) doesn't hit the
+ ;; strict decrypt on the corrupted plaintext value
+ (t2/query {:update :api_key
+ :set {:key (encryption/maybe-encrypt (u.password/hash-bcrypt "mb_encrypted123"))}
+ :where [:= :id api-key-id]})))))
+
(deftest ^:parallel current-user-info-for-api-key-test-1b
(testing "Various invalid API keys do not modify the request"
(are [req] (= req (#'mw.session/merge-current-user-info req))
diff --git a/test/metabase/util/encryption_test.clj b/test/metabase/util/encryption_test.clj
index 2d968de5b13..df25264a9a0 100644
--- a/test/metabase/util/encryption_test.clj
+++ b/test/metabase/util/encryption_test.clj
@@ -2,7 +2,6 @@
"Tests for encryption of Metabase DB details."
(:require
[buddy.core.codecs :as codecs]
- [clojure.string :as str]
[clojure.test :refer :all]
[metabase.settings.models.setting.cache :as setting.cache]
[metabase.test :as mt]
@@ -77,30 +76,23 @@
(encryption/decrypt secret-2 (encryption/encrypt secret "WOW"))))))
(deftest ^:parallel maybe-decrypt-not-encrypted-test
- (testing "trying to `maybe-decrypt` something that's not encrypted should return it as-is"
+ (testing "trying to `maybe-decrypt-accepting-plaintext` something that's not encrypted should return it as-is"
(is (= "{\"a\":100}"
- (encryption/maybe-decrypt secret "{\"a\":100}")))
+ (encryption/maybe-decrypt-accepting-plaintext secret "{\"a\":100}")))
(is (= "abc"
- (encryption/maybe-decrypt secret "abc")))))
+ (encryption/maybe-decrypt-accepting-plaintext secret "abc")))))
(deftest ^:parallel maybe-decrypt-with-wrong-key-test
- (testing (str "trying to decrypt something that is encrypted with the wrong key with `maybe-decrypt` should return "
- "the ciphertext...")
+ (testing (str "decrypting something encrypted with a different key using `maybe-decrypt-accepting-plaintext` throws "
+ "rather than returning the ciphertext — returning it would let a re-encrypting caller double-encrypt it")
(let [original-ciphertext (encryption/encrypt secret "WOW")]
- (is (= original-ciphertext
- (encryption/maybe-decrypt secret-2 original-ciphertext))))))
-
-(defn- includes-encryption-warning? [log-messages]
- (some (fn [{:keys [level message]}]
- (and (= level :warn)
- (str/includes? message (str "Cannot decrypt encrypted String. Have you changed or forgot to set "
- "MB_ENCRYPTION_SECRET_KEY?"))))
- log-messages))
+ (is (thrown? Throwable
+ (encryption/maybe-decrypt-accepting-plaintext secret-2 original-ciphertext))))))
(deftest ^:parallel no-errors-for-unencrypted-test
(testing "Something obviously not encrypted should avoiding trying to decrypt it (and thus not log an error)"
(mt/with-log-messages-for-level [messages :warn]
- (encryption/maybe-decrypt secret "abc")
+ (encryption/maybe-decrypt-accepting-plaintext secret "abc")
(is (empty? (messages))))))
(def ^:private fake-ciphertext
@@ -109,20 +101,29 @@
have the same size"
(apply str (repeat 64 "a")))
-(deftest ^:parallel log-warning-on-failure-test
- (testing (str "Something that is not encrypted, but might be (is the correct shape etc) should attempt to be "
- "decrypted. If unable to decrypt it, log a warning.")
- (mt/with-log-messages-for-level [messages :warn]
- (encryption/maybe-decrypt secret fake-ciphertext)
- (is (includes-encryption-warning? (messages))))
- (mt/with-log-messages-for-level [messages :warn]
- (encryption/maybe-decrypt secret-2 (encryption/encrypt secret "WOW"))
- (is (includes-encryption-warning? (messages))))))
-
(deftest ^:parallel possibly-encrypted-test
- (testing "Something that is not encrypted, but might be should return the original text"
- (is (= fake-ciphertext
- (encryption/maybe-decrypt secret fake-ciphertext)))))
+ (testing "a value shaped like ciphertext but that cannot be decrypted with the current key throws rather than being returned as-is"
+ (is (thrown? Throwable
+ (encryption/maybe-decrypt-accepting-plaintext secret fake-ciphertext)))))
+
+(deftest ^:parallel maybe-decrypt-strict-test
+ (testing "strict `maybe-decrypt`"
+ (testing "decrypts a genuinely encrypted value"
+ (is (= "WOW" (encryption/maybe-decrypt secret (encryption/encrypt secret "WOW")))))
+ (testing "throws on a value that is not encrypted"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"not encrypted"
+ (encryption/maybe-decrypt secret "{\"a\":100}")))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"not encrypted"
+ (encryption/maybe-decrypt secret "abc"))))
+ (testing "throws on a value that looks encrypted but fails to decrypt (wrong key or corrupt)"
+ (is (thrown? Throwable (encryption/maybe-decrypt secret fake-ciphertext)))
+ (is (thrown? Throwable (encryption/maybe-decrypt secret-2 (encryption/encrypt secret "WOW")))))
+ (testing "decrypts an encrypted byte array"
+ (is (= "WOW" (String. ^bytes (encryption/maybe-decrypt-bytes secret (encryption/encrypt-bytes secret (.getBytes "WOW")))))))
+ (testing "passes nil through but rejects a blank (non-encrypted) string"
+ (is (nil? (encryption/maybe-decrypt nil)))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"not encrypted"
+ (encryption/maybe-decrypt secret ""))))))
(deftest ^:parallel stream-encryption-test
(testing "Can encrypt stream"