Skip to content

Allow OPTIONS * requests to pass cookie path check for default setting #977

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
23 changes: 21 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ var defer = typeof setImmediate === 'function'
? setImmediate
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }

/**
* Verifies if the original request path matches the path specified in the cookie options.
* The main purpose is to ensure that the session middleware processes requests that are intended for paths
* that match the cookie path.
*
* A special case is handled for OPTIONS requests with a wildcard '*', which is considered a match only
* if the cookie path is the default '/'. This is due to the semantics of HTTP that allows '*' as a wildcard
* for all paths in OPTIONS requests.
*
* @param {string} originalPath - The original request path.
* @param {string} cookiePath - The path for which the cookie is set.
* @returns {boolean} Returns true if the original path matches the cookie path, false otherwise.
*/
function verifyPath(originalPath, cookiePath) {
if (originalPath === '*') {
return cookiePath === '/';
}
return originalPath.indexOf(cookiePath || '/') === 0;
}

/**
* Setup session store with the given `options`.
*
Expand Down Expand Up @@ -190,10 +210,9 @@ function session(options) {
next()
return
}

// pathname mismatch
var originalPath = parseUrl.original(req).pathname || '/'
if (originalPath.indexOf(cookieOptions.path || '/') !== 0) {
if (!verifyPath(originalPath, cookieOptions.path)) {
debug('pathname mismatch')
next()
return
Expand Down