Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue 15: compare Host and Origin to check if req is xdomain #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/ring/middleware/cors.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
(ns ring.middleware.cors
"Ring middleware for Cross-Origin Resource Sharing."
(:require [clojure.set :as set]
[clojure.string :as str]
(:require [clojure
[set :as set]
[string :as str]]
[ring.util.response :refer [get-header]]))

(defn origin
"Returns the Origin request header."
[request] (get-header request "origin"))

(defn host
"Returns the Host request header or empty string for convenience"
[request] (or (get-header request "host") ""))

(defn xdomain?
"Given a request check if Host and Origin headers mismatch"
[request]
(boolean
(when-let [orig (origin request)]
(not (= (str/replace orig #"^.*?//" "")
(host request))))))

(defn preflight?
"Returns true if the request is a preflight request"
[request]
Expand Down Expand Up @@ -112,7 +125,6 @@
response))
response))


(defn add-access-control
"Add the access-control headers to the response based on the rules
and what came on the header."
Expand Down Expand Up @@ -149,7 +161,7 @@
:headers {}
:body "preflight complete"}]
(add-access-control request access-control blank-response))
(if (origin request)
(if (xdomain? request)
(if (allow-request? request access-control)
(if-let [response (handler request)]
(add-access-control request access-control response))
Expand Down
105 changes: 67 additions & 38 deletions test/ring/middleware/cors_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,56 @@
(:require [clojure.test :refer :all]
[ring.middleware.cors :refer :all]))

(deftest test-xdomain?
(testing "only mismatching origin and host is considered xdomain"
(are [host origin expected]
(is (= expected
(xdomain?
{:headers {"host" host
"origin" origin}
:request-method :get})))
"burningswell.com" "somedomain.com" true
"www.burningswell.com" "http://www.burningswell.com" false
"burningswell.com" "http://www.burningswell.com:4242" true)))

(deftest test-allow-request?
(testing "with empty vector"
(is (not (allow-request? {:headers {"origin" "http://eample.com"}}
(is (not (allow-request? {:headers {"host" "example.com"
"origin" "http://eample.com"}}
{:access-control-allow-origin []}))))
(testing "with one regular expressions"
(are [origin expected]
(is (= expected
(allow-request?
{:headers {"origin" origin}
:request-method :get}
{:access-control-allow-origin [#"http://(.*\.)?burningswell.com"]
:access-control-allow-methods #{:get :put :post}})))
nil false
"" false
"http://example.com" false
"http://burningswell.com" true))
(are [host origin expected]
(is (= expected
(allow-request?
{:headers {"host" host
"origin" origin}
:request-method :get}
{:access-control-allow-origin
[#"http://(.*\.)?burningswell.com"]
:access-control-allow-methods
#{:get :put :post}})))
"" nil false
"" "" false
"anotherexample.com" "http://example.com" false
"anotherburningswell.com" "http://burningswell.com" true))

(testing "with multiple regular expressions"
(are [origin expected]
(is (= expected
(allow-request?
{:headers {"origin" origin}
:request-method :get}
{:access-control-allow-origin
[#"http://(.*\.)?burningswell.com"
#"http://example.com"]
:access-control-allow-methods #{:get :put :post}})))
nil false
"" false
"http://example.com" true
"http://burningswell.com" true
"http://api.burningswell.com" true
"http://dev.burningswell.com" true)))
(are [host origin expected]
(is (= expected
(allow-request?
{:headers {"host" host
"origin" origin}
:request-method :get}
{:access-control-allow-origin
[#"http://(.*\.)?burningswell.com"
#"http://example.com"]
:access-control-allow-methods #{:get :put :post}})))
"" nil false
"" "" false
"anotherdomain.com" "http://example.com" true
"anotherdomain.com" "http://burningswell.com" true
"anotherdomain.com" "http://api.burningswell.com" true
"anotherdomain.com" "http://dev.burningswell.com" true)))

(defn handler [request]
((wrap-cors (fn [_] {})
Expand All @@ -44,7 +62,8 @@

(deftest test-preflight
(testing "whitelist concrete headers"
(let [headers {"origin" "http://example.com"
(let [headers {"host" "anotherdomain.com"
"origin" "http://example.com"
"access-control-request-method" "POST"
"access-control-request-headers" "Accept, Content-Type"}]
(is (= {:status 200,
Expand All @@ -67,14 +86,16 @@
:access-control-allow-methods #{:get :put :post})
{:request-method :options
:uri "/"
:headers {"origin" "http://example.com"
:headers {"host" "anotherdomain.com"
"origin" "http://example.com"
"access-control-request-method" "POST"
"access-control-request-headers" "x-foo, x-bar"}}))))

(testing "whitelist headers ignore case"
(is (= (handler {:request-method :options
:uri "/"
:headers {"origin" "http://example.com"
:headers {"host" "anotherdomain.com"
"origin" "http://example.com"
"access-control-request-method" "POST"
"access-control-request-headers"
"ACCEPT, CONTENT-TYPE"}})
Expand All @@ -88,11 +109,13 @@
(is (empty? (handler
{:request-method :options
:uri "/"
:headers {"origin" "http://example.com"
:headers {"host" "anotherdomain.com"
"origin" "http://example.com"
"access-control-request-method" "DELETE"}}))))

(testing "header not allowed"
(let [headers {"origin" "http://example.com"
(let [headers {"host" "anotherdomain.com"
"origin" "http://example.com"
"access-control-request-method" "GET"
"access-control-request-headers" "x-another-custom-header"}]
(is (empty? (handler
Expand All @@ -103,7 +126,8 @@
(deftest test-preflight-header-subset
(is (= (handler {:request-method :options
:uri "/"
:headers {"origin" "http://example.com"
:headers {"host" "anotherdomain.com"
"origin" "http://example.com"
"access-control-request-method" "POST"
"access-control-request-headers" "Accept"}})
{:status 200
Expand All @@ -118,19 +142,22 @@
"Access-Control-Allow-Origin" "http://example.com"}}
(handler {:request-method :post
:uri "/"
:headers {"origin" "http://example.com"}}))))
:headers {"host" "anotherdomain.com"
"origin" "http://example.com"}}))))
(testing "failure"
(is (empty? (handler {:request-method :get
:uri "/"
:headers {"origin" "http://foo.com"}})))))
:headers {"host" "anotherdomain.com"
"origin" "http://foo.com"}})))))

(deftest test-no-cors-header-when-handler-returns-nil
(is (nil? ((wrap-cors (fn [_] nil)
:access-control-allow-origin #".*example.com"
:access-control-allow-methods [:get])
{:request-method
:get :uri "/"
:headers {"origin" "http://example.com"}}))))
:headers {"host" "anotherdomain.com"
"origin" "http://example.com"}}))))

(deftest test-options-without-cors-header
(is (empty? ((wrap-cors
Expand All @@ -144,7 +171,8 @@
:access-control-allow-origin #".*"
:access-control-allow-methods [:get :post :patch :put :delete])
{:request-method :options
:headers {"origin" "http://foo.com"}
:headers {"host" "anotherdomain.com"
"origin" "http://foo.com"}
:uri "/"}))))

(deftest additional-headers
Expand All @@ -155,7 +183,8 @@
:access-control-expose-headers "Etag")
{:request-method :get
:uri "/"
:headers {"origin" "http://example.com"}})]
:headers {"host" "anotherdomain.com"
"origin" "http://example.com"}})]
(is (= {:status 200
:headers
{"Access-Control-Allow-Credentials" "true"
Expand Down