Commit graph

45 commits

Author SHA1 Message Date
Thalia
2e7a155640 Exempt users with 'ipblock-exempt' right from all cookie blocks
A cookie block is a block in the database, pointed to by a cookie.
Blocked users who switch accounts or IP addresses may continue to
be blocked due to a cookie pointing to a block against their
original account or IP address.

This extends the 'ipblock-exempt' right to exempt users with the
right from all cookie blocks. The rationale for this is:
* A cookie block against an IP does not affect any logged in users
  (as codified in BlockManager::shouldApplyCookieBlock).
* A cookie block against a user's own account does not need to be
  enforced since they will be blocked directly.
* It was decided in T233441 that a cookie block against a different
  user's account should not affect a user with the 'ipblock-exempt'
  right, to avoid users with this right from being blocked as
  collateral damage.

Bug: T233441
Change-Id: I3912b13240e0b42ad83a9527eeba844e8601d764
2020-02-07 11:56:48 -08:00
James D. Forrester
0958a0bce4 Coding style: Auto-fix MediaWiki.Usage.IsNull.IsNull
Change-Id: I90cfe8366c0245c9c67e598d17800684897a4e27
2020-01-10 14:17:13 -08:00
jenkins-bot
cf92c2dc44 Merge "Use namespaced IPUtils class" 2020-01-01 23:56:22 +00:00
Kunal Mehta
99007e96c7 Use namespaced IPUtils class
Change-Id: I047e099a93203a59093946d336a143d899d0271f
2020-01-01 02:36:49 -08:00
Daimona Eaytoy
dbf0990447 Avoid PHP scalar type juggling in includes/ (part 2)
Continuation of e5444ea55a.

Change-Id: I9f95e7de4e219dee3abcdd210bb708d949f378d0
2019-12-30 20:57:18 +00:00
Dayllan Maza
e774df240a Remove blocker dependency for System and Composite blocks
The motivations behind this change is T227892 and that a blocker for a System or
Composite block provides no useful information for the end user.

Here is what's changing:
* Move the $blocker property to DatabaseBlock, since this is the only type of
  block that can be created by a user.
* Move handling of the 'by' and 'byName' constructor option from AbstractBlock to
  DatabaseBlock.
* getBy(), getByName(),  are now abstracts methods and each block type have to provide
  their own implementation
* getBlocker(), setBlocker() are being deprecated in AbstractBlock and moved as internal
   methods into DatabaseBlock

Bug: T227892
Depends-On: Ie2aa00cfec5e166460bcaeddba3c601fe0627c13
Change-Id: I1b17c652b12d98de3d996d309d4d3f22074be411
2019-10-31 07:45:07 -04:00
Tchanders
a6533885b8 Revert "Revert "Store block reasons as CommentStoreComments in block classes""
This reverts commit 5f06efb318, which
reverted 9335363789, which makes
the deprecated property AbstractBlock::mReason private.

After 9335363789, AbstractBlock::mReason is obsolete, since the block
reason is now stored as a CommentStoreComment, AbstractBlock::reason.

Change-Id: Ica0a74be90383689ca8e4cfe6d0fb25c9a5942c5
2019-10-20 10:41:17 +01:00
Daimona Eaytoy
5f06efb318 Revert "Store block reasons as CommentStoreComments in block classes"
This reverts commit 9335363789.

Reason for revert: It's full of code accessing AbstractBlock::mReason
out there, see [1]. Also, it was never hard deprecated. While that may
be acceptable under some circumstances, it's definitely not OK to remove
code when there are consumers around. I'd have fixed it right now without
reverting if it were a single repo, but there's just too many.

[1] - https://codesearch.wmflabs.org/search/?q=-%3EmReason&i=nope&files=&repos=

Change-Id: I8669f502b50cff89e28dada0f65fe2b130ae9b37
2019-10-19 18:55:45 +00:00
Thalia
9335363789
Store block reasons as CommentStoreComments in block classes
AbstractBlock::setReason now accepts a string, Message or
CommentStoreComment. The CommentStoreComment is accessed via
AbstractBlock::getReasonComment.

AbstractBlock::getReason returns the reason as a string, with
the language and format consistent with how block reasons were
built before this commit. This method is deprecated, since it
makes assumptions about the language and format needed. The
deprecated mReason property is no longer public.

Doing this (and T227005) will remove the implicit dependency of
BlockManager::getUserBlock on language, which causes a recursion
error if the block is checked before the user has loaded. It also
provides a mechanism for getting the block reason in a language
specified by the caller. (This does not apply to DatabaseBlock
reasons entered via the Special:Block form, which were not and
are still not translatable.)

This commit also updates authentication classes to return the
translated reason.

Bug: T227007
Change-Id: Iec36876e930dff96a256aebbdc39cbfb331c244e
2019-10-18 17:47:56 -04:00
Thalia
f8f458d921 Ensure block cookie is not removed early if blocked user logs out
Follow-up to 235cade5a3: if a block cookie already exists, do nothing
if shouldApplyCookieBlock determines that the block should apply to
the user.

Bug: T233595
Change-Id: I85b7e1153225770218e5ea1607b46fa7ed7d8101
2019-10-15 13:38:04 +01:00
Thalia
235cade5a3 Clear block cookie when tracking block, not when checking block
Before this commit, clearing a block cookie happens as a
side-effect of checking for a block cookie. After, a block cookie
is cleared if trackBlockWithCookie finds that there should be no
block cookie, but there is one.

Bug: T233595
Change-Id: Id5777361f95c60d2849cacba82f2ed9add647abf
2019-10-09 14:09:34 +01:00
James D. Forrester
40c35286cb Services: Convert BlockManager's static to a const now HHVM is gone
Change-Id: I01d6e18fc30bd61ba7ea5ce1c7c646524579c4ba
2019-10-08 11:24:22 -07:00
Timo Tijhof
0e1e4ee5de
block: Allow cookie-block tracking from any uncached web request
This was previously hardcoded from three places: 1) Upon viewing EditPage,
2) Upon viewing SpecialCreateAccount, 3) For any url if the user is
logged-in (User::loadFromSession/isLoggedIn).

== User::loadFromSession

Performing cookie blocks from here created a circular dependency because
Block may need the user language for localisation, which is determined by
asking the User object. This was previously worked around by using a
DeferredUpdate (T180050, T226777). Moving this logic explicitly to the
end of the pre-send cycle in MediaWiki::preOutputCommit breaks the cycle.
This is also where other request-specific handling resides already.

== Limited effect on unregistered users

When an unregistered user performs an edit, and gets blocked,
the cookie block is not applied until they open built-in editor
or CreateAccount page. This makes it more likely for a user's
IP to change meanwhile. Either intentionally, or simply due to
IPs varying naturally (e.g. between mobile locations, or when
going on/off WiFi). By applying it throughout sessioned page
views for unregistered users, it is more likely to get set.
Similar to what was already done for logged-in users.

This commit also makes the intent of not caching EditPage and
SpecialCreateAccount explicit. This was previously implicit
through nothing having called setCdnMaxage() and/or due to
Session::persist being checked for by OutputPage::sendCacheControl.

Bug: T233594
Change-Id: Icf5a00f9b41d31bb6d4742c049feca0039d0c9d9
2019-10-01 13:52:58 -04:00
Umherirrender
268346e562 phan: Enable PhanTypeMismatchArgument issue
Bug: T231636
Depends-On: I5de4f8f32a47c3f41c990ffe2ebd091fc23d1a58
Change-Id: I34d65fe3ff1916f2af675f0b1f19641b0cdfadc0
2019-09-19 20:11:42 +02:00
jenkins-bot
e2f0ee49d0 Merge "Log DNS blacklist matches with info level, non-matches with debug level" 2019-09-14 20:16:10 +00:00
Martin Urbanec
c64d6c4379 Log DNS blacklist matches with info level, non-matches with debug level
Bug: T230822
Change-Id: I036b38ac322181fdba4e9e6c1ff539f79b5bb79c
2019-09-14 18:49:30 +02:00
Daimona Eaytoy
290ab29617 Declare dynamic properties
This is for classes with a single undeclared property - aside from
BlockManager: I3f51fd3579514b83b567dfe20926df2f0930dc85 removed the
declaration of $permissionManager without actually removing all uses.

