RequestSanitizer.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Drupal\Core\Security;
  3. use Drupal\Component\Utility\UrlHelper;
  4. use Symfony\Component\HttpFoundation\ParameterBag;
  5. use Symfony\Component\HttpFoundation\Request;
  6. /**
  7. * Sanitizes user input.
  8. */
  9. class RequestSanitizer {
  10. /**
  11. * Request attribute to mark the request as sanitized.
  12. */
  13. const SANITIZED = '_drupal_request_sanitized';
  14. /**
  15. * The name of the setting that configures the whitelist.
  16. */
  17. const SANITIZE_WHITELIST = 'sanitize_input_whitelist';
  18. /**
  19. * The name of the setting that determines if sanitized keys are logged.
  20. */
  21. const SANITIZE_LOG = 'sanitize_input_logging';
  22. /**
  23. * Strips dangerous keys from user input.
  24. *
  25. * @param \Symfony\Component\HttpFoundation\Request $request
  26. * The incoming request to sanitize.
  27. * @param string[] $whitelist
  28. * An array of keys to whitelist as safe. See default.settings.php.
  29. * @param bool $log_sanitized_keys
  30. * (optional) Set to TRUE to log keys that are sanitized.
  31. *
  32. * @return \Symfony\Component\HttpFoundation\Request
  33. * The sanitized request.
  34. */
  35. public static function sanitize(Request $request, $whitelist, $log_sanitized_keys = FALSE) {
  36. if (!$request->attributes->get(self::SANITIZED, FALSE)) {
  37. $update_globals = FALSE;
  38. $bags = [
  39. 'query' => 'Potentially unsafe keys removed from query string parameters (GET): %s',
  40. 'request' => 'Potentially unsafe keys removed from request body parameters (POST): %s',
  41. 'cookies' => 'Potentially unsafe keys removed from cookie parameters: %s',
  42. ];
  43. foreach ($bags as $bag => $message) {
  44. if (static::processParameterBag($request->$bag, $whitelist, $log_sanitized_keys, $bag, $message)) {
  45. $update_globals = TRUE;
  46. }
  47. }
  48. if ($update_globals) {
  49. $request->overrideGlobals();
  50. }
  51. $request->attributes->set(self::SANITIZED, TRUE);
  52. }
  53. return $request;
  54. }
  55. /**
  56. * Processes a request parameter bag.
  57. *
  58. * @param \Symfony\Component\HttpFoundation\ParameterBag $bag
  59. * The parameter bag to process.
  60. * @param string[] $whitelist
  61. * An array of keys to whitelist as safe.
  62. * @param bool $log_sanitized_keys
  63. * Set to TRUE to log keys that are sanitized.
  64. * @param string $bag_name
  65. * The request parameter bag name. Either 'query', 'request' or 'cookies'.
  66. * @param string $message
  67. * The message to log if the parameter bag contains keys that are removed.
  68. * If the message contains %s that is replaced by a list of removed keys.
  69. *
  70. * @return bool
  71. * TRUE if the parameter bag has been sanitized, FALSE if not.
  72. */
  73. protected static function processParameterBag(ParameterBag $bag, $whitelist, $log_sanitized_keys, $bag_name, $message) {
  74. $sanitized = FALSE;
  75. $sanitized_keys = [];
  76. $bag->replace(static::stripDangerousValues($bag->all(), $whitelist, $sanitized_keys));
  77. if (!empty($sanitized_keys)) {
  78. $sanitized = TRUE;
  79. if ($log_sanitized_keys) {
  80. trigger_error(sprintf($message, implode(', ', $sanitized_keys)));
  81. }
  82. }
  83. if ($bag->has('destination')) {
  84. $destination = $bag->get('destination');
  85. $destination_dangerous_keys = static::checkDestination($destination, $whitelist);
  86. if (!empty($destination_dangerous_keys)) {
  87. // The destination is removed rather than sanitized because the URL
  88. // generator service is not available and this method is called very
  89. // early in the bootstrap.
  90. $bag->remove('destination');
  91. $sanitized = TRUE;
  92. if ($log_sanitized_keys) {
  93. trigger_error(sprintf('Potentially unsafe destination removed from %s parameter bag because it contained the following keys: %s', $bag_name, implode(', ', $destination_dangerous_keys)));
  94. }
  95. }
  96. // Sanitize the destination parameter (which is often used for redirects)
  97. // to prevent open redirect attacks leading to other domains.
  98. if (UrlHelper::isExternal($destination)) {
  99. // The destination is removed because it is an external URL.
  100. $bag->remove('destination');
  101. $sanitized = TRUE;
  102. if ($log_sanitized_keys) {
  103. trigger_error(sprintf('Potentially unsafe destination removed from %s parameter bag because it points to an external URL.', $bag_name));
  104. }
  105. }
  106. }
  107. return $sanitized;
  108. }
  109. /**
  110. * Checks a destination string to see if it is dangerous.
  111. *
  112. * @param string $destination
  113. * The destination string to check.
  114. * @param array $whitelist
  115. * An array of keys to whitelist as safe.
  116. *
  117. * @return array
  118. * The dangerous keys found in the destination parameter.
  119. */
  120. protected static function checkDestination($destination, array $whitelist) {
  121. $dangerous_keys = [];
  122. $parts = UrlHelper::parse($destination);
  123. // If there is a query string, check its query parameters.
  124. if (!empty($parts['query'])) {
  125. static::stripDangerousValues($parts['query'], $whitelist, $dangerous_keys);
  126. }
  127. return $dangerous_keys;
  128. }
  129. /**
  130. * Strips dangerous keys from $input.
  131. *
  132. * @param mixed $input
  133. * The input to sanitize.
  134. * @param string[] $whitelist
  135. * An array of keys to whitelist as safe.
  136. * @param string[] $sanitized_keys
  137. * An array of keys that have been removed.
  138. *
  139. * @return mixed
  140. * The sanitized input.
  141. */
  142. protected static function stripDangerousValues($input, array $whitelist, array &$sanitized_keys) {
  143. if (is_array($input)) {
  144. foreach ($input as $key => $value) {
  145. if ($key !== '' && $key[0] === '#' && !in_array($key, $whitelist, TRUE)) {
  146. unset($input[$key]);
  147. $sanitized_keys[] = $key;
  148. }
  149. else {
  150. $input[$key] = static::stripDangerousValues($input[$key], $whitelist, $sanitized_keys);
  151. }
  152. }
  153. }
  154. return $input;
  155. }
  156. }