Commit graph

269 commits

Author SHA1 Message Date
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
jenkins-bot
4301ad207c Merge "exception: Tolerate no service container when trying DB rollback" 2022-09-30 19:04:02 +00:00
daniel
085b9d18c6 exception: Tolerate no service container when trying DB rollback
If the exception handler gets triggered before a service container is
available, it should not attempt a DB rollback. Attempting to access
the DBLoadBalancerFactory service object would cause a secondary error,
obscuring the original issue.

This might happen when the exception handler is triggered during
bootstrapping or in the installer.

Change-Id: I644bd0953aa5e690fea16d9fc11ca3f24cb3f104
2022-09-30 18:42:10 +00:00
Alexander Vorwerk
22dca3ab89 BadTitleError: Enable 1 hour CDN cache for this error response
Varnish is unable to cache these currently. However since pages with an
invalid title will never have any variable/editable content, we can
cache them unconditionally.

Also, undeprecate OutputPage::enableClientCache for this since it
is clearly useful here, but the parameter is being removed since
OutputPage::disableClientCache should be used to disable client side
caching instead.

Bug: T316932
Change-Id: Ib98fd165bffd9ca3dcbbcb8c6f06964dba7c90b7
2022-09-26 20:27:24 +00:00
Umherirrender
0f49ae2759 Use MainConfigNames constant to refer configs
Change-Id: Iddef589423d1e3f609b3cfbf6cc7437c6ad830b0
2022-08-17 21:27:48 +02:00
Fomafix
fdcaf249e3 Use spacey style also for code in comments and documentation
Change-Id: I5ab30489fb1241b89d1bec88a4d1fcebcc69bb9a
2022-08-12 08:59:10 +00:00
Thiemo Kreuz
e23b070b45 Make use of ?? and ?: operators where it makes sense
Change-Id: I1d9d62d80b17d1a05222c6e82f631cb801c311a8
2022-08-04 21:43:12 +02:00
Timo Tijhof
e506500bf0 language: Improve type hints in MessageCache.php
* Replace is_/throw with native where possible.
* Prefer strict comparisons where possible.
* Remove `@throws` for exceptions that are not meant to be checked
  or caught by callers.
* Make the separateCacheableRows() return hint more precise.

Change-Id: I1c14bb8faaf1b377b6d179d96e18331acff23c5b
2022-07-15 05:37:51 +00:00
Matěj Suchánek
1865180ae7 Do minor code cleanup
Remove dead code and fix typos. Should cause no change in behavior.

Change-Id: I5d293b842bc93a28b8bcd799a31b5e6e30fe692e
2022-06-24 13:52:42 +02:00
Umherirrender
eaac04cd3b Replace deprecated wfReadOnly/wfReadOnlyReason
Bug: T283978
Change-Id: I49f8c7bf1162dc834a1708e2e581f6fb264bbd0a
2022-06-20 15:35:24 +00:00
Timo Tijhof
0bb1538929 REST: Hide exception message when wgShowExceptionDetails=false
This reverts parts of I8520d8cb16 and Ib941c22d6b7e.

The documentation of ShowExceptionDetails, as well as all other
uses of it (e.g. MWExeceptionRenderer for index.php, API, and
ResourceLoader) take it to mean to hide both exception message and
details.

This is why MWExceptionHandler didn't have, and didn't need,
the added complexity of this as a parameter as this method
simply wouldn't be called at all in that case.

* Rename the method added in I8520d8cb16 to match the one
  in MWExceptionRenderer.

* Update REST handling to now print any exception details
  when it is true.

* Remove the now-unused code introduced in Ib941c22d6b7e.

Change-Id: I1a9920dea0bafe315a20489efbe46ea9b55b0f74
2022-05-26 14:45:50 +01:00
Timo Tijhof
30fe871e82 exception: Inject wgShowExceptionDetails from Setup.php
Follows-up I8520d8cb16 and Ib941c22d6b7ec5f1b9, and adds an internal
setter for use by Setup.php, and for wmf-config:rpc/RunSingleJob.php
which is a use case for setting it after Setup.php but before most
code execution. Interact with a component owner instead of directly
maintaining state and providing a shared API through the Config object.

