ExternalFormUrlTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Drupal\KernelTests\Core\Form;
  3. use Drupal\Core\Form\FormInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Drupal\user\Entity\User;
  7. use Symfony\Component\HttpFoundation\Request;
  8. /**
  9. * Ensures that form actions can't be tricked into sending to external URLs.
  10. *
  11. * @group system
  12. */
  13. class ExternalFormUrlTest extends KernelTestBase implements FormInterface {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public static $modules = ['user', 'system'];
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getFormId() {
  22. return 'external_form_url_test';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function buildForm(array $form, FormStateInterface $form_state) {
  28. $form['something'] = [
  29. '#type' => 'textfield',
  30. '#title' => 'What do you think?',
  31. ];
  32. return $form;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function validateForm(array &$form, FormStateInterface $form_state) {}
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function submitForm(array &$form, FormStateInterface $form_state) {}
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function setUp() {
  46. parent::setUp();
  47. $this->installSchema('system', ['key_value_expire', 'sequences']);
  48. $this->installEntitySchema('user');
  49. $test_user = User::create([
  50. 'name' => 'foobar',
  51. 'mail' => 'foobar@example.com',
  52. ]);
  53. $test_user->save();
  54. \Drupal::service('current_user')->setAccount($test_user);
  55. }
  56. /**
  57. * Tests form behaviour.
  58. */
  59. public function testActionUrlBehavior() {
  60. // Create a new request which has a request uri with multiple leading
  61. // slashes and make it the master request.
  62. $request_stack = \Drupal::service('request_stack');
  63. /** @var \Symfony\Component\HttpFoundation\RequestStack $original_request */
  64. $original_request = $request_stack->pop();
  65. // Just request some more so there is no request left.
  66. $request_stack->pop();
  67. $request_stack->pop();
  68. $request = Request::create($original_request->getSchemeAndHttpHost() . '//example.org');
  69. $request_stack->push($request);
  70. $form = \Drupal::formBuilder()->getForm($this);
  71. $markup = \Drupal::service('renderer')->renderRoot($form);
  72. $this->setRawContent($markup);
  73. $elements = $this->xpath('//form/@action');
  74. $action = (string) $elements[0];
  75. $this->assertEqual($original_request->getSchemeAndHttpHost() . '//example.org', $action);
  76. // Create a new request which has a request uri with a single leading slash
  77. // and make it the master request.
  78. $request_stack = \Drupal::service('request_stack');
  79. $original_request = $request_stack->pop();
  80. $request = Request::create($original_request->getSchemeAndHttpHost() . '/example.org');
  81. $request_stack->push($request);
  82. $form = \Drupal::formBuilder()->getForm($this);
  83. $markup = \Drupal::service('renderer')->renderRoot($form);
  84. $this->setRawContent($markup);
  85. $elements = $this->xpath('//form/@action');
  86. $action = (string) $elements[0];
  87. $this->assertEqual('/example.org', $action);
  88. }
  89. }