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)))
(map->Middleware m))
(def ^:dynamic *max-compile-depth* 10)
(extend-protocol IntoMiddleware
#?(:clj clojure.lang.APersistentVector
@ -41,14 +43,21 @@
(into-middleware (create this) data opts))
Middleware
(into-middleware [{:keys [wrap compile] :as this} data opts]
(into-middleware [{:keys [compile] :as this} data opts]
(if-not compile
this
(if-let [middeware (into-middleware (compile data opts) data opts)]
(map->Middleware
(merge
(dissoc this :create)
(impl/strip-nils middeware))))))
(let [compiled (::compiled opts 0)
opts (assoc opts ::compiled (inc compiled))]
(when (>= compiled *max-compile-depth*)
(throw
(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
(into-middleware [_ _ _]))

View file

@ -111,6 +111,10 @@
(is (= [:data :value :request] (app :request)))
(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"
(let [app (->app [{:compile (constantly nil)}
{:compile (constantly nil)}] identity)]