Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent duplicate session cookies if possible [IMMUTANT-272]
If ring and the servlet container are using the same session cookie name, we can detect that and remove ring's version of the cookie from the response.
- Loading branch information
Showing
8 changed files
with
224 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
modules/web/src/main/clojure/immutant/web/session/internal.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
;; Copyright 2008-2013 Red Hat, Inc, and individual contributors. | ||
;; | ||
;; This is free software; you can redistribute it and/or modify it | ||
;; under the terms of the GNU Lesser General Public License as | ||
;; published by the Free Software Foundation; either version 2.1 of | ||
;; the License, or (at your option) any later version. | ||
;; | ||
;; This software is distributed in the hope that it will be useful, | ||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
;; Lesser General Public License for more details. | ||
;; | ||
;; You should have received a copy of the GNU Lesser General Public | ||
;; License along with this software; if not, write to the Free | ||
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
;; 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
|
||
(ns ^:no-doc immutant.web.session.internal | ||
(:use [immutant.web.internal :only [current-servlet-request]]) | ||
(:require [immutant.util :as util] | ||
[immutant.web.session :as session])) | ||
|
||
(def ^:private cookie-encoder | ||
(util/try-resolve-any | ||
'ring.util.codec/form-encode ;; ring >= 1.1.0 | ||
'ring.util.codec/url-encode)) | ||
|
||
(defn ^:private cookie-matches-servlet-session-cookie? | ||
[^javax.servlet.http.HttpSession session ^String cookie] | ||
(.startsWith cookie | ||
(str (:cookie-name (session/session-cookie-attributes)) | ||
\= | ||
(cookie-encoder (.getId session))))) | ||
|
||
(defn ^:internal servlet-session-wrapper | ||
"Middleware that attempts to prevent duplicate cookies when the | ||
servlet session is being used. If the servlet session is active and | ||
the response includes a cookie with the same name and id, it is | ||
stripped." | ||
[handler] | ||
(fn [request] | ||
(let [response (handler request) | ||
session (.getSession current-servlet-request false)] | ||
(if (and session (session/using-servlet-session? session)) | ||
(update-in response [:headers "Set-Cookie"] | ||
#(remove | ||
(partial cookie-matches-servlet-session-cookie? session) | ||
%)) | ||
response)))) |
46 changes: 46 additions & 0 deletions
46
modules/web/src/test/clojure/immutant/web/session/test/internal.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
;; Copyright 2008-2012 Red Hat, Inc, and individual contributors. | ||
;; | ||
;; This is free software; you can redistribute it and/or modify it | ||
;; under the terms of the GNU Lesser General Public License as | ||
;; published by the Free Software Foundation; either version 2.1 of | ||
;; the License, or (at your option) any later version. | ||
;; | ||
;; This software is distributed in the hope that it will be useful, | ||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
;; Lesser General Public License for more details. | ||
;; | ||
;; You should have received a copy of the GNU Lesser General Public | ||
;; License along with this software; if not, write to the Free | ||
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
;; 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
|
||
(ns immutant.web.session.test.internal | ||
(:use immutant.web.session.internal | ||
clojure.test) | ||
(:require [immutant.web.test.session-utils :as util] | ||
[ring.middleware.cookies :as cookies])) | ||
|
||
(defn create-response [cookies _] | ||
{:headers {"Set-Cookie" cookies}}) | ||
|
||
(defn mock-session-cookie-attributes [] | ||
{:cookie-name "the-session"}) | ||
|
||
(deftest handler-should-strip-cookie-with-servlet-session-name | ||
(with-redefs [immutant.web.session/session-cookie-attributes mock-session-cookie-attributes] | ||
(are [session-id cookies expected] | ||
(= expected | ||
(util/session-fixture | ||
session-id | ||
#(get-in | ||
((servlet-session-wrapper (partial create-response | ||
(#'cookies/write-cookies cookies))) | ||
nil) | ||
[:headers "Set-Cookie"]))) | ||
|
||
"session-id" {:the-session "session-id"} [] | ||
"session-id" {:the-session "session-id", :ham "biscuit"} ["ham=biscuit"] | ||
"session-id" {:my-session "session-id"} ["my-session=session-id"] | ||
"session-id" {:the-session "foo"} ["the-session=foo"] | ||
"has space" {:the-session "has space"} []))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters