Skip to content

Commit 2b3ea70

Browse files
committed
Fix wrap-forwarded-remote addr security issue
Middleware function incorrectly used the first rather than last value present in the X-Forwarded-For header. This could result in attackers being able to spoof the :remote-addr key if this middleware was used. Reported by Daniel Compton <desk@danielcompton.net>.
1 parent d34af9f commit 2b3ea70

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

src/ring/middleware/proxy_headers.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
(defn wrap-forwarded-remote-addr
66
"Middleware that changes the :remote-addr of the request map to the
7-
first value present in the X-Forwarded-For header."
7+
last value present in the X-Forwarded-For header."
88
[handler]
99
(fn [request]
1010
(if-let [forwarded-for (get-in request [:headers "x-forwarded-for"])]
11-
(let [remote-addr (str/trim (re-find #"^[^,]*" forwarded-for))]
11+
(let [remote-addr (str/trim (re-find #"[^,]*$" forwarded-for))]
1212
(handler (assoc request :remote-addr remote-addr)))
1313
(handler request))))

test/ring/middleware/proxy_headers_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
(testing "with multiple proxies"
2222
(let [req (-> (request :get "/")
2323
(assoc :remote-addr "127.0.0.1")
24-
(header "x-forwarded-for" "1.2.3.4, 10.0.1.9, 192.168.4.98"))
24+
(header "x-forwarded-for" "10.0.1.9, 192.168.4.98, 1.2.3.4"))
2525
resp (handler req)]
2626
(is (= (:body resp) "1.2.3.4"))))))

0 commit comments

Comments
 (0)