PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/aegir/patches/8-core/SA-CORE-2018-004-D8.patch

http://github.com/omega8cc/nginx-for-drupal
Patch | 160 lines | 148 code | 12 blank | 0 comment | 0 complexity | a2fa6f53f201305ed3e149a371d61560 MD5 | raw file
  1. From bb6d396609600d1169da29456ba3db59abae4b7e Mon Sep 17 00:00:00 2001
  2. From: xjm <xjm@65776.no-reply.drupal.org>
  3. Date: Wed, 25 Apr 2018 10:39:01 -0500
  4. Subject: [PATCH] SA-CORE-2018-004 by David_Rothstein, alexpott, larowlan,
  5. Heine, Pere Orga, tim.plunkett, mlhess, xjm, Jasu_M, drumm, cashwilliams,
  6. quicksketch, dawehner, pwolanin, samuel.mortenson
  7. ---
  8. core/lib/Drupal/Core/Security/RequestSanitizer.php | 98 +++++++++++++++++-----
  9. core/modules/file/src/Element/ManagedFile.php | 4 +
  10. 2 files changed, 82 insertions(+), 20 deletions(-)
  11. diff --git a/core/lib/Drupal/Core/Security/RequestSanitizer.php b/core/lib/Drupal/Core/Security/RequestSanitizer.php
  12. index 8ba17b95cf..44815f68cd 100644
  13. --- a/core/lib/Drupal/Core/Security/RequestSanitizer.php
  14. +++ b/core/lib/Drupal/Core/Security/RequestSanitizer.php
  15. @@ -2,6 +2,8 @@
  16. namespace Drupal\Core\Security;
  17. +use Drupal\Component\Utility\UrlHelper;
  18. +use Symfony\Component\HttpFoundation\ParameterBag;
  19. use Symfony\Component\HttpFoundation\Request;
  20. /**
  21. @@ -39,33 +41,89 @@ class RequestSanitizer {
  22. */
  23. public static function sanitize(Request $request, $whitelist, $log_sanitized_keys = FALSE) {
  24. if (!$request->attributes->get(self::SANITIZED, FALSE)) {
  25. - // Process query string parameters.
  26. - $get_sanitized_keys = [];
  27. - $request->query->replace(static::stripDangerousValues($request->query->all(), $whitelist, $get_sanitized_keys));
  28. - if ($log_sanitized_keys && !empty($get_sanitized_keys)) {
  29. - trigger_error(sprintf('Potentially unsafe keys removed from query string parameters (GET): %s', implode(', ', $get_sanitized_keys)));
  30. + $update_globals = FALSE;
  31. + $bags = [
  32. + 'query' => 'Potentially unsafe keys removed from query string parameters (GET): %s',
  33. + 'request' => 'Potentially unsafe keys removed from request body parameters (POST): %s',
  34. + 'cookies' => 'Potentially unsafe keys removed from cookie parameters: %s',
  35. + ];
  36. + foreach ($bags as $bag => $message) {
  37. + if (static::processParameterBag($request->$bag, $whitelist, $log_sanitized_keys, $bag, $message)) {
  38. + $update_globals = TRUE;
  39. + }
  40. }
  41. -
  42. - // Request body parameters.
  43. - $post_sanitized_keys = [];
  44. - $request->request->replace(static::stripDangerousValues($request->request->all(), $whitelist, $post_sanitized_keys));
  45. - if ($log_sanitized_keys && !empty($post_sanitized_keys)) {
  46. - trigger_error(sprintf('Potentially unsafe keys removed from request body parameters (POST): %s', implode(', ', $post_sanitized_keys)));
  47. + if ($update_globals) {
  48. + $request->overrideGlobals();
  49. }
  50. + $request->attributes->set(self::SANITIZED, TRUE);
  51. + }
  52. + return $request;
  53. + }
  54. - // Cookie parameters.
  55. - $cookie_sanitized_keys = [];
  56. - $request->cookies->replace(static::stripDangerousValues($request->cookies->all(), $whitelist, $cookie_sanitized_keys));
  57. - if ($log_sanitized_keys && !empty($cookie_sanitized_keys)) {
  58. - trigger_error(sprintf('Potentially unsafe keys removed from cookie parameters: %s', implode(', ', $cookie_sanitized_keys)));
  59. + /**
  60. + * Processes a request parameter bag.
  61. + *
  62. + * @param \Symfony\Component\HttpFoundation\ParameterBag $bag
  63. + * The parameter bag to process.
  64. + * @param string[] $whitelist
  65. + * An array of keys to whitelist as safe.
  66. + * @param bool $log_sanitized_keys
  67. + * Set to TRUE to log keys that are sanitized.
  68. + * @param string $bag_name
  69. + * The request parameter bag name. Either 'query', 'request' or 'cookies'.
  70. + * @param string $message
  71. + * The message to log if the parameter bag contains keys that are removed.
  72. + * If the message contains %s that is replaced by a list of removed keys.
  73. + *
  74. + * @return bool
  75. + * TRUE if the parameter bag has been sanitized, FALSE if not.
  76. + */
  77. + protected static function processParameterBag(ParameterBag $bag, $whitelist, $log_sanitized_keys, $bag_name, $message) {
  78. + $sanitized = FALSE;
  79. + $sanitized_keys = [];
  80. + $bag->replace(static::stripDangerousValues($bag->all(), $whitelist, $sanitized_keys));
  81. + if (!empty($sanitized_keys)) {
  82. + $sanitized = TRUE;
  83. + if ($log_sanitized_keys) {
  84. + trigger_error(sprintf($message, implode(', ', $sanitized_keys)));
  85. }
  86. + }
  87. - if (!empty($get_sanitized_keys) || !empty($post_sanitized_keys) || !empty($cookie_sanitized_keys)) {
  88. - $request->overrideGlobals();
  89. + if ($bag->has('destination')) {
  90. + $destination_dangerous_keys = static::checkDestination($bag->get('destination'), $whitelist);
  91. + if (!empty($destination_dangerous_keys)) {
  92. + // The destination is removed rather than sanitized because the URL
  93. + // generator service is not available and this method is called very
  94. + // early in the bootstrap.
  95. + $bag->remove('destination');
  96. + $sanitized = TRUE;
  97. + if ($log_sanitized_keys) {
  98. + trigger_error(sprintf('Potentially unsafe destination removed from %s parameter bag because it contained the following keys: %s', $bag_name, implode(', ', $destination_dangerous_keys)));
  99. + }
  100. }
  101. - $request->attributes->set(self::SANITIZED, TRUE);
  102. }
  103. - return $request;
  104. + return $sanitized;
  105. + }
  106. +
  107. + /**
  108. + * Checks a destination string to see if it is dangerous.
  109. + *
  110. + * @param string $destination
  111. + * The destination string to check.
  112. + * @param array $whitelist
  113. + * An array of keys to whitelist as safe.
  114. + *
  115. + * @return array
  116. + * The dangerous keys found in the destination parameter.
  117. + */
  118. + protected static function checkDestination($destination, array $whitelist) {
  119. + $dangerous_keys = [];
  120. + $parts = UrlHelper::parse($destination);
  121. + // If there is a query string, check its query parameters.
  122. + if (!empty($parts['query'])) {
  123. + static::stripDangerousValues($parts['query'], $whitelist, $dangerous_keys);
  124. + }
  125. + return $dangerous_keys;
  126. }
  127. /**
  128. diff --git a/core/modules/file/src/Element/ManagedFile.php b/core/modules/file/src/Element/ManagedFile.php
  129. index ca4e887a1b..6f01ee552e 100644
  130. --- a/core/modules/file/src/Element/ManagedFile.php
  131. +++ b/core/modules/file/src/Element/ManagedFile.php
  132. @@ -8,6 +8,7 @@
  133. use Drupal\Core\Ajax\AjaxResponse;
  134. use Drupal\Core\Ajax\ReplaceCommand;
  135. use Drupal\Core\Form\FormStateInterface;
  136. +use Drupal\Core\Render\Element;
  137. use Drupal\Core\Render\Element\FormElement;
  138. use Drupal\Core\Site\Settings;
  139. use Drupal\Core\Url;
  140. @@ -175,6 +176,9 @@ public static function uploadAjaxCallback(&$form, FormStateInterface &$form_stat
  141. $form_parents = explode('/', $request->query->get('element_parents'));
  142. + // Sanitize form parents before using them.
  143. + $form_parents = array_filter($form_parents, [Element::class, 'child']);
  144. +
  145. // Retrieve the element to be rendered.
  146. $form = NestedArray::getValue($form, $form_parents);
  147. --
  148. 2.15.1 (Apple Git-101)