Change-Id: Ic2a95f77071312041be6e0633ea9b5325e98de42
2019-09-14 15:21:26 +02:00
Thalia
5fffa5c07a Pass the user and request into BlockManager::getUserBlock
Blocks are checked from the User object. Specifically,
User::getBlockedStatus instantiates a BlockManager and calls
BlockManager::getUserBlock. However, checking the block often depends
on knowing more about the state than the User should know. As a result,
the global user and request objects were passed into the block manager
on construction.

Whether the global request object should be passed into a service
constructor is still up for debate, so this moves the check for the
global state back to User::getBlockedStatus for now. (Note that it
reintroduces the problem of the User knowing more about state than it
should.)

This change also makes clearer the cases in which
BlockManager::getUserBlock is called from the User.

Different blocks may be sought, depending on the user and their
permissions. The user may be:
(1) The global user (and can be affected by IP blocks). The global
    request object is needed for checking the IP address, the XFF
    header and the cookies.
(2) The global user (and exempt from IP blocks). The global request
    object is needed for checking the cookies.
(3) Another user (not the global user). No request object is available
    or needed; just look for a block against the user account.

Cases #1 and #2 check whether the global user is blocked in practice;
the block may due to their user account being blocked or to an IP
address block or cookie block (or multiple of these). Case #3 simply
checks whether a user's account is blocked, and does not determine
whether the person using that account is affected in practice by any
IP address or cookie blocks.

Bug: T231919
Change-Id: I3f51fd3579514b83b567dfe20926df2f0930dc85
2019-09-11 08:23:54 +01:00
Daimona Eaytoy
c659bc6308 Unsuppress another phan issue (part 7)
Bug: T231636
Depends-On: I2cd24e73726394e3200a570c45d5e86b6849bfa9
Depends-On: I4fa3e6aad872434ca397325ed7a83f94973661d0
Change-Id: Ie6233561de78457cae5e4e44e220feec2d1272d8
2019-09-03 17:19:21 +00:00
Thalia
81f96bee1f Use UserIdentity::isRegistered instead of User::isAnon in BlockManager
BlockManager::getBlockFromCookieValue accepts a UserIdentity, so should
only call methods defined in that interface.

Change-Id: If30df15b800cceee0ad052a2e0524df8b28d9901
2019-08-27 07:23:58 +01:00
Thalia
fc0067d7d5 Disambiguate confusing $fromReplica variable in BlockManager
Change-Id: Ifd9bfbc40a52add5e8478c31a55a1c22e9d4693d
2019-08-23 13:32:41 +01:00
Thalia
7a5508573a Ensure block hooks keep user state consistent with realistic blocks
Several block-related hooks allow the user to be put into in a state
that is inconsistent with blocks that can actually be made:
* With UserIsHidden, User::mHideName can be set to true without there
  being a block
* With UserIsBlockedFrom, a user can be blocked from editing a page
  without there being a block
* With GetBlockedStatus, public block properties can be arbitrarily
  set on a user

These problems are mostly theoretical, but mean that it is impossible to
make some basic assumptions, e.g. that a user who is blocked from a page
must have a block. The hooks are not widely used, and with a few changes
we can make them more robust so such assumptions can be made.

This patch:
* Ensures UserIsBlockedFrom is only called if there is a block. This
  would be a breaking change if any extensions were using this to block
  an unblocked user; the intended use case is clearly for extensions to
  allow user talk page access to blocked users.
* Adds a new hook, GetUserBlockComplete, which passes the block for
  modification. This should be used instead GetBlockedStatus and
  UserIsHidden, which will be deprecated in the future.
* Allows the 'hideName' option to be passed into the AbstractBlock
  constructor so that suppressing system blocks can be made.

Bug: T228948
Bug: T229035
Change-Id: I6f145335abeb16775b08e8c7c751a01f113281e3
2019-08-21 17:38:52 +01:00
jenkins-bot
36fdf484e9 Merge "Replace User::isAllowed with PermissionManager." 2019-08-21 08:00:33 +00:00
Petr Pchelko
1d286560d2 Replace User::isAllowed with PermissionManager.
Covers root includes, actions, api, block, changes,
changetags, diff and PermissionManager itself.

