@@ -200,12 +200,15 @@ function session(options) {
200200 // pathname mismatch
201201 var originalPath = parseUrl . original ( req ) . pathname || '/'
202202 var resolvedCookieOptions = typeof cookieOptions === 'function' ? cookieOptions ( req ) : cookieOptions
203- if ( originalPath . indexOf ( resolvedCookieOptions . path || '/' ) !== 0 ) {
203+ var cfgPath = resolvedCookieOptions . path || '/'
204+
205+ if ( ! rfcPathMatch ( originalPath , cfgPath ) ) {
204206 debug ( 'pathname mismatch' )
205207 next ( )
206208 return
207209 }
208210
211+
209212 // ensure a secret is available or bail
210213 if ( ! secret && ! req . secret ) {
211214 next ( new Error ( 'secret option required for sessions' ) ) ;
@@ -523,6 +526,42 @@ function session(options) {
523526 } ;
524527} ;
525528
529+ /**
530+ * Check if the cookiePath matches the requestPath following the
531+ * rules in RFC 6265 section 5.1.4.
532+ *
533+ * @param {String } requestPath
534+ * @param {String } cookiePath
535+ * @return {Boolean }
536+ * @private
537+ */
538+
539+ function rfcPathMatch ( requestPath , cookiePath ) {
540+ // Normalize inputs (Node 0.8-safe)
541+ requestPath = ( typeof requestPath === 'string' && requestPath . length ) ? requestPath : '/' ;
542+ cookiePath = ( typeof cookiePath === 'string' && cookiePath . length ) ? cookiePath : '/' ;
543+
544+ // Root cookie matches everything
545+ if ( cookiePath === '/' ) return true ;
546+
547+ // Exact match
548+ if ( requestPath === cookiePath ) return true ;
549+
550+ // Prefix match
551+ if ( requestPath . indexOf ( cookiePath ) === 0 ) {
552+ // If cookiePath ends with '/', any longer requestPath is OK
553+ if ( cookiePath . charAt ( cookiePath . length - 1 ) === '/' ) return true ;
554+
555+ // Otherwise the next char after the prefix must be '/'
556+ var nextChar = requestPath . length > cookiePath . length
557+ ? requestPath . charAt ( cookiePath . length )
558+ : '' ;
559+ return nextChar === '/' ;
560+ }
561+
562+ return false ;
563+ }
564+
526565/**
527566 * Generate a session ID for a new session.
528567 *
0 commit comments