SpecialLog: Don't throw exceptions on invalid date from user input
authorKunal Mehta <legoktm@member.fsf.org>
Sun, 23 Sep 2018 18:58:14 +0000 (11:58 -0700)
committerJames D. Forrester <jforrester@wikimedia.org>
Mon, 24 Sep 2018 15:56:48 +0000 (08:56 -0700)
If users provide invalid input to the date option on Special:Log (most likely
an intentional thing given the calendar input widget), don't let the
TimestampException bubble up - just discard the invalid date.

Integration test included, which fails without this patch.

Bug: T201411
Change-Id: Ie1a9a84343ae4e78e076586f759917e5fd5af33c

includes/specials/SpecialLog.php
tests/phpunit/includes/specials/SpecialLogTest.php [new file with mode: 0644]

index d700c39..54afde1 100644 (file)
@@ -21,6 +21,8 @@
  * @ingroup SpecialPage
  */
 
+use Wikimedia\Timestamp\TimestampException;
+
 /**
  * A special page that lists log entries
  *
@@ -63,10 +65,18 @@ class SpecialLog extends SpecialPage {
                // Set date values
                $dateString = $this->getRequest()->getVal( 'wpdate' );
                if ( !empty( $dateString ) ) {
-                       $dateStamp = MWTimestamp::getInstance( $dateString . ' 00:00:00' );
-                       $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
-                       $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
-                       $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
+                       try {
+                               $dateStamp = MWTimestamp::getInstance( $dateString . ' 00:00:00' );
+                       } catch ( TimestampException $e ) {
+                               // If users provide an invalid date, silently ignore it
+                               // instead of letting an exception bubble up (T201411)
+                               $dateStamp = false;
+                       }
+                       if ( $dateStamp ) {
+                               $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
+                               $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
+                               $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
+                       }
                }
 
                # Don't let the user get stuck with a certain date
diff --git a/tests/phpunit/includes/specials/SpecialLogTest.php b/tests/phpunit/includes/specials/SpecialLogTest.php
new file mode 100644 (file)
index 0000000..d66280e
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+/**
+ * @license GPL-2.0-or-later
+ * @author Legoktm
+ */
+
+/**
+ * @covers SpecialLog
+ */
+class SpecialLogTest extends SpecialPageTestBase {
+
+       /**
+        * Returns a new instance of the special page under test.
+        *
+        * @return SpecialPage
+        */
+       protected function newSpecialPage() {
+               return new SpecialLog();
+       }
+
+       /**
+        * Verify that no exception was thrown for an invalid date
+        * @see T201411
+        */
+       public function testInvalidDate() {
+               list( $html, ) = $this->executeSpecialPage(
+                       '',
+                       // There is no 13th month
+                       new FauxRequest( [ 'wpdate' => '2018-13-01' ] ),
+                       'qqx'
+               );
+               $this->assertContains( '(log-summary)', $html );
+       }
+
+}