Change-Id: I5c3d4b11f4e0eb3906ccb5b5fe3979e026459859
2022-05-22 19:46:18 +00:00
daniel
05b0937bdf Remove access to config globals from includes/exception
Allow callers of MWExceptionHandler::getStructuredExceptionData() and
jsonSerializeException() to explicitly control whether a backtrace is
included in the return value. This avoids the need to rely on the
LogExceptionBacktrace setting in static methods.

Bug: T294739
Change-Id: Ib941c22d6b7ec5f1b984bf5ded90652e42ad7b67
2022-05-12 20:33:15 +02:00
Aaron Schulz
db36853718 rdbms: make automatic connection recovery more robust
Rename canRecoverFromDisconnect() in order to better describe
its function. Make it use the transaction ID and query walltime
as arguments and return an ERR_* class constant instead of a bool.
Avoid retries of slow queries that yield lost connection errors.

Track session state errors caused by the loss of named locks or
temp tables (e.g. during connection loss). Such errors will prevent
further queries except for rollback() and flushSession(), which must
be issued to resolve the error.

Add flushPrimarySessions() methods to LBFactory/LoadBalancer
and use it in places where session state loss is meant to be
safely aknowledged.

Change-Id: I60532f86e629c83b357d4832d1963eca17752944
2022-04-14 11:09:31 +10:00
Alexander Vorwerk
60c3cae9b9 Update comment to use AtEase
Change-Id: I00bf95631509747330d1f58133046d489eed475e
2022-04-06 01:50:55 +02:00
Umherirrender
1f71eccf63 phan: Disable null_casts_as_any_type setting
Make phan stricter about null types by setting null_casts_as_any_type to
false (the default in mediawiki-phan-config)
Remaining false positive issues are suppressed.
The suppression and the setting change can only be done together

Bug: T242536
Bug: T301991
Change-Id: I0f295382b96fb3be8037a01c10487d9d591e7e01
2022-03-21 18:25:07 +00:00
Ladsgroup
31c1ca8658 Revert "rdbms: make automatic connection recovery apply to more cases"
This reverts commit 4cac31de4e.

Reason for revert: Blocking the train, reverting the chain.

Change-Id: I7f275b3a25379c6f3256e90947c8eed4b232c0f4
2022-03-17 20:11:10 +01:00
Aaron Schulz
4cac31de4e rdbms: make automatic connection recovery apply to more cases
Rename canRecoverFromDisconnect() in order to better describe
its function. Make it use the transaction ID and query walltime
as arguments and return an ERR_* class constant instead of a bool.
Avoid retries of slow queries that yield lost connection errors.

Add methods and class constants to track session state errors
caused by the loss of named locks or temp tables. Such errors can
be resolved by a "session flush" method.

Make assertQueryIsCurrentlyAllowed() better distinguish ROLLBACK
queries from ROLLBACK TO SAVEPOINT queries. For some scenarios,
only full tranasction ROLLBACK queries should be allowed.

Add flushSession() method to Database and flushPrimarySessions()
methods to LBFactory/LoadBalancer.

Also:
* Rename wasKnownStatementRollbackError() and make it take the
  error number as an argument, similar to wasConnectionError().
  Add mysql error codes for query timeouts since they only cause
  statement rollbacks.
* Rename wasConnectionError() and mark it as protected. This is an
  internal method with no outside callers.
* Rename wasQueryTimeout(), remove some HHVM-specific code, and
  simplify the arguments.
* Make executeQuery() use a for loop for the query retry logic
  to reduce code duplication.
* Move the error state setting logic in executeQueryAttempt() up
  in order to reduce code duplication.
* Move the beginIfImplied() call in executeQueryAttempt() up to the
  retry loop in executeQuery(). This narrows the executeQueryAttempt()
  concerns to sending a single query and updating tracking fields.
* Make closeConnection() and doHandleSessionLossPreconnect() in
  DatabaseSqlite more consistent with the base class by releasing named locks.
* Mark trxStatus() as @internal.

