Commit graph

95 commits

Author SHA1 Message Date
Umherirrender
f910edce6d Add @phan-mandatory-param for function name arguments
Providing the function name is often optional from the php code,
but it is needed for better logging, so make it mandatory and let phan
report issues about this.

Bug: T374546
Depends-On: Iaed5489a85a5a6e685829e151436afc94310fbd0
Depends-On: Ie2a1e5052e5b61bbb5b89905de942f47d3f1413d
Change-Id: I5227f2fa65850ac8c6f620900f22d1f4e7bfd470
2024-09-20 16:23:17 +00:00
jenkins-bot
70e6842e56 Merge "Add Phan taint test for StatusFormatter methods" 2024-07-26 20:25:19 +00:00
Umherirrender
fc9e42823b rdbms: Create IReadableDatabase::andExpr() / ::orExpr()
Avoid the call to internal constructor of AndExpressionGroup and
OrExpressionGroup by creating a factory function similiar as the
IReadableDatabase::expr function for Expression objects.

This is also a replacement for calls to ISQLPlatform::makeList with
LIST_AND or LIST_OR argument to reduce passing sql as string to the
query builders.

Created two functions to allow the return type to be set for both
expression group to allow further calls of ->and() or ->or() on the
returned object.
Depending on the length of the array argument to makeList() it is
sometimes hard to see if the list gets converted to AND or OR, having
the operator in the function name makes it easier to read, so two
functions are helpful in this case as well.

Bug: T358961
Change-Id: Ica29689cbd0b111b099bb09b20845f85ae4c3376
2024-07-11 15:29:20 +00:00
Bartosz Dziewoński
0b61d6feb4 Add Phan taint test for StatusFormatter methods
Change-Id: I35839c7b8a0454d1913bfee0700f5cc3313456c1
2024-07-09 01:12:10 +02:00
jenkins-bot
6dfa3b8a3b Merge "rdbms: Create RawSQLValue for SET clauses in update/upsert" 2024-05-29 11:31:31 +00:00
Bartosz Dziewoński
bce5fd0a18 rdbms: Add Phan type hint for allowed operators in expressions
It is really annoying that we have to escape '<' and '>'
as '\x3E' and '\x3C', otherwise Phan will not accept them
(emitting PhanUnextractableAnnotation).

Phan will also escape them in the output, generating errors like:
  Argument 2 ($op) is '=<' of type '=\x3c'
  but \Wikimedia\Rdbms\IDatabase::expr() takes
  '!='|'='|'LIKE'|'NOT LIKE'|'\x3c'|'\x3c='|'\x3e'|'\x3e='

I've wanted to do this for a while, but I wasn't sure if it was
worth it, because the escaping makes the type hints and the errors
hard to read. Today I noticed a bug in UserSelectQueryBuilder
that would have been caught by this, and convinced myself.

Change-Id: I7a72368446f463a99b3d0cc7a90e8f7cdca454fe
2024-05-22 01:38:31 +02:00
Bartosz Dziewoński
2a6f12dd7d rdbms: Create RawSQLValue for SET clauses in update/upsert
Bug: T210206
Co-Authored-By: Umherirrender <umherirrender_de.wp@web.de>
Follow-Up: Ieb73d449262e22557f6f470105ca65ab0afc50e3
Change-Id: I18c6973713badd2b035aa7f44048344f3494b7b0
2024-05-21 19:16:16 +00:00
Bartosz Dziewoński
9ae6fbda79 rdbms: Warn when given an assoc array but keys are ignored
In some cases ISQLPlatform::makeList() will accept an associative
array (with string keys), but ignore the keys completely. Provide a
warning when this happens, and improve type hints so that Phan warns
about it too.

(We only warn about string keys, and not about gaps in an array
with int keys, because they arise often due to using array_diff()
or array_unique(), and that would trigger too many warnings.)

In Expression we already type-hinted it as a list (int keys with no
gaps), and we're intentionally less flexible about some other cases,
so check for gaps too and throw an exception instead of a warning.

Change-Id: I63717d16eae7cccd929b5d232944b97989113b1e
2024-05-20 17:20:28 +00:00
Bartosz Dziewoński
600b012b49 rdbms: Tighten type checks and PHPDoc for SQL WHERE/$conds arrays
In all cases, these mistakes would have already caused an error
("Wikimedia\Rdbms\Expression could not be converted to string")
or a notice ("Array to string conversion"). But now we also get
Phan warnings to avoid them, and a better message if they happen.

