Commit graph

159 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
daniel
3d55397207 Move creation of BlockErrorFormatter into FormatterFactory
The idea is that all formatters that need the user language or
other request specific context should be instantiated by
FormatterFactory.

Change-Id: I8334cc89dcf0f293298b82e004116be50a90f0d1
2024-01-26 13:03:44 -05:00
Umherirrender
9c6866f1c7 tests: Remove explicit clean up for database tests
Now done via ChangedTablesTracker

Change-Id: Ia72616b31e67c55347c1e80b4474d61bcd0fecdd
2024-01-20 19:55:27 +01:00
jenkins-bot
c5c3c1c692 Merge "tests: Remove cleanup of blocks, done by ChangedTablesTracker" 2024-01-20 15:40:45 +00:00
Umherirrender
723c10d6dc tests: Improve DatabaseBlockTest::addDBDataOnce
Use the normalized data in the block list and
avoid user handling to speed up test

Change-Id: Ide5aa5353b9f3e5c1c004212248ab4d8c884eb94
2024-01-20 11:50:26 +01:00
Umherirrender
16a436ce6c tests: Remove cleanup of blocks, done by ChangedTablesTracker
In DatabaseBlockTest changed from addDBDataOnce() to addDBData() as
ChangedTablesTracker cannot reset tables changed by addDBDataOnce()
between each test run.

Remove also User::clearInstanceCache done together with the deletion as
the TestUser class is calling this already between tests.

Change-Id: Ibd5e544138a2a9b554abc2dea54a5db38f9a828f
2024-01-20 11:41:15 +01:00
Alexander Vorwerk
e838ef9773 Don't allow passing a db to DatabaseBlockStore::insertBlock
Bug: T291849
Change-Id: I5f5cec791eb2999d7295440248f74fba79156b6f
2024-01-01 14:23:24 +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
Martin Urbanec
c9908da103 IP Masking: Expire temporary accounts in 1 year
Why:
Temporary accounts (introduced as part of IP Masking)
are supposed to expire 1 year after their registration.
Automatic account expiration can be done via a maintenance
script, which would be periodically executed via cron / systemd.

Make it possible for extensions to provide their own logic
for generating a list of temporary accounts to invalidate.
This is used in CentralAuth to base registration timestamp
on the global registration timestamp.

The default behavior is "temporary accounts do not expire",
given the feature requires a maintenance script to run
periodically, which will not be the case on third party
instances.

What:
* Add `expireAfterDays` to $wgAutoCreateTempUser, controlling
  how many days temporary accounts have.
* Add UserSelectQueryBuilder::whereRegisteredTimestamp(),
  filtering accounts based on user_registration.
* Add ExpireTemporaryAccounts maintenance script, which is
  @stable to extend.

Bug: T344695
Change-Id: If17bf84ee6620c8eb784b7d835682ad5e7afdfcc
2023-11-09 15:11:48 +01:00
jenkins-bot
5a7226f41d Merge "Stop using nonexistent DatabaseBlock constructor options" 2023-11-06 21:41:01 +00:00
jenkins-bot
b011c84294 Merge "block,Permissions: Blocks should not apply to implicit rights" 2023-11-06 17:31:40 +00:00
daniel
898d11f9b7 block,Permissions: Blocks should not apply to implicit rights
Implicit rights represent actions granted to all users.
They can be rate limited, but not revoked.

Bug: T350202
Bug: T350117
Bug: T350347
Change-Id: I22bf5e60c38900f881d43577351761827066a25c
2023-11-06 17:06:47 +00: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
c638dac3f2 Add tests for new DatabaseBlockStore and BlockManager methods
Adapted from DannyS712's tests of DatabaseBlockFactory
Ie26b589c2ba5ec2715446944ec8cf761676be026

Co-Authored-by: DannyS712 <dannys712.wiki@gmail.com>
Change-Id: Ic4fa2ceea13658853eae48d754c4b84d6f8be3f8
2023-11-03 17:03:33 +11:00
Tim Starling
dfc691bfc5 Migrate callers of deprecated method BlockManager::getUserBlock()
Bug: T345683
Depends-On: I2fff3ff961f788ca2fa84eba54293edf9145d738
Change-Id: I6d00af72cdda2a949b861dba402cf2f0c70d1b49
2023-10-27 17:42:29 +00:00
Thalia
e521abbcd8 Display all error messages for a CompositeBlock
* Add getMessages method to BlockErrorFormatter that will get
  error messages for all the applicable blocks.