Bug: T281451
Bug: T293859
Change-Id: I200f90e413b8a725828745f81925b54985c72180
2022-03-09 15:49:38 +11:00
Kunal Mehta
11c7478c23 Use HTTP 404 for BadTitleErrors instead of HTTP 400
The use of HTTP 400 dates back to T35646, which was to address
caching proxies and mobile browsers incorrectly caching bad titles
as valid content. However, this also means that caches in front of
MediaWiki, like Varnish, don't cache it either. Since we know that
these titles will always have no content, having these get cached
in Varnish is totally fine. Presumably the 404 will be enough to
tell other crawlers or scrapers that there's still no content on
these pages.

There's some room for debate on whether a HTTP 400 or 404 is more
technically correct here, but emitting a 404 seems like the more
pragmatic option.

Change-Id: I7b16f30ca6fd9a68f2a410692582692610f1f944
2022-02-27 00:08:58 -08:00
Umherirrender
b126dbe3f2 Fix various documentation related to null types
The functions returning null or the class property is set explict null

Found by phan strict checks

Change-Id: I4a271093fb6526564d8083a08249c64cb21f2453
2022-02-26 10:31:24 +01:00
Timo Tijhof
e8fefb931d exception: Simplify MWExceptionRenderer to reduce influence of config
Remove use of two configuration globals. This follows-up to
commit 47adb6d65a (I1a691f01cd82e60) which aimed to access all config
via MediaWikiServices, but that isn't safe during error handling.

These two were only used in the low-level "plain text" and
"basic HTML" error pages, which already can't have any branding,
skinning or localisation; and are not how most exceptions are rendered.
in those edge cases, we can use the name of the software instead of
(trying) to use the name of the site.

== Remove use of $wgSitename  ==

In msg() and reportHTML(), remove use of $wgSitename in favour of a
generic fallback matching DefaulSettings.php. This does not affect
the common case of runtime fatals and timeouts, as we're only
changing the fallback after wfMessage() fails in msg(), or when
OutputPage is unavailable, which is typically only if services,
localisation or DB are also down (or not yet loaded). Most exceptions
happen when and after those have initialised fine.

Test case 1:
(Clean state) Edit ViewAction::show() to add `throw new RuntimeException();`
as its first statement, then try to view the main page.
This error page is unchanged. It is skinned, localised, and still uses
the configured sitename in the doc title.

Test case 2:
Edit index.php to call foo() instead of wfIndexMain(),
then try to view the main page. Before, this "minimal HTML" error page
would have a doc title of "Internal error - MyWiki", and now
"Internal error - MediaWiki".

Test case 3:
(Clean state) Edit Message::text() to add `throw new RuntimeException();`,
then try to view the main page. This results in a "plain text" error
page that doesn't even have an HTML doc, and is also unchanged.

== Remove use of $wgMimeType ==

In output(), remove use of $wgMimeType and remove the Content-Type
header that it was used for. This was redundant because the next
statements (reportHTML) already outputs Content-type. In reportHTML,
we sometimes delegate to OutputPage (if safe) and that honours
$wgMimeType already. In other cases, it is handled inline in with a
basic HTML error page, and that branch also sets the Content-Type
header already. In one case (reportOutageHTML) a header was not yet
set. For that one, I've added the missing header call and made it
explicitly text/html.

This is technically a bugfix, because our basic HTML error page is
HTML5, whereas $wgMimeType (which exists to allow enabling XHTML)
can be XHTML which we weren't following. In OutputPage and
Html::htmlHeader that would normally result in outputting `<?xml`.

Test case:
Edit ViewAction::show(), and add `throw new RuntimeException();`
as its first statement, then try to view the main page.
In devtools>network, there is still a proper Content-Type
and charset on the error page document.

Change-Id: I03cfa2b6155fb711582164852e7cab4c325a1b92
2022-01-30 03:11:01 +00:00
Jon Robson
6e314cd01f Exceptions should use the message box class
Shown when $wgShowExceptionDetails = false;

Bug: T300306
Change-Id: I69fb272a3e685a3f1cf3df24963b4e30ff17e362
2022-01-29 01:00:08 +00:00
Timo Tijhof
42ed920a7a exception: Undo use of MediaWikiServices
Partially reverts 47adb6d65a (I1a691f01cd82e60) as use of service wiring
is not safe in early code like entry points, Setup (incl WebStart/AutoLoader),
and error handling.