Bug: T220191
Change-Id: Ic027d32f5dd8f4c74865df0c8a9fcf91123c889c
2019-08-20 14:43:51 -07:00
jenkins-bot
4304174a4c Merge "Clear block cookie if the value is invalid" 2019-08-20 19:21:15 +00:00
Thalia
07e5d6962f Put block cookie expiry into UTC for comparison with current time
Change-Id: Ia01a364a01ebacec8783ecdcfe4a129cfb746eb8
2019-08-20 16:22:14 +01:00
Dayllan Maza
dbc0d3c884 Clear block cookie if the value is invalid
When a block cookie is present and the block is invalid or doesn't exists
or the cookie value is invalid or malformed, the cookie is removed.

Bug: T227678
Change-Id: Icaff594686c16a0eb8551b2a4392a14a969b43b0
2019-08-19 14:59:25 -04:00
Umherirrender
2664eeb632 Clean up spacing of doc comments
Align the doc stars and normalize start and end tokens

Change-Id: Ib0d92e128e7b882bb5b838bd00c74fc16ef14303
2019-08-05 22:29:50 +00:00
Thalia
f7cddcf7c1 Remove deprecated handling of array keys for $wgProxyList
Change-Id: Ic9cc2a5585180ab57fd361342cbac8210b094a5c
2019-07-24 21:45:45 +01:00
Thalia
786a7a168a Pass in ServiceOptions to BlockManager
Change-Id: Ic63d7ff35a71e36c4e6157e9d472e2870f95f00d
2019-07-10 16:33:21 +01:00
Thalia
4b90befa9a Tidy up conditions for applying a block from a cookie
Change-Id: Id9dd6ae395f5bb811db4c741be9db8aa2eb6fb70
2019-07-10 16:28:55 +01:00
Thalia
6c0bc95a63 Mention relevant tasks in comment (follow-up to c8733333)
Change-Id: I25a8c018c879dd13967cd67fd51eae9768e3aa4b
2019-07-01 12:55:32 +01:00
jenkins-bot
9e43263709 Merge "Defer cookie block checks to resolve a circular dependency" 2019-07-01 11:09:45 +00:00
Max Semenik
c873333333 Defer cookie block checks to resolve a circular dependency
User needs to load user preferences to get preferred language, which
calls User::load() which calls User::loadFromSession().

User::loadFromSession() tries to load blocks which might use messages
which need user language which calls User::load() because the previous
call to it haven't completed yet...

We have a protection against infinite recursion to prevent this from
completely crashing MW, however this patch fixes the main issue: loading
too much stuff when a User is initialized.

Bug: T180050
Change-Id: I63af6d2239b36124d5ed382b8d2aab82c8d54d69
2019-06-27 22:37:44 -07:00
Max Semenik
48d355de81 MediaWiki\Block namespace minor tweaks
* Visibility
* Parameter type hints and docs
* Minor doc fixes

Change-Id: I46d4b99e18cffdf8323fb01b7ed30f3eda2906d1
2019-06-27 13:42:54 -07:00
Thalia
9510597f26 Filter out duplicate autoblocks when checking for blocks
Follow-up to I7654907.

Bug: T225919
Change-Id: I67e72d6c88e3cbfd9515a016b2782d1d9b123775
2019-06-20 22:01:44 +01:00
Thalia
f32ef0627c Filter out blocks with duplicate IDs when checking for blocks
Bug: T225919
Change-Id: I76549072d53083e6057f4fd8fe963e8989daa25c
2019-06-17 13:24:56 +01:00
Thalia
6a216b3831 Add a custom block message for composite blocks
Give a custom reason and include the user's IP address and the
expiry of the longest block.

Bug: T225748
Change-Id: Ie147cca625365c1a01e2ed18819e5d6e3946865e
2019-06-14 13:41:26 +01:00
Thalia
1eaf65d0a5 Add CompositeBlock class for enforcing multiple blocks
Create a CompositeBlock class which extends AbstractBlock and
adds the property $originalBlocks. This is for situations where
more than one block applies to a user/IP, and avoids the need
to choose just one of these blocks to enforce.

