Skip to content

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

Don't let a failed advisory matching query override the version check (#79466)

Ngoc Khuat2366d9fc6b7e
diff --git a/enterprise/backend/src/metabase_enterprise/security_center/matching.clj b/enterprise/backend/src/metabase_enterprise/security_center/matching.clj
index 0a53fe4301c..3b22f353672 100644
--- a/enterprise/backend/src/metabase_enterprise/security_center/matching.clj
+++ b/enterprise/backend/src/metabase_enterprise/security_center/matching.clj
@@ -103,24 +103,28 @@
(mu/defn evaluate-advisory :- ::schema/match-status
"Resolve a match status from a version-range check and a matching-query result.
+ Affected means in-range AND matched, so `:error` (couldn't determine) only
+ survives when the version check didn't already settle it — the same way SQL
+ gives `FALSE AND NULL` = `FALSE`.
+
+ in-range? = false → :resolved if matched, else :not_affected
query-result = :error → :error
query-result = false → :not_affected
- in-range? = true → :active
- otherwise → :resolved"
+ otherwise → :active"
[in-range? :- boolean?
query-result :- QueryResult]
(cond
+ (not in-range?)
+ (if (true? query-result) :resolved :not_affected)
+
(= query-result :error)
:error
(not query-result)
:not_affected
- in-range?
- :active
-
:else
- :resolved))
+ :active))
(defn evaluate-advisory!
"Evaluate a single advisory: run the matching query, resolve the status, and
diff --git a/enterprise/backend/test/metabase_enterprise/security_center/matching_test.clj b/enterprise/backend/test/metabase_enterprise/security_center/matching_test.clj
index 819fd769978..fbf4607ab2d 100644
--- a/enterprise/backend/test/metabase_enterprise/security_center/matching_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/security_center/matching_test.clj
@@ -74,9 +74,11 @@
(is (false? (in-range? nil []))))))
(deftest ^:parallel evaluate-advisory-test
- (testing ":error query-result propagates regardless of in-range?"
- (is (= :error (matching/evaluate-advisory true :error)))
- (is (= :error (matching/evaluate-advisory false :error))))
+ (testing ":error only survives when the version check didn't already settle it"
+ (is (= :error (matching/evaluate-advisory true :error)))
+ (testing "out-of-range: the version alone rules the instance out, so an
+ undeterminable query must not escalate to :error"
+ (is (= :not_affected (matching/evaluate-advisory false :error)))))
(testing "falsey query-result → :not_affected regardless of in-range?"
(is (= :not_affected (matching/evaluate-advisory true false)))
(is (= :not_affected (matching/evaluate-advisory false false))))
@@ -182,8 +184,30 @@
(matching/evaluate-advisory! advisory)
(is (=? {:match_status :error
:last_evaluated_at some?}
+ (t2/select-one :model/SecurityAdvisory :id (:id advisory))))))))
+
+;; Out-of-range: current instance version is not covered by [0.0.1, 0.0.2).
+(deftest evaluate-advisory!-out-of-range-test
+ (with-redefs [config/mb-version-info {:tag "v1.55.0"}]
+ ;; An advisory whose query names a table this version doesn't have yet. Reported as
+ ;; :error before, which the notification task treats the same as :active — so instances
+ ;; the version check had already cleared got notified anyway. See GDGT-2972.
+ (testing "out-of-range + query error → :not_affected, never :error"
+ (mt/with-temp [:model/SecurityAdvisory advisory
+ {:advisory_id "SC-MATCH-005"
+ :severity "high"
+ :title "Test"
+ :description "Test"
+ :remediation "Upgrade"
+ :affected_versions [{:min "0.0.1" :fixed "0.0.2"}]
+ :matching_query {:default {:select [1] :from [:nonexistent_table] :limit 1}}
+ :match_status "unknown"
+ :published_at #t "2026-03-24T00:00:00Z"
+ :updated_at #t "2026-03-24T00:00:00Z"}]
+ (matching/evaluate-advisory! advisory)
+ (is (=? {:match_status :not_affected
+ :last_evaluated_at some?}
(t2/select-one :model/SecurityAdvisory :id (:id advisory))))))
- ;; Out-of-range: current instance version is not covered by [0.0.1, 0.0.2).
;; The matching_query in these tests would throw if executed — that's how we
;; verify the short-circuit actually avoids running the query.
(let [past #t "2020-01-01T00:00:00Z"]
@@ -236,10 +260,13 @@
(matching/evaluate-advisory! advisory)
(is (=? {:match_status :resolved
:last_evaluated_at some?}
- (t2/select-one :model/SecurityAdvisory :id (:id advisory))))))
- ;; vLOCAL_DEV / vUNKNOWN both parse to nil. An unparseable instance version
- ;; must never produce :active — we cannot claim an instance is affected when
- ;; we can't even compare its version to the affected ranges.
+ (t2/select-one :model/SecurityAdvisory :id (:id advisory))))))))
+
+;; vLOCAL_DEV / vUNKNOWN both parse to nil. An unparseable instance version
+;; must never produce :active — we cannot claim an instance is affected when
+;; we can't even compare its version to the affected ranges.
+(deftest evaluate-advisory!-unparseable-version-test
+ (with-redefs [config/mb-version-info {:tag "v1.55.0"}]
(doseq [tag ["vLOCAL_DEV" "vUNKNOWN"]]
(testing (str "unparseable instance version " (pr-str tag) " never yields :active")
(is (nil? (matching/parse-version tag)))