From 2ed5b48067ddf0ef2188f094089fc55605267dd1 Mon Sep 17 00:00:00 2001 From: Kimmo Rantala Date: Thu, 15 Oct 2020 23:33:20 +0300 Subject: [PATCH] Fix resource handler path matching File/resource handler checks that uri actually matches to path instead of comparing just path length to uri length. --- modules/reitit-ring/src/reitit/ring.cljc | 10 +++++----- test/clj/reitit/http_test.clj | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 1da06ed3..727536b4 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -208,9 +208,9 @@ join-paths (fn [& paths] (str/replace (str/replace (str/join "/" paths) #"([/]+)" "/") #"/$" "")) response (fn [path] - (if-let [response (or (paths (join-paths "/" path)) - (response-fn path options))] - (response/content-type response (mime-type/ext-mime-type path)))) + (if-let [response (or (paths (join-paths "/" path)) + (response-fn path options))] + (response/content-type response (mime-type/ext-mime-type path)))) path-or-index-response (fn [path uri] (or (response path) (loop [[file & files] index-files] @@ -221,8 +221,8 @@ handler (if path (fn [request] (let [uri (:uri request)] - (if-let [path (if (>= (count uri) path-size) (subs uri path-size))] - (path-or-index-response path uri)))) + (if (.startsWith uri path) + (path-or-index-response (subs uri path-size) uri)))) (fn [request] (let [uri (:uri request) path (-> request :path-params parameter)] diff --git a/test/clj/reitit/http_test.clj b/test/clj/reitit/http_test.clj index 27e28b6f..196fb732 100644 --- a/test/clj/reitit/http_test.clj +++ b/test/clj/reitit/http_test.clj @@ -491,6 +491,8 @@ (testing "not found" (let [response (app (request "/not-found"))] + (is (= 404 (:status response)))) + (let [response (app {:uri "/XXXXX/hello.json" :request-method :get})] (is (= 404 (:status response))))) (testing "3-arity"