From b3ad35803934e66a7917aae91c4eb7bf391cd255 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Fri, 28 Jun 2024 16:17:44 +0300
Subject: [PATCH] Issue #3447748 by joelpittet, ramil g, smustgrave: TypeError:
 Illegal offset type in isset or empty in
 FormValidator->performRequiredValidation()

---
 core/lib/Drupal/Core/Form/FormValidator.php         |  6 ++++++
 .../Drupal/Tests/Core/Form/FormValidatorTest.php    | 13 +++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/core/lib/Drupal/Core/Form/FormValidator.php b/core/lib/Drupal/Core/Form/FormValidator.php
index f8c62379ce0c..def5f87191d3 100644
--- a/core/lib/Drupal/Core/Form/FormValidator.php
+++ b/core/lib/Drupal/Core/Form/FormValidator.php
@@ -345,6 +345,12 @@ protected function performRequiredValidation(&$elements, FormStateInterface &$fo
       if (is_array($elements['#value'])) {
         $value = in_array($elements['#type'], ['checkboxes', 'tableselect']) ? array_keys($elements['#value']) : $elements['#value'];
         foreach ($value as $v) {
+          if (!is_scalar($v)) {
+            $message_arguments['%type'] = gettype($v);
+            $form_state->setError($elements, $this->t('The submitted value type %type in the %name element is not allowed.', $message_arguments));
+            $this->logger->error('The submitted value type %type in the %name element is not allowed.', $message_arguments);
+            continue;
+          }
           if (!isset($options[$v])) {
             $message_arguments['%choice'] = $v;
             $form_state->setError($elements, $this->t('The submitted value %choice in the %name element is not allowed.', $message_arguments));
diff --git a/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php b/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php
index 18952cecd8ca..029ed51a46aa 100644
--- a/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php
+++ b/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php
@@ -462,6 +462,19 @@ public static function providerTestPerformRequiredValidation() {
         'Test cannot be longer than <em class="placeholder">7</em> characters but is currently <em class="placeholder">8</em> characters long.',
         FALSE,
       ],
+      [
+        [
+          '#type' => 'select',
+          '#options' => [
+            'foo' => 'Foo',
+            'bar' => 'Bar',
+          ],
+          '#value' => [[]],
+          '#multiple' => TRUE,
+        ],
+        'The submitted value type <em class="placeholder">array</em> in the <em class="placeholder">Test</em> element is not allowed.',
+        TRUE,
+      ],
     ];
   }
 
-- 
GitLab