* Call getMessages from PermissionManager, UserAuthority and
  EditPage.
* ApiBase and UserBlockedError still call getMessage since
  they work with a single message.

Bug: T344463
Change-Id: If240562dd25435d9c6c824dd595d58bb8672de2b
2023-10-27 13:05:08 +01:00
Thalia
24a3dc58d6 Clean up BlockErrorFormatter and test
* Add type hint and return type
* Remove unnecessary request mock

Change-Id: I094090ca4d1314b8630220db3e720bc4ba39320e
2023-10-27 13:04:59 +01:00
Thalia
6e858b7a5e BlockErrorFormatterTest: Use class @covers annotation
Change-Id: I358c9df417e5893e7f19be5f8218c7410c9a4cd5
2023-10-26 16:04:26 +00:00
Tim Starling
9e7c9c13b9 Multiblocks preparation in User::getBlock(), PermissionManager and BlockManager
Functional changes:

* On account creation, if both a user block and an IP block apply,
  return a CompositeBlock instead of picking a winner.
* On other actions, instead of returning the whole CompositeBlock if
  some part of it matches the specified page and action, filter the
  individual parts of the CompositeBlock by whether or not they match.

Refactoring:

* Add BlockManager::getBlock(), which is the same as getUserBlock()
  except that it expects the caller to do IP exemption checking.
  Deprecate getUserBlock().
* Migrate some callers of User::getBlock() to use BlockManager.
* Have PermissionManager use BlockManager instead of
  User::getBlock().
* Add $request parameter to PermissionManager::getApplicableBlock(),
  to fix the weird bundling of the request with the User.
* Move the createaccount block database fetch from PermissionManager to
  BlockManager, where it can easily be cached.
* Fix code duplication between PermissionManager::isBlockedFrom() and
  PermissionManager::getApplicableBlock() by having the former call the
  latter.
* In CheckBlocksSecondaryAuthenticationProvider, use
  AuthManager::getRequest(). Previously it used the global request and
  also User::getRequest() via User::getBlock().

Bug: T345683
Change-Id: Icd75025665282a34d8854bda267a0eb3d759719c
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
jenkins-bot
10e72000f9 Merge "Add CompositeBlock::createFromBlocks" 2023-10-13 00:23:36 +00:00
Gergő Tisza
6b0c3f63ea Add CompositeBlock::createFromBlocks
All GetUserBlock hook handlers face a common problem: when there
are multiple blocks that match, or there is another block passed
down by core or previous extensions, they need to create a
composite block since they can only pass forward a single Block
object. Even though the logic for doing so is not complicated,
it's better to centralize it so it's one less thing the extension
author needs to think about.

Unlike the original code snippet, the target is not passed in
explicitly but taken from the first composing block. Since
CompositeBlock is created after target filtering already happend,
this shouldn't make much difference; and the target property of a
composite block which contains blocks with different targets
isn't particularly meaningful.

Change-Id: I14f754e3c479ce61d18d7d2ebd1656940088d67d
2023-10-11 05:19:18 +00: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
jenkins-bot
6e3ca8501b Merge "Add Block::toArray()" 2023-09-22 09:02:32 +00:00
Amir Sarabadani
abbb4281cb tests: Migrate more cases to SQB
Bug: T344971
Change-Id: Ia69d82d6a6e623b9032240dc910fb47ff5887661
2023-09-21 18:30:37 +00:00
Tim Starling
03a9bf2794 Add Block::toArray()
There are 7 callers of CompositeBlock::getOriginalBlocks() in code
search, and all are following a pattern like

  $blocks = $block instanceof CompositeBlock
    ? $block->getOriginalBlock() : [ $block ]

or can be reorganised to follow such a pattern, which suggests the need
for a public interface along these lines.

So, add Block::toArray() which returns the list of blocks in a
CompositeBlock, or wraps the single block in an array if the block is
not a CompositeBlock.

Also, remove the need for a phan-var override by moving the instanceof
DatabaseBlock check from shouldTrackBlockWithCookie() to its caller.

