From 80d8af47f7dc64d6615f813dc3d69230230d0580 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 3 Nov 2022 16:40:59 +0100 Subject: [PATCH] Produce HTML for invalid JSON implementations of ContentHandler::fillParserOutput MUST set HTML if $cpoParams->getGenerateHtml() returns true. Bug: T321319 Change-Id: Ibd43f7420e949666649752dce7072dc35bc1f440 --- includes/content/JsonContentHandler.php | 10 ++++++++-- .../content/JsonContentHandlerIntegrationTest.php | 10 +++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/includes/content/JsonContentHandler.php b/includes/content/JsonContentHandler.php index bfad6851157..4e166a3c76e 100644 --- a/includes/content/JsonContentHandler.php +++ b/includes/content/JsonContentHandler.php @@ -129,8 +129,14 @@ class JsonContentHandler extends CodeContentHandler { '@phan-var JsonContent $content'; // FIXME: WikiPage::doUserEditContent generates parser output before validation. // As such, native data may be invalid (though output is discarded later in that case). - if ( $cpoParams->getGenerateHtml() && $content->isValid() ) { - $parserOutput->setText( $content->rootValueTable( $content->getData()->getValue() ) ); + if ( $cpoParams->getGenerateHtml() ) { + if ( $content->isValid() ) { + $parserOutput->setText( $content->rootValueTable( $content->getData()->getValue() ) ); + } else { + $error = wfMessage( 'invalid-json-data' )->parse(); + $parserOutput->setText( $error ); + } + $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] ); } else { $parserOutput->setText( null ); diff --git a/tests/phpunit/includes/content/JsonContentHandlerIntegrationTest.php b/tests/phpunit/includes/content/JsonContentHandlerIntegrationTest.php index 94506c76881..9f47a6a2728 100644 --- a/tests/phpunit/includes/content/JsonContentHandlerIntegrationTest.php +++ b/tests/phpunit/includes/content/JsonContentHandlerIntegrationTest.php @@ -48,6 +48,10 @@ class JsonContentHandlerIntegrationTest extends MediaWikiLangTestCase { '<script>alert("evil!")</script>"' . '', ], + [ + '{ broken JSON ]', + 'Invalid JSON: $1', + ], ]; } @@ -56,7 +60,11 @@ class JsonContentHandlerIntegrationTest extends MediaWikiLangTestCase { * @covers JsonContentHandler::fillParserOutput */ public function testFillParserOutput( $data, $expected ) { - $content = new JsonContent( FormatJson::encode( $data ) ); + if ( !is_string( $data ) ) { + $data = FormatJson::encode( $data ); + } + + $content = new JsonContent( $data ); $contentRenderer = $this->getServiceContainer()->getContentRenderer(); $parserOutput = $contentRenderer->getParserOutput( $content,