Page MenuHomePhabricator

With $wgUseMediaWikiUIEverywhere = true, Xml::input() with class attribute causes warning or TypeError: htmlspecialchars() expects parameter 1 to be string, array given
Closed, ResolvedPublic

Description

Seen in a CI build (Gerrit change) from FlaggedRevs code, but reproducible without FR:

> $wgUseMediaWikiUIEverywhere = true;
= true

> Xml::input( 'name', false, false, [ 'class' => 'class' ] )

   TypeError  htmlspecialchars(): Argument #1 ($string) must be of type string, array given.

(Prior to PHP 8, this is a warning instead of a TypeError, see 3v4l.) Seemingly related to the recent changes for T182050 / T341534.

Event Timeline

The general problem here is that Xml::input() uses Html::getTextInputAttributes(), which recently started returning array attribute values under some conditions, but Xml::element() (unlike Html::element()) doesn’t support array attribute values.

Not sure what the best fix is:

  • limit Html::getTextInputAttributes() to string attribute values
  • make Xml::element() support array attribute values
  • make Xml::input() not use the Html method

Given that Html::expandAttributes() normalizes duplicate class values anyway, I’d say there’s no need for getTextInputAttributes() to do the current !in_array( $cdxInputClass, $classAsArray ) check – it can just append the class to the string. (But it still needs to deal with the possibility that the existing class attribute is either a string or an array.)

Given that Html::expandAttributes() normalizes duplicate class values anyway, I’d say there’s no need for getTextInputAttributes() to do the current !in_array( $cdxInputClass, $classAsArray ) check – it can just append the class to the string. (But it still needs to deal with the possibility that the existing class attribute is either a string or an array.)

Hang on, I misunderstood the code. It’s not trying to prevent a duplicate class, it’s checking the existence of $cdxInputClass before adding a different class to the array. I guess the idea is that $wgUseMediaWikiUIEverywhere forces mw-ui-input upon all inputs, except Codex inputs (cdx-text-input__input), which are exempt?

I can also now confirm that this is reliably blocking Wikibase CI, it’s not just a one-off (other build for other change).

Change 937071 had a related patch set uploaded (by Urbanecm; author: Urbanecm):

[mediawiki/core@master] Html::getTextInputAttributes: Do not force class attribute to be an array

https://gerrit.wikimedia.org/r/937071

The general problem here is that Xml::input() uses Html::getTextInputAttributes(), which recently started returning array attribute values under some conditions, but Xml::element() (unlike Html::element()) doesn’t support array attribute values.

The direct issue is that Html::getTextInputAttributes() now (under certain conditions) forces $attrs['class'] to be an array, even when it originally wasn't. I think the best immediate fix is to make the method respect the original form in which the parameter is passed. Longer-term, we should probably implement one of the three ideas you listed in your original comment.

The general problem here is that Xml::input() uses Html::getTextInputAttributes(), which recently started returning array attribute values under some conditions, but Xml::element() (unlike Html::element()) doesn’t support array attribute values.

The direct issue is that Html::getTextInputAttributes() now (under certain conditions) forces $attrs['class'] to be an array, even when it originally wasn't. I think the best immediate fix is to make the method respect the original form in which the parameter is passed. Longer-term, we should probably implement one of the three ideas you listed in your original comment.

My patch fixes the immediate issue I described above and it seems to have passed with the Wikidata change as well.

Change 936738 had a related patch set uploaded (by Urbanecm; author: Matěj Suchánek):

[mediawiki/core@master] Always return the class as string from Html::getTextInputAttributes

https://gerrit.wikimedia.org/r/936738

I had already pushed https://gerrit.wikimedia.org/r/c/mediawiki/core/+/936738 for review, where I implemented what Lucas proposes:

limit Html::getTextInputAttributes() to string attribute values

Choose whichever you prefer to get this fixed.
My point is: If you audit Html::getTextInputAttributes() calls, you will soon get confused by the existence of the interfaces Html:: and Xml:: with identically named methods ::expandAttributes. Do you know if any of them supports array attributes? Xml:: probably does not as we have just found out, even though it vaguely declares @param array $attribs Optional custom attributes. Can you tell if Html:: does? This should probably be clarified.

