Guard for infinite middleware compilation

This commit is contained in:
Tommi Reiman 2017-12-05 08:52:53 +02:00
parent 259c8e01bb
commit 5fdaf609c2
2 changed files with 19 additions and 6 deletions

View file

@ -16,6 +16,8 @@
(str "Middleware can't have both :wrap and :compile defined " m) m))) (str "Middleware can't have both :wrap and :compile defined " m) m)))
(map->Middleware m)) (map->Middleware m))
(def ^:dynamic *max-compile-depth* 10)
(extend-protocol IntoMiddleware (extend-protocol IntoMiddleware
#?(:clj clojure.lang.APersistentVector #?(:clj clojure.lang.APersistentVector
@ -41,14 +43,21 @@
(into-middleware (create this) data opts)) (into-middleware (create this) data opts))
Middleware Middleware
(into-middleware [{:keys [wrap compile] :as this} data opts] (into-middleware [{:keys [compile] :as this} data opts]
(if-not compile (if-not compile
this this
(if-let [middeware (into-middleware (compile data opts) data opts)] (let [compiled (::compiled opts 0)
(map->Middleware opts (assoc opts ::compiled (inc compiled))]
(merge (when (>= compiled *max-compile-depth*)
(dissoc this :create) (throw
(impl/strip-nils middeware)))))) (ex-info
(str "Too deep middleware compilation - " compiled)
{:this this, :data data, :opts opts})))
(if-let [middeware (into-middleware (compile data opts) data opts)]
(map->Middleware
(merge
(dissoc this :create)
(impl/strip-nils middeware)))))))
nil nil
(into-middleware [_ _ _])) (into-middleware [_ _ _]))

View file

@ -111,6 +111,10 @@
(is (= [:data :value :request] (app :request))) (is (= [:data :value :request] (app :request)))
(is (= 4 @calls))))) (is (= 4 @calls)))))
(testing "too deeply compiled Middleware fails"
(binding [middleware/*max-compile-depth* 2]
(is (thrown? Exception (->app [[(middleware/create mw3) :value]] identity)))))
(testing "nil unmounts the middleware" (testing "nil unmounts the middleware"
(let [app (->app [{:compile (constantly nil)} (let [app (->app [{:compile (constantly nil)}
{:compile (constantly nil)}] identity)] {:compile (constantly nil)}] identity)]