Change-Id: I021b8bf34dabe71ec196e89c957238247b1502d0
2022-01-12 22:09:05 +00:00
TChin
47adb6d65a Refactor global variables to use MediaWikiServices instead
Automatically refactors wg prefixed globals to use MediaWikiServices config using Rector. Doesn't include files that set globals or files that fail CI.

Rector Gist: https://gist.github.com/tchin25/7cc54f6d23aedef010b22e4dfbead228

* This patch uses a modified source code rector library for our specific use case and the rector will have different effects without it.

A writeup for future reference is here: https://meta.wikimedia.org/wiki/User:TChin_(WMF)/Using_Rector_On_MediaWiki

Change-Id: I1a691f01cd82e60bf41207d32501edb4b9835e37
2022-01-10 13:55:53 -05:00
Siddharth VP
b77dd0640c Fix typos in comments (M)
Change-Id: I5ab88a01ba3e5ea2aae853bb6f06492fbc84ceb5
2022-01-09 23:00:20 +05:30
Reedy
2a2bb1e9bd Remove or replace usages of "sane"
Bug: T254646
Change-Id: I096b2cf738a1395a14f1d47bcbed0c2c686c2581
2021-11-22 13:35:17 +00:00
jenkins-bot
79e081810d Merge "Revert "Introduce AbstractLocalizedException"" 2021-09-14 20:10:48 +00:00
TChin
e67f822bc9 Revert "Introduce AbstractLocalizedException"
This reverts commit 0d453d915a.

Reason for revert: Trying to find a better approach than an abstract class

Change-Id: I69c0ec25cb920f41cb0eb6b3ad921d7b02290581
2021-09-14 19:01:21 +00:00
jenkins-bot
eaa6763dee Merge "Introduce AbstractLocalizedException" 2021-09-10 17:56:08 +00:00
TChin
0d453d915a Introduce AbstractLocalizedException
AbstractLocalizedException acts like a shim to ILocalizedException in preperation for refactoring

Bug: T287405
Change-Id: I01e40e82457d49487829d9a435b7a1198369ecf6
2021-09-10 10:32:57 -04:00
jenkins-bot
7d9a98af09 Merge "MWExceptionHandler::rollbackMasterChangesAndLog: Hard-deprecate, unused anywhere" 2021-09-08 20:52:42 +00:00
jenkins-bot
2202cebdeb Merge "Using @return never documentation on always-throw-function" 2021-09-08 03:12:06 +00:00
James D. Forrester
7792a3bc74 MWExceptionHandler::rollbackMasterChangesAndLog: Hard-deprecate, unused anywhere
Change-Id: Ie96598f4e7230f8a9f56516676833b340e0c3fbb
2021-09-07 18:49:44 -07:00
Umherirrender
44fd53fee3 Using @return never documentation on always-throw-function
This helps phan to detect unreachable code and also impossible types
after the functions.
It helps phan to avoid false positives for array keys
when the keys are checked before

Bug: T240141
Change-Id: I895f70e82b3053a46cd44135b15437e6f82a07b2
2021-09-07 17:29:03 +02:00
James D. Forrester
3407458ea0 MWExceptionHandler: Rename rollbackMasterChangesAndLog to rollbackPrimaryChangesAndLog
Change-Id: I9a90b4f74eb65cd9e20ae9faa6d1949be96543c0
2021-09-03 17:36:34 -07:00
James D. Forrester
10324c232a ILoadBalancer/ILBFactory: Rename rollbackMasterChanges to rollbackPrimaryChanges
Bug: T282894
Change-Id: I31794e052d71160195dd3b6c29fea24bc98b356b
2021-09-02 12:50:52 -07:00
libraryupgrader
5357695270 build: Updating dependencies
composer:
* mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0
  The following sniffs now pass and were enabled:
  * Generic.ControlStructures.InlineControlStructure
  * MediaWiki.PHPUnit.AssertCount.NotUsed

npm:
* svgo: 2.3.0 → 2.3.1
  * https://npmjs.com/advisories/1754 (CVE-2021-33587)

Change-Id: I2a9bbee2fecbf7259876d335f565ece4b3622426
2021-07-22 03:36:05 +00:00
Gergő Tisza
3bf86e2413 Handle INormalizedException in MWExceptionHandler
Log INormalizedException messages in a structured way, allowing
the logging infrastructure to group them better.

