 core/modules/statistics/statistics.php             |  9 ++++-
 .../src/Functional/StaitsticsInvalidPostTest.php   | 40 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/core/modules/statistics/statistics.php b/core/modules/statistics/statistics.php
index a43509e..bc57b8e 100644
--- a/core/modules/statistics/statistics.php
+++ b/core/modules/statistics/statistics.php
@@ -22,7 +22,14 @@
   ->get('count_content_views');
 
 if ($views) {
-  $nid = filter_input(INPUT_POST, 'nid', FILTER_VALIDATE_INT);
+  // Checks if posted nid is in range of valid integers and prevent a
+  // PDOException.
+  $nid = filter_input(INPUT_POST, 'nid', FILTER_VALIDATE_INT, [
+    'options' => [
+      'min_range' => 0,
+      'max_range' => 4294967295,
+    ],
+  ]);
   if ($nid) {
     $container->get('request_stack')->push(Request::createFromGlobals());
     $container->get('statistics.storage.node')->recordView($nid);
diff --git a/core/modules/statistics/tests/src/Functional/StaitsticsInvalidPostTest.php b/core/modules/statistics/tests/src/Functional/StaitsticsInvalidPostTest.php
index e69de29..dd30ba9 100644
--- a/core/modules/statistics/tests/src/Functional/StaitsticsInvalidPostTest.php
+++ b/core/modules/statistics/tests/src/Functional/StaitsticsInvalidPostTest.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Drupal\Tests\statistics\Functional;
+
+/**
+ * Tests posting to statistics.php with invalid values.
+ *
+ * @group statistics
+ */
+class StatisticsInvalidPostTest extends StatisticsTestBase {
+  /**
+   * Test if nothing breaks when posting with invalid params.
+   */
+  public function testInvalidPost() {
+
+    $client = \Drupal::httpClient();
+    // Manually calling statistics.php.
+    $nid = 'a string instead of an integer';
+    $client->post($this->buildUrl(drupal_get_path('module', 'statistics') . '/statistics.php'), ['form_params' => ['nid' => $nid]]);
+
+    $result = db_select('node_counter', 'n')
+      ->fields('n', ['nid'])
+      ->condition('n.nid', $nid)
+      ->execute()
+      ->fetchAssoc();
+    $this->assertEqual($result['nid'], NULL, 'Verifying that nothing is written to the node_counter table.');
+
+    // An id greater than int(10), the maximum nid database limit.
+    $nid = 123456789012;
+    $client->post($this->buildUrl(drupal_get_path('module', 'statistics') . '/statistics.php'), ['form_params' => ['nid' => $nid]]);
+
+    $result = db_select('node_counter', 'n')
+      ->fields('n', ['nid'])
+      ->condition('n.nid', $nid)
+      ->execute()
+      ->fetchAssoc();
+    $this->assertEqual($result['nid'], NULL, 'Verifying that nothing is written to the node_counter table.');
+  }
+
+}