Change 937071 abandoned by Urbanecm:

[mediawiki/core@master] Html::getTextInputAttributes: Do not force class attribute to be an array

Reason:

in favour of 936738

https://gerrit.wikimedia.org/r/937071

My point is: If you audit Html::getTextInputAttributes() calls, you will soon get confused by the existence of the interfaces Html:: and Xml:: with identically named methods ::expandAttributes. Do you know if any of them supports array attributes? Xml:: probably does not as we have just found out, even though it vaguely declares @param array $attribs Optional custom attributes. Can you tell if Html:: does? This should probably be clarified.

Html::expandAttributes() has some longer phpdoc explaining that it supports array values for “attributes that can contain space-separated lists (class, accesskey and rel)”.

Change 936738 merged by jenkins-bot:

[mediawiki/core@master] Always return the class as string from Html::getTextInputAttributes

https://gerrit.wikimedia.org/r/936738

The CI issue should now be resolved; there still needs to be a decision whether additional changes of the class's handling of nested arrays passed as attributes. Leaving the task opened for now.

I suppose it’s also worth asking why FlaggedRevs’ RevisionReviewFormUI uses a bunch of Xml::* methods (Xml::input(), Xml::label(), Xml::openElement(), etc.) in a method called getHtml(). Probably just ancient history that should be fixed nowadays?

Change 937080 had a related patch set uploaded (by Lucas Werkmeister (WMDE); author: Lucas Werkmeister (WMDE)):

[mediawiki/core@master] Html: Support more attr types in getTextInputAttributes()

https://gerrit.wikimedia.org/r/937080

The CI issue should now be resolved; there still needs to be a decision whether additional changes of the class's handling of nested arrays passed as attributes. Leaving the task opened for now.

Thanks for the quick ci fix; we hit this while working on PageTriage last night and were scratching our heads. Builds are passing for us now.

Change 937140 had a related patch set uploaded (by Urbanecm; author: Urbanecm):

[mediawiki/core@master] [tests] Add a regression test for Xml::input

https://gerrit.wikimedia.org/r/937140

This probably needs backporting?

Good point, otherwise FlaggedRevs will probably start crashing in production.

Change 937140 merged by jenkins-bot:

[mediawiki/core@master] [tests] Add a regression test for Xml::input

https://gerrit.wikimedia.org/r/937140

Change 937170 had a related patch set uploaded (by Urbanecm; author: Urbanecm):

[mediawiki/core@master] [tests] Improve regression test for Xml::input

https://gerrit.wikimedia.org/r/937170

Change 937113 had a related patch set uploaded (by Urbanecm; author: Matěj Suchánek):

[mediawiki/core@wmf/1.41.0-wmf.17] Always return the class as string from Html::getTextInputAttributes

https://gerrit.wikimedia.org/r/937113

Change 937113 merged by jenkins-bot:

[mediawiki/core@wmf/1.41.0-wmf.17] Always return the class as string from Html::getTextInputAttributes

https://gerrit.wikimedia.org/r/937113

