Skip to content

Instantly share code, notes, and snippets.

@pounard
Created March 28, 2018 20:26
Show Gist options
  • Save pounard/afa28924427304ced7d5d64495b87bb8 to your computer and use it in GitHub Desktop.
Save pounard/afa28924427304ced7d5d64495b87bb8 to your computer and use it in GitHub Desktop.
Backport of https://www.drupal.org/sa-core-2018-002 for Drupal 6
commit f3c67888421b31f71ae0a8cabb5259676150baa4
Author: Pierre Rineau <pierre.rineau@makina-corpus.com>
Date: Wed Mar 28 22:24:01 2018 +0200
https://www.drupal.org/sa-core-2018-002 backport from Drupal 7
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 23179ca98d..c14d966d86 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -1142,6 +1142,10 @@ function _drupal_bootstrap($phase) {
timer_start('page');
// Initialize the configuration
conf_init();
+
+ // Sanitize unsafe keys from the request.
+ require_once './includes/request-sanitizer.inc';
+ DrupalRequestSanitizer::sanitize();
break;
case DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE:
diff --git a/includes/request-sanitizer.inc b/includes/request-sanitizer.inc
new file mode 100644
index 0000000000..662581edc7
--- /dev/null
+++ b/includes/request-sanitizer.inc
@@ -0,0 +1,82 @@
+<?php
+
+/**
+ * @file
+ * Contains code for sanitizing user input from the request.
+ */
+
+/**
+ * Sanitizes user input from the request.
+ */
+class DrupalRequestSanitizer {
+
+ /**
+ * Tracks whether the request was already sanitized.
+ */
+ protected static $sanitized = FALSE;
+
+ /**
+ * Modifies the request to strip dangerous keys from user input.
+ */
+ public static function sanitize() {
+ if (!self::$sanitized) {
+ $whitelist = variable_get('sanitize_input_whitelist', array());
+ $log_sanitized_keys = variable_get('sanitize_input_logging', FALSE);
+
+ // Process query string parameters.
+ $get_sanitized_keys = array();
+ $_GET = self::stripDangerousValues($_GET, $whitelist, $get_sanitized_keys);
+ if ($log_sanitized_keys && $get_sanitized_keys) {
+ trigger_error(strtr('Potentially unsafe keys removed from query string parameters (GET): @keys', array('@keys' => implode(', ', $get_sanitized_keys))), E_USER_NOTICE);
+ }
+
+ // Process request body parameters.
+ $post_sanitized_keys = array();
+ $_POST = self::stripDangerousValues($_POST, $whitelist, $post_sanitized_keys);
+ if ($log_sanitized_keys && $post_sanitized_keys) {
+ trigger_error(strtr('Potentially unsafe keys removed from request body parameters (POST): @keys', array('@keys' => implode(', ', $post_sanitized_keys))), E_USER_NOTICE);
+ }
+
+ // Process cookie parameters.
+ $cookie_sanitized_keys = array();
+ $_COOKIE = self::stripDangerousValues($_COOKIE, $whitelist, $cookie_sanitized_keys);
+ if ($log_sanitized_keys && $cookie_sanitized_keys) {
+ trigger_error(strtr('Potentially unsafe keys removed from cookie parameters (COOKIE): @keys', array('@keys' => implode(', ', $cookie_sanitized_keys))), E_USER_NOTICE);
+ }
+
+ $request_sanitized_keys = array();
+ $_REQUEST = self::stripDangerousValues($_REQUEST, $whitelist, $request_sanitized_keys);
+
+ self::$sanitized = TRUE;
+ }
+ }
+
+ /**
+ * Strips dangerous keys from the provided input.
+ *
+ * @param mixed $input
+ * The input to sanitize.
+ * @param string[] $whitelist
+ * An array of keys to whitelist as safe.
+ * @param string[] $sanitized_keys
+ * An array of keys that have been removed.
+ *
+ * @return mixed
+ * The sanitized input.
+ */
+ protected static function stripDangerousValues($input, array $whitelist, array &$sanitized_keys) {
+ if (is_array($input)) {
+ foreach ($input as $key => $value) {
+ if ($key !== '' && $key[0] === '#' && !in_array($key, $whitelist, TRUE)) {
+ unset($input[$key]);
+ $sanitized_keys[] = $key;
+ }
+ else {
+ $input[$key] = self::stripDangerousValues($input[$key], $whitelist, $sanitized_keys);
+ }
+ }
+ }
+ return $input;
+ }
+
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment