From 674e486cb5e47c4467b98dd6a961b2ba4787043b Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Mon, 29 Oct 2018 19:30:59 -0700 Subject: [PATCH] Recognize CSS/JS redirects with non-ASCII targets The regex in JavaScriptContent and CssContent was trying to pass urlencoded stuff to Title::newFromText(), which would fail. Make sure we urldecode it first. Bug: T208264 Change-Id: I189c4c308da2875839ad8c1061332500f0e6d244 --- includes/content/CssContent.php | 2 +- includes/content/JavaScriptContent.php | 2 +- tests/phpunit/includes/content/CssContentHandlerTest.php | 4 ++++ tests/phpunit/includes/content/CssContentTest.php | 4 ++++ .../includes/content/JavaScriptContentHandlerTest.php | 4 ++++ tests/phpunit/includes/content/JavaScriptContentTest.php | 5 +++++ 6 files changed, 19 insertions(+), 2 deletions(-) diff --git a/includes/content/CssContent.php b/includes/content/CssContent.php index b4f5196de6..a09cd151a1 100644 --- a/includes/content/CssContent.php +++ b/includes/content/CssContent.php @@ -104,7 +104,7 @@ class CssContent extends TextContent { // Extract the title from the url preg_match( '/title=(.*?)&action=raw/', $text, $matches ); if ( isset( $matches[1] ) ) { - $title = Title::newFromText( $matches[1] ); + $title = Title::newFromText( urldecode( $matches[1] ) ); if ( $title ) { // Have a title, check that the current content equals what // the redirect content should be diff --git a/includes/content/JavaScriptContent.php b/includes/content/JavaScriptContent.php index 4bde738919..69ed8d47a3 100644 --- a/includes/content/JavaScriptContent.php +++ b/includes/content/JavaScriptContent.php @@ -106,7 +106,7 @@ class JavaScriptContent extends TextContent { // Extract the title from the url preg_match( '/title=(.*?)\\\\u0026action=raw/', $text, $matches ); if ( isset( $matches[1] ) ) { - $title = Title::newFromText( $matches[1] ); + $title = Title::newFromText( urldecode( $matches[1] ) ); if ( $title ) { // Have a title, check that the current content equals what // the redirect content should be diff --git a/tests/phpunit/includes/content/CssContentHandlerTest.php b/tests/phpunit/includes/content/CssContentHandlerTest.php index 7ca1afce8e..66adf738c8 100644 --- a/tests/phpunit/includes/content/CssContentHandlerTest.php +++ b/tests/phpunit/includes/content/CssContentHandlerTest.php @@ -35,6 +35,10 @@ class CssContentHandlerTest extends MediaWikiLangTestCase { 'Gadget:FooBaz.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ], + [ + 'User:😂/unicode.css', + '/* #REDIRECT */@import url(//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.css&action=raw&ctype=text/css);' + ], ]; // phpcs:enable } diff --git a/tests/phpunit/includes/content/CssContentTest.php b/tests/phpunit/includes/content/CssContentTest.php index f5cc05e0f9..1b5b976b34 100644 --- a/tests/phpunit/includes/content/CssContentTest.php +++ b/tests/phpunit/includes/content/CssContentTest.php @@ -106,6 +106,10 @@ class CssContentTest extends JavaScriptContentTest { [ 'MediaWiki:MonoBook.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ], [ 'User:FooBar/common.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ], [ 'Gadget:FooBaz.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ], + [ + 'User:😂/unicode.css', + '/* #REDIRECT */@import url(//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.css&action=raw&ctype=text/css);' + ], # No #REDIRECT comment [ null, "@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ], # Wrong domain diff --git a/tests/phpunit/includes/content/JavaScriptContentHandlerTest.php b/tests/phpunit/includes/content/JavaScriptContentHandlerTest.php index b5e3ab4a12..6a22fa0632 100644 --- a/tests/phpunit/includes/content/JavaScriptContentHandlerTest.php +++ b/tests/phpunit/includes/content/JavaScriptContentHandlerTest.php @@ -35,6 +35,10 @@ class JavaScriptContentHandlerTest extends MediaWikiLangTestCase { 'Gadget:FooBaz.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");' ], + [ + 'User:😂/unicode.js', + '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.js\u0026action=raw\u0026ctype=text/javascript");' + ], ]; // phpcs:enable } diff --git a/tests/phpunit/includes/content/JavaScriptContentTest.php b/tests/phpunit/includes/content/JavaScriptContentTest.php index 9506c364c5..2c61b7db2f 100644 --- a/tests/phpunit/includes/content/JavaScriptContentTest.php +++ b/tests/phpunit/includes/content/JavaScriptContentTest.php @@ -313,6 +313,11 @@ class JavaScriptContentTest extends TextContentTest { 'Gadget:FooBaz.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");' ], + // Unicode + [ + 'User:😂/unicode.js', + '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.js\u0026action=raw\u0026ctype=text/javascript");' + ], // No #REDIRECT comment [ null, -- 2.20.1