|
13 | 13 | (str "ALLOW-FROM " (:allow-from frame-options)) |
14 | 14 | (str/upper-case (name frame-options)))) |
15 | 15 |
|
| 16 | +(defn- format-xss-protection [enable? options] |
| 17 | + (str (if enable? "1" "0") (if options "; mode=block"))) |
| 18 | + |
16 | 19 | (defn- wrap-x-header [handler header-name header-value] |
17 | 20 | (fn |
18 | 21 | ([request] |
|
66 | 69 | {:pre [(= content-type-options :nosniff)]} |
67 | 70 | (wrap-x-header handler "X-Content-Type-Options" (name content-type-options))) |
68 | 71 |
|
| 72 | +(defn xss-protection-response |
| 73 | + "Add the X-XSS-Protection header to the response. See: wrap-xss-protection." |
| 74 | + ([response enable?] |
| 75 | + (xss-protection-response response enable? nil)) |
| 76 | + ([response enable? options] |
| 77 | + (some-> response |
| 78 | + (resp/header "X-XSS-Protection" (format-xss-protection enable? options))))) |
| 79 | + |
69 | 80 | (defn wrap-xss-protection |
70 | 81 | "Middleware that adds the X-XSS-Protection header to the response. This header |
71 | 82 | enables a heuristic filter in browsers for detecting cross-site scripting |
|
77 | 88 | :mode - currently accepts only :block |
78 | 89 |
|
79 | 90 | See: http://msdn.microsoft.com/en-us/library/dd565647(v=vs.85).aspx" |
80 | | - [handler enable? & [options]] |
81 | | - {:pre [(or (nil? options) (= options {:mode :block}))]} |
82 | | - (let [header-value (str (if enable? "1" "0") (if options "; mode=block"))] |
83 | | - (fn [request] |
84 | | - (if-let [response (handler request)] |
85 | | - (resp/header response "X-XSS-Protection" header-value))))) |
| 91 | + ([handler enable?] |
| 92 | + (wrap-xss-protection handler enable? nil)) |
| 93 | + ([handler enable? options] |
| 94 | + {:pre [(or (nil? options) (= options {:mode :block}))]} |
| 95 | + (wrap-x-header handler "X-XSS-Protection" (format-xss-protection enable? options)))) |
0 commit comments