request-sanitizer.inc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * @file
  4. * Contains code for sanitizing user input from the request.
  5. */
  6. /**
  7. * Sanitizes user input from the request.
  8. */
  9. class DrupalRequestSanitizer {
  10. /**
  11. * Tracks whether the request was already sanitized.
  12. */
  13. protected static $sanitized = FALSE;
  14. /**
  15. * Modifies the request to strip dangerous keys from user input.
  16. */
  17. public static function sanitize() {
  18. if (!self::$sanitized) {
  19. $whitelist = variable_get('sanitize_input_whitelist', array());
  20. $log_sanitized_keys = variable_get('sanitize_input_logging', FALSE);
  21. // Process query string parameters.
  22. $get_sanitized_keys = array();
  23. $_GET = self::stripDangerousValues($_GET, $whitelist, $get_sanitized_keys);
  24. if ($log_sanitized_keys && $get_sanitized_keys) {
  25. _drupal_trigger_error_with_delayed_logging(format_string('Potentially unsafe keys removed from query string parameters (GET): @keys', array('@keys' => implode(', ', $get_sanitized_keys))), E_USER_NOTICE);
  26. }
  27. // Process request body parameters.
  28. $post_sanitized_keys = array();
  29. $_POST = self::stripDangerousValues($_POST, $whitelist, $post_sanitized_keys);
  30. if ($log_sanitized_keys && $post_sanitized_keys) {
  31. _drupal_trigger_error_with_delayed_logging(format_string('Potentially unsafe keys removed from request body parameters (POST): @keys', array('@keys' => implode(', ', $post_sanitized_keys))), E_USER_NOTICE);
  32. }
  33. // Process cookie parameters.
  34. $cookie_sanitized_keys = array();
  35. $_COOKIE = self::stripDangerousValues($_COOKIE, $whitelist, $cookie_sanitized_keys);
  36. if ($log_sanitized_keys && $cookie_sanitized_keys) {
  37. _drupal_trigger_error_with_delayed_logging(format_string('Potentially unsafe keys removed from cookie parameters (COOKIE): @keys', array('@keys' => implode(', ', $cookie_sanitized_keys))), E_USER_NOTICE);
  38. }
  39. $request_sanitized_keys = array();
  40. $_REQUEST = self::stripDangerousValues($_REQUEST, $whitelist, $request_sanitized_keys);
  41. self::$sanitized = TRUE;
  42. }
  43. }
  44. /**
  45. * Strips dangerous keys from the provided input.
  46. *
  47. * @param mixed $input
  48. * The input to sanitize.
  49. * @param string[] $whitelist
  50. * An array of keys to whitelist as safe.
  51. * @param string[] $sanitized_keys
  52. * An array of keys that have been removed.
  53. *
  54. * @return mixed
  55. * The sanitized input.
  56. */
  57. protected static function stripDangerousValues($input, array $whitelist, array &$sanitized_keys) {
  58. if (is_array($input)) {
  59. foreach ($input as $key => $value) {
  60. if ($key !== '' && $key[0] === '#' && !in_array($key, $whitelist, TRUE)) {
  61. unset($input[$key]);
  62. $sanitized_keys[] = $key;
  63. }
  64. else {
  65. $input[$key] = self::stripDangerousValues($input[$key], $whitelist, $sanitized_keys);
  66. }
  67. }
  68. }
  69. return $input;
  70. }
  71. }