Bug: T345683
Change-Id: If769db9a831f63a79b57fb3bc9c7cece511c70c3
2023-09-21 10:27:21 +10:00
jenkins-bot
512502fcc9 Merge "Blocks cleanup" 2023-09-20 05:43:55 +00:00
Tim Starling
cdbd39bfdf Blocks cleanup
* Make BlockManager internal methods private, since nothing calls them
  anymore.
* In AbstractBlock and DatabaseBlock, remove deprecated public
  properties mExpiry, mHideName, mTimestamp, mAuto and mParentBlockId.
* In BlockRestrictionStore, remove all the "instanceof Restriction"
  checks. If someone passes in something that's not a Restriction, we
  should throw, not ignore it, because we don't know the caller's
  intention. Add a type declaration to $hasher in equals() so that it
  will throw.
* Remove the "m" prefix from all private and protected properties.
  AbstractBlock is not stable to override so this is not a stable
  interface break.
* In BlockRestrictionStore::restrictionsToRemove(), use an O(N)
  algorithm.
* In BlockRestrictionStore::rowToRestriction(), use a switch instead of
  a type map, so that the calls are statically analyzable.
* In BlockUser::__construct(), fix the initialisation order issue by
  inlining the relevant logic.
* Rename variable $actionRestriction.
* In Special:Block, fix call to deprecated method getTargetAndType(),
  and hard deprecate it. @deprecated has the effect of deprecating a
  method for both internal and external callers, there's no such thing
  as an external-only deprecation. So it's necessary to rename it if you
  want to keep it as a private method.

Bug: T345683
Change-Id: If4a4a18d7b5fec825417de81302266119c215fd3
2023-09-20 09:40:52 +10: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
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
AnaïsGueyte
58c9abee47 Fix composite block start time
Fix the start time for composite block in the block error message

Bug: T323105
Change-Id: I29b9f4ba84ee760425cc9ba3a1c58ed22e889d86
2023-08-28 13:04:31 -03:00
AnaïsGueyte
4251445825 Remove CompositeBlock::testGetPermissionsError
Test no longer needed as covered by BlockErrorFormatterTest::testGetMessage

Change-Id: Ia194cafc824c3a9a985460a803f2fda919aff503
2023-08-28 12:33:39 -03:00
Thalia
393a3cf8a3 CompositeBlockTest: Use class @covers annotation instead of per-method
Change-Id: I8d8fd804790a84174c43d30b349d09c16b4b64b5
2023-08-22 15:54:49 +01:00
Umherirrender
d7f959e528 tests: Use namespaced Wikimedia\Rdbms\IDatabase
Bug: T344536
Change-Id: I29c80fe2ac3effd5e9df4402c598dc33c1b23d5e
2023-08-20 15:18:57 +02:00
jenkins-bot
65ca23c095 Merge "Ensure that soft blocks block temporary users" 2023-08-17 10:32:07 +00:00
Daimona Eaytoy
234d5ed105 Fix tests not properly cleaning up the DB
- UserEditTrackerTest: determine whether to create the page based on
  WikiPage::exists() instead of a method parameter. The current
  implementation only works because the page is not deleted between test
  runs, and the creation query is ignored. However, if the first call to
  `editTrackerDoEdit` doesn't have $create=true, tests would fail. For
  instance, testGetEditTimestamp would fail when run on its own because
  the revision cannot be inserted if the page doesn't exist. Just use
  WikiPage::exists instead of forcing callers to handle this correctly.
- DatabaseBlockTest: use addDBDataOnce instead of a DIY implementation.
  This also makes the tests more deterministic, because the records
  needed by the test class are created immediately. Also avoid redundant
  User::addToDatabase and ::saveSettings, these are already done by
  TestUser.
- PageRestrictionTest: avoid Title::newFromID which is not guaranteed to
  succeed if all pages have been deleted by that point. The second part
  of the test was effectively doing the same thing as the first part, so
  just remove it.
- WikiPageDbTest: avoid expensive page deletions in tearDown. These are
  all unnecessary because the DB is cleaned up after each test (and
  'page' is explicitly included in $tablesUsed). In fact, deleting test
  pages as done there can be even worse than not doing anything, because
  it creates log entries etc. Add page_restrictions to tablesUsed as
  that's not truncated automatically.