Add tests describing the invalid cases.

Change-Id: Idd4ba30a8145451e85018c35d5441910af76f009
2024-05-17 14:45:53 +00:00
Ebrahim Byagowi
14b4269d83 Add namespace and deprecation alias to Xml and XmlSelect
This patch introduces a new namespace declaration,
MediaWiki\Xml and adds Xml and XmlSelect to it
and establishes class aliases marked as deprecated
since version 1.43.

Bug: T353458
Change-Id: I45cccd540b6e15f267d3ab588a064fbeb719d921
2024-05-16 15:23:14 +03:30
Bartosz Dziewoński
e7c3381306 rdbms: Add more tests, and Phan tests, for expr() / Expression
I've been having fun documenting all the terrible array shapes
accepted by the various RDBMS methods with Phan type hints,
but I'm increasingly worried that either Phan will change how
it interprets them or that someone will break them by accident.

Add a test file, similar to the one we have for testing taint
annotations for Phan SecurityCheckPlugin, to guard against that.
Start filling it with some tests for expr() / Expression.

Add two missing test cases I noticed while writing these.

Change-Id: Icb54d5a7529f7f82ff5d130dcea0a22450155c10
2024-05-12 01:45:45 +02:00
Umherirrender
04a8388d43 rdbms: Expand taint annotations for SelectQueryBuilder/UnionQueryBuilder
phan-taint-check-plugin hardcodes the sql options
GROUP BY, ORDER BY, HAVING, USE INDEX, IGNORE INDEX
to have taints. The query builder should have same taints
(MWVisitor::checkSQLOptions)

Change-Id: Ie56e1d30a760a0203de6989972cfa1215344fd31
2024-04-04 19:58:20 +00:00
jenkins-bot
e2bbc1ae0c Merge "rdbms: Add taint annotations to ReplaceQueryBuilder" 2024-04-04 19:19:45 +00:00
jenkins-bot
531dac838b Merge "rdbms: Annotate ReplaceQueryBuilder::rows as list" 2024-04-04 19:04:50 +00:00
Umherirrender
6c49f3aef4 rdbms: Add taint annotations to ReplaceQueryBuilder
Similiar to the same-named functions in InsertQueryBuilder

Change-Id: I830ea07e4eb84ff39beb70ec50544212e9dc0ea3
2024-04-04 19:46:00 +02:00
Amir Sarabadani
27dca7fb2a rdbms: Introduce RawSQLExpression for edge cases
This is useful for cases that we can't use the common expression
builders but we don't have a choice but to pass raw SQL. Common usecases
are comparing fields ("a.user_id > b.user_id") and function calls
("user_editcount > COUNT(rev_id)")

Bug: T210206
Change-Id: Ieb73d449262e22557f6f470105ca65ab0afc50e3
2024-04-02 20:04:58 +02:00
Umherirrender
999f1f6641 rdbms: Annotate ReplaceQueryBuilder::rows as list
Annotate InsertQueryBuilder::rows the same

Change-Id: Idb828d9fd15a10890830ce7d381c266f6b8f19a3
2024-04-02 00:12:55 +02:00
Bartosz Dziewoński
dfea47fa1b Annotate SQL expression builder methods with @phan-side-effect-free
…to avoid mistakes where the return value of ->and() etc. isn't used.

https://github.com/phan/phan/wiki/Annotating-Your-Source-Code#phan-side-effect-free
> `@phan-side-effect-free` can be used to indicate that a function,
> method, or closure does not have any side effects, and should have
> its return value be used.

Change-Id: I610435f4cbf7335605a6713758d15f8722478b72
2024-03-06 17:29:50 +00:00
C. Scott Ananian
72c4945a72 [ParserOutput] Rename $mText to $mRawText and ::setText() to ::setRawText()
ParserOutput::getText() is not a simple getter, but does
transformations on the "text" of the ParserOutput; the simple getter
is named ::getRawText().

To maintain consistency, rename ParserOutput::setText() to
::setRawText() and the property name ParserOutput::$mText to
::$mRawText so future readers are not confused.

The JSON property name as it appears in the serialized ParserCache
is left as 'Text' so that we don't have any forward- or backward-
rollback issues.

