r62007 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62006‎ | r62007 | r62008 >
Date:04:08, 5 February 2010
Author:mah
Status:deferred
Tags:
Comment:
Add domain checking to HTTP cookie checking.
Modified paths:
  • /trunk/phase3/includes/HttpFunctions.php (modified) (history)
  • /trunk/phase3/maintenance/tests/HttpTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tests/HttpTest.php
@@ -443,24 +443,28 @@
444444 $cj = new CookieJar;
445445
446446 $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
447 - $cj->parseCookieResponseHeader( $h[0] );
 447+ $cj->parseCookieResponseHeader( $h[0], "www.example.com" );
448448 $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/", "www.example.com"));
449449
450450 $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
451 - $cj->parseCookieResponseHeader( $h[1] );
 451+ $cj->parseCookieResponseHeader( $h[1], "www.example.com" );
452452 $this->assertEquals("", $cj->serializeToHttpRequest("/", "www.example.com"));
453453 $this->assertEquals("name4=value2", $cj->serializeToHttpRequest("/path/", "www.example.com"));
454454
455455 $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
456 - $cj->parseCookieResponseHeader( $h[2] );
 456+ $cj->parseCookieResponseHeader( $h[2], "www.example.com" );
457457 $this->assertEquals("name4=value2; name5=value3", $cj->serializeToHttpRequest("/path/", "www.example.com"));
458458
459 - $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-1999 13:46:00 GMT";
460 - $cj->parseCookieResponseHeader( $h[3] );
 459+ $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
 460+ $cj->parseCookieResponseHeader( $h[3], "www.example.com" );
461461 $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
462462
 463+ $h[] = "name6=value0; domain=.example.net; path=/path/; expires=Mon, 09-Dec-1999 13:46:00 GMT";
 464+ $cj->parseCookieResponseHeader( $h[4], "www.example.net" );
 465+ $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
 466+
463467 $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
464 - $cj->parseCookieResponseHeader( $h[4] );
 468+ $cj->parseCookieResponseHeader( $h[5], "www.example.net" );
465469 $this->assertEquals("name6=value4", $cj->serializeToHttpRequest("/path/", "www.example.net"));
466470 }
467471
Index: trunk/phase3/includes/HttpFunctions.php
@@ -502,7 +502,7 @@
503503 }
504504
505505 class CookieJar {
506 - private $cookie;
 506+ private $cookie = array();
507507
508508 /**
509509 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
@@ -538,30 +538,44 @@
539539 * Parse the content of an Set-Cookie HTTP Response header.
540540 * @param $cookie string
541541 */
542 - public function parseCookieResponseHeader ( $cookie, $domain = null ) {
 542+ public function parseCookieResponseHeader ( $cookie, $domain ) {
543543 $len = strlen( "Set-Cookie:" );
544544 if ( substr_compare( "Set-Cookie:", $cookie, 0, $len, TRUE ) === 0 ) {
545545 $cookie = substr( $cookie, $len );
546546 }
547547
548548 $bit = array_map( 'trim', explode( ";", $cookie ) );
549 - list($name, $value) = explode( "=", array_shift( $bit ), 2 );
550 - $attr = array();
551 - foreach( $bit as $piece ) {
552 - $parts = explode( "=", $piece );
553 - if( count( $parts ) > 1 ) {
554 - $attr[strtolower( $parts[0] )] = $parts[1];
 549+ if ( count($bit) >= 1 ) {
 550+ list($name, $value) = explode( "=", array_shift( $bit ), 2 );
 551+ $attr = array();
 552+ foreach( $bit as $piece ) {
 553+ $parts = explode( "=", $piece );
 554+ if( count( $parts ) > 1 ) {
 555+ $attr[strtolower( $parts[0] )] = $parts[1];
 556+ } else {
 557+ $attr[strtolower( $parts[0] )] = true;
 558+ }
 559+ }
 560+
 561+ if( !isset( $attr['domain'] ) ) {
 562+ $attr['domain'] = $domain;
555563 } else {
556 - $attr[strtolower( $parts[0] )] = true;
 564+ /* If domain is given, it has to contain at least two dots */
 565+ if ( strrpos( $attr['domain'], '.' ) === false
 566+ || strrpos( $attr['domain'], '.' ) === strpos( $attr['domain'], '.' ) ) {
 567+ return;
 568+ }
 569+ if ( substr( $attr['domain'], 0, 1 ) === '.' ) {
 570+ $attr['domain'] = substr( $attr['domain'], 1 );
 571+ }
 572+ if ( strlen( $attr['domain'] ) < strlen( $domain )
 573+ && substr_compare( $domain, $attr['domain'], -strlen( $attr['domain'] ),
 574+ strlen( $attr['domain'] ), TRUE ) != 0 ) {
 575+ return; /* silently reject a bad cookie */
 576+ }
557577 }
 578+ $this->setCookie( $name, $value, $attr );
558579 }
559 -
560 - if( !isset( $attr['domain'] ) ) {
561 - $attr['domain'] = $domain;
562 - } else {
563 - /* FIXME: Check that domain is valid */
564 - }
565 - $this->setCookie( $name, $value, $attr );
566580 }
567581 }
568582

Status & tagging log