Page MenuHomePhabricator

Upload API throws an error when uploading files that exceed $wgUploadSizeWarning but not $wgMaxUploadSize
Closed, ResolvedPublicBUG REPORT

Description

Steps to replicate the issue (include links if applicable):

  • Set $wgEnableUploads = true;
  • Set $wgMaxUploadSize = 5242880; (5MiB)
  • Set $wgUploadSizeWarning = 1048576; (1MiB)
  • If you have WikiEditor installed:
    • Open the editor on any page
    • Select the "Images and media" button
    • In the dialog, select "Upload"
    • In the upload dialog, select a file which is over 1MiB but under 5MiB
  • If you don't have WikiEditor installed:
    • Visit Special:Upload
    • Select a (for example, JPEG) file which is over 1MiB and under 5MiB in the Choose File option
    • Run the following JavaScript:
new mw.Api().upload(document.getElementById('wpUploadFile'), {
    filename: 'Your file.jpg'
})
.then(d => console.log('It works!'))
.catch((_, info) => console.error('Nope', info.error));

What happens?: The API throws the following error (anonymized):

{
    "error": {
        "code": "internal_api_error_InvalidArgumentException",
        "info": "[134c82cb4b267260be809e60] Exception caught: UploadBase::makeWarningsSerializable: Unexpected object of class Wikimedia\\Message\\ScalarParam",
        "errorclass": "InvalidArgumentException",
        "*": "InvalidArgumentException at $IP/includes/upload/UploadBase.php(772)\nfrom $IP/includes/upload/UploadBase.php(772)\n#0 [internal function]: UploadBase::{closure}()\n#1 $IP/includes/upload/UploadBase.php(765): array_walk_recursive()\n#2 $IP/includes/api/ApiUpload.php(937): UploadBase::makeWarningsSerializable()\n#3 $IP/includes/api/ApiUpload.php(249): MediaWiki\\Api\\ApiUpload->getApiWarnings()\n#4 $IP/includes/api/ApiUpload.php(172): MediaWiki\\Api\\ApiUpload->getContextResult()\n#5 $IP/includes/api/ApiMain.php(1974): MediaWiki\\Api\\ApiUpload->execute()\n#6 $IP/includes/api/ApiMain.php(942): MediaWiki\\Api\\ApiMain->executeAction()\n#7 $IP/includes/api/ApiMain.php(913): MediaWiki\\Api\\ApiMain->executeActionWithErrorHandling()\n#8 $IP/includes/api/ApiEntryPoint.php(153): MediaWiki\\Api\\ApiMain->execute()\n#9 $IP/includes/MediaWikiEntryPoint.php(200): MediaWiki\\Api\\ApiEntryPoint->execute()\n#10 $IP/api.php(44): MediaWiki\\MediaWikiEntryPoint->run()\n#11 {main}"
    }
}

This happens because UploadBase::checkFileSize sets an array in the large-file key with a Message::sizeParam, and ApiUpload::getApiWarnings then runs that through UploadBase::makeWarningsSerializable, which throws an error if it encounters an unserializable object (which is Wikimedia\Message\ScalarParam in this case).

What should have happened instead?: It should have properly told me what's the warning.

Software version 1.43.0 (5976d8f)

Other information (browser name/version, screenshots, etc.):

This is how the error looks like inside of the upload dialog:

image.png (227×498 px, 22 KB)

It is likely that this affects more than just size warnings. For example, UploadBase::checkUnwantedFileExtensions returns a Wikimedia\Message\ListParam, though I couldn't figure out the purpose of said function.

Event Timeline

Message::sizeParam returns an object since 9d56257d8c154cf3020ae7d0a1dd2f45d5211453

The extra serializable check was added years before in 51e837f68f6df7fdc6cb35803e497bfc0532c861

matmarex added a project: MW-1.43-release.

That method shouldn't throw when encountering MessageParam objects. They are serializable and they can be included in API results too (which is a hack, but it's intended exactly for cases like this).

Fix should be backported to 1.43.

Change #1135840 had a related patch set uploaded (by Bartosz Dziewoński; author: Bartosz Dziewoński):

[mediawiki/core@master] UploadBase: makeWarningsSerializable() should accept MessageParam objects

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

This is how the error looks like inside of the upload dialog:

image.png (227×498 px, 22 KB)

This ugly appearance is T383124: OOUI process dialog errors are not night mode compatible.

Change #1135840 merged by jenkins-bot:

[mediawiki/core@master] UploadBase: makeWarningsSerializable() should accept MessageParam objects

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

Change #1136060 had a related patch set uploaded (by Bartosz Dziewoński; author: Bartosz Dziewoński):

[mediawiki/core@REL1_43] UploadBase: makeWarningsSerializable() should accept MessageParam objects

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

Change #1136060 merged by jenkins-bot:

[mediawiki/core@REL1_43] UploadBase: makeWarningsSerializable() should accept MessageParam objects

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

matmarex removed a project: Patch-For-Review.

Thank you for the detailed bug report, @KockaAdmiralac!