Change-Id: I3ef34814ab9473cc70d0a6806e8c5a4a02b73491
2024-02-20 17:13:28 +00:00
James D. Forrester
102a4f8a35 build: Upgrade mediawiki/mediawiki-phan-config from 0.13.0 to 0.14.0 manually
* Switch out raw Exceptions, mostly for InvalidArgumentExceptions.
  * Fake exceptions triggered to give Monolog a backtrace are for
    some reason "traditionally" RuntimeExceptions, instead, so we
    continue to use that pattern in remaining locations.
* Just entirely give up on PostgresResultWrapper's resource vs. object mess.
* Drop now-unneeded false positive hits.

Change-Id: Id183ab60994cd9c6dc80401d4ce4de0ddf2b3da0
2024-02-10 02:22:41 +00:00
Brian Wolff
f1af33be38 Add taint annotations for ParserOutput
Change-Id: Id73b8f22f8877442f114bf7b41d0f9ea47fb4283
2024-01-12 14:17:21 +00:00
Amir Sarabadani
f64db42f62 rdbms: Drop some of class aliases of DatabaseMySQL
They were supposed to be dropped before 1.41 release.

Depends-On: Icd972535725e65e6eaed25607f1fd1c69d6b3a5c
Change-Id: Ieeaf39ec07407a55daa51c350cd57b66283a27bb
2023-10-31 23:18:21 +00:00
Bartosz Dziewoński
05603086cd Add taint-check annotations and test for SQL expression builder
Change-Id: Ia3c8d1faff20b86e0a5ddf5adbc05dc9daff26cf
2023-10-20 20:30:06 +00:00
Brian Wolff
85ab3f49a5 Improve OutputPage taint annotations and cover with more tests
addHeadItem was marking the wrong parameter as exec tainted.
Add tests for methods that should have their taint infered.
Add some additional taint annotations that seem missing.

Change-Id: I5d11d1c6ce44d18cca6844f2bad2c10b1ae43311
2023-10-04 12:14:39 -07:00
Brian Wolff
d56be83239 Make OutputPage::addSubtitle as tainted
See related T347746

Change-Id: I20a7b78440e22d373424c555809d582ebc065201
2023-10-04 10:53:52 -07:00
jenkins-bot
f247852e07 Merge "Add base taint-check annotations to Delete- and UpdateQueryBuilder" 2023-09-28 14:35:57 +00:00
jenkins-bot
21853ff498 Merge "Add base taint-check annotations to InsertQueryBuilder" 2023-09-28 14:35:45 +00:00
Daimona Eaytoy
eabe54c620 Add base taint-check annotations to Delete- and UpdateQueryBuilder
Bug: T253380
Change-Id: I398a65e22b154702f6ec42bfff2676f4720b99ea
2023-09-28 02:06:34 +02:00
Daimona Eaytoy
57a311c375 Add base taint-check annotations to InsertQueryBuilder
Note that row() and rows() cannot be described with annotations, and
they need to be hardcoded in taint-check instead
(I7623ba5112bfffbcf972cc366e0db99ee75b448e).

Also annotate SelectQueryBuilder::caller(), as a follow-up to
Ic9fb15e083cca75c2b5c6bddd1df87b148acca6e.

Bug: T253380
Change-Id: Id4a884f5b91683d3946c712ac4149ffe855d7683
2023-09-28 01:58:20 +02:00
jenkins-bot
e96af817d8 Merge "Add base taint-check annotations to SelectQueryBuilder" 2023-09-25 15:47:58 +00:00
Daimona Eaytoy
40a4dbb99e Add base taint-check annotations to SelectQueryBuilder
This covers the tables, fields, and conds methods. Options and join
conds are more complex and will have to be special-cased in the plugin
itself.

Bug: T253380
Change-Id: Ic9fb15e083cca75c2b5c6bddd1df87b148acca6e
2023-09-23 22:32:25 +02:00
Daimona Eaytoy
becfd3ceb3 Add another batch of taint-check annotations
Copied from MediaWikiSecurityCheckPlugin.php.

Duplicate annotations from Xml::encode* to the corresponding
Html::encode* methods, given that these were moved recently but not
hardcoded in taint-check.

As the only difference, remove the HTML taintedness type from the return
value of Message::rawParams. If the argument is unsafe, it's reported
immediately thanks to exec_html. Else, it does not contribute to the
taintedness of the return value.

