Skip to content

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

5.8 KB
Raw Blame
(ns metabase-enterprise.sandbox.models.params.field-values
  (:require
   [metabase-enterprise.sandbox.api.table :as table]
   [metabase-enterprise.sandbox.db :as sandbox.db]
   [metabase-enterprise.sandbox.query-processor.middleware.sandboxing :as sandboxing]
   [metabase.api.common :as api]
   [metabase.premium-features.core :as premium-features :refer [defenterprise]]
   [metabase.warehouse-schema.models.field :as field]
   [toucan2.core :as t2]))

(comment api/keep-me)

(defn field-is-sandboxed?
  "Check if a field is sandboxed."
  [{:keys [table], :as field}]
  ;; slight optimization: for the `field-id->field-values` version we can batched hydrate `:table` to avoid having to
  ;; make a bunch of calls to fetch Table. For `get-or-create-field-values` we don't hydrate `:table` so we can fall
  ;; back to fetching it manually with `field/table`
  (table/only-sandboxed-perms? (or table (field/table field))))

(defn- table-id->sandbox
  "Find the GTAP for current user that apply to table `table-id`. Returns nil when there is no current user (e.g. when
  called from a background sync), since sandboxes are scoped to a user's group memberships."
  [table-id]
  (when api/*current-user-id*
    (let [group-ids (sandbox.db/user-group-ids api/*current-user-id*)
          sandboxes (when (seq group-ids)
                      (sandbox.db/sandboxes-for-groups-and-table group-ids table-id))]
      (when sandboxes
        (sandboxing/assert-one-sandbox-per-table sandboxes)
        ;; there should be only one gtap per table and we only need one table here
        ;; see docs in [[metabase.permissions.models.permissions]] for more info
        (t2/hydrate (first sandboxes) :card)))))

(defn- field->sandbox-attributes-for-current-user
  "Returns the gtap attributes for current user that applied to `field`.

  The gtap-attributes is a list with 3 elements:
  1. card-id - for GTAP that use a saved question
  2. the timestamp when the saved question was last updated
  3. a map:
    if query is mbql query:
      - with an entry for every user-attribute in the GTAP's `attribute_remappings`
      - value is the user-attribute of current user corresponding to the key
    for native query, this map will be the login-attributes of user

  The query processor applies *every* attribute remapping when it runs the sandboxed query —
  including remappings whose target is a column of a joined table, or a column referenced by
  name — so every remapped attribute can change which rows the user sees and must be part of
  this fingerprint. Narrowing the map to remappings that target the sandboxed table's own
  columns let two users with different values for a joined-table attribute share one cached
  FieldValues row (SEC-874).

  For example we have an GTAP rules
  {:card_id              1 ;; a mbql query
   :attribute_remappings {\"State\" [:dimension [:field 3 nil]]}}

  And users with login-attributes {\"State\" \"CA\"}

  ;; (field-id->gtap-attributes-for-current-user (t2/select-one Field :id 3))
  ;; -> [1, {\"State\" \"CA\"}]"
  [{table-id :table_id, :as _field}]
  (when-let [sandbox (table-id->sandbox table-id)]
    (let [login-attributes     (api/current-user-attributes)
          attribute-remappings (:attribute_remappings sandbox)]
      [(:card_id sandbox)
       (-> sandbox :card :updated_at)
       (if (= :native (get-in sandbox [:card :query_type]))
         ;; For sandbox that uses native query, we can't narrow down to the exact attribute
         ;; that affect the current table. So we just hash the whole login-attributes of users.
         ;; This makes hashing a bit less efficient but it ensures that user get a new hash
         ;; if they change login attributes
         login-attributes
         (into {} (for [k (keys attribute-remappings)]
                    [k (get login-attributes k)])))])))

(defenterprise hash-input-for-sandbox
  "Returns a hash-input for FieldValues if the field is sandboxed.
  The attributes are nil when they can't be resolved, and while the `:sandboxes` feature is unavailable."
  :feature :none
  [field]
  (when (field-is-sandboxed? field)
    {:sandbox-attributes (when (premium-features/has-feature? :sandboxes)
                           (field->sandbox-attributes-for-current-user field))}))

(defenterprise sandbox-token-for-table
  "Sandbox fingerprint for the current user on `table-id` (GTAP card, its version, and resolved
  user-attribute values), or nil when the user is not *enforced*-sandboxed on that table. Reuses
  the same enforcement guard (`field-is-sandboxed?`) and attribute-extraction as the FieldValues
  cache, so superusers and users with full access via another group correctly get no token, and
  two genuinely-sandboxed users \"share a sandbox\" iff they'd see the same rows. The card's
  `:updated_at` is stringified to give it a printed form that is stable across processes and
  versions: the caller digests this value rather than storing it, and a digest is only comparable
  if the bytes going into it are reproducible.

  The raw attribute values in here never reach storage — [[metabase.permissions.data-access-token]]
  replaces this whole value with a SHA-256 of it before returning a token.

  When the enforcement guard says the user IS sandboxed but the attribute lookup finds nothing
  (the guard and the lookup consult different subsystems), we must not return nil — the
  compatibility gate reads nil as \"unrestricted\", which would fail open. Instead we return a
  user-scoped indeterminate token that matches no other user's token — fail closed."
  :feature :none
  [table-id]
  (let [field {:table_id table-id}]
    (when (field-is-sandboxed? field)
      (if-let [[card-id updated-at attributes] (field->sandbox-attributes-for-current-user field)]
        [card-id (str updated-at) attributes]
        [::indeterminate-sandbox api/*current-user-id*]))))