2016-02-01 20:44:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Session;
|
|
|
|
|
|
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
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-06-30 15:09:24 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2016-02-02 16:37:27 +00:00
|
|
|
use Psr\Log\LogLevel;
|
2020-01-10 00:00:51 +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
|
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
|
|
|
* @covers \MediaWiki\Session\CookieSessionProvider
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
private function getConfig() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return new \HashConfig( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'CookiePrefix' => 'CookiePrefix',
|
|
|
|
|
'CookiePath' => 'CookiePath',
|
|
|
|
|
'CookieDomain' => 'CookieDomain',
|
|
|
|
|
'CookieSecure' => true,
|
|
|
|
|
'CookieHttpOnly' => true,
|
2020-07-01 00:01:00 +00:00
|
|
|
'CookieSameSite' => '',
|
2016-02-01 20:44:03 +00:00
|
|
|
'SessionName' => false,
|
2016-05-13 00:03:20 +00:00
|
|
|
'CookieExpiration' => 100,
|
|
|
|
|
'ExtendedLoginCookieExpiration' => 200,
|
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
|
|
|
'ForceHTTPS' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* @return HookContainer
|
|
|
|
|
*/
|
|
|
|
|
private function getHookContainer() {
|
|
|
|
|
// Need a real HookContainer for testPersistSession() which modifies $wgHooks
|
|
|
|
|
return MediaWikiServices::getInstance()->getHookContainer();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* Provider for testing both values of $wgForceHTTPS
|
|
|
|
|
*/
|
|
|
|
|
public static function provideForceHTTPS() {
|
|
|
|
|
return [
|
|
|
|
|
[ false ],
|
|
|
|
|
[ true ]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
public function testConstructor() {
|
|
|
|
|
try {
|
|
|
|
|
new CookieSessionProvider();
|
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( \InvalidArgumentException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'MediaWiki\\Session\\CookieSessionProvider::__construct: priority must be specified',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2016-02-17 09:09:32 +00:00
|
|
|
new CookieSessionProvider( [ 'priority' => 'foo' ] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( \InvalidArgumentException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
try {
|
2016-02-17 09:09:32 +00:00
|
|
|
new CookieSessionProvider( [ 'priority' => SessionInfo::MIN_PRIORITY - 1 ] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( \InvalidArgumentException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
try {
|
2016-02-17 09:09:32 +00:00
|
|
|
new CookieSessionProvider( [ 'priority' => SessionInfo::MAX_PRIORITY + 1 ] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( \InvalidArgumentException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2016-02-17 09:09:32 +00:00
|
|
|
new CookieSessionProvider( [ 'priority' => 1, 'cookieOptions' => null ] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( \InvalidArgumentException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'MediaWiki\\Session\\CookieSessionProvider::__construct: cookieOptions must be an array',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$config = $this->getConfig();
|
2017-04-19 19:37:35 +00:00
|
|
|
$p = TestingAccessWrapper::newFromObject(
|
2016-02-17 09:09:32 +00:00
|
|
|
new CookieSessionProvider( [ 'priority' => 1 ] )
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
$p->setLogger( new \TestLogger() );
|
|
|
|
|
$p->setConfig( $config );
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, $p->priority );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'callUserSetCookiesHook' => false,
|
|
|
|
|
'sessionName' => 'CookiePrefix_session',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $p->params );
|
|
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'prefix' => 'CookiePrefix',
|
|
|
|
|
'path' => 'CookiePath',
|
|
|
|
|
'domain' => 'CookieDomain',
|
|
|
|
|
'secure' => true,
|
|
|
|
|
'httpOnly' => true,
|
2020-07-01 00:01:00 +00:00
|
|
|
'sameSite' => '',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $p->cookieOptions );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$config->set( 'SessionName', 'SessionName' );
|
2017-04-19 19:37:35 +00:00
|
|
|
$p = TestingAccessWrapper::newFromObject(
|
2016-02-17 09:09:32 +00:00
|
|
|
new CookieSessionProvider( [ 'priority' => 3 ] )
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
$p->setLogger( new \TestLogger() );
|
|
|
|
|
$p->setConfig( $config );
|
|
|
|
|
$this->assertEquals( 3, $p->priority );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'callUserSetCookiesHook' => false,
|
|
|
|
|
'sessionName' => 'SessionName',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $p->params );
|
|
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'prefix' => 'CookiePrefix',
|
|
|
|
|
'path' => 'CookiePath',
|
|
|
|
|
'domain' => 'CookieDomain',
|
|
|
|
|
'secure' => true,
|
|
|
|
|
'httpOnly' => true,
|
2020-07-01 00:01:00 +00:00
|
|
|
'sameSite' => '',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $p->cookieOptions );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$p = TestingAccessWrapper::newFromObject( new CookieSessionProvider( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 10,
|
|
|
|
|
'callUserSetCookiesHook' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
'cookieOptions' => [
|
2016-02-01 20:44:03 +00:00
|
|
|
'prefix' => 'XPrefix',
|
|
|
|
|
'path' => 'XPath',
|
|
|
|
|
'domain' => 'XDomain',
|
|
|
|
|
'secure' => 'XSecure',
|
|
|
|
|
'httpOnly' => 'XHttpOnly',
|
2020-07-01 00:01:00 +00:00
|
|
|
'sameSite' => 'XSameSite',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2016-02-01 20:44:03 +00:00
|
|
|
'sessionName' => 'XSession',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
$p->setLogger( new \TestLogger() );
|
|
|
|
|
$p->setConfig( $config );
|
|
|
|
|
$this->assertEquals( 10, $p->priority );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'callUserSetCookiesHook' => true,
|
|
|
|
|
'sessionName' => 'XSession',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $p->params );
|
|
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'prefix' => 'XPrefix',
|
|
|
|
|
'path' => 'XPath',
|
|
|
|
|
'domain' => 'XDomain',
|
|
|
|
|
'secure' => 'XSecure',
|
|
|
|
|
'httpOnly' => 'XHttpOnly',
|
2020-07-01 00:01:00 +00:00
|
|
|
'sameSite' => 'XSameSite',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $p->cookieOptions );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBasics() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$provider = new CookieSessionProvider( [ 'priority' => 10 ] );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-03-19 00:08:06 +00:00
|
|
|
$this->assertTrue( $provider->persistsSessionId() );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertTrue( $provider->canChangeUser() );
|
|
|
|
|
|
2016-06-22 16:36:16 +00:00
|
|
|
$extendedCookies = [ 'UserID', 'UserName', 'Token' ];
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$extendedCookies,
|
2017-04-19 19:37:35 +00:00
|
|
|
TestingAccessWrapper::newFromObject( $provider )->getExtendedLoginCookies(),
|
2016-06-22 16:36:16 +00:00
|
|
|
'List of extended cookies (subclasses can add values, but we\'re calling the core one here)'
|
|
|
|
|
);
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
$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 testProvideSessionInfo() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 20,
|
|
|
|
|
'sessionName' => 'session',
|
2016-02-17 09:09:32 +00:00
|
|
|
'cookieOptions' => [ 'prefix' => 'x' ],
|
|
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider = new CookieSessionProvider( $params );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger = new \TestLogger( true );
|
|
|
|
|
$provider->setLogger( $logger );
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider->setConfig( $this->getConfig() );
|
|
|
|
|
$provider->setManager( new SessionManager() );
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = static::getTestSysop()->getUser();
|
2016-02-01 20:44:03 +00:00
|
|
|
$id = $user->getId();
|
|
|
|
|
$name = $user->getName();
|
|
|
|
|
$token = $user->getToken( true );
|
|
|
|
|
|
2020-05-11 17:38:16 +00:00
|
|
|
$this->hideDeprecated(
|
|
|
|
|
'UserSetCookies hook (used in '
|
|
|
|
|
. get_class( $this->getMockBuilder( __CLASS__ ) )
|
|
|
|
|
. '::onUserSetCookies)'
|
|
|
|
|
);
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
|
|
|
|
|
|
|
|
|
|
// No data
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNull( $info );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// Session key only
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'session' => $sessionId,
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNotNull( $info );
|
|
|
|
|
$this->assertSame( $params['priority'], $info->getPriority() );
|
|
|
|
|
$this->assertSame( $sessionId, $info->getId() );
|
|
|
|
|
$this->assertNotNull( $info->getUserInfo() );
|
|
|
|
|
$this->assertSame( 0, $info->getUserInfo()->getId() );
|
|
|
|
|
$this->assertNull( $info->getUserInfo()->getName() );
|
|
|
|
|
$this->assertFalse( $info->forceHTTPS() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
|
[
|
2016-02-02 16:37:27 +00:00
|
|
|
LogLevel::DEBUG,
|
|
|
|
|
'Session "{session}" requested without UserID cookie',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// User, no session key
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'xUserID' => $id,
|
|
|
|
|
'xToken' => $token,
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNotNull( $info );
|
|
|
|
|
$this->assertSame( $params['priority'], $info->getPriority() );
|
|
|
|
|
$this->assertNotSame( $sessionId, $info->getId() );
|
|
|
|
|
$this->assertNotNull( $info->getUserInfo() );
|
|
|
|
|
$this->assertSame( $id, $info->getUserInfo()->getId() );
|
|
|
|
|
$this->assertSame( $name, $info->getUserInfo()->getName() );
|
|
|
|
|
$this->assertFalse( $info->forceHTTPS() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// User and session key
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'session' => $sessionId,
|
|
|
|
|
'xUserID' => $id,
|
|
|
|
|
'xToken' => $token,
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNotNull( $info );
|
|
|
|
|
$this->assertSame( $params['priority'], $info->getPriority() );
|
|
|
|
|
$this->assertSame( $sessionId, $info->getId() );
|
|
|
|
|
$this->assertNotNull( $info->getUserInfo() );
|
|
|
|
|
$this->assertSame( $id, $info->getUserInfo()->getId() );
|
|
|
|
|
$this->assertSame( $name, $info->getUserInfo()->getName() );
|
|
|
|
|
$this->assertFalse( $info->forceHTTPS() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// User with bad token
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'session' => $sessionId,
|
|
|
|
|
'xUserID' => $id,
|
|
|
|
|
'xToken' => 'BADTOKEN',
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNull( $info );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
|
[
|
2016-02-02 16:37:27 +00:00
|
|
|
LogLevel::WARNING,
|
|
|
|
|
'Session "{session}" requested with invalid Token cookie.'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// User id with no token
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'session' => $sessionId,
|
|
|
|
|
'xUserID' => $id,
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNotNull( $info );
|
|
|
|
|
$this->assertSame( $params['priority'], $info->getPriority() );
|
|
|
|
|
$this->assertSame( $sessionId, $info->getId() );
|
|
|
|
|
$this->assertNotNull( $info->getUserInfo() );
|
|
|
|
|
$this->assertFalse( $info->getUserInfo()->isVerified() );
|
|
|
|
|
$this->assertSame( $id, $info->getUserInfo()->getId() );
|
|
|
|
|
$this->assertSame( $name, $info->getUserInfo()->getName() );
|
|
|
|
|
$this->assertFalse( $info->forceHTTPS() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'xUserID' => $id,
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNull( $info );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// User and session key, with forceHTTPS flag
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'session' => $sessionId,
|
|
|
|
|
'xUserID' => $id,
|
|
|
|
|
'xToken' => $token,
|
|
|
|
|
'forceHTTPS' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNotNull( $info );
|
|
|
|
|
$this->assertSame( $params['priority'], $info->getPriority() );
|
|
|
|
|
$this->assertSame( $sessionId, $info->getId() );
|
|
|
|
|
$this->assertNotNull( $info->getUserInfo() );
|
|
|
|
|
$this->assertSame( $id, $info->getUserInfo()->getId() );
|
|
|
|
|
$this->assertSame( $name, $info->getUserInfo()->getName() );
|
|
|
|
|
$this->assertTrue( $info->forceHTTPS() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// Invalid user id
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'session' => $sessionId,
|
|
|
|
|
'xUserID' => '-1',
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNull( $info );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// User id with matching name
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'session' => $sessionId,
|
|
|
|
|
'xUserID' => $id,
|
|
|
|
|
'xUserName' => $name,
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNotNull( $info );
|
|
|
|
|
$this->assertSame( $params['priority'], $info->getPriority() );
|
|
|
|
|
$this->assertSame( $sessionId, $info->getId() );
|
|
|
|
|
$this->assertNotNull( $info->getUserInfo() );
|
|
|
|
|
$this->assertFalse( $info->getUserInfo()->isVerified() );
|
|
|
|
|
$this->assertSame( $id, $info->getUserInfo()->getId() );
|
|
|
|
|
$this->assertSame( $name, $info->getUserInfo()->getName() );
|
|
|
|
|
$this->assertFalse( $info->forceHTTPS() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// User id with wrong name
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'session' => $sessionId,
|
|
|
|
|
'xUserID' => $id,
|
|
|
|
|
'xUserName' => 'Wrong',
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info = $provider->provideSessionInfo( $request );
|
|
|
|
|
$this->assertNull( $info );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
|
[
|
2016-02-02 16:37:27 +00:00
|
|
|
LogLevel::WARNING,
|
|
|
|
|
'Session "{session}" requested with mismatched UserID and UserName cookies.',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
], $logger->getBuffer() );
|
2016-02-02 16:37:27 +00:00
|
|
|
$logger->clearBuffer();
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetVaryCookies() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$provider = new CookieSessionProvider( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 1,
|
|
|
|
|
'sessionName' => 'MySessionName',
|
2016-02-17 09:09:32 +00:00
|
|
|
'cookieOptions' => [ 'prefix' => 'MyCookiePrefix' ],
|
|
|
|
|
] );
|
|
|
|
|
$this->assertArrayEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'MyCookiePrefixToken',
|
|
|
|
|
'MyCookiePrefixLoggedOut',
|
|
|
|
|
'MySessionName',
|
|
|
|
|
'forceHTTPS',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $provider->getVaryCookies() );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSuggestLoginUsername() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$provider = new CookieSessionProvider( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 1,
|
|
|
|
|
'sessionName' => 'MySessionName',
|
2016-02-17 09:09:32 +00:00
|
|
|
'cookieOptions' => [ 'prefix' => 'x' ],
|
|
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$request = new \FauxRequest();
|
2019-09-17 14:03:28 +00:00
|
|
|
$this->assertNull( $provider->suggestLoginUsername( $request ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'xUserName' => 'Example',
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertEquals( 'Example', $provider->suggestLoginUsername( $request ) );
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
/** @dataProvider provideForceHTTPS */
|
|
|
|
|
public function testPersistSession( $forceHTTPS ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$provider = new CookieSessionProvider( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 1,
|
|
|
|
|
'sessionName' => 'MySessionName',
|
|
|
|
|
'callUserSetCookiesHook' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
'cookieOptions' => [ 'prefix' => 'x' ],
|
|
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$config = $this->getConfig();
|
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 );
|
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
|
|
|
$hookContainer = $this->getHookContainer();
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider->setLogger( new \TestLogger() );
|
|
|
|
|
$provider->setConfig( $config );
|
|
|
|
|
$provider->setManager( SessionManager::singleton() );
|
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
|
|
|
$provider->setHookContainer( $hookContainer );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
|
2016-01-30 01:09:57 +00:00
|
|
|
$store = new TestBagOStuff();
|
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
|
|
|
|
|
|
|
|
// For User::requiresHTTPS
|
|
|
|
|
$this->setMwGlobals( [ 'wgForceHTTPS' => $forceHTTPS ] );
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = static::getTestSysop()->getUser();
|
2016-02-01 20:44:03 +00:00
|
|
|
$anon = new User;
|
|
|
|
|
|
|
|
|
|
$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,
|
|
|
|
|
'idIsSafe' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] ),
|
2016-02-01 20:44:03 +00:00
|
|
|
$store,
|
|
|
|
|
new \Psr\Log\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
|
|
|
$hookContainer,
|
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
|
|
|
|
2021-03-20 15:18:58 +00:00
|
|
|
$mock = $this->getMockBuilder( \stdClass::class )
|
|
|
|
|
->addMethods( [ 'onUserSetCookies' ] )
|
2017-04-05 23:39:50 +00:00
|
|
|
->getMock();
|
2016-02-01 20:44:03 +00:00
|
|
|
$mock->expects( $this->never() )->method( 'onUserSetCookies' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// Anonymous user
|
|
|
|
|
$backend->setUser( $anon );
|
|
|
|
|
$backend->setRememberUser( true );
|
|
|
|
|
$backend->setForceHTTPS( false );
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->persistSession( $backend, $request );
|
|
|
|
|
$this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'xUserName' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
|
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 ( $forceHTTPS ) {
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'forceHTTPS' ) );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $backend->getData() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// Logged-in user, no remember
|
|
|
|
|
$backend->setUser( $user );
|
|
|
|
|
$backend->setRememberUser( false );
|
|
|
|
|
$backend->setForceHTTPS( false );
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->persistSession( $backend, $request );
|
|
|
|
|
$this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
|
|
|
|
|
$this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
|
|
|
|
|
$this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
|
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 ( $forceHTTPS ) {
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'forceHTTPS' ) );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $backend->getData() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
// Logged-in user, remember
|
|
|
|
|
$backend->setUser( $user );
|
|
|
|
|
$backend->setRememberUser( true );
|
|
|
|
|
$backend->setForceHTTPS( true );
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$time = time();
|
|
|
|
|
$provider->persistSession( $backend, $request );
|
|
|
|
|
$this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
|
|
|
|
|
$this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
|
|
|
|
|
$this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
|
|
|
|
|
$this->assertSame( $user->getToken(), $request->response()->getCookie( 'xToken' ) );
|
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 ( $forceHTTPS ) {
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'forceHTTPS' ) );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertSame( 'true', $request->response()->getCookie( 'forceHTTPS' ) );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $backend->getData() );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideCookieData
|
|
|
|
|
* @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 testCookieData( $secure, $remember, $forceHTTPS ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'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
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$provider = new CookieSessionProvider( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 1,
|
|
|
|
|
'sessionName' => 'MySessionName',
|
|
|
|
|
'callUserSetCookiesHook' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
'cookieOptions' => [ 'prefix' => 'x' ],
|
|
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$config = $this->getConfig();
|
|
|
|
|
$config->set( 'CookieSecure', $secure );
|
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 );
|
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
|
|
|
$hookContainer = $this->getHookContainer();
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider->setLogger( new \TestLogger() );
|
|
|
|
|
$provider->setConfig( $config );
|
|
|
|
|
$provider->setManager( SessionManager::singleton() );
|
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
|
|
|
$provider->setHookContainer( $hookContainer );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = static::getTestSysop()->getUser();
|
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
|
|
|
$this->assertSame( $user->requiresHTTPS(), $forceHTTPS, 'sanity check' );
|
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,
|
|
|
|
|
'idIsSafe' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] ),
|
2016-01-30 01:09:57 +00:00
|
|
|
new TestBagOStuff(),
|
2016-02-01 20:44:03 +00:00
|
|
|
new \Psr\Log\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
|
|
|
$hookContainer,
|
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->setUser( $user );
|
|
|
|
|
$backend->setRememberUser( $remember );
|
|
|
|
|
$backend->setForceHTTPS( $secure );
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$time = time();
|
|
|
|
|
$provider->persistSession( $backend, $request );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$defaults = [
|
2016-02-01 20:44:03 +00:00
|
|
|
'expire' => (int)100,
|
|
|
|
|
'path' => $config->get( 'CookiePath' ),
|
|
|
|
|
'domain' => $config->get( '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' => $config->get( 'CookieHttpOnly' ),
|
|
|
|
|
'raw' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-06-22 16:36:16 +00:00
|
|
|
|
|
|
|
|
$normalExpiry = $config->get( 'CookieExpiration' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$extendedExpiry = $config->get( 'ExtendedLoginCookieExpiration' );
|
2018-06-20 05:26:57 +00:00
|
|
|
$extendedExpiry = (int)( $extendedExpiry ?? 0 );
|
2016-02-17 09:09:32 +00:00
|
|
|
$expect = [
|
|
|
|
|
'MySessionName' => [
|
2016-02-01 20:44:03 +00:00
|
|
|
'value' => (string)$sessionId,
|
|
|
|
|
'expire' => 0,
|
2016-02-17 09:09:32 +00:00
|
|
|
] + $defaults,
|
|
|
|
|
'xUserID' => [
|
2016-02-01 20:44:03 +00:00
|
|
|
'value' => (string)$user->getId(),
|
2016-06-22 16:36:16 +00:00
|
|
|
'expire' => $remember ? $extendedExpiry : $normalExpiry,
|
2016-02-17 09:09:32 +00:00
|
|
|
] + $defaults,
|
|
|
|
|
'xUserName' => [
|
2016-02-01 20:44:03 +00:00
|
|
|
'value' => $user->getName(),
|
2016-06-22 16:36:16 +00:00
|
|
|
'expire' => $remember ? $extendedExpiry : $normalExpiry
|
2016-02-17 09:09:32 +00:00
|
|
|
] + $defaults,
|
|
|
|
|
'xToken' => [
|
2016-02-01 20:44:03 +00:00
|
|
|
'value' => $remember ? $user->getToken() : '',
|
|
|
|
|
'expire' => $remember ? $extendedExpiry : -31536000,
|
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
|
|
|
] + $defaults
|
|
|
|
|
];
|
|
|
|
|
if ( !$forceHTTPS ) {
|
|
|
|
|
$expect['forceHTTPS'] = [
|
2016-02-01 20:44:03 +00:00
|
|
|
'value' => $secure ? 'true' : '',
|
|
|
|
|
'secure' => false,
|
2018-07-30 11:15:55 +00:00
|
|
|
'expire' => $secure ? ( $remember ? $defaults['expire'] : 0 ) : -31536000,
|
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
|
|
|
] + $defaults;
|
|
|
|
|
}
|
2016-02-01 20:44:03 +00:00
|
|
|
foreach ( $expect as $key => $value ) {
|
|
|
|
|
$actual = $request->response()->getCookieData( $key );
|
|
|
|
|
if ( $actual && $actual['expire'] > 0 ) {
|
|
|
|
|
// Round expiry so we don't randomly fail if the seconds ticked during the test.
|
|
|
|
|
$actual['expire'] = round( $actual['expire'] - $time, -2 );
|
|
|
|
|
}
|
|
|
|
|
$this->assertEquals( $value, $actual, "Cookie $key" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideCookieData() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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' ] )->getMock();
|
2016-02-01 20:44:03 +00:00
|
|
|
$sentResponse->expects( $this->any() )->method( 'headersSent' )
|
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
|
$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();
|
2016-02-01 20:44:03 +00:00
|
|
|
$sentRequest->expects( $this->any() )->method( 'response' )
|
|
|
|
|
->will( $this->returnValue( $sentResponse ) );
|
|
|
|
|
return $sentRequest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPersistSessionWithHook() {
|
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
|
|
|
$hookContainer = $this->getHookContainer();
|
2016-02-17 09:09:32 +00:00
|
|
|
$provider = new CookieSessionProvider( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 1,
|
|
|
|
|
'sessionName' => 'MySessionName',
|
|
|
|
|
'callUserSetCookiesHook' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
'cookieOptions' => [ 'prefix' => 'x' ],
|
|
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider->setLogger( new \Psr\Log\NullLogger() );
|
|
|
|
|
$provider->setConfig( $this->getConfig() );
|
|
|
|
|
$provider->setManager( SessionManager::singleton() );
|
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
|
|
|
$provider->setHookContainer( $hookContainer );
|
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
|
|
|
// For User::requiresHTTPS
|
|
|
|
|
$this->setMwGlobals( [ 'wgForceHTTPS' => false ] );
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
|
2016-01-30 01:09:57 +00:00
|
|
|
$store = new TestBagOStuff();
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = static::getTestSysop()->getUser();
|
2016-02-01 20:44:03 +00:00
|
|
|
$anon = new User;
|
|
|
|
|
|
|
|
|
|
$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,
|
|
|
|
|
'idIsSafe' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] ),
|
2016-02-01 20:44:03 +00:00
|
|
|
$store,
|
|
|
|
|
new \Psr\Log\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
|
|
|
$hookContainer,
|
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
|
|
|
|
|
|
|
|
// Anonymous user
|
2021-03-20 15:18:58 +00:00
|
|
|
$mock = $this->getMockBuilder( \stdClass::class )
|
|
|
|
|
->addMethods( [ 'onUserSetCookies' ] )->getMock();
|
2016-02-01 20:44:03 +00:00
|
|
|
$mock->expects( $this->never() )->method( 'onUserSetCookies' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$backend->setUser( $anon );
|
|
|
|
|
$backend->setRememberUser( true );
|
|
|
|
|
$backend->setForceHTTPS( false );
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->persistSession( $backend, $request );
|
|
|
|
|
$this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'xUserName' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $backend->getData() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$provider->persistSession( $backend, $this->getSentRequest() );
|
|
|
|
|
|
|
|
|
|
// Logged-in user, no remember
|
2017-04-05 23:39:50 +00:00
|
|
|
$mock = $this->getMockBuilder( __CLASS__ )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'onUserSetCookies' ] )->getMock();
|
2016-02-01 20:44:03 +00:00
|
|
|
$mock->expects( $this->once() )->method( 'onUserSetCookies' )
|
2016-02-10 17:10:38 +00:00
|
|
|
->will( $this->returnCallback( function ( $u, &$sessionData, &$cookies ) use ( $user ) {
|
|
|
|
|
$this->assertSame( $user, $u );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'wsUserID' => $user->getId(),
|
|
|
|
|
'wsUserName' => $user->getName(),
|
|
|
|
|
'wsToken' => $user->getToken(),
|
2016-02-17 09:09:32 +00:00
|
|
|
], $sessionData );
|
|
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'UserID' => $user->getId(),
|
|
|
|
|
'UserName' => $user->getName(),
|
|
|
|
|
'Token' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
], $cookies );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$sessionData['foo'] = 'foo!';
|
|
|
|
|
$cookies['bar'] = 'bar!';
|
|
|
|
|
return true;
|
|
|
|
|
} ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$backend->setUser( $user );
|
|
|
|
|
$backend->setRememberUser( false );
|
|
|
|
|
$backend->setForceHTTPS( false );
|
|
|
|
|
$backend->setLoggedOutTimestamp( $loggedOut = time() );
|
|
|
|
|
$request = new \FauxRequest();
|
2020-05-11 17:38:16 +00:00
|
|
|
|
|
|
|
|
$this->hideDeprecated( 'UserSetCookies hook (used in onUserSetCookies)' );
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider->persistSession( $backend, $request );
|
|
|
|
|
$this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
|
|
|
|
|
$this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
|
|
|
|
|
$this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
|
|
|
|
|
$this->assertSame( 'bar!', $request->response()->getCookie( 'xbar' ) );
|
|
|
|
|
$this->assertSame( (string)$loggedOut, $request->response()->getCookie( 'xLoggedOut' ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'wsUserID' => $user->getId(),
|
|
|
|
|
'wsUserName' => $user->getName(),
|
|
|
|
|
'wsToken' => $user->getToken(),
|
|
|
|
|
'foo' => 'foo!',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $backend->getData() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$provider->persistSession( $backend, $this->getSentRequest() );
|
|
|
|
|
|
|
|
|
|
// Logged-in user, remember
|
2017-04-05 23:39:50 +00:00
|
|
|
$mock = $this->getMockBuilder( __CLASS__ )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'onUserSetCookies' ] )->getMock();
|
2016-02-01 20:44:03 +00:00
|
|
|
$mock->expects( $this->once() )->method( 'onUserSetCookies' )
|
2016-02-10 17:10:38 +00:00
|
|
|
->will( $this->returnCallback( function ( $u, &$sessionData, &$cookies ) use ( $user ) {
|
|
|
|
|
$this->assertSame( $user, $u );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'wsUserID' => $user->getId(),
|
|
|
|
|
'wsUserName' => $user->getName(),
|
|
|
|
|
'wsToken' => $user->getToken(),
|
2016-02-17 09:09:32 +00:00
|
|
|
], $sessionData );
|
|
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'UserID' => $user->getId(),
|
|
|
|
|
'UserName' => $user->getName(),
|
|
|
|
|
'Token' => $user->getToken(),
|
2016-02-17 09:09:32 +00:00
|
|
|
], $cookies );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$sessionData['foo'] = 'foo 2!';
|
|
|
|
|
$cookies['bar'] = 'bar 2!';
|
|
|
|
|
return true;
|
|
|
|
|
} ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$backend->setUser( $user );
|
|
|
|
|
$backend->setRememberUser( true );
|
|
|
|
|
$backend->setForceHTTPS( true );
|
|
|
|
|
$backend->setLoggedOutTimestamp( 0 );
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->persistSession( $backend, $request );
|
|
|
|
|
$this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
|
|
|
|
|
$this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
|
|
|
|
|
$this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
|
|
|
|
|
$this->assertSame( $user->getToken(), $request->response()->getCookie( 'xToken' ) );
|
|
|
|
|
$this->assertSame( 'true', $request->response()->getCookie( 'forceHTTPS' ) );
|
|
|
|
|
$this->assertSame( 'bar 2!', $request->response()->getCookie( 'xbar' ) );
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'wsUserID' => $user->getId(),
|
|
|
|
|
'wsUserName' => $user->getName(),
|
|
|
|
|
'wsToken' => $user->getToken(),
|
|
|
|
|
'foo' => 'foo 2!',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $backend->getData() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$provider->persistSession( $backend, $this->getSentRequest() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnpersistSession() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$provider = new CookieSessionProvider( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 1,
|
|
|
|
|
'sessionName' => 'MySessionName',
|
2016-02-17 09:09:32 +00:00
|
|
|
'cookieOptions' => [ 'prefix' => 'x' ],
|
|
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider->setLogger( new \Psr\Log\NullLogger() );
|
|
|
|
|
$provider->setConfig( $this->getConfig() );
|
|
|
|
|
$provider->setManager( SessionManager::singleton() );
|
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
|
|
|
$provider->setHookContainer( $this->getHookContainer() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->unpersistSession( $request );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'MySessionName' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'xUserName' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
|
|
|
|
|
$this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
|
|
|
|
|
|
|
|
|
|
$provider->unpersistSession( $this->getSentRequest() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSetLoggedOutCookie() {
|
2017-04-19 19:37:35 +00:00
|
|
|
$provider = TestingAccessWrapper::newFromObject( new CookieSessionProvider( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 1,
|
|
|
|
|
'sessionName' => 'MySessionName',
|
2016-02-17 09:09:32 +00:00
|
|
|
'cookieOptions' => [ 'prefix' => 'x' ],
|
|
|
|
|
] ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider->setLogger( new \Psr\Log\NullLogger() );
|
|
|
|
|
$provider->setConfig( $this->getConfig() );
|
|
|
|
|
$provider->setManager( SessionManager::singleton() );
|
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
|
|
|
$provider->setHookContainer( $this->getHookContainer() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$t1 = time();
|
|
|
|
|
$t2 = time() - 86400 * 2;
|
|
|
|
|
|
|
|
|
|
// Set it
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->setLoggedOutCookie( $t1, $request );
|
|
|
|
|
$this->assertSame( (string)$t1, $request->response()->getCookie( 'xLoggedOut' ) );
|
|
|
|
|
|
|
|
|
|
// Too old
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$provider->setLoggedOutCookie( $t2, $request );
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
|
|
|
|
|
|
|
|
|
|
// Don't reset if it's already set
|
|
|
|
|
$request = new \FauxRequest();
|
2016-02-17 09:09:32 +00:00
|
|
|
$request->setCookies( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'xLoggedOut' => $t1,
|
2016-02-17 09:09:32 +00:00
|
|
|
], '' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider->setLoggedOutCookie( $t1, $request );
|
|
|
|
|
$this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* To be mocked for hooks, since PHPUnit can't otherwise mock methods that
|
|
|
|
|
* take references.
|
2021-02-01 19:38:05 +00:00
|
|
|
* @param User $user
|
|
|
|
|
* @param array &$sessionData
|
|
|
|
|
* @param string[] &$cookies
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
public function onUserSetCookies( $user, &$sessionData, &$cookies ) {
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-26 20:02:56 +00:00
|
|
|
public function testGetCookie() {
|
|
|
|
|
$provider = new CookieSessionProvider( [
|
|
|
|
|
'priority' => 1,
|
|
|
|
|
'sessionName' => 'MySessionName',
|
|
|
|
|
'cookieOptions' => [ 'prefix' => 'x' ],
|
|
|
|
|
] );
|
|
|
|
|
$provider->setLogger( new \Psr\Log\NullLogger() );
|
|
|
|
|
$provider->setConfig( $this->getConfig() );
|
|
|
|
|
$provider->setManager( SessionManager::singleton() );
|
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
|
|
|
$provider->setHookContainer( $this->getHookContainer() );
|
2017-04-19 19:37:35 +00:00
|
|
|
$provider = TestingAccessWrapper::newFromObject( $provider );
|
2016-02-26 20:02:56 +00:00
|
|
|
|
|
|
|
|
$request = new \FauxRequest();
|
|
|
|
|
$request->setCookies( [
|
|
|
|
|
'xFoo' => 'foo!',
|
|
|
|
|
'xBar' => 'deleted',
|
|
|
|
|
], '' );
|
|
|
|
|
$this->assertSame( 'foo!', $provider->getCookie( $request, 'Foo', 'x' ) );
|
|
|
|
|
$this->assertNull( $provider->getCookie( $request, 'Bar', 'x' ) );
|
|
|
|
|
$this->assertNull( $provider->getCookie( $request, 'Baz', 'x' ) );
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 00:03:20 +00:00
|
|
|
public function testGetRememberUserDuration() {
|
|
|
|
|
$config = $this->getConfig();
|
|
|
|
|
$provider = new CookieSessionProvider( [ 'priority' => 10 ] );
|
|
|
|
|
$provider->setLogger( new \Psr\Log\NullLogger() );
|
|
|
|
|
$provider->setConfig( $config );
|
|
|
|
|
$provider->setManager( SessionManager::singleton() );
|
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
|
|
|
$provider->setHookContainer( $this->getHookContainer() );
|
2016-05-13 00:03:20 +00:00
|
|
|
|
|
|
|
|
$this->assertSame( 200, $provider->getRememberUserDuration() );
|
|
|
|
|
|
|
|
|
|
$config->set( 'ExtendedLoginCookieExpiration', null );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 100, $provider->getRememberUserDuration() );
|
|
|
|
|
|
|
|
|
|
$config->set( 'ExtendedLoginCookieExpiration', 0 );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( null, $provider->getRememberUserDuration() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetLoginCookieExpiration() {
|
|
|
|
|
$config = $this->getConfig();
|
2017-04-19 19:37:35 +00:00
|
|
|
$provider = TestingAccessWrapper::newFromObject( new CookieSessionProvider( [
|
2016-05-13 00:03:20 +00:00
|
|
|
'priority' => 10
|
|
|
|
|
] ) );
|
|
|
|
|
$provider->setLogger( new \Psr\Log\NullLogger() );
|
|
|
|
|
$provider->setConfig( $config );
|
|
|
|
|
$provider->setManager( SessionManager::singleton() );
|
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
|
|
|
$provider->setHookContainer( $this->getHookContainer() );
|
2016-05-13 00:03:20 +00:00
|
|
|
|
2016-06-22 16:36:16 +00:00
|
|
|
// First cookie is an extended cookie, remember me true
|
|
|
|
|
$this->assertSame( 200, $provider->getLoginCookieExpiration( 'Token', true ) );
|
|
|
|
|
$this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', true ) );
|
|
|
|
|
|
|
|
|
|
// First cookie is an extended cookie, remember me false
|
|
|
|
|
$this->assertSame( 100, $provider->getLoginCookieExpiration( 'UserID', false ) );
|
|
|
|
|
$this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', false ) );
|
2016-05-13 00:03:20 +00:00
|
|
|
|
|
|
|
|
$config->set( 'ExtendedLoginCookieExpiration', null );
|
|
|
|
|
|
2016-06-22 16:36:16 +00:00
|
|
|
$this->assertSame( 100, $provider->getLoginCookieExpiration( 'Token', true ) );
|
|
|
|
|
$this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', true ) );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 100, $provider->getLoginCookieExpiration( 'Token', false ) );
|
|
|
|
|
$this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', false ) );
|
2016-05-13 00:03:20 +00:00
|
|
|
}
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|