Bug: T321806
Change-Id: I5ed340e1d127fb3eab6d6f9b905693d05a393360
2023-09-23 20:02:59 +00:00
Daimona Eaytoy
d31d5bfa92 Add taint-check annotations to database methods
These were copied verbatim from MediaWikiSecurityCheckPlugin.php.

The exception is selectRowCount(), whose return value was hardcoded as
tainted in taint-check, but that's clearly not the case because it
returns just an integer.

Suppress a new issue in DbTestPreviewer that wasn't previously spotted
because the code ends up using IReadableDatabase::selectRow, but the
hardcoded data in taint-check only considers IDatabase and concrete
classes, not IReadableDatabase.

Bug: T321806
Change-Id: I0c59d286c50de4ea79571242649bdda8aec67b57
2023-09-23 19:33:33 +02:00
Daimona Eaytoy
ebb742d04f Add second batch of taint-check annotations
Based on taint-check's MediaWikiSecurityCheckPlugin.php.

Bug: T321806
Change-Id: I1c86215afb531de03eae0c583de6b2866e50f9eb
2023-09-23 18:18:17 +02:00
Daimona Eaytoy
95d75e831a Add first batch of taint-check annotations
These are the same as taint-check's MediaWikiSecurityCheckPlugin.php.

The notable exception is methods in WebRequest that were previously
hardcoded as returning a safe value. This was a consequence of said
methods return safe types (e.g., int, bool). Instead of adding
taint-check annotations, add return typehints instead, which let
taint-check remove any taintedness.

Fix some taint-check issues that were previously not spotted or whose
suppressions were removed in other patches.

Also fix the following bugs spotted by phan thanks to the type hints:
- SpecialExport did not have explicit handling of null $depth, and just
  returned 0 because null fails both the < and the > comparisons.
- Improve documentation of params and props in ProtectedPagesPager.
  SpecialProtectedPages can pass null $namespace and $size.
- Remove unused parameter from SpecialProtectedPages::showOptions, of
  which $ns and $size were not documented as nullable.
- Add FIXMEs in SpecialVersion about very inconsistent escaping.

Bug: T321806
Change-Id: I726f528856614c92329683a0ad8936a42e262748
2023-09-23 17:51:06 +02:00
Daimona Eaytoy
d1e0d5a4ae Add tests for taint-check
Make sure that taint-check knows about these methods, especially as we
move the taintedness info from the plugin to annotations on the methods.

Some tests are currently disabled because they would fail. These will
be enabled in subsequent commits while adding annotations to the
relevant methods.

Note that the disabling needs to be done by making the suppression
annotation invalid (done by prepending 'xxx' in this patch), because
commenting out the code or adding a method-wide @suppress won't work.

Bug: T321806
Change-Id: I8b89a22a5c23a2ab25329bcb06c673168d24683d
2023-09-23 16:38:30 +02:00
Daimona Eaytoy
4f2692a056 Kill tests/phan
As per comments on phabricator, the Suppressor was used when phan still
didn't have per-line suppression, which is no longer the case. The phan
script was also outdated and broken, so just remove everything.

Bug: T267859
Change-Id: Ic4a68dd30a4481656a5c44b2446cf851ea638c1c
2020-11-17 16:04:33 +01:00
Reedy
3e36ba9816 tests: Explicitly wrap an XML call in libxml_disable_entity_loader()
As per https://www.php.net/manual/en/function.libxml-disable-entity-loader.php
this is technically unnecessary.

>However, as of libxml 2.9.0 entity substitution is disabled by default,
>so there is no need to disable the loading of external entities.

See also https://github.com/php/php-src/pull/5867

>Since the release of libxml 2.9.0 in 2012 external entity loading is
>disabled in libxml by default. This means that using
>libxml_disable_entity_loader() is no longer needed.

Hopefully helps prevent false positive reports from security scanning tools.

Change-Id: I7cabc5b8d44813d709a11db2f219ae16260542c7
2020-11-15 02:03:02 +00:00
Umherirrender
d621adbcb6 build: Updating mediawiki/mediawiki-codesniffer to 32.0.0
Exclude failing sniff to fix in follow ups
Includes some simply fix, most are autofix

Change-Id: I5bb4743f08618bb6226bc2a4cc7f4d73a7ad142d
2020-10-28 20:06:22 +00:00
Reedy
9f2ffdfbd4 Remove "Squiz.WhiteSpace.FunctionSpacing" from phpcs exclusions
Change-Id: I78b3315f26ab91b6b443f5b028a635552f82f5a3
2019-05-11 02:44:26 +01:00
Kunal Mehta
e7186d68bd Upgrade to newer phan
MediaWiki core now runs phan 1.2.6, bringing in nearly 2+ years of upstream
fixes.

