Commit graph

138 commits

Author SHA1 Message Date
James D. Forrester
4bae64d1c7 Namespace includes/context
Bug: T353458
Change-Id: I4dbef138fd0110c14c70214282519189d70c94fb
2024-02-08 11:07:01 -05:00
Martin Urbanec
29af4dd074 Move user options related classes into its own namespace
There are a couple of user options related classes already,
and the T321527 work on dynamic defaults is going to add
even more. Let's move them into a separate namespace
to make core a bit more organized.

Old name is kept as an alias for compatibility purposes.

Bug: T321527
Bug: T352284
Change-Id: I9822eb1553870b876d0b8a927e4e86c27d83bd52
2023-11-29 13:27:13 +01:00
Umherirrender
d85abecc71 tests: Avoid dynamic properties in auth tests
- Set only properties defined by the class
- Use the DynamicPropertyTestHelper
- Use the existing local variables with & in closures

Dynamic properties are deprecated in php8.2

Bug: T326478
Change-Id: I7312268ecfd7c66fec72cbb2fa3df72fd1f3044c
2023-11-24 16:25:31 +00:00
Umherirrender
2eca47315f tests: Avoid dynamic properties in AuthenticationProvider Test
Deprecated in php8.2

Bug: T326478
Change-Id: I6a883d9d4bdd210e098cde90458af51857e5f136
2023-11-22 21:52:13 +01:00
James D. Forrester
4ed5ca48b1 Follow-up 71ff05267: Stop writing to tablesUsed in tests, now unnecessary
Bug: T342301
Change-Id: I5ea01f7ee103570165261bde0965c5b65e04c369
2023-11-21 09:02:48 -05:00
Tim Starling
990acd88be Stop using nonexistent DatabaseBlock constructor options
Remove the 'user' option from the documentation and defaults. It has
been ignored since a3b4881f6f (2021, 1.37).

Fix tests which pass unknown constructor options. I found these by
temporarily patching the constructor to throw when an unknown option is
given.

Change-Id: I95cb2a38a5688d83c90ad501786f6d6bb02765ba
2023-11-06 12:03:51 +11:00
Tim Starling
cc40f0a12d Migrate callers of DatabaseBlock methods moved to DatabaseBlockStore
* Migrate callers of DatabaseBlock methods newListFromTarget,
  newFromID, newFromTarget, doAutoblock.
* Deprecate DatabaseBlock methods newFromID and getAutoblockExpiry.
  These are the methods with no extension callers in code search.

Bug: T255433
Change-Id: If0358459f53d32e7fe984c2fb8b61e0088f28922
2023-11-06 12:03:51 +11:00
Tim Starling
d06ac1a3cd Move DatabaseBlock read query methods to DatabaseBlockStore
* Move to DatabaseBlockStore the DatabaseBlock methods newFromID,
  getQueryInfo, getRangeCond, newFromRow, isExemptedFromAutoblocks,
  doAutoblock, updateTimestamp, getAutoblockExpiry, newFromTarget,
  newListFromTarget.
* Split DatabaseBlock::getBlocksForIPList. Now
  BlockManager::getBlocksForIPList() is responsible for XFF header
  validation and trusted proxy handling. DatabaseBlockStore::
  newListFromIPs() just does the queries and constructs the Block
  objects.
* In DatabaseBlockStore::newFromRow() and doAutoblock(), use the
  DatabaseBlock constructor instead of calling many setter methods. Add
  constructor options decodedExpiry, decodedTimestamp, id,
  parentBlockId and restrictions to support this.
* Move isExemptedFromAutoblocks() to its own service. Remove the cache
  since in my testing with production eval.php, the WAN cache fetch is
  10 times slower than just using the message cache, contradicting the
  comment written in 2008.
* Fix AuthManagerTest which was previously passing an unrecognised
  "restrictions" option to DatabaseBlock. Now that the option actually
  works, we have to use the right type.