Mentioned in SAL (#wikimedia-operations) [2023-07-11T20:33:55Z] <urbanecm@deploy1002> Started scap: Backport for [[gerrit:936097|Logos: Fixes grantswiki and idwiktionary]], [[gerrit:937177|Drop idwiktionary wordmark]], [[gerrit:937113|Always return the class as string from Html::getTextInputAttributes (T341566)]]

Mentioned in SAL (#wikimedia-operations) [2023-07-11T20:35:27Z] <urbanecm@deploy1002> jdlrobson and urbanecm: Backport for [[gerrit:936097|Logos: Fixes grantswiki and idwiktionary]], [[gerrit:937177|Drop idwiktionary wordmark]], [[gerrit:937113|Always return the class as string from Html::getTextInputAttributes (T341566)]] synced to the testservers: mwdebug2002.codfw.wmnet, mwdebug1001.eqiad.wmnet, mwdebug2001.codfw.wmnet, mwdebug1002.eqiad.wmnet

Mentioned in SAL (#wikimedia-operations) [2023-07-11T20:45:06Z] <urbanecm@deploy1002> Finished scap: Backport for [[gerrit:936097|Logos: Fixes grantswiki and idwiktionary]], [[gerrit:937177|Drop idwiktionary wordmark]], [[gerrit:937113|Always return the class as string from Html::getTextInputAttributes (T341566)]] (duration: 11m 10s)

Change 937170 abandoned by Urbanecm:

[mediawiki/core@master] [tests] Improve regression test for Xml::input

Reason:

per Lucas

https://gerrit.wikimedia.org/r/937170

Change 937121 had a related patch set uploaded (by Lucas Werkmeister (WMDE); author: Lucas Werkmeister (WMDE)):

[mediawiki/core@wmf/1.41.0-wmf.17] Html: Support more attr types in getTextInputAttributes()

https://gerrit.wikimedia.org/r/937121

Change 937080 merged by jenkins-bot:

[mediawiki/core@master] Html: Support more attr types in getTextInputAttributes()

https://gerrit.wikimedia.org/r/937080

This probably needs backporting?

Good point, otherwise FlaggedRevs will probably start crashing in production.

But probably only on mobile – AFAICT we don’t set $wgUseMediaWikiUIEverywhere = true normally in production, it just gets set by MobileFrontend. (The CI stack trace linked in the task description also contains a MinervaNeue class.)

Change 937121 merged by jenkins-bot:

[mediawiki/core@wmf/1.41.0-wmf.17] Html: Support more attr types in getTextInputAttributes()

https://gerrit.wikimedia.org/r/937121

Mentioned in SAL (#wikimedia-operations) [2023-07-12T13:21:40Z] <lucaswerkmeister-wmde@deploy1002> Started scap: Backport for [[gerrit:937121|Html: Support more attr types in getTextInputAttributes() (T341566)]]

Mentioned in SAL (#wikimedia-operations) [2023-07-12T13:23:19Z] <lucaswerkmeister-wmde@deploy1002> lucaswerkmeister-wmde: Backport for [[gerrit:937121|Html: Support more attr types in getTextInputAttributes() (T341566)]] synced to the testservers: mwdebug1001.eqiad.wmnet, mwdebug1002.eqiad.wmnet, mwdebug2001.codfw.wmnet, mwdebug2002.codfw.wmnet

Mentioned in SAL (#wikimedia-operations) [2023-07-12T13:29:20Z] <lucaswerkmeister-wmde@deploy1002> Finished scap: Backport for [[gerrit:937121|Html: Support more attr types in getTextInputAttributes() (T341566)]] (duration: 07m 40s)

Lucas_Werkmeister_WMDE claimed this task.

I think we’re all done here?

I suppose it’s also worth asking why FlaggedRevs’ RevisionReviewFormUI uses a bunch of Xml::* methods (Xml::input(), Xml::label(), Xml::openElement(), etc.) in a method called getHtml(). Probably just ancient history that should be fixed nowadays?

Perhaps we should have a general “deprecate Xml methods for building HTML” task (I can’t find any existing one).

+1 to depreciating Xml class or perhaps merging it with Html and leaving an alias.

We can't completely remove the Xml methods, as sometimes MediaWiki actually needs to generate XML markup (and not HTML) – for example the output of Special:Export, the API with format=xml, or the Atom/RSS feeds. But we shouldn't use them to generate HTML markup, and I'd be happy to review patches that replace the uses of and then deprecate individual methods that clearly are intended to generate HTML, like Xml::input(). This seems like a low priority thing though, as I haven't seen them misused like this in new code (even by new contributors); it's mostly an issue in code written in the XHTML era like FlaggedRevs.

A codesearch for Xml:: reveals 218 files across 38 repos.

Perhaps we should create a parent ticket for "replacing Xml with Html in all repos, unless actual XML output is needed (e.g. Special:Export, RSS)" where the codesearch results can be explored a bit more.

And then a subticket for FlaggedRevs since that was the hotspot in this ticket.