Verified Commit f600cefa authored by Alex Pott's avatar Alex Pott
Browse files

task: #3633288 Add test coverage for SA-CORE-2026-001

By: xjm
By: akalata
By: benjifisher
By: drumm
By: larowlan
By: mlhess
By: neclimdul
By: pandaski
By: poker10
By: ram4nd
(cherry picked from commit 7312a125)
parent 4584557f
Loading
Loading
Loading
Loading
Loading
+92
Changes for core/modules/system/tests/modules/dialog_renderer_test/src/Controller/TestController.php: 92 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
namespace Drupal\dialog_renderer_test\Controller;

use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;

/**
@@ -120,6 +122,96 @@ public function linksDisplay() {
          ],
        ],
      ],
      'attack_modal' => [
        '#title' => 'Attack modal!',
        '#type' => 'link',
        '#url' => Url::fromRoute('dialog_renderer_test.modal_content'),
        '#attributes' => [
          'class' => ['use-ajax'],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'buttons' => [
              [
                'text' => '<script data-attack>alert("💥")</script>',
                'onclick' => 'alert("💥 onclick attack");',
              ],
              [
                'icon' => '<script data-attack>alert("💥")</script>',
                'onmouseover' => 'alert("💥 onmouseover attack");',
              ],
              [
                'text' => 'Button with onscroll',
                'onscroll' => 'alert("💥 onscroll attack");',
              ],
              [
                'text' => 'Button with onfocus',
                'onfocus' => 'alert("💥 onfocus attack");',
              ],
              [
                'text' => 'Button with onblur',
                'onblur' => 'alert("💥 onblur attack");',
              ],
              [
                'text' => 'Button with onkeydown',
                'onkeydown' => 'alert("💥 onkeydown attack");',
              ],
              [
                'text' => 'Button with onload',
                'onload' => 'alert("💥 onload attack");',
              ],
              [
                'text' => 'Button with multiple handlers',
                'onclick' => 'alert("💥 click");',
                'onmouseover' => 'alert("💥 hover");',
                'class' => 'safe-class',
              ],
              [
                'click' => '() => { alert("💥") }',
              ],
              [
                'html' => '<script data-attack>alert("💥")</script>',
              ],
            ],
          ]),
        ],
        '#attached' => [
          'library' => [
            'core/drupal.ajax',
          ],
        ],
      ],
      'attack_modal_raw' => [
        '#markup' => Markup::create(\sprintf(
          '<a href="%s" class="use-ajax" data-dialog-type="modal" data-dialog-options="%s">Attack modal raw!</a>',
          Url::fromRoute('dialog_renderer_test.modal_content')->toString(),
          Html::escape(Json::encode([
            'buttons' => [
              [
                'text' => '<script data-attack>alert("💥")</script>',
                'onclick' => 'alert("💥 raw onclick");',
              ],
              [
                'icon' => '<script data-attack>alert("💥")</script>',
                'onmouseover' => 'alert("💥 raw onmouseover");',
              ],
              [
                'text' => 'Raw combined attack',
                'onclick' => 'alert("💥");',
                'onkeydown' => 'window.location="http://evil.com";',
                'onfocus' => 'document.cookie="stolen=true";',
              ],
              [
                'click' => '() => { alert("💥") }',
              ],
            ],
          ]))
        )),
        '#attached' => [
          'library' => [
            'core/drupal.ajax',
          ],
        ],
      ],
      'no_close_modal' => [
        '#title' => 'Hidden close button modal!',
        '#type' => 'link',
+62
Changes for core/modules/system/tests/src/FunctionalJavascript/ModalRendererTest.php: 62 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -62,6 +62,24 @@ public function testModalRenderer(): void {
    // Tabbable should focus the first tabbable item inside button pane.
    $this->assertJsCondition('document.activeElement === tabbable.tabbable(document.querySelector(".ui-dialog .ui-dialog-buttonpane"))[0]');

    $this->drupalGet('/dialog_renderer-test-links');
    $this->clickLink('Attack modal!');
    $session_assert->waitForElementVisible('css', '.ui-dialog');
    $session_assert->assertVisibleInViewport('css', '.ui-dialog .ui-dialog-buttonpane');

    // Tabbable should focus the first tabbable item inside button pane.
    $this->assertJsCondition('document.activeElement === tabbable.tabbable(document.querySelector(".ui-dialog .ui-dialog-buttonpane"))[0]');
    $this->assertSession()->elementNotExists('css', 'script[data-attack]');

    $this->drupalGet('/dialog_renderer-test-links');
    $this->clickLink('Attack modal raw!');
    $session_assert->waitForElementVisible('css', '.ui-dialog');
    $session_assert->assertVisibleInViewport('css', '.ui-dialog .ui-dialog-buttonpane');

    // Tabbable should focus the first tabbable item inside button pane.
    $this->assertJsCondition('document.activeElement === tabbable.tabbable(document.querySelector(".ui-dialog .ui-dialog-buttonpane"))[0]');
    $this->assertSession()->elementNotExists('css', 'script[data-attack]');

    $this->drupalGet('/dialog_renderer-test-links');
    $this->clickLink('Content link modal!');
    $session_assert->waitForElementVisible('css', '.ui-dialog');
@@ -100,6 +118,50 @@ public function testModalRenderer(): void {
    $this->clickLink('Auto buttons true!');
    $this->assertNotNull($session_assert->waitForElement('css', '.ui-dialog-buttonpane .ui-dialog-buttonset .js-form-submit'));
    $session_assert->elementExists('css', '.ui-dialog-buttonpane .ui-dialog-buttonset .js-form-submit');

    // Test event handler attributes are filtered from attack modal buttons.
    $this->drupalGet('/dialog_renderer-test-links');
    $this->clickLink('Attack modal!');
    $session_assert->waitForElementVisible('css', '.ui-dialog');
    $session_assert->assertVisibleInViewport('css', '.ui-dialog .ui-dialog-buttonpane');

    // Verify buttons are present.
    $buttons = $this->getSession()->getPage()->findAll('css', '.ui-dialog-buttonpane button');
    $this->assertGreaterThan(0, count($buttons));

    // Get the HTML of the button pane to check for event handler attributes.
    $button_pane_html = $this->getSession()->getPage()->find('css', '.ui-dialog-buttonpane')->getHtml();

    // Verify that no event handler attributes are present in button HTML.
    $event_handlers = [
      'onclick', 'onmouseover', 'onscroll', 'onfocus', 'onblur',
      'onkeydown', 'onload', 'onmousedown', 'onmouseup', 'onkeyup',
    ];

    foreach ($event_handlers as $handler) {
      $session_assert->elementNotExists('css', "[$handler]");
    }

    // Verify that button text content is preserved while malicious attributes are filtered.
    $this->assertStringContainsString('Button with onscroll', $button_pane_html);
    $this->assertStringContainsString('Button with multiple handlers', $button_pane_html);

    // Test the raw attack modal version as well.
    $this->drupalGet('/dialog_renderer-test-links');
    $this->clickLink('Attack modal raw!');
    $session_assert->waitForElementVisible('css', '.ui-dialog');
    $session_assert->assertVisibleInViewport('css', '.ui-dialog .ui-dialog-buttonpane');

    // Get the HTML again for the raw version.
    $button_pane_html_raw = $this->getSession()->getPage()->find('css', '.ui-dialog-buttonpane')->getHtml();

    // Verify event handlers are also not present in the raw version.
    foreach ($event_handlers as $handler) {
      $this->assertStringNotContainsString($handler . '=', $button_pane_html_raw);
    }

    // Verify that legitimate button content is preserved in the raw version.
    $this->assertStringContainsString('Raw combined attack', $button_pane_html_raw);
  }

  /**