diff options
author | Máté Szabó <mszabo@wikimedia.org> | 2025-02-04 13:17:43 +0100 |
---|---|---|
committer | Reedy <reedy@wikimedia.org> | 2025-02-04 14:54:31 +0000 |
commit | b99dcc23bc40ba9e08a0ddfe3482cde0a5be37b0 (patch) | |
tree | f9b423625a080f85a9dcc2cb5c78bee6899393e4 /includes/edit | |
parent | e751026153425afa47ecdb3fd20082bbceda88d3 (diff) | |
download | mediawikicore-b99dcc23bc40ba9e08a0ddfe3482cde0a5be37b0.tar.gz mediawikicore-b99dcc23bc40ba9e08a0ddfe3482cde0a5be37b0.zip |
parser: Gracefully handle invalid ParsoidRenderID keys
Why:
- ParsoidRenderID::newFromKey() validates incoming keys and throws an
InvalidArgumentException if a required key component was missing.
- It does so by eagerly destructuring the return value of explode(),
which causes a PHP Notice for invalid inputs as the expected offsets
won't exist then.
What:
- Check the count of key parts before destructuring.
- Add unit tests.
Bug: T385567
Change-Id: I1d936ae038f85ffa2e5d1d3d8a75fdc75e4c8ef8
(cherry picked from commit eec130925c081c2da1c475f9a9ce719e6838ca51)
Diffstat (limited to 'includes/edit')
-rw-r--r-- | includes/edit/ParsoidRenderID.php | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/includes/edit/ParsoidRenderID.php b/includes/edit/ParsoidRenderID.php index 005da7b305c8..fc82c5a6d1a4 100644 --- a/includes/edit/ParsoidRenderID.php +++ b/includes/edit/ParsoidRenderID.php @@ -5,6 +5,7 @@ namespace MediaWiki\Edit; use InvalidArgumentException; use MediaWiki\Parser\ParserOutput; use Stringable; +use function count; /** * Represents the identity of a specific rendering of a specific revision @@ -37,12 +38,14 @@ class ParsoidRenderID implements Stringable { * */ public static function newFromKey( string $key ): self { - [ $revisionID, $uniqueID ] = explode( '/', $key, 2 ); + $parts = explode( '/', $key, 2 ); - if ( $revisionID === null || $uniqueID === null ) { + if ( count( $parts ) < 2 ) { throw new InvalidArgumentException( 'Bad key: ' . $key ); } + [ $revisionID, $uniqueID ] = $parts; + return new self( (int)$revisionID, $uniqueID ); } |