Skip to content

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

Throw on uncoerceable user attributes (#82118)

metamben2659b8794175
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj
index 5aa0506d6f0..948788052ff 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj
@@ -120,12 +120,22 @@
(when (not attr-value)
(throw (ex-info (tru "Query requires user attribute `{0}`" (name attr-name))
{:type qp.error-type/missing-required-parameter})))
- {:type (if (and field-base-type (isa? field-base-type :type/Number))
- :number/=
- :string/=)
- :target target
- ;; :number/= and :string/= are variadic operators that require a sequential value
- :value [(attr-value->param-value field-base-type attr-value)]}))
+ (let [param-value (attr-value->param-value field-base-type attr-value)]
+ (when (nil? param-value)
+ ;; Without this a nil `param-value` propagates as `[nil]`, which
+ ;; `parameters.mbql/expand` treats as "no value" and filter is dropped. (#81821)
+ (throw (ex-info (tru "User attribute `{0}` value `{1}` cannot be coerced to column type {2}"
+ (name attr-name) attr-value field-base-type)
+ {:type qp.error-type/invalid-parameter
+ :attribute-name attr-name
+ :attribute-value attr-value
+ :field-base-type field-base-type})))
+ {:type (if (and field-base-type (isa? field-base-type :type/Number))
+ :number/=
+ :string/=)
+ :target target
+ ;; :number/= and :string/= are variadic operators that require a sequential value
+ :value [param-value]})))
(mu/defn- sandbox->parameters :- [:maybe [:sequential ::lib.schema.parameter/parameter]]
[metadata-providerable :- ::lib.schema.metadata/metadata-providerable
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
index 597a5a13d8f..fc9278cf964 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
@@ -370,6 +370,24 @@
(run-venues-count-query)))
(fails-without-token (run-venues-count-query))))))
+(deftest e2e-uncoerceable-attribute-fails-closed-test
+ (mt/test-drivers (e2e-test-drivers)
+ (testing "uncoerceable user attribute does not silently drop the sandbox filter (#81821)"
+ (testing "integer column"
+ (met/with-gtaps! {:gtaps {:venues (venues-category-mbql-gtap-def)}, :attributes {"cat" "a"}}
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"User attribute `cat` value `a` cannot be coerced"
+ (run-venues-count-query)))))
+ (testing "float column"
+ (met/with-gtaps! {:gtaps {:venues {:query (mt/mbql-query venues)
+ :remappings {:cat ["variable" [:field (mt/id :venues :latitude) nil]]}}}
+ :attributes {"cat" "a"}}
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"User attribute `cat` value `a` cannot be coerced"
+ (run-venues-count-query))))))))
+
(deftest e2e-test-6
(mt/test-drivers (e2e-test-drivers)
(testing "Another basic test, this one uses a stringified float for the login attribute"
@@ -2009,7 +2027,18 @@
(:type (attr-remapping->parameter mp {"cat" "50"} ["cat" [:variable [:field (mt/id :venues :price) nil]]])))))
(testing "text field → :string/="
(is (= :string/=
- (:type (attr-remapping->parameter mp {"cat" "foo"} ["cat" [:variable [:field (mt/id :venues :name) nil]]]))))))))
+ (:type (attr-remapping->parameter mp {"cat" "foo"} ["cat" [:variable [:field (mt/id :venues :name) nil]]])))))
+ (testing "uncoerceable attribute against numeric field throws instead of dropping the filter (#81821)"
+ (testing "integer column"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"User attribute `cat` value `a` cannot be coerced"
+ (attr-remapping->parameter mp {"cat" "a"} ["cat" [:variable [:field (mt/id :venues :price) nil]]]))))
+ (testing "float column"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"User attribute `cat` value `a` cannot be coerced"
+ (attr-remapping->parameter mp {"cat" "a"} ["cat" [:variable [:field (mt/id :venues :latitude) nil]]]))))))))
(deftest unix-timestamp-coercion-with-mbql-sandbox-test
(testing "UNIX timestamp coercion should be applied when querying through an MBQL sandbox (#69867)"