Skip to content

Commit

Permalink
Added a path to all the flash cookies. Changed flash data cookies to …
Browse files Browse the repository at this point in the history
…always use the same name.
  • Loading branch information
Davis Clark committed Nov 16, 2011
1 parent 28e3663 commit a675776
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions lib/flash/flashCookieDataProvider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var security = require("../security"),
util = require("../util"),
DEFAULT_ENCRYPTION_KEY = "a720efa0-04e8-11e1-a8b5-000c290196f7";
DEFAULT_ENCRYPTION_KEY = "a720efa0-04e8-11e1-a8b5-000c290196f7",
COOKIE_NAME = "jsgi_flash_data";



Expand All @@ -13,8 +14,8 @@ function CookieDataProvider(options) {

CookieDataProvider.prototype.previousFlash = function(req, flashId) {
var cookies = util.parseCookies(req);
if (cookies && cookies[flashId]) {
return JSON.parse(security.decrypt(decodeURIComponent(cookies[flashId]), this.options.encryptionKey || DEFAULT_ENCRYPTION_KEY));
if (cookies && cookies[COOKIE_NAME]) {
return JSON.parse(security.decrypt(decodeURIComponent(cookies[COOKIE_NAME]), this.options.encryptionKey || DEFAULT_ENCRYPTION_KEY));
} else return null;
};

Expand All @@ -35,7 +36,7 @@ CookieDataProvider.prototype.finalize = function(req, res, flashId) {
var data = this.flashData[flashId];
delete this.flashData[flashId];

var cookie = flashId + "=" + encodeURIComponent(security.encrypt(JSON.stringify(data), this.options.encryptionKey || DEFAULT_ENCRYPTION_KEY)) + ";";
var cookie = COOKIE_NAME + "=" + encodeURIComponent(security.encrypt(JSON.stringify(data), this.options.encryptionKey || DEFAULT_ENCRYPTION_KEY)) + ";Path=/;";

if (Object.prototype.toString.call(res.headers["Set-Cookie"]) === "[object String]") {
var existing = res.headers["Set-Cookie"];
Expand All @@ -54,10 +55,10 @@ CookieDataProvider.prototype.clear = function(req, res, flashId) {
res.headers["Set-Cookie"] = res.headers["Set-Cookie"] || [];

var cookies = util.parseCookies(req);
if (cookies && cookies[flashId]) {
if (cookies && cookies[COOKIE_NAME]) {
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
var clearCookie = flashId + "=expiring; Expires=" + yesterday.toUTCString() + ";";
var clearCookie = COOKIE_NAME + "=expiring; Expires=" + yesterday.toUTCString() + ";Path=/;";

if (Object.prototype.toString.call(res.headers["Set-Cookie"]) === "[object String]") {
var existing = res.headers["Set-Cookie"];
Expand All @@ -66,7 +67,7 @@ CookieDataProvider.prototype.clear = function(req, res, flashId) {
}

res.headers["Set-Cookie"] = res.headers["Set-Cookie"].filter(function(el) {
return !(new RegExp("^" + flashId + "=").test(el));
return !(new RegExp("^" + COOKIE_NAME + "=").test(el));
});
res.headers["Set-Cookie"].push(clearCookie);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/flash/flashIdCookieProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CookieIdProvider.prototype.finalize = function(req, res, flashId) {
res = res || {};
res.headers = res.headers || {};
res.headers["Set-Cookie"] = res.headers["Set-Cookie"] || [];
var cookie = flashCookieName + "=" + flashId + ";";
var cookie = flashCookieName + "=" + flashId + ";Path=/;";

if (Object.prototype.toString.call(res.headers["Set-Cookie"]) === "[object String]") {
var existing = res.headers["Set-Cookie"];
Expand All @@ -49,7 +49,7 @@ CookieIdProvider.prototype.clear = function(req, res, flashId) {
if (cookies && cookies[flashCookieName]) {
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
var clearCookie = flashCookieName + "=expiring; Expires=" + yesterday.toUTCString() + ";";
var clearCookie = flashCookieName + "=expiring; Expires=" + yesterday.toUTCString() + ";Path=/;";

if (Object.prototype.toString.call(res.headers["Set-Cookie"]) === "[object String]") {
var existing = res.headers["Set-Cookie"];
Expand Down

0 comments on commit a675776

Please sign in to comment.