Skip to content

Commit a4f3670

Browse files
committed
Add :safe-header default to :anti-forgery
Provides a more convenient way of safely POSTing via XHR.
1 parent b46fff3 commit a4f3670

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ The following configuration keys are supported:
121121

122122
- `:anti-forgery` -
123123
Set to true to add CSRF protection via the [ring-anti-forgery][5]
124-
library.
124+
library, or supply a map of options to be passed to the middleware.
125125

126126
- `:content-type-options` -
127127
Prevents attacks based around media-type confusion. See:

src/ring/middleware/defaults.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
:session {:flash true
4848
:cookie-attrs {:http-only true}
4949
:store default-session-store}
50-
:security {:anti-forgery true
50+
:security {:anti-forgery {:safe-header "X-Ring-Anti-Forgery"}
5151
:frame-options :sameorigin
5252
:content-type-options :nosniff}
5353
:static {:resources "public"}

test/ring/middleware/defaults_test.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,11 @@
241241
(assoc-in [:security :xss-protection :mode] :block))))
242242
resp (handler (request :get "/"))]
243243
(is (not (nil? (get-in resp [:headers "X-XSS-Protection"]))))
244-
(is (= (get-in resp [:headers "X-XSS-Protection"]) "1; mode=block")))))
244+
(is (= (get-in resp [:headers "X-XSS-Protection"]) "1; mode=block"))))
245+
246+
(testing "anti-forgery"
247+
(let [handler (-> (constantly (response "foo"))
248+
(wrap-defaults site-defaults))]
249+
(is (= 403 (:status (handler (request :post "/")))))
250+
(is (= 200 (:status (handler (-> (request :post "/")
251+
(header "X-Ring-Anti-Forgery" "1")))))))))

0 commit comments

Comments
 (0)