From 8c22ddf5cfa492a95c7f7c64f5f878db626c395b Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 11 Oct 2016 23:28:19 +0100 Subject: [PATCH] Issue #2699489 by catch, tim.plunkett: FormBuilder $ajax_form_request check does not check which AJAX form is being requested --- core/lib/Drupal/Core/Form/FormBuilder.php | 6 +++-- .../Tests/Core/Form/FormBuilderTest.php | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 544ee94b1bc..878967b9c91 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -316,7 +316,7 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS // In case the post request exceeds the configured allowed size // (post_max_size), the post request is potentially broken. Add some // protection against that and at the same time have a nice error message. - if ($ajax_form_request && !isset($form_state->getUserInput()['form_id'])) { + if ($ajax_form_request && !$request->request->has('form_id')) { throw new BrokenPostRequestException($this->getFileUploadMaxSize()); } @@ -327,7 +327,9 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS // then passed through // \Drupal\Core\Form\FormAjaxResponseBuilderInterface::buildResponse() to // build a proper AJAX response. - if ($ajax_form_request && $form_state->isProcessingInput()) { + // Only do this when the form ID matches, since there is no guarantee from + // $ajax_form_request that it's an AJAX request for this particular form. + if ($ajax_form_request && $form_state->isProcessingInput() && $request->request->get('form_id') == $form_id) { throw new FormAjaxException($form, $form_state); } diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index 1530292d906..dfbf843a0c7 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -568,6 +568,29 @@ class FormBuilderTest extends FormTestBase { $this->formBuilder->buildForm($form_arg, $form_state); } + /** + * @covers ::buildForm + */ + public function testGetPostAjaxRequest() { + $request = new Request([FormBuilderInterface::AJAX_FORM_REQUEST => TRUE], ['form_id' => 'different_form_id']); + $request->setMethod('POST'); + $this->requestStack->push($request); + + $form_state = (new FormState()) + ->setUserInput([FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]) + ->setMethod('get') + ->setAlwaysProcess() + ->disableRedirect() + ->set('ajax', TRUE); + + $form_id = '\Drupal\Tests\Core\Form\TestForm'; + $expected_form = (new TestForm())->buildForm([], $form_state); + + $form = $this->formBuilder->buildForm($form_id, $form_state); + $this->assertFormElement($expected_form, $form, 'test'); + $this->assertSame('test-form', $form['#id']); + } + /** * @covers ::buildForm *