Bug: T255433
Change-Id: I5049e60be1681f67fcca133e569e315792dc42dd
2023-11-03 11:00:57 +11:00
daniel
04197cb13f Introduce StatusFormatter
This takes us one step closer to deprecating Status,
so we can isolate StatusValue from presentation logic.

FormatterFactory is introduced as a mechanism for
getting instance of formatters that need access to the user interface
language and other request dependent information.

Usage is demonstrated in thumb.php, SpecialCreateAccount, and
SearchHandler. The examples indicates that there is no work do
be done around ErrorPageError and LocalizedHttpException.

Change-Id: I7fe5fee24cadf934e578c36856cc5d45fb9d0981
2023-10-27 14:56:51 +02:00
Tim Starling
66f85fa125 AuthManager: deny auto-creation for globally blocked users
* In AuthManager::autoCreateUser(), check the permissions of the
  performer instead of relying on the secondary providers. This means
  that auto-creation will be denied when the anonymous user is globally
  IP-blocked.
* Remove create account block check from
  CheckBlocksSecondaryAuthenticationProvider. testUserForCreation() is
  supposed to only do target name checks, but it's not actually
  possible to block a non-existent local name. So we don't need this
  code.
* Add a $performer parameter to autoCreateUser() so that
  Special:CreateLocalAccount can have elevated permissions when it
  creates an account with IP block exemption.
* When a performer is passed, don't use the session as a cache.
* Since we are passing autocreateaccount as the action to
  PermissionManager instead of createaccount, some special cases need to
  be tweaked. Previously AuthManager checked for either
  autocreateaccount or createaccount rights. Now PermissionManager does
  that when the action is autocreateaccount.

By removing redundant checks from testUserForCreation(), the number of
ipblocks queries during a normal Special:CreateAccount post request is
reduced from 8 to 6.

The CentralAuth change I7e7a7fc8bcd86285f857063a38de02b41b5175d0 should
be merged immediately after this one.

Bug: T234371
Bug: T345683
Change-Id: If2937c7d717d2adc249f608d4585122b02a43fff
2023-10-20 10:07:50 +11:00
Tim Starling
a588e3969d Add PermissionManager::$blockManager
PermissionManager has an extensive need for block information, and it
gets it from the global state with User::getBlock(). In the future it
should get block information from BlockManager.

However, this is complicated because BlockManager already depends on
PermissionManager, solely for ipblock-exempt rights. For now, have
BlockManager use the service container for this information.

To simplify review, the changes which use
PermissionManager::$blockManager will be in a subsequent commit.

Bug: T345683
Change-Id: I7a92eed142dcf2595cc3e2c5fa85a362c2a10d99
2023-10-19 10:15:20 +11:00
Amir Sarabadani
cd2e19c050 tests: Use $this->getDb() instead of wfGetDB() in integration tests
Deprecated long time ago.

Bug: T330641
Change-Id: Ia57f12d350c3346029aafae25534c9ed262a7e98
2023-09-25 21:08:09 +02:00
Amir Sarabadani
c00148768c Introduce LoggingSelectQueryBuilder
To replace DatabaseLogEntry::getSelectQueryData()

Many migrations can't happen right now because of use of
ChangeTagsStore::modifyDisplayQuery() but I will fix that soon.

Bug: T344971
Change-Id: Iafb2bb5ea3a0da49da1dafb69b65dea3c15029ed
2023-09-25 15:51:24 +02:00
Amir Sarabadani
abbb4281cb tests: Migrate more cases to SQB
Bug: T344971
Change-Id: Ia69d82d6a6e623b9032240dc910fb47ff5887661
2023-09-21 18:30:37 +00:00
James D. Forrester
c1599c91b3 Namespace Config-related classes under \MediaWiki\Config
Bug: T166010
Change-Id: I4066885a7ea071d22497abcdb3f95e73e154d08c
2023-09-21 05:41:58 +00:00
James D. Forrester
1d0b7ae1e2 Namespace User under \MediaWiki\User
Bug: T166010
Change-Id: I7257302b485588af31384d4f7fc8e30551f161f1
2023-09-19 19:18:16 +00:00
Amir Sarabadani
dec0cf54a2 tests: Migrate more easy cases to SQB
The script couldn't find these because they were using wfGetDb(). I
fixed that and that cleaned up a lot more.