Configuration was moved from `tests/phan` to `.phan/`. The legacy bash wrapper
script is still kept in the repository in its own location for any extensions
that are still using it. It should be removed before 1.33 is released.

Since there's a lot of new issues being flagged, all currently failing issues
are suppressed, and will be fixed in follow-up patches.

We're dropping the jetbrains/phpstorm-stubs repository in favor of just
the minimal stubs we need. Stubs for PHP extensions are kept in
the new `.phan/internal_stubs` directory, since they're in a slightly
different format than normal stubs.

Normal stubs are kept in `.phan/stubs`. wikidiff2 and excimer are kept with
these since we're also the upstream for them.

Change-Id: I3fe437befa17f4fbaf97aa6271f659b56021f396
2019-03-23 18:06:35 -07:00
Kunal Mehta
737036afb5 Use mediawiki/mediawiki-phan-config 0.3.0
Use the library instead of duplicating most of the config/defaults that it
provides. MediaWiki core is different of course, so we have to override a
bunch of file/directory lists, but there was a lot being duplicated.

This is the first step in migrating to a newer phan version.

Change-Id: Ib5987ebdf208138d97e1aba8ef54438064063fe9
2019-03-21 19:05:06 -07:00
Umherirrender
3643883a34 Remove suppress of PhanRedefineFunctionInternal
There is no longer an override

Change-Id: Ie7c99228135689d20ab6ffb287c978642851f3ee
2019-03-09 12:30:16 +00:00
Tim Starling
6373e3d1d4 Add ProfilerExcimer
Add a Profiler subclass for the new excimer extension. Since it does not
provide function counts, it's a little bit awkward to return it in the
format required by getFunctionStats(), but getOutput() works quite well.

Fix totally broken ProfilerOutputDb, the first parameter to the
onTransactionCommitOrIdle callback is not a Database. It is in the
second parameter, but most callers do not use it.

Change-Id: Icb20f3a5b0b09ff2905f1711f3681c398aa026e2
Depends-On: I6a9ccf5a12ef998e029033adf08af95c42fb7f8e
2018-12-07 11:42:59 +11:00
Erik Bernhardson
08dba3de30 Remove PhanUndeclaredStaticMethod from blacklist
There was a single instance of this issue which was reasonably simple
to solve. The code that triggered the issue was not wrong, but calling
the grandparent constructor directly is not the cleanest code so it
seems right to fix it. Switch revision initialization into an
overridable function to remove the need to even have a constructor in
the class.

Change-Id: Ic2af0d93e8d55e93061e3f4b5c7a4122509543f0
2018-08-28 05:38:43 +00:00
Timo Tijhof
55875bcd2d maintenance: Move backup.inc to a regular php class file
Move the class to maintenance/includes/, following the precedent
set by d788692076.

Bug: T184782
Change-Id: I0ba86e4401e2c97db4cf2ad9f0e78c04b5565ee8
2018-08-02 17:20:30 +01:00
Umherirrender
130ec2523d Fix PhanTypeMismatchDeclaredParam
Auto fix MediaWiki.Commenting.FunctionComment.DefaultNullTypeParam sniff

Change-Id: I865323fd0295aabd06f3e3c75e0e5043fb31069e
2018-07-07 00:34:30 +00:00
Erik Bernhardson
cba9297518 Remove PhanParamReqAfterOpt from blacklist
Change-Id: I569656068c9034f7325312afcce837e2712b4f29
2018-06-09 09:21:12 +00:00
Umherirrender
0123e91fc2 Remove renamed .inc files from phan config
Follow-Up: Ic441087702376b1ca0e70554c71cdf7ecad908af (for userOptions.inc)
Follow-Up: I999f5842625c752f01c1eb4b012431c4a24ce7ce (for importImages.inc)
Change-Id: I3e1c694f61960e9e4b50a7855927d039e93928fb
2018-05-31 18:44:51 +02:00
Kunal Mehta
9004601318 Enable a bunch of disabled phan checks that are no longer failing
Change-Id: I471bffa8a4aa20d22e7e1830a2b01fce3e099d9e
2018-05-17 23:27:59 -07:00