Change-Id: I877909d1113ab93b4b8a115af5bb0fe039ea32d6
2021-07-07 22:01:27 +02:00
Andre Klapper
6077aa3ac4 Make "A database query error has occurred" message link to documentation
Bug: T272588
Change-Id: I87e7b9014ed4220383c3c83f265274e79643f59e
2021-06-23 17:44:30 +02:00
Arlo Breault
cd6b498718 Suppress timeout reporting if headers are already sent
Bug: T280450
Change-Id: I5f03e24f73f3f3063df92c9b9e1f449ca4ffa02e
2021-06-09 18:09:19 -04:00
DannyS712
f057d2253c Replace deprecated uses of PermissionManager with GroupPermissionsLookup
In a few places where a PermissionManager is used
but only GroupPermissionsLookup is needed

Also update references to the class in PermissionManager
that referred to it as GroupPermissionLookup

Change-Id: I5d7a13900852a38768a106aeee1ce012c3a04ea2
2021-05-26 05:47:21 +00:00
daniel
753b1bcaff Introduce Block interface and replace AbstractBlock.
In order to allow Authority to know about user blocks,
we need a narrow interface to represent such blocks.

This deprecates some methods on AbstractBlocks in favor
of new methods on the Block interface that avoid binding to
the User class.

Bug: T271494
Change-Id: I7bb950533970984a014de0434518fbbefb695131
2021-05-11 11:36:11 +02:00
Timo Tijhof
9911b03707 exception: Restore "PHP Deprecated" prefix for E_USER_DEPRECATED
Follows-up f2543d442a, in which I accidentally removed the prefix.

Also, while at it, drop the "$class: " prefix that we normally
add to exceptions. For errors we internally construct ErrorException,
but this is something a clas name rarely seen by PHP developers and
has sometimes led to confusions about whether this is an exception
or not. We did still have "PHP Notice" and "PHP Warning" right after
it (except for deprecations) which should act as confidence indicator
that they are in fact not exceptions. We also have the channel
name ("error" instead of "exception"). But removing this will help
boost that confidence further, plus it had no added value anyhow, so
less is more?

Change-Id: Ib93f48c94d642f519558aab40d143b43e1d9ed16
2021-04-07 19:21:29 +01:00
Petr Pchelko
58385ac713 Convert users of BlockErrorFormatter to UserIdentity
Change-Id: Ib655179851101fc9d249be5839b343c9c8bd98d2
2021-03-17 19:37:08 +00:00
Cindy Cicalese
f9e6c7b85b Allow PermissionError to be constructed with a PermissionStatus
Bug: T271504
Change-Id: I74d651c43cb67e928c627cb3812c6135232c99d8
2021-03-01 21:11:57 +00:00
Umherirrender
8de3b7d324 Use static closures where safe to use
This is micro-optimization of closure code to avoid binding the closure
to $this where it is not needed.

Created by I25a17fb22b6b669e817317a0f45051ae9c608208

Change-Id: I0ffc6200f6c6693d78a3151cb8cea7dce7c21653
2021-02-11 00:13:52 +00:00
Tim Starling
2697837fd8 RequestTimeout library integration
* Add $wgRequestTimeLimit. If it is non-null, it starts a time limit,
  using Excimer with a fallback to set_time_limit().
* Add pretty formatting of timeout exceptions thrown by the library.
  Related refactoring.
* Expose the library's critical section feature in MediaWikiServices
* In Setup.php, call warnIfHeadersSent() before sending session headers.
  This helped to debug a related issue I had.
* In wfTransactionalTimeLimit() use the new library, and respect the
  existing library time limit if it is larger than
  $wgTransactionalTimeLimit.

Bug: T269326
Depends-On: I6409ad8a5cba775c27b0d5a79d3300c4dac4c91a
Change-Id: I2e6f6351c451407c06cc7e20932548f7b62e36b6
2021-02-08 11:58:45 +11:00
Timo Tijhof
3a044d0f02 exception: Remove "exception_id" key in favour of reqId
This key predates MediaWiki's standardised notion of request IDs,
and of PSR-3 and Monolog usage.

Bug: T199607
Change-Id: Ibdd5bf12591761ab45be12ba72943e9d94f678cb
2021-01-18 05:33:17 +00:00