Bug: T344971
Change-Id: I5c4c5fa65c42292bfb4d870936775e598fb30719
2023-09-18 20:53:39 +02:00
Amir Sarabadani
5bd33d46ef Reorg: Move WebRequest to includes\Request
This has been approved as part of RFC T166010

Bug: T321882
Change-Id: I6bbdbbe6ea48cc1f50bc568bb8780fc7c5361a6f
2023-09-11 21:44:34 +01:00
jenkins-bot
fc40cf1067 Merge "Make "temp reserved" usernames not be "usable"" 2023-09-06 21:11:52 +00:00
Amir Sarabadani
f4e68e055f Reorg: Move Status to MediaWiki\Status\
This class is used heavily basically everywhere, moving it to Utils
wouldn't make much sense. Also with this change, we can move
StatusValue to MediaWiki\Status as well.

Bug: T321882
Depends-On: I5f89ecf27ce1471a74f31c6018806461781213c3
Change-Id: I04c1dcf5129df437589149f0f3e284974d7c98fa
2023-08-25 15:44:17 +02:00
Tim Starling
07f1bc9f89 Make "temp reserved" usernames not be "usable"
If a username matches $wgAutoCreateTempUser['reservedPattern'], but the
username is not a valid temp user, return false from
UserNameUtils::isUsable().

This makes reserved patterns analogous to $wgReservedUsernames, and
provides a simple way to reject central sessions for temp users on wikis
where temp users are disabled.

Bug: T342475
Change-Id: I1d7f843e71e605fccf3de93fd145ff43e396619f
2023-08-25 15:06:22 +10:00
Daimona Eaytoy
2668b94a5a Do not use UTSysop directly in auth tests
These tests are all making two assumptions:
- That the sysop test account exists
- That its name is UTSysop

Both assumptions happen to be true right now, but the first one will no
longer be after change I30861742. The second one will probably remain
true for a while, but still, tests shouldn't rely on this implementation
detail when possible.

If a test needs an exiting test user, it should call getTestUser /
getTestSysop.

Use mocks or different usernames where the user actually doesn't matter,
e.g. in non-Database tests where UTSysop already doesn't exist.

Bug: T342428
Change-Id: Ie77e72f5a5ee6a2ef4ec9dceaa9044bb690f68b2
2023-08-16 00:34:30 +00:00
Thalia
13f4d6bc83 Ensure temporary users are blocked by configured IP blocks
Temporary users are blocked if their IP address is listed in
$wgDnsBlacklistUrls or $wgSoftBlockRanges.

Bug: T343704
Change-Id: Ia3383bd10c3c6c35c586550b68f4af5f4659e815
2023-08-07 14:14:54 +01:00
Derick Alangi
891787e10c
Auth: Introduce several AuthManager session keys as constants
This patch introduces session keys as constants as they repeat across
this class but are the same strings. Making them as constants will ensure
they're the same across usage and avoid errors.

If we ever want to change this key in the future, we can just change the
value of the constant and no need to change all occurrences in the entire
file.

== Introduces ==
+ AUTHN_STATE
+ ACCOUNT_CREATION_STATE
+ ACCOUNT_LINK_STATE
+ AUTOCREATE_BLOCKLIST

NOTE: Constants created as public but marked as internal to be used by
relevant tests in core.

Change-Id: I49e4ea01f0f8b548e32f88653e67ba4058c8bc07
2023-08-06 12:55:40 +01:00
Tim Starling
9b3c418d01 Make tests override needed configuration
* Fix tests depending on $wgUsePigLatinVariant=true, which is in
  DevelopmentSettings.php but not TestSetup::applyInitialConfig().
* Fix test depending on DNS resolution details.

