Skip to content

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

Cache parse-approved-origins to stop per-request Invalid-URL log spam (#81126)

dpsuttonfd707ac9a3ba
diff --git a/src/metabase/server/middleware/security.clj b/src/metabase/server/middleware/security.clj
index 03715f78a2e..ba4cd1ba774 100644
--- a/src/metabase/server/middleware/security.clj
+++ b/src/metabase/server/middleware/security.clj
@@ -1,6 +1,7 @@
(ns metabase.server.middleware.security
"Ring middleware for adding security-related headers to API responses."
(:require
+ [clojure.core.memoize :as memoize]
[clojure.java.io :as io]
[clojure.string :as str]
[environ.core :as env]
@@ -421,12 +422,20 @@
(= reference-port "*")
(= port reference-port)))
-(defn parse-approved-origins
- "Parses the space separated string of approved origins"
+(defn- parse-approved-origins*
[approved-origins-raw]
(let [urls (str/split approved-origins-raw #" +")]
(keep (comp parse-url strip-origin-path) urls)))
+;; [[parse-approved-origins]] runs on essentially every API response via [[approved-origin?]]. Cache the parse (and
+;; its `Invalid URL` logging) against the origins string, which is a slow-moving global. A small LRU bound fits the few
+;; distinct strings callers pass -- the merged SDK+MCP allowlist and the MCP-only one -- without either evicting the
+;; other.
+(def ^{:arglists '([approved-origins-raw])}
+ parse-approved-origins
+ "Parses the space separated string of approved origins. Result is LRU-cached against the string."
+ (memoize/lru parse-approved-origins* :lru/threshold 4))
+
(def ^:private loopback-hosts
"Set of hostnames/IPs that represent loopback addresses.
Note: IPv6 addresses come from parse-url with brackets, e.g. [::1]"