Commit graph

113 commits

Author SHA1 Message Date
Derick Alangi
0177af4c3c search: Deprecate the use of SearchResultThumbnail::getSize()
(quote): Not only is this intensive to compute (and could be an
attack vector). See also: T323125.

Bug: T323126
Change-Id: I404630ea9d580bce91d7c58f73391ce6b672178c
2023-05-29 11:04:31 +00:00
Tim Starling
5114267deb Rest: Allow private caching of requests with a session
Change-Id: I1ca1c8b3400510e8ca29b24002fddd8edf78e93c
2023-05-19 11:42:38 +10:00
Máté Szabó
3e910f1d5c Rest: Prevent caching responses for logged-in users
d70fbfc691 introduced a new helper
function to Rest handlers that prevent caching the response if it sets
any cookies. However, responses to requests where a cookie-based session
(anonymous user with session cookies or logged-in user) are not safe to
cache at all because the session manager may itself attempt to set
cookies on the response outside of the Rest framework, and the response
contents themselves may depend on user-specific invariants, such as the
user's permissions if the current wiki is private (i.e. restricts the
'read' permission to a subset of user groups). We currently rely on
HeaderCallback to fix the first case for us, and don't cover the second
case, so fix it by explicitly sending Cache-Control: private for Rest
responses for requests with an active cookie-based session.

Bug: T264631
Bug: T285210
Change-Id: I9dec6d4accd5de2bd1bde352d45f82c433913d54
2023-05-19 11:11:58 +10:00
Amir Sarabadani
775bfb6dc3 Rest: Migrate rest of handlers to IConnectionProvider
Instead of using LoadBalancer