Change-Id: I877dc3323bf4024caab7666a8820103de0b48d23
2023-07-27 06:40:48 +00:00
Tim Starling
f6b1c1f84a Fix flaky test testSecuritySensitiveOperationStatus
equalTo() no longer takes a $delta parameter, so use equalToWithDelta()
instead.

Increase the delta to 2 seconds.

Add comments for readability.

Bug: T342775
Change-Id: I85c7a650a45dbdaf0645f45d1277b1e7515a06eb
2023-07-27 09:48:09 +10:00
Daimona Eaytoy
2d9b8797ef AuthManagerTest: Use localhost instead of local.wmftest.net
AIUI, the latter is vagrant-specific and doesn't work e.g. in
MediaWiki-Docker.

Change-Id: I63891b6796dbaeab63e7f34b1fddfafa5106fd1f
2023-07-16 20:26:22 +02:00
Lucas Werkmeister
5f2bfc44ce Simplify HookContainer (v2)
This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating
change I7d690a1172 (commit d139eb07fe). The only change from the
original is in getHookMethodName(), additionally replacing '-' with '_'
(not just ':' and '\'). The original commit message follows:

This converts all hook handlers to the same internal representation.
This is done lazily, when the hook is run for the first time.

The logic for temporarily disabling handlers by calling scopedRegister()
with the $replace parameter set has been greatly simplified.

There are some minor changes to the class's interface and behavior,
none of which should be breaking changes:
* run() will emit deprecation warnings if and only if it was called
  with the deprecationVersion option set, for all kinds of handlers.
  The idea is that deprecated hooks should emit a warning either from
  run(), or from emitDeprecationWarnings(). The latter happens if the
  hook is listed in DeprecatedHooks.
* register() now also accepts hook handlers declared in the way that
  extensions register hooks.
* Attempts to call register() with an invalid hook definition now
  result in an invalidArgumentException.
* Attempts to call register() for a deprecated hook will consistently
  result in a deprecation warning.
* The internal getRegisteredHooks() method has been removed in favor
  of the identical getHookNames() method.
* The internal getLegacyHandlers method has been removed in favor
  of getHandlerDescriptions() and getHandlerCallbacks().
* The call order changed so that dynamically registered handlers
  are called last, instead of getting called before handler objects
  from extensions.

Bug: T338213
Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 12:06:23 +02:00
Lucas Werkmeister
b0317287bc Revert "Simplify HookContainer"
This apparently caused some change in how hook handlers are called (it
now calls e.g.  AbuseFilterHookHandler::onAbuseFilter-generateUserVars()
instead of AbuseFilterHookHandler::onAbuseFilter_generateUserVars()),
causing both test failures and errors on Beta.

This reverts commit d139eb07fe.

Bug: T338213
Change-Id: I50c3d1c5dfd2d7eeac59992156a8a644cf0197e5
2023-06-06 11:12:18 +02:00
daniel
d139eb07fe Simplify HookContainer
This converts all hook handlers to the same internal representation.
This is done lazily, when the hook is run for the first time.

The logic for temporarily disabling handlers by calling scopedRegister()
with the $replace parameter set has been greatly simplified.

There are some minor changes to the class's interface and behavior,
none of which should be breaking changes:
* run() will emit deprecation warnings if and only if it was called
  with the deprecationVersion option set, for all kinds of handlers.
  The idea is that deprecated hooks should emit a warning either from
  run(), or from emitDeprecationWarnings(). The latter happens if the
  hook is listed in DeprecatedHooks.
* register() now also accepts hook handlers declared in the way that
  extensions register hooks.
* Attempts to call register() with an invalid hook definition now
  result in an invalidArgumentException.
* Attempts to call register() for a deprecated hook will consistently
  result in a deprecation warning.
* The internal getRegisteredHooks() method has been removed in favor
  of the identical getHookNames() method.
* The internal getLegacyHandlers method has been removed in favor
  of getHandlerDescriptions() and getHandlerCallbacks().
* The call order changed so that dynamically registered handlers
  are called last, instead of getting called before handler objects
  from extensions.

Change-Id: I7d690a1172af44a90b957b2274d68e51b7f09938
2023-06-04 15:36:38 +02:00
Amir Sarabadani
623d3dc419 rdbms: Move ReadOnlyMode and ConfiguredReadOnlyMode to rdbms library
The db/ directory does not have an owner and it's a mess in general.
These classes don't depend on anything in core except the rdbms library.
Let's simply move it there. In other words, Krinkle made me do it.

Since the class was moved in I6202e52ba73 merged less than a week ago,
no need to alias anything.

Bug: T321882
Change-Id: I24ceeb8bf765a50f441270136acd612359d50aa2
2023-05-04 23:41:21 +02:00
Amir Sarabadani
d59ffa4202 Reorg: Move ReadOnlyMode classes to db/
They are not suitable to go to rdbms library as they depend on mediawiki
pieces, the second best place is the db/ directory.

Bug: T321882
Change-Id: I6202e52ba7306d74261206c2ba7930c5f1a0a18e
2023-04-28 12:13:01 +02:00
thiemowmde
d47215586c Make use of MainConfigNames references in a few more places
Change-Id: I29499c29e7f8c6ca7627b130b1fe648e79100582
2023-04-21 11:11:44 +02:00
Tim Starling
be3018b268 Just another 80 or so PHPStorm inspection fixes (#4)
* Unnecessary regex modifier. I agree with this inspection which flags
  /s modifiers on regexes that don't use a dot.
* Property declared dynamically.
* Unused local variable. But it's acceptable for an unused local
  variable to take the return value of a method under test, when it is
  being tested for its side-effects. And it's acceptable for an unused
  local variable to document unused list expansion elements, or the
  nature of array keys in a foreach.

Change-Id: I067b5b45dd1138c00e7269b66d3d1385f202fe7f
2023-03-25 00:39:06 +00:00
Tim Starling
5e30a927bc tests: Make some PHPUnit data providers static
Just methods where adding "static" to the declaration was enough, I
didn't do anything with providers that used $this.

Initially by search and replace. There were many mistakes which I
found mostly by running the PHPStorm inspection which searches for
$this usage in a static method. Later I used the PHPStorm "make static"
action which avoids the more obvious mistakes.

Bug: T332865
Change-Id: I47ed6692945607dfa5c139d42edbd934fa4f3a36
2023-03-24 02:53:57 +00:00
Umherirrender
a160240191 tests: Replace deprecated User::idFromName
Bug: T325687
Change-Id: I8319fc256f95c100aecbc31e24524f0208e3b0d5
2022-12-23 14:18:20 +00:00
Reedy
27ee63f8c9 Remove pre PHP 7.4 serialize()/unserialize()
Depends-On: I3b893152ed278a72b5e74ccf91a3381f319bfcd9
Change-Id: I9f69a9316eaec23103262ec2ffd5b85baaf16f3f
2022-11-10 19:25:01 +00:00
Amir Sarabadani
bbe704b5c1 Reorg: Move some of request related classes to MediaWiki/Request
Redoing I5ea70120d74 but without moving WebRequest that caused issues
with phan-taint-plugin.

Moving:
 - DerivativeRequest
 - FauxRequest
 - FauxRequestUpload
 - PathRouter
 - WebRequestUpload

Bug: T321882
Change-Id: I832b133aaf61ee9f6190b0227d2f3de99bd1717b
2022-10-28 10:15:31 +00:00
Zabe
f6b9381d7f Revert "Reorg: Move some of request related classes to MediaWiki/Request"
This reverts commit 2bdc0b2b72.

Reason for revert: T166010#8349431

Bug: T166010
Change-Id: Idcd3025647aec99532f5d69b9c1718c531761283
2022-10-27 13:14:16 +00:00
Amir Sarabadani
2bdc0b2b72 Reorg: Move some of request related classes to MediaWiki/Request
Moving:
 - DerivativeRequest
 - FauxRequest
 - FauxRequestUpload
 - PathRouter
 - WebRequest
 - WebRequestUpload

Bug: T166010
Change-Id: I5ea70120d745f2876ae31d039f3f8a51e49e9ad8
2022-10-26 16:49:10 +02:00
Tim Starling
0077c5da15 Use short array destructuring instead of list()
Introduced in PHP 7.1. Because it's shorter and looks nice.

I used regex replacement.

Change-Id: I0555e199d126cd44501f859cb4589f8bd49694da
2022-10-21 15:33:37 +11:00
Umherirrender
bbaa4c25d9 tests: Use MainConfigNames constants on overrideConfigValue
Change-Id: Ieeefb88daf55a976883f6e20ef6881c89a7f7630
2022-07-12 19:34:54 +02:00
daniel
c6c62e2c8f PHPUnit: enable main object cache in tests
This sets MainCacheType to 'hash', so the main cache is functional
within test cases. Between tests we are already calling ObjectCache::clear,
so the cache will not leak data into other test cases.

It turned out that the way we have been overriding configuration
settings in MediaWikiIntegrationTestCase is insufficient, since the
overrides would only apply in the main Config object, not in global
variables. This means they could not be overwritten with setMwGlobals.
So this patch also changes how we apply config overrides for test setup.

Config overrides that happen during tests work as before.

Depends-On: Ic36e92b73f22fd64e2d0b680d8e900f598158460
Change-Id: Ia60cb0bc18b235f23d9778da8241f65accfc0d35
2022-07-07 16:26:03 +10:00
daniel
bf092744c9 PHPUnit: introduce setMainCache
The main object cache is disabled during testing. Some integration tests
need it though. This provides a clean way to enable it, to replace the hacks
that were used so far.

Note that we may want to enable the main cache during testing soon. When
that happens, this method is still useful to disable the cache in certain
tests, and to set a specific cache instance.

Change-Id: I04ae1bf1b6b2c8f6310acd2edf89459d01a9c870
2022-07-07 16:25:59 +10:00
daniel
033060bbaa PHPUnit: introduce overrideConfigValues.
overrideConfigValues() is introduced as a replacement for
setMwGlobals(). Since we are moving away from reading configuration
from global variables, test cases should not use global variables to set
configuration.

Bug: T294739
Change-Id: I9d41045bd13932bd853f53e00322233fc4f1321c
2022-06-21 13:29:29 +02:00
Reedy
41c42d5435 Tests: Cleanup some unnecessary nested function calls
Replace ->will( ->return with ->willReturn(

Change-Id: Ia2dfafa03cac8169d86d6fa5a30b73bfad1fe9fa
2022-06-06 01:02:34 +01:00
jenkins-bot
d3327f3fdb Merge "auth: Introduce quick permissions checks for AuthManager" 2022-06-01 23:22:41 +00:00
Thalia
8f274480b7 auth: Introduce quick permissions checks for AuthManager
Deprecate AuthManager::checkAccountCreatePermissions in favour of
::authorizeCreateAccount, for full permissions checks and
::probablyCanCreateAccount, for quick permissions checks.

Update SpecialCreateAccount to use the appropriate checks, and
add SpecialCreateAccountTest.

Bug: T283265
Change-Id: I385f2b56ef61f64a4c61e9c97683bef564892dce
2022-06-01 08:06:21 +01:00
Gergő Tisza
1f7561e59f Revert "Tombstone the old session on SessionBackend::resetId()"
This reverts commit ec3da4589bebeb46d7f1544dc46f24baec334966.
Caused frequent session loss in the Wikimedia cluster.

Bug: T299193
Bug: T309616
Change-Id: I3a410df88071d72078672cf1b670e81c11b28117
(cherry picked from commit d1a1fcedc9eace8a5f4a8454eff44a7ed898848a)
2022-05-31 16:37:31 +00:00
Umherirrender
f01a391f0d tests: Fix types in documentation by adding use statements
Some types used in @param/@return are not correct,
because the class is not used with namespace

Change-Id: Ic2643687378a6addabc2c9cd402224e442257f86
2022-05-29 07:11:15 +00:00