RequestSanitizer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Drupal\Core\Security;
  3. use Symfony\Component\HttpFoundation\Request;
  4. /**
  5. * Sanitizes user input.
  6. */
  7. class RequestSanitizer {
  8. /**
  9. * Request attribute to mark the request as sanitized.
  10. */
  11. const SANITIZED = '_drupal_request_sanitized';
  12. /**
  13. * The name of the setting that configures the whitelist.
  14. */
  15. const SANITIZE_WHITELIST = 'sanitize_input_whitelist';
  16. /**
  17. * The name of the setting that determines if sanitized keys are logged.
  18. */
  19. const SANITIZE_LOG = 'sanitize_input_logging';
  20. /**
  21. * Strips dangerous keys from user input.
  22. *
  23. * @param \Symfony\Component\HttpFoundation\Request $request
  24. * The incoming request to sanitize.
  25. * @param string[] $whitelist
  26. * An array of keys to whitelist as safe. See default.settings.php.
  27. * @param bool $log_sanitized_keys
  28. * (optional) Set to TRUE to log an keys that are sanitized.
  29. *
  30. * @return \Symfony\Component\HttpFoundation\Request
  31. * The sanitized request.
  32. */
  33. public static function sanitize(Request $request, $whitelist, $log_sanitized_keys = FALSE) {
  34. if (!$request->attributes->get(self::SANITIZED, FALSE)) {
  35. // Process query string parameters.
  36. $get_sanitized_keys = [];
  37. $request->query->replace(static::stripDangerousValues($request->query->all(), $whitelist, $get_sanitized_keys));
  38. if ($log_sanitized_keys && !empty($get_sanitized_keys)) {
  39. trigger_error(sprintf('Potentially unsafe keys removed from query string parameters (GET): %s', implode(', ', $get_sanitized_keys)));
  40. }
  41. // Request body parameters.
  42. $post_sanitized_keys = [];
  43. $request->request->replace(static::stripDangerousValues($request->request->all(), $whitelist, $post_sanitized_keys));
  44. if ($log_sanitized_keys && !empty($post_sanitized_keys)) {
  45. trigger_error(sprintf('Potentially unsafe keys removed from request body parameters (POST): %s', implode(', ', $post_sanitized_keys)));
  46. }
  47. // Cookie parameters.
  48. $cookie_sanitized_keys = [];
  49. $request->cookies->replace(static::stripDangerousValues($request->cookies->all(), $whitelist, $cookie_sanitized_keys));
  50. if ($log_sanitized_keys && !empty($cookie_sanitized_keys)) {
  51. trigger_error(sprintf('Potentially unsafe keys removed from cookie parameters: %s', implode(', ', $cookie_sanitized_keys)));
  52. }
  53. if (!empty($get_sanitized_keys) || !empty($post_sanitized_keys) || !empty($cookie_sanitized_keys)) {
  54. $request->overrideGlobals();
  55. }
  56. $request->attributes->set(self::SANITIZED, TRUE);
  57. }
  58. return $request;
  59. }
  60. /**
  61. * Strips dangerous keys from $input.
  62. *
  63. * @param mixed $input
  64. * The input to sanitize.
  65. * @param string[] $whitelist
  66. * An array of keys to whitelist as safe.
  67. * @param string[] $sanitized_keys
  68. * An array of keys that have been removed.
  69. *
  70. * @return mixed
  71. * The sanitized input.
  72. */
  73. protected static function stripDangerousValues($input, array $whitelist, array &$sanitized_keys) {
  74. if (is_array($input)) {
  75. foreach ($input as $key => $value) {
  76. if ($key !== '' && $key[0] === '#' && !in_array($key, $whitelist, TRUE)) {
  77. unset($input[$key]);
  78. $sanitized_keys[] = $key;
  79. }
  80. else {
  81. $input[$key] = static::stripDangerousValues($input[$key], $whitelist, $sanitized_keys);
  82. }
  83. }
  84. }
  85. return $input;
  86. }
  87. }