Bug: T330641
Change-Id: I276da1460d9bbc6159fb2d496220b9cd2d2c9261
2023-04-20 14:38:39 +02:00
Tim Starling
580ec48e5b Fix more PHPStorm inspections (#2)
* Illegal string offset and invalid argument supplied to foreach, due to incorrect type information
* Array internal pointer reset is unnecessary
* $hookData unused since MW 1.35 due to incomplete revert
* array_push() with single element
* Unnecessary sprintf()
* for loop can be replaced with str_repeat()
* preg_replace() can be replaced with rtrim()
* array_values() call is redundant
* Unnecessary cast to string
* Unnecessary ternary. Often the result relies on short-circuit evaluation, but I find it more readable nonetheless.

Change-Id: I4c45bdb59b51b243fa96286bec8b58deb097d707
2023-03-25 00:19:58 +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
Daimona Eaytoy
19f8127ef0 Make it possible to override the session in REST API tests
The current signature of the various execute methods only takes a
boolean parameter to determine if the session should be safe against
CSRF, but that does not give callers fine-grained control over the
Session object, including setting a specific token.

Also, do not use createNoOpMock in getSession(), since it implies
strong assertions on what methods are called. This way, getSession
can also be used to get a simple mock session that tests may further
manipulate.

Make $csrfSafe parameter of SessionHelperTestTrait::getSession
mandatory. This way, callers are forced to think what makes sense in
each use case. The various methods in HandlerTestTrait now default to
a session that is safe against CSRF. This assumes that most REST
handlers don't care about the session, and that any handler that does
care about the session and where someone needs to test the behaviour
in case of bad/missing token will explicitly provide a Session that
is NOT safe against CSRF.

Typehint the return value of Session(Backend)::getUser so that PHPUnit
will automatically make it return a mock User object even if the method
is not explicitly mocked. Remove a useless PHPUnit assertion -- setting
the return value to be X and then veryfing that is equal to X is a
tautology, and can only fail if the test itself is flawed (as was the
case, since it was using stdClass as the return type for all
methods). Remove the getUser test case altogether, there's no way to
make it work given the DummySessionBackend, and the test isn't that
helpful anyway. More and more methods will have the same issue as soon
as their return value is typehinted.

Follow-up: I2a9215bf909b83564247ded95ecdb4ead0615150
Change-Id: Ic51dc3e7bf47c81f2ac4705308bb9ecd8275bbaf
2023-02-06 18:56:51 +01:00
Alexander Vorwerk
c62ac1bf56 Rest: Check for the correct header when setting cache-control
The response header is called 'Set-Cookie', not 'Cookie'.

Change-Id: Ifbf8755f73bcb68e95d26cb1500e3526db308186
2023-01-21 04:01:59 +01:00
Derick Alangi
1afd52e3e4 REST: Move Helper classes to their own namespace
Mixing Handlers with Helpers doesn't look nice for consistency
reasons. Helpers should be in their own place (grouped) in the
Handlers directory as they're really "helpers for the handlers".

Change-Id: Ieeb7a0a706a4cb38778f312bfbfe781a1f366d14
2023-01-16 21:16:09 +01:00
Daniel Kinzler
f2e9d5108d REST: collect metrics on endpoint access
This is a modified version of Ie282bc5b5f5df0bbd6a40c8362ba73fcbbf36c2e
which was reverted in 5c7cca8776.

Bug: T321969
Change-Id: I566d54a473aa51c4cdaada21a49d63c0624aab93
2023-01-12 14:50:58 +00:00
jenkins-bot
4fde3b96de Merge "ParsoidOutputAccess should support all models that serialize to wikitext." 2022-12-16 19:36:18 +00:00
daniel
e1c3af9177 ParsoidOutputAccess should support all models that serialize to wikitext.
The motivation is to restore parsoid support for the content models
defined in the Proofread extension.

Bug: T246403
Change-Id: I33d269e42fede28139f7c923504326a77d11ee13
2022-12-16 12:20:10 +01:00
Derick Alangi
ce8e5f1549 Introduce HtmlMessageOutputHelper for system messages
This introduces an interface HtmlOutputHelper that is implemented
by both HtmlMessageOutputHelper or HtmlOutputRendererHelper based
on the page we're dealing with.

Bug: T323558
Change-Id: I1fb8dcc5cc05ce3f32f3c1862b88045f1c8e612b
2022-12-16 11:49:56 +01:00
jenkins-bot
dba0bf3877 Merge "add redirects to page/history and link endpoints" 2022-12-15 11:37:43 +00:00
jenkins-bot
81ee9e8ca8 Merge "Revert "REST: collect metrics on endpoint access"" 2022-12-12 22:16:02 +00:00
Daniel Kinzler
5c7cca8776 Revert "REST: collect metrics on endpoint access"
This reverts commit d32c260ed0.

Reason for revert: Timo has reservations, I'll submit an updated version later.

Change-Id: I71d4d61a879fda4dccfc105127446cfedde75a7b
2022-12-12 20:05:06 +00:00
msantos
63deedc94c add redirects to page/history and link endpoints
* Add normalization redirects for page/history, page/history/counts
and links/language endpoints

Bug: T301372
Change-Id: I731fbfdcfb538c3e8a5dd32349ebaa0e642bf338
2022-12-09 12:17:38 +00:00
jenkins-bot
d9eca3447e Merge "REST: collect metrics on endpoint access" 2022-12-06 21:03:41 +00:00
daniel
d32c260ed0 REST: collect metrics on endpoint access
This collects metrics on how often each endpoint is hit, and with which
code it responds.

Change-Id: Ie282bc5b5f5df0bbd6a40c8362ba73fcbbf36c2e
2022-12-06 12:46:01 +01:00
msantos
fb5c29e2c0 Follow redirects for page/{title} formats source/bare
* Share logic previously implemented for html/with formats through
a trait class

* source/bare formats doesn't execute a temporary redirect. the
JSON body will contain a key "redirect_target" instead if a wiki
redirect is found

* Introduce PageRedirectHandlerTest to test redirect logic shared
between multiple handlers

* Move Handler instatiation to HandlerTestTrait

* Update api-testing tests in Update.js

Change-Id: Id66e33e19adabdb3c9621eaea4a5d441f23edafd
2022-12-02 13:22:14 -03:00
Umherirrender
c6fecd5ab0 tests: Replace assertEmpty with assertSame
assertSame avoids use of loose comparisons and
allows to check the expected type

Change-Id: If821d1be2bc1ff9f8b70968a339f33c7a8e8880a
2022-11-25 00:42:53 +01:00
thiemowmde
0b80e9ebcc Fix incomplete ITextFormatter mocks
Otherwise the mocked getLangCode() method returns null, which is not
allowed any more in PHP 8.1.

Bug: T289926
Required-For: I7e026cca216aba24ee5d5662b6fca322b3cec9ae
Change-Id: I178def7f03a44f6b49cdb461d9ab340e1c89517f
2022-11-21 10:00:57 +01:00
daniel
d70fbfc691 REST: introduce applyCacheControl()
This adds applyCacheControl() to the Handler base class.
REST handlers can override this method to apply cache control to the
response.

Change-Id: Ib31b91a0048dbc57fbcadfec6533e672c1567509
2022-11-21 01:22:34 +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
43a93d9782 Use the null coalescing assignment operator
Available since PHP 7.4.

Automated search, manual replacement.

Change-Id: Ibb163141526e799bff08cfeb4037b52144bb39fa
2022-10-21 13:26:49 +11:00
Daimona Eaytoy
7a494d1fa4 tests: Upgrade PHPUnit from 8.5+ to 9.5+
* DeprecatedHooksTest: Don't use assertContains().
* Replace uses of deprecated asserts:
  - assertFileNotExists() -> assertFileDoesNotExist()
* Update hierarchy of MediaWikiPHPUnitResultPrinter, since ResultPrinter
  is an interface in PHPUnit 9.
* Remove temporary forward-compat methods.
* Remove directories that don't exist from tests/phpunit/suite.xml, since
  they now make PHPUnit exit:
   - tests/phpunit/skins, it used to have SideBarTest, then moved to
     tests/phpunit/includes/skins
   - tests/phpunit/documentation, it used to have ReleaseNotesTest, then
     moved to tests/phpunit/unit/documentation
* Update configuration with --migrate-configuration and reformat.
* Avoid redefining getMockBuilder() in
  ActionModuleBasedHandlerTestTrait, use a @method annotation instead.
* In RCCacheEntryFactoryTest, avoid using internal PHPUnit logic for
  HTML validation, and use native PHP methods instead. The code was
  copied from Xml::load (moved to \Xml\Loader::load in PHPUnit 9) and
  simplified for this use case.

Bug: T243600
Bug: T262076
Change-Id: I851b9158b73d0cfc315eed9d63b15c54b05895e3
2022-10-08 02:03:55 +02:00
Daimona Eaytoy
0efa03ec0e phpunit: Avoid deprecated at() matcher
It was deprecated in PHPUnit 9, as tests should not rely on the
invocation order.

Bug: T243600
Change-Id: I8a4e44feb53d44aa9fd6facd5f85529d5bbdc45b
2022-10-06 18:57:24 -04:00
Matthias Mullie
c4a36b0023 Introduce SearchResultThumbnailProvider & move hook + NS_FILE thumbs in
What was previously a REST API-only feature (the thumbnails
hook allowing for thumbnails for non-file pages via the
PageImages extension) is now also being adopted in the main
search page.

That hook will now be called with NS_FILE result thumbnails
pre-filled, which was not the case previously. PageImages
essentially duplicated NS_FILE thumbnail logic that was
already present in Special:Search, so that can (and will
in a follow-up patch) then be removed there. Special:Search
will then simply take whatever is produced from the provider
(which will include both NS_FILE thumbs - which it handled
already - as well as whatever else it receives from the hook),
as will the REST API (which already received both)

Since thumbnails can now come in for multiple namespaces &
having some of those results with & others without a thumbnail
can be quite jarring, it was decided that we'd display
placeholder images (for certain namespaces). This is now
controlled by $wgThumbnailNamespaces.

I also split up a few things in FullSearchResultWidget::
generateFileHtml for more clarity.

Meanwhile also updated mediawiki.special.search.styles.less
to use variables for known colors.

Also implemented a 'transform' (required for testing this
change properly) and 'getDisplayWidthHeight' (it became
needed after implementing transform) callback function for
mock Files, and updated some existing tests in response to
these changes.

And some more Rest test files have been updated to allow
passing around a HookContainer instead of only an array of
hooks (from which a new HookContainer would then be created)
to allow the same container to be used across all relevant
objects, who may have it injected as dependency.

Bug: T306883
Change-Id: I2a679b51758020d3e822da01a1bde1ae632b0b0a
2022-10-03 10:52:50 +00:00
Umherirrender
167fb2a979 unit tests: Use MainConfigNames constant to refer configs
When creating ServiceOptions objects or fake HashConfigs use the
constant to refer the config name

Change-Id: I59a29f25b76e896c07e82156c6cc4494f98e64cc
2022-08-17 22:33:58 +02:00
Umherirrender
227d4ff702 search: Replace deprecated User::getOption
Bug: T296083
Change-Id: I83e4cca11c9266e93b237ed11c14293a001609dc
2022-08-05 22:31:12 +00:00
Brian Wolff
6114f0f1ba Make HandlerTestTrait compatible with php8.1
Cast values to string. This seems to be consistent with what
Router::substPathParams does, and also would be consistent with
the pre php-8.1 behaviour.

Bug: T313663
Change-Id: I631478101b7a679efa7bb897c6c2bb5d51d6281b
2022-07-23 11:58:47 -07:00
daniel
aad4f8719a Clarify handling of verifier headers in POST and PUT
Besides being used for caching of the responses of GET requests,
the ETag and Last-Modified headers can be used with PUT and POST
requests to control the conditional execution of requests using
the If-Match and If-Unmodified-Since headers.

For that reason it is important to distinguish between the ETag and
modification date of a resource at the time a request is received
(before the resource is modified), and at the time the response is
being sent (after the resource has been modified). The before-state
is used to check conditionals, while the after-state can be sent back
to the client in the response.

For a GET request, it is reasonable to re-use the ETag and Last-Modified
values that were used to check conditionals in the response.
For PUT and POST, that would generally be wrong, since the resource has
since changed.

Bug: T311819
Change-Id: I1a09b0ef35ae2365e0b261fb5295003aec1f5b17
2022-07-05 03:38:45 +00:00
Bill Pirkle
7295100773 Allow REST API handlers to require csrf-safe session providers
Bug: T305043
Depends-On: Ic7c1b19b86e8a151e2d42aaec00ef0e89db77f08
Change-Id: Ic6bd48b400ecd839ef99b518ef955781470cd05c
2022-05-20 16:52:54 +00:00
Umherirrender
2909d06a08 Use new namespace for revision related classes
All revision related classes are namespaced MediaWiki\Revision
instead of MediaWiki\Storage since 1.32. The old namespaced
class names are deprecated and only kept for backwards-compatibility.

Bug: T305784
Change-Id: I34e492d84d9fc4bc78481667202716d93b3c43cb
2022-04-14 23:03:43 +02:00
Nikki Nikkhoui
9761e17107 Include Special pages in Search Results
Only check if "real" pages are redirects,
since our RedirectLookup::getRedirectTarget()
does not allow passing in a non-real (e.g.
special page) for redirect lookup.

Bug: T301346, T303352
Change-Id: Icd1fd980bb98f7271b26048b6901927b35549820
2022-04-08 00:01:59 +00:00
jenkins-bot
5ec380a8f6 Merge "Integration test for extension registration" 2022-03-16 17:05:36 +00:00
daniel
1c7ffcd46a Integration test for extension registration
This adds full integration tests for extension registration, ensuring
that ExtensionProcessor and ExtensionRegistry work together as expected.

The existing tests for ExtensionProcessor and ExtensionRegistry are
converted to "pure" unit tests.

This change is intended to protect against bugs while refactoring
ExtensionProcessor and ExtensionRegistry to better integrate with the
new SettingsBuilder class.

Bug: T297166
Change-Id: I2d9cc373b8a4aa014aef93b0a8e8d83758851783
2022-03-11 21:43:41 +00:00
Alexander Vorwerk
9b09bf3112 Use updated ObjectFactory namespace
Depends-On: I99c5e5664d2401c36a9890f148eba7c25e6e8324
Depends-On: I48ab818b2965da14af15ef370aa83ad9455badd9
Depends-On: I018371e4b77911e56152ca7b2df734afc73f58a5
Change-Id: I04ebdb52102f6191d49a9cc70b1f98308299e72f
2022-03-09 23:04:51 +00:00
Alexander Vorwerk
c4649ddaee SearchHandler: skip search results which are no proper pages
Bug: T301346
Change-Id: Iaf38791bb0cb98983d8a1c8dc87a7ebdd96e4235
2022-02-09 16:06:13 +01:00
Nikki Nikkhoui
9805798a21 Add matched_title field to /search/page results
Add a field to the response object of the REST
endpoint /search/page to display the title of the page
that the given page is a redirect to, or null if
the page is not a redirect.

Bug: T296671
Change-Id: I6673d50e8eae822455972403c82ec33e6ffce5dd
2022-02-08 10:18:46 -05:00
Petr Pchelko
a7b0143c57 File: remove deprecated methods
Change-Id: I961e62f812f2e1b86b858be7eeee8bc042542689
2021-11-01 19:58:21 -07:00
Nikki Nikkhoui
0b3a4c0fa8 Do not cache private wiki completion results
Previously, when a user with correct permissions uses completion search on a
private wiki, the results are returned and cached. Since we are on a private wiki,
we don't want to cache results since the content is not accessible to all users.

Now, content that is not accessible to all users will not be cached.

This patch achieves this by setting the appropriate Cache-Control response headers
for the MW REST Search endpoint.

Bug: T292763
Change-Id: I693b4088df9c0520d5238c286312ec52ab273604
2021-10-12 13:12:05 +00:00
Kunal Mehta
a85f569dd1 Revert "Use CsrfTokenSet as CSRF token source"
This reverts commit 0d75fdb4f7.

Bug: T287542
Change-Id: Iedd3461869f973f8d621a39e6ad4674cbb577551
2021-08-05 15:48:26 -07:00
Petr Pchelko
339fd93f67 Make MovePageTest actually test valid moves
Before MovePageTest was skipped if the move was valid,
claiming we can't test actual moves. Now we can.

Additionally, use MediaTestTrait for file and repo
mocking.

Change-Id: Ie8a1edbdb2f22432919f03a60c2dacc5d4528615
2021-07-28 09:18:59 -07:00
DannyS712
91ac8a717d Add UserNameUtils to DummyServicesTrait, and use it
Allows us to reduce direct mocking in individual tests, or relying
on MediaWikiServices, by having a reusable service instead that
can be used in unit tests.

Make use of it in a few places.

This dummy service is also less coupled to individual tests;
in the rest handler tests, replacing mocks that report that
only a specific string is an ip address with the actual implementation
that accepts other ip address, and in BlockUtilsTest use
an actually invalid user name instead of making the mock
UserNameUtils complain about a perfectly valid name.

Change-Id: Iaa3195002ac4ae7e0c9c58ed57c80c02762d4070
2021-07-27 12:17:06 +00:00
Petr Pchelko
0d75fdb4f7 Use CsrfTokenSet as CSRF token source
Change-Id: I079d2c802d9b48d6abf7f37fa9ef7dafac631345
2021-07-12 14:19:15 -07:00
Petr Pchelko
5455e58967 Deprecate File::getUser in favor of File::getUploader
Change-Id: I8a45a8fdfa827f203e6bc123cb685d02c3612bb0
2021-06-02 09:06:09 -07:00
Petr Pchelko
4ca16e8d08 Eliminate use of Title object in REST infrastructure
Change-Id: I585f0f23cac5f6dc2a4879f69f7b83828fda3dd3
2021-05-05 18:54:58 -07:00