Behaviour of the resulting block is determined by combining the
strictest parameters of the original blocks.

Also add DatabaseBlock::newListFromTarget, which is similar to
DatabaseBlock::newFromTarget, but returns all relevant blocks,
rather than choosing the most specific one.

For tracking a CompositeBlock with a cookie, examine the
original blocks and only track the first trackable block that
is found.

Bug: T206163
Change-Id: I088401105ac8ceb2c6117c6d2fcdb277c754d882
2019-06-12 19:16:52 +01:00
Thalia
c5991f614f Move cookie-blocking methods to BlockManager
Move the cookie blocking logic into one place. Specifically, move
these methods to the BlockManager:
* User::trackBlockWithCookie
* DatabaseBlock::setCookie
* DatabaseBlock::clearCookie
* DatabaseBlock::getCookieValue
* DatabaseBlock::getIdFromCookieValue
* AbstractBlock::shouldTrackWithCookie

After this, BlockManager::trackBlockWithCookie should be called to
track a block, and BlockManager::clearBlockCookie should be called
to unset the cookie. The other methods in the above list are
helper methods that are made private or marked internal.

Also update places in core that call User::trackBlockWithCookie to
BlockManager::trackBlockWithCookie

Bug: T225141
Change-Id: I818962c6932c01c841a549a101637e00a7593e48
2019-06-11 15:08:21 +01:00
Thalia
e65a5b5882 Rename Block to MediaWiki\Block\DatabaseBlock
Keep Block as a deprecated class alias for DatabaseBlock.
Update calls to the Block constructor and Block static
methods from external classes.

Also update documentation in several places that refer to
blocks as Blocks.

Bug: T222737
Change-Id: I6d96b63ca0a84bee19486471e0a16a53a79d768a
2019-05-28 12:20:48 +01:00
Thalia
e9714e6a8d Make improvements to BlockManagerTest
Remove reliance on real domain and add more test cases.

Change-Id: Icd67fe8c1c9223a92a3775d34d0453328442d89e
2019-05-27 23:30:24 +01:00
Thalia
2843213120 Fix AbstractBlock param types in documentation
Change-Id: I503375485956d3c05da445542419fb62684ae34a
2019-05-14 13:42:50 +01:00
Thalia
824655f3b7 Separate Block into AbstractBlock, Block and SystemBlock
This commit splits the existing Block class into AbstractBlock, Block
and SystemBlock.

Before this patch, the Block class represents several types of
blocks, which can be separated into blocks stored in the database,
and temporary blocks created by the system. These are now
represented by Block and SystemBlock, which inherit from
AbstractBlock.

This lays the foundations for:
* enforcing block parameters from multiple blocks that apply to a
user/IP address
* improvements to the Block API, including the addition of services

Breaking changes: functions expecting a Block object should still
expect a Block object if it came from the database, but other
functions may now need to expect an AbstractBlock or SystemBlock
object. (Note that an alternative naming scheme, in which the
abstract class is called Block and the subclasses are DatabaseBlock
and SystemBlock, avoids this breakage. However, it introduces more
breakages to calls to static Block methods and new Block
instantiations.)

Changes to tests: system blocks don't set the $blockCreateAccount or
$mExipry block properties, so remove/change any tests that assume
they do.

Bug: T222737
Change-Id: I83bceb5e5049e254c90ace060f8f8fad44696c67
2019-05-07 17:36:31 -05:00
Thalia
52f7720227 Introduce a BlockManager service
This introduces a minimal BlockManager service, for getting blocks
that apply to a User.

Move the part of User::getBlockedStatus that checks for the blocks
into BlockManager::getUserBlock, and move the related helper
methods from User to BlockManager.

Hard deprecate or remove these helper methods, and move to private
methods in the BlockManager:
* User::getBlockFromCookieValue
* User::isLocallyBlockedProxy
* User::inDnsBlacklist

Soft deprecate these helper methods, and move to public methods in
the BlockManager:
* User::isDnsBlacklisted

Add tests to cover the methods moved to BlockManager.

Bug: T219441
Change-Id: I0af658d71288376735cebe541215383b56bb72e5
2019-04-29 17:47:55 +01:00