Skip to content

Commit b89ef9a

Browse files
committed
Add async support to wrap-content-type-options
1 parent 85af729 commit b89ef9a

2 files changed

Lines changed: 39 additions & 16 deletions

File tree

src/ring/middleware/x_headers.clj

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
(str "ALLOW-FROM " (:allow-from frame-options))
1414
(str/upper-case (name frame-options))))
1515

16-
(defn- add-header [response header value]
17-
(some-> response (resp/header header value)))
16+
(defn- wrap-x-header [handler header-name header-value]
17+
(fn
18+
([request]
19+
(some-> (handler request) (resp/header header-name header-value)))
20+
([request respond raise]
21+
(handler request #(respond (some-> % (resp/header header-name header-value))) raise))))
1822

1923
(defn frame-options-response
2024
"Add the X-Frame-Options header to the response. See: wrap-frame-options."
2125
[response frame-options]
22-
(add-header response "X-Frame-Options" (format-frame-options frame-options)))
26+
(some-> response (resp/header "X-Frame-Options" (format-frame-options frame-options))))
2327

2428
(defn wrap-frame-options
2529
"Middleware that adds the X-Frame-Options header to the response. This governs
@@ -41,13 +45,13 @@
4145
{:pre [(or (= frame-options :deny)
4246
(= frame-options :sameorigin)
4347
(allow-from? frame-options))]}
44-
(let [header-name "X-Frame-Options"
45-
header-value (format-frame-options frame-options)]
46-
(fn
47-
([request]
48-
(add-header (handler request) header-name header-value))
49-
([request respond raise]
50-
(handler request #(respond (add-header % header-name header-value)) raise)))))
48+
(wrap-x-header handler "X-Frame-Options" (format-frame-options frame-options)))
49+
50+
(defn content-type-options-response
51+
"Add the X-Content-Type-Options header to the response.
52+
See: wrap-content-type-options."
53+
[response content-type-options]
54+
(some-> response (resp/header "X-Content-Type-Options" (name content-type-options))))
5155

5256
(defn wrap-content-type-options
5357
"Middleware that adds the X-Content-Type-Options header to the response. This
@@ -60,9 +64,7 @@
6064
http://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx"
6165
[handler content-type-options]
6266
{:pre [(= content-type-options :nosniff)]}
63-
(fn [request]
64-
(if-let [response (handler request)]
65-
(resp/header response "X-Content-Type-Options" (name content-type-options)))))
67+
(wrap-x-header handler "X-Content-Type-Options" (name content-type-options)))
6668

6769
(defn wrap-xss-protection
6870
"Middleware that adds the X-XSS-Protection header to the response. This header

test/ring/middleware/x_headers_test.clj

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@
6161
(is (nil? @resp)))))
6262

6363
(deftest test-wrap-content-type-options
64-
(let [handle-hello (constantly
65-
(-> (response "hello")
66-
(content-type "text/plain")))]
64+
(let [handle-hello (constantly (-> (response "hello") (content-type "text/plain")))]
6765
(testing "nosniff"
6866
(let [handler (wrap-content-type-options handle-hello :nosniff)
6967
resp (handler (request :get "/"))]
@@ -79,6 +77,29 @@
7977
(let [handler (wrap-content-type-options (constantly nil) :nosniff)]
8078
(is (nil? (handler (request :get "/"))))))))
8179

80+
(deftest test-wrap-content-type-options-cps
81+
(testing "nosniff"
82+
(let [handler (-> (fn [_ respond _]
83+
(respond (-> (response "hello") (content-type "text/plain"))))
84+
(wrap-content-type-options :nosniff))
85+
resp (promise)
86+
ex (promise)]
87+
(handler (request :get "/") resp ex)
88+
(is (not (realized? ex)))
89+
(is (= @resp {:status 200
90+
:headers {"X-Content-Type-Options" "nosniff"
91+
"Content-Type" "text/plain"}
92+
:body "hello"}))))
93+
94+
(testing "nil response"
95+
(let [handler (-> (fn [_ respond _] (respond nil))
96+
(wrap-content-type-options :nosniff))
97+
resp (promise)
98+
ex (promise)]
99+
(handler (request :get "/") resp ex)
100+
(is (not (realized? ex)))
101+
(is (nil? @resp)))))
102+
82103
(deftest test-wrap-xss-protection
83104
(let [handle-hello (constantly (response "hello"))]
84105
(testing "enable"

0 commit comments

Comments
 (0)