2016-02-01 20:44:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Session;
|
|
|
|
|
|
2020-06-30 15:09:24 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2021-04-29 20:44:45 +00:00
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
use TestLogger;
|
2016-02-01 20:44:03 +00:00
|
|
|
use User;
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Session
|
|
|
|
|
* @group Database
|
|
|
|
|
* @covers MediaWiki\Session\ImmutableSessionProviderWithCookie
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCase {
|
2021-05-26 15:50:28 +00:00
|
|
|
use SessionProviderTestTrait;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2021-04-29 20:44:45 +00:00
|
|
|
private function getProvider( $name, $prefix = null, $forceHTTPS = false, $logger = null ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
$config = new \HashConfig();
|
|
|
|
|
$config->set( 'CookiePrefix', 'wgCookiePrefix' );
|
Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:
* It makes the HTTP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
$config->set( 'ForceHTTPS', $forceHTTPS );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [
|
2016-02-01 20:44:03 +00:00
|
|
|
'sessionCookieName' => $name,
|
2016-02-17 09:09:32 +00:00
|
|
|
'sessionCookieOptions' => [],
|
|
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( $prefix !== null ) {
|
|
|
|
|
$params['sessionCookieOptions']['prefix'] = $prefix;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 18:53:04 +00:00
|
|
|
$provider = $this->getMockBuilder( ImmutableSessionProviderWithCookie::class )
|
2016-02-17 09:09:32 +00:00
|
|
|
->setConstructorArgs( [ $params ] )
|
2016-02-01 20:44:03 +00:00
|
|
|
->getMockForAbstractClass();
|
2021-05-26 15:50:28 +00:00
|
|
|
$this->initProvider( $provider, $logger ?? new TestLogger(), $config, new SessionManager() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
return $provider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstructor() {
|
2016-03-28 18:53:04 +00:00
|
|
|
$provider = $this->getMockBuilder( ImmutableSessionProviderWithCookie::class )
|
2016-02-01 20:44:03 +00:00
|
|
|
->getMockForAbstractClass();
|
2017-04-19 19:37:35 +00:00
|
|
|
$priv = TestingAccessWrapper::newFromObject( $provider );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertNull( $priv->sessionCookieName );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $priv->sessionCookieOptions );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-03-28 18:53:04 +00:00
|
|
|
$provider = $this->getMockBuilder( ImmutableSessionProviderWithCookie::class )
|
2016-02-17 09:09:32 +00:00
|
|
|
->setConstructorArgs( [ [
|
2016-02-01 20:44:03 +00:00
|
|
|
'sessionCookieName' => 'Foo',
|
2016-02-17 09:09:32 +00:00
|
|
|
'sessionCookieOptions' => [ 'Bar' ],
|
|
|
|
|
] ] )
|
2016-02-01 20:44:03 +00:00
|
|
|
->getMockForAbstractClass();
|
2017-04-19 19:37:35 +00:00
|
|
|
$priv = TestingAccessWrapper::newFromObject( $provider );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame( 'Foo', $priv->sessionCookieName );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [ 'Bar' ], $priv->sessionCookieOptions );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
try {
|
2016-03-28 18:53:04 +00:00
|
|
|
$provider = $this->getMockBuilder( ImmutableSessionProviderWithCookie::class )
|
2016-02-17 09:09:32 +00:00
|
|
|
->setConstructorArgs( [ [
|
2016-02-01 20:44:03 +00:00
|
|
|
'sessionCookieName' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
] ] )
|
2016-02-01 20:44:03 +00:00
|
|
|
->getMockForAbstractClass();
|
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( \InvalidArgumentException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'sessionCookieName must be a string',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2016-03-28 18:53:04 +00:00
|
|
|
$provider = $this->getMockBuilder( ImmutableSessionProviderWithCookie::class )
|
2016-02-17 09:09:32 +00:00
|
|
|
->setConstructorArgs( [ [
|
2016-02-01 20:44:03 +00:00
|
|
|
'sessionCookieOptions' => 'x',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ] )
|
2016-02-01 20:44:03 +00:00
|
|
|
->getMockForAbstractClass();
|
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( \InvalidArgumentException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'sessionCookieOptions must be an array',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBasics() {
|
|
|
|
|
$provider = $this->getProvider( null );
|
|
|
|
|
$this->assertFalse( $provider->persistsSessionID() );
|
|
|
|
|
$this->assertFalse( $provider->canChangeUser() );
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider( 'Foo' );
|
|
|
|
|
$this->assertTrue( $provider->persistsSessionID() );
|
|
|
|
|
$this->assertFalse( $provider->canChangeUser() );
|
|
|
|
|
|
|
|
|
|
$msg = $provider->whyNoSession();
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( \Message::class, $msg );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame( 'sessionprovider-nocookies', $msg->getKey() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetVaryCookies() {
|
|
|
|
|
$provider = $this->getProvider( null );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $provider->getVaryCookies() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$provider = $this->getProvider( 'Foo' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [ 'wgCookiePrefixFoo' ], $provider->getVaryCookies() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$provider = $this->getProvider( 'Foo', 'Bar' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [ 'BarFoo' ], $provider->getVaryCookies() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$provider = $this->getProvider( 'Foo', '' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [ 'Foo' ], $provider->getVaryCookies() );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetSessionIdFromCookie() {
|
|
|
|
|
$this->setMwGlobals( 'wgCookiePrefix', 'wgCookiePrefix' );
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'' => 'empty---------------------------',
|
|
|
|
|
'Foo' => 'foo-----------------------------',
|
|
|
|
|
'wgCookiePrefixFoo' => 'wgfoo---------------------------',
|
|
|
|
|
'BarFoo' => 'foobar--------------------------',
|
|
|
|
|
'bad' => 'bad',
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$provider = TestingAccessWrapper::newFromObject( $this->getProvider( null ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
try {
|
|
|
|
|
$provider->getSessionIdFromCookie( $request );
|
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( \BadMethodCallException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'MediaWiki\\Session\\ImmutableSessionProviderWithCookie::getSessionIdFromCookie ' .
|
|
|
|
|
'may not be called when $this->sessionCookieName === null',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$provider = TestingAccessWrapper::newFromObject( $this->getProvider( 'Foo' ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame(
|
|
|
|
|
'wgfoo---------------------------',
|
|
|
|
|
$provider->getSessionIdFromCookie( $request )
|
|
|
|
|
);
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$provider = TestingAccessWrapper::newFromObject( $this->getProvider( 'Foo', 'Bar' ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame(
|
|
|
|
|
'foobar--------------------------',
|
|
|
|
|
$provider->getSessionIdFromCookie( $request )
|
|
|
|
|
);
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$provider = TestingAccessWrapper::newFromObject( $this->getProvider( 'Foo', '' ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame(
|
|
|
|
|
'foo-----------------------------',
|
|
|
|
|
$provider->getSessionIdFromCookie( $request )
|
|
|
|
|
);
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$provider = TestingAccessWrapper::newFromObject( $this->getProvider( 'bad', '' ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame( null, $provider->getSessionIdFromCookie( $request ) );
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$provider = TestingAccessWrapper::newFromObject( $this->getProvider( 'none', '' ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame( null, $provider->getSessionIdFromCookie( $request ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getSentRequest() {
|
2018-01-13 00:02:09 +00:00
|
|
|
$sentResponse = $this->getMockBuilder( \FauxResponse::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'headersSent', 'setCookie', 'header' ] )
|
2017-04-05 23:39:50 +00:00
|
|
|
->getMock();
|
2021-04-22 08:28:11 +00:00
|
|
|
$sentResponse->method( 'headersSent' )
|
|
|
|
|
->willReturn( true );
|
2016-02-01 20:44:03 +00:00
|
|
|
$sentResponse->expects( $this->never() )->method( 'setCookie' );
|
|
|
|
|
$sentResponse->expects( $this->never() )->method( 'header' );
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$sentRequest = $this->getMockBuilder( \FauxRequest::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'response' ] )->getMock();
|
2021-04-22 08:28:11 +00:00
|
|
|
$sentRequest->method( 'response' )
|
|
|
|
|
->willReturn( $sentResponse );
|
2016-02-01 20:44:03 +00:00
|
|
|
return $sentRequest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider providePersistSession
|
|
|
|
|
* @param bool $secure
|
|
|
|
|
* @param bool $remember
|
Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:
* It makes the HTTP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
* @param bool $forceHTTPS
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:
* It makes the HTTP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
public function testPersistSession( $secure, $remember, $forceHTTPS ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'wgCookieExpiration' => 100,
|
|
|
|
|
'wgSecureLogin' => false,
|
Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:
* It makes the HTTP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
'wgForceHTTPS' => $forceHTTPS,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2021-04-29 20:44:45 +00:00
|
|
|
$provider = $this->getProvider( 'session', null, $forceHTTPS, new NullLogger() );
|
2017-04-19 19:37:35 +00:00
|
|
|
$priv = TestingAccessWrapper::newFromObject( $provider );
|
2016-02-17 09:09:32 +00:00
|
|
|
$priv->sessionCookieOptions = [
|
2016-02-01 20:44:03 +00:00
|
|
|
'prefix' => 'x',
|
|
|
|
|
'path' => 'CookiePath',
|
|
|
|
|
'domain' => 'CookieDomain',
|
|
|
|
|
'secure' => false,
|
|
|
|
|
'httpOnly' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
|
|
|
|
|
$user = User::newFromName( 'UTSysop' );
|
2021-11-21 19:13:24 +00:00
|
|
|
$this->assertSame( $forceHTTPS, $user->requiresHTTPS() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$backend = new SessionBackend(
|
|
|
|
|
new SessionId( $sessionId ),
|
2016-02-17 09:09:32 +00:00
|
|
|
new SessionInfo( SessionInfo::MIN_PRIORITY, [
|
2016-02-01 20:44:03 +00:00
|
|
|
'provider' => $provider,
|
|
|
|
|
'id' => $sessionId,
|
|
|
|
|
'persisted' => true,
|
|
|
|
|
'userInfo' => UserInfo::newFromUser( $user, true ),
|
|
|
|
|
'idIsSafe' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] ),
|
2016-01-30 01:09:57 +00:00
|
|
|
new TestBagOStuff(),
|
2021-04-29 20:44:45 +00:00
|
|
|
new NullLogger(),
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->createHookContainer(),
|
2016-02-01 20:44:03 +00:00
|
|
|
10
|
|
|
|
|
);
|
2017-04-19 19:37:35 +00:00
|
|
|
TestingAccessWrapper::newFromObject( $backend )->usePhpSessionHandling = false;
|
2016-02-01 20:44:03 +00:00
|
|
|
$backend->setRememberUser( $remember );
|
|
|
|
|
$backend->setForceHTTPS( $secure );
|
|
|
|
|
|
|
|
|
|
// No cookie
|
|
|
|
|
$priv->sessionCookieName = null;
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->persistSession( $backend, $request );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $request->response()->getCookies() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// Cookie
|
|
|
|
|
$priv->sessionCookieName = 'session';
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$time = time();
|
|
|
|
|
$provider->persistSession( $backend, $request );
|
|
|
|
|
|
|
|
|
|
$cookie = $request->response()->getCookieData( 'xsession' );
|
2019-12-13 14:29:10 +00:00
|
|
|
$this->assertIsArray( $cookie );
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( isset( $cookie['expire'] ) && $cookie['expire'] > 0 ) {
|
|
|
|
|
// Round expiry so we don't randomly fail if the seconds ticked during the test.
|
|
|
|
|
$cookie['expire'] = round( $cookie['expire'] - $time, -2 );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'value' => $sessionId,
|
|
|
|
|
'expire' => null,
|
|
|
|
|
'path' => 'CookiePath',
|
|
|
|
|
'domain' => 'CookieDomain',
|
Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:
* It makes the HTTP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
'secure' => $secure || $forceHTTPS,
|
2016-02-01 20:44:03 +00:00
|
|
|
'httpOnly' => true,
|
|
|
|
|
'raw' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
], $cookie );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$cookie = $request->response()->getCookieData( 'forceHTTPS' );
|
Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:
* It makes the HTTP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
if ( $secure && !$forceHTTPS ) {
|
2019-12-13 14:29:10 +00:00
|
|
|
$this->assertIsArray( $cookie );
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( isset( $cookie['expire'] ) && $cookie['expire'] > 0 ) {
|
|
|
|
|
// Round expiry so we don't randomly fail if the seconds ticked during the test.
|
|
|
|
|
$cookie['expire'] = round( $cookie['expire'] - $time, -2 );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'value' => 'true',
|
2016-05-13 00:03:20 +00:00
|
|
|
'expire' => null,
|
2016-02-01 20:44:03 +00:00
|
|
|
'path' => 'CookiePath',
|
|
|
|
|
'domain' => 'CookieDomain',
|
|
|
|
|
'secure' => false,
|
|
|
|
|
'httpOnly' => true,
|
|
|
|
|
'raw' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
], $cookie );
|
2016-02-01 20:44:03 +00:00
|
|
|
} else {
|
|
|
|
|
$this->assertNull( $cookie );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Headers sent
|
|
|
|
|
$request = $this->getSentRequest();
|
|
|
|
|
$provider->persistSession( $backend, $request );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $request->response()->getCookies() );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function providePersistSession() {
|
Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:
* It makes the HTTP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
return \ArrayUtils::cartesianProduct(
|
|
|
|
|
[ false, true ], // $secure
|
|
|
|
|
[ false, true ], // $remember
|
|
|
|
|
[ false, true ] // $forceHTTPS
|
|
|
|
|
);
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnpersistSession() {
|
2021-04-29 20:44:45 +00:00
|
|
|
$provider = $this->getProvider( 'session', '', false, new NullLogger() );
|
2017-04-19 19:37:35 +00:00
|
|
|
$priv = TestingAccessWrapper::newFromObject( $provider );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// No cookie
|
|
|
|
|
$priv->sessionCookieName = null;
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->unpersistSession( $request );
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'session', '' ) );
|
|
|
|
|
|
|
|
|
|
// Cookie
|
|
|
|
|
$priv->sessionCookieName = 'session';
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->unpersistSession( $request );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'session', '' ) );
|
|
|
|
|
|
|
|
|
|
// Headers sent
|
|
|
|
|
$request = $this->getSentRequest();
|
|
|
|
|
$provider->unpersistSession( $request );
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'session', '' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|