The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Stop caching 404s for hashed assets (#82796)
Romeo Van Snickc916fa723a07
diff --git a/src/metabase/request/util.clj b/src/metabase/request/util.cljindex d84020276aa..fdf2c0d74ba 100644--- a/src/metabase/request/util.clj+++ b/src/metabase/request/util.clj@@ -69,17 +69,24 @@[{:keys [uri]}](re-matches #"^/embed/sdk/v\d+$" uri))+(def ^:private cache-busted-asset-pattern+ "Matches a file the build emits under `/app/dist`, where the name carries a content hash either as+ `<name>.<hash>.<ext>` or as `<hash>.<ext>`. Requiring the hash is what makes a far-future cache+ safe: changed content always arrives at a new URL."+ #"^/app/dist/(?:.+\.)?[a-f0-9]{8,}(?:\.[a-z0-9]+)+$")++(def ^:private static-font-pattern+ "Matches a bundled font. Fonts ship at a stable path and are treated as static."+ #"^/app/fonts/.+\.(?:woff2?|ttf|otf|eot)$")+(defn cacheable?"Can the ring request be permanently cached?"[{:keys [request-method uri], :as _request}]- (and (= request-method :get)- (or- ;; match requests that are js/css and have a cache-busting hex string- (re-matches #"^/app/dist/.+\.[a-f0-9]+\.(js|css)$" uri)- ;; any resource that is named as a cache-busting hex string (e.g. images)- (re-matches #"^/app/dist/[a-f0-9]+.*$" uri)- ;; font files are static and should be cached- (re-matches #"^/app/fonts/.+\.(woff2?|ttf|otf|eot)$" uri))))+ (boolean+ (and (= request-method :get)+ uri+ (or (re-matches cache-busted-asset-pattern uri)+ (re-matches static-font-pattern uri)))))(def https-state"Whether the original request reached us over HTTPS: `:https`, `:http`, or `:unknown`. Require `:https` to skip adiff --git a/src/metabase/server/middleware/security.clj b/src/metabase/server/middleware/security.cljindex c866b57f161..c0d0a21b61f 100644--- a/src/metabase/server/middleware/security.clj+++ b/src/metabase/server/middleware/security.clj@@ -634,6 +634,15 @@(remove #(covers-instance-origin? self %) hosts)hosts))+(defn- cacheable-status?+ "Whether a response with this status may carry a far-future cache header. A 404 for a hashed+ asset is transient: during a rolling deploy a client can ask an instance that does not have the+ file yet. Caching that answer for a year leaves the asset unreachable long after the deploy."+ [status]+ (boolean (and status+ (or (<= 200 status 299)+ (= status 304)))))+(defn- add-security-headers* [request response];; merge is other way around so that handler can override headers(let [headers (security-headers@@ -648,7 +657,8 @@(request/data-app? request) :self((some-fn request/public? request/embed?) request) :any:else :none)- :allow-cache? (request/cacheable? request)+ :allow-cache? (and (request/cacheable? request)+ (cacheable-status? (:status response))):data-app-iframe? (data-app-iframe-request? request);; Per-app `allowed_hosts` → `connect-src`/`form-action` (iframe;; doc) and `frame-src` (both the iframe doc and the top page,diff --git a/test/metabase/request/util_test.clj b/test/metabase/request/util_test.cljindex 2e22622061a..d1fad4cb1b0 100644--- a/test/metabase/request/util_test.clj+++ b/test/metabase/request/util_test.clj@@ -12,23 +12,41 @@[metabase.util.json :as json][ring.mock.request :as ring.mock]))+(defn- cacheable? [uri]+ (req.util/cacheable? {:request-method :get :uri uri}))+(deftest ^:parallel cacheable?-test- (testing "JS/CSS with cache-busting hash are cacheable"- (is (some? (req.util/cacheable? {:request-method :get :uri "/app/dist/main.abc123def.js"})))- (is (some? (req.util/cacheable? {:request-method :get :uri "/app/dist/styles.abc123def.css"}))))- (testing "Resources in /app/dist/ with hex hash prefix are cacheable"- (is (some? (req.util/cacheable? {:request-method :get :uri "/app/dist/abc123def456.png"}))))- (testing "Font files are cacheable"- (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/Lato/lato-v16-latin-regular.woff2"})))- (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/Lato/lato-v16-latin-regular.woff"})))- (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/CustomFont/custom.ttf"})))- (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/CustomFont/custom.otf"})))- (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/CustomFont/custom.eot"}))))- (testing "Non-GET requests are not cacheable"- (is (not (req.util/cacheable? {:request-method :post :uri "/app/fonts/Lato/lato.woff2"}))))- (testing "Other paths are not cacheable"- (is (not (req.util/cacheable? {:request-method :get :uri "/api/dashboard/1"})))- (is (not (req.util/cacheable? {:request-method :get :uri "/app/dist/main.js"})))))+ (testing "a content hash in the name makes an asset cacheable, whatever its extension"+ (are [uri] (true? (cacheable? uri))+ "/app/dist/main.abc123def.js"+ "/app/dist/styles.abc123def.css"+ "/app/dist/abc123def456.png"+ "/app/dist/abc123def456.svg"+ "/app/dist/e6cb5532b88aec25.gif"+ ;; the build emits bare `<hash>.<ext>` today, but a named variant must stay cacheable+ "/app/dist/embed-js-example.a1b2c3d4e5f6.png"+ ;; compressed and sourcemap companions keep the hash of the file they belong to+ "/app/dist/home.713b08815fb2dac3.js.map"+ "/app/dist/0f617f82e97365e8.svg.br"))+ (testing "fonts are cacheable"+ (are [uri] (true? (cacheable? uri))+ "/app/fonts/Lato/lato-v16-latin-regular.woff2"+ "/app/fonts/Lato/lato-v16-latin-regular.woff"+ "/app/fonts/CustomFont/custom.ttf"+ "/app/fonts/CustomFont/custom.otf"+ "/app/fonts/CustomFont/custom.eot"))+ (testing "a name without a content hash is never cacheable, even when it starts with hex digits"+ (are [uri] (false? (cacheable? uri))+ "/app/dist/main.js"+ "/app/dist/favicon.png"+ "/app/dist/abc.png"+ "/app/dist/deadbee.css"))+ (testing "only GETs are cacheable"+ (is (false? (req.util/cacheable? {:request-method :post :uri "/app/dist/abc123def456.png"}))))+ (testing "other paths are not cacheable"+ (are [uri] (false? (cacheable? uri))+ "/api/dashboard/1"+ "/app/assets/img/logo.svg")))(deftest ^:parallel https-state-test(doseq [[headers expected] {{"x-forwarded-proto" "https"} :httpsdiff --git a/test/metabase/server/middleware/security_test.clj b/test/metabase/server/middleware/security_test.cljindex cbca61d4af2..469e2993b8c 100644--- a/test/metabase/server/middleware/security_test.clj+++ b/test/metabase/server/middleware/security_test.clj@@ -1094,3 +1094,28 @@(is (true? (server.settings/csp-img-enabled)))(server.settings/csp-img-enabled! false)(is (false? (server.settings/csp-img-enabled))))))++(defn- cache-control-for+ "Run `response` through [[mw.security/add-security-headers]] for a GET of `uri`."+ [uri response]+ (let [handler (mw.security/add-security-headers (fn [_req respond _raise] (respond response)))+ result (promise)]+ (handler {:request-method :get :uri uri} #(deliver result %) #(deliver result %))+ (get-in @result [:headers "Cache-Control"])))++(deftest far-future-cache-only-for-successful-responses-test+ (testing "a hashed asset that exists is cached for a long time"+ (is (= "public, max-age=31536000"+ (cache-control-for "/app/dist/abc123def456.png" {:status 200 :body "x"}))))+ (testing "a 304 keeps the freshness information the browser already stored"+ (is (= "public, max-age=31536000"+ (cache-control-for "/app/dist/abc123def456.png" {:status 304}))))+ (testing "a 404 for a hashed asset is never cached: during a rolling deploy a client can reach+ an instance that does not have the file yet, and caching that leaves the asset broken"+ (doseq [status [404 500 302]]+ (is (= "max-age=0, no-cache, must-revalidate, proxy-revalidate"+ (cache-control-for "/app/dist/abc123def456.png" {:status status :body "nope"}))+ (str "status " status))))+ (testing "a missing font is not cached either"+ (is (= "max-age=0, no-cache, must-revalidate, proxy-revalidate"+ (cache-control-for "/app/fonts/Lato/lato-v16-latin-regular.woff2" {:status 404})))))