- DBSiteStoreTest: testReset would fail when run on its own because it
  depends on test sites being inserted in testGetSites. Make the test
  add the sites it needs, so that they can safely be cleared between
  test runs.

Change-Id: I1065fb3e8507b4b1a3bf185181f2f3059a97fd04
2023-08-16 01:05:11 +00:00
Thalia
3eff3be2a4 Ensure that soft blocks block temporary users
Why:

* A soft block against an IP address does not block logged-in
  users, but it should block temporary users, otherwise anonymous
  users can circumvent it by creating a temporary account.

What:

* Return a block for a temporary user if a soft block is found
  against their IP address, from DatabaseBlock::newLoad
* Return soft blocks for temporary users in
  BlockManager::getXffBlocks
* Update documentation to make clear that soft blocks apply to
  temporary users, including where they are referred to as
  anon-only blocks

Bug: T343714
Change-Id: I5a2928eb3370bd47544b164b08014ef62297ce36
2023-08-15 14:26:44 +01:00
jenkins-bot
eafcbe2578 Merge "Ensure temporary users are blocked by configured IP blocks" 2023-08-09 15:55:28 +00:00
jenkins-bot
e26f241ebd Merge "BlockManagerTest: Add tests that cover untested service options" 2023-08-08 05:42:32 +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
Thalia
9a647b34b0 BlockManagerTest: Add tests that cover untested service options
Bug: T342885
Change-Id: I21575eda13f9468da8c54e0b1347c4078c3a3af4
2023-08-07 10:52:15 +01:00
Daimona Eaytoy
d3243571d0 Fix DB usage in non-database tests (part 2)
Mock the relevant services that need the DB instead, when possible. When
not possible, e.g. because DB access is needed for the test to make
sense, add the test to the Database group instead.

Change-Id: Iefbfe00bedc243906c6b860572568343268646cc
2023-08-06 22:57:28 +00:00
jenkins-bot
25bcb59653 Merge "block: Simplify deletion by parent id for BlockRestrictionStore" 2023-08-02 16:55:20 +00:00
Thalia
3184678f10 BlockManagerTest: Use class @covers instead of per-method annotations
Change-Id: I643286fe2eeaf361f4c55cac787d933ba9786162
2023-07-26 16:27:53 +01:00
Amir Sarabadani
e7dcb3802e tests: Migrate Database::select usages to SelectQueryBuilder
Bug: T311866
Change-Id: I0324919f4e40f4e2db3dab1e9aacf74177170b20
2023-07-17 14:24:30 +02:00
Umherirrender
e11a6165ec block: Simplify deletion by parent id for BlockRestrictionStore
Remove BlockRestrictionStore::deleteByBlockId and replace by
select on parent id and delete on primary key.
This avoids that restriction store needs to determine the rows via
deleteJoin and the block store via parent select. Just use the primary
key in both functions for deletion and combine the delete for parent and
normal block id where possible.
In case there are no parent blocks this also removes a possible gap
lock. In case the unblock and the autoblock happens in same second the
autoblock may there without its block

Change-Id: I274d35834ce1e3e1d67fabd698d9a1cb3de9687a
2023-07-13 00:17:37 +00:00
Daimona Eaytoy
1c6e872bd4 Replace usages of deprecated MWException
Use SPL exceptions instead when the exception is unchecked.

Bug: T328220
Change-Id: Ia1e5edc2ef3269a44b670262b78b305d07559829
2023-06-06 17:05:32 +02:00
Alexander Vorwerk
31d063ca71 Deprecate passing a db to DatabaseBlockStore::insertBlock()
Instead of passing a custom database to DatabaseBlockStore::instertBlock
callers should rather just fetch a correct DatabaseBlockStore
using the new DatabaseBlockStoreFactory service.

Bug: T291849
Change-Id: Ia1319f35bbc5bd786d1d96d330f94255d7b3582a
2023-05-20 11:02:45 +02:00
Tim Starling
a784eae6d7 Clean up IDatabase::deleteJoin() return value assumptions
It was decided that deleteJoin() should continue to return void, so the
comment in BlockRestrictionStoreTest and the pass through in
BlockRestrictionStore were not correct.

Change-Id: I634e1e722d25d0cddeb27e051cb0d6da414ee393
2023-03-24 22:26:08 +00:00