Fix parsing usernames with +

urldecode() should be used for decoding URL query parameters,
rawurldecode() should be used for decoding URL paths.

Bug: T367977
Change-Id: I7a7b14da85fb89f612c701d2746803d830017842
This commit is contained in:
Bartosz Dziewoński 2024-06-19 17:03:48 +02:00
parent aff2c0be56
commit 20ff1a519f
3 changed files with 20 additions and 2 deletions

View file

@ -1234,7 +1234,7 @@ class CommentParser {
) {
$parsoidHref = $part['template']['target']['href'];
Assert::precondition( substr( $parsoidHref, 0, 2 ) === './', "href has valid format" );
$out[] = urldecode( substr( $parsoidHref, 2 ) );
$out[] = rawurldecode( substr( $parsoidHref, 2 ) );
} else {
$out[] = null;
}

View file

@ -499,7 +499,7 @@ class CommentUtils {
) . '/';
$matches = null;
if ( preg_match( $articlePathRegexp, $path, $matches ) ) {
return urldecode( $matches[1] );
return rawurldecode( $matches[1] );
}
return null;
}

View file

@ -36,6 +36,7 @@ class CommentUtilsTest extends IntegrationTestCase {
/**
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils::getTitleFromUrl
* @dataProvider provideGetTitleFromUrl_ShortUrl
* @dataProvider provideGetTitleFromUrl_Decoding
* @dataProvider provideGetTitleFromUrl_ConfusingShortUrl
* @dataProvider provideGetTitleFromUrl_NoShortUrl
*/
@ -46,6 +47,23 @@ class CommentUtilsTest extends IntegrationTestCase {
);
}
public static function provideGetTitleFromUrl_Decoding() {
// Standard short URL configuration like on Wikimedia wikis
$config = new HashConfig( [ 'ArticlePath' => '/wiki/$1' ] );
// In URL paths, non-percent-encoded `+` represents itself
yield [ 'A+B', '/wiki/A+B', $config ];
yield [ 'A B', '/wiki/A B', $config ];
yield [ 'A+B', '/wiki/A%2BB', $config ];
yield [ 'A B', '/wiki/A%20B', $config ];
// In URL query parameters, non-percent-encoded `+` represents ` `
yield [ 'A B', '/w/index.php?title=A+B', $config ];
yield [ 'A B', '/w/index.php?title=A B', $config ];
yield [ 'A+B', '/w/index.php?title=A%2BB', $config ];
yield [ 'A B', '/w/index.php?title=A%20B', $config ];
}
public static function provideGetTitleFromUrl_ShortUrl() {
// Standard short URL configuration like on Wikimedia wikis
$config = new HashConfig( [ 'ArticlePath' => '/wiki/$1' ] );