summaryrefslogtreecommitdiff
path: root/includes/form.inc
diff options
context:
space:
mode:
Diffstat (limited to 'includes/form.inc')
-rw-r--r--includes/form.inc42
1 files changed, 39 insertions, 3 deletions
diff --git a/includes/form.inc b/includes/form.inc
index bec7d1e1c..846bcb51f 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -167,6 +167,12 @@ function drupal_get_form($form_id) {
* processed.
* - base_form_id: Identification for a base form, as declared in a
* hook_forms() implementation.
+ * - immutable: If this flag is set to TRUE, a new form build id is
+ * generated when the form is loaded from the cache. If it is subsequently
+ * saved to the cache again, it will have another cache id and therefore
+ * the original form and form-state will remain unaltered. This is
+ * important when page caching is enabled in order to prevent form state
+ * from leaking between anonymous users.
* - rebuild_info: Internal. Similar to 'build_info', but pertaining to
* drupal_rebuild_form().
* - rebuild: Normally, after the entire form processing is completed and
@@ -458,16 +464,24 @@ function drupal_rebuild_form($form_id, &$form_state, $old_form = NULL) {
$form = drupal_retrieve_form($form_id, $form_state);
// If only parts of the form will be returned to the browser (e.g., Ajax or
- // RIA clients), re-use the old #build_id to not require client-side code to
- // manually update the hidden 'build_id' input element.
+ // RIA clients), or if the form already had a new build ID regenerated when it
+ // was retrieved from the form cache, reuse the existing #build_id.
// Otherwise, a new #build_id is generated, to not clobber the previous
// build's data in the form cache; also allowing the user to go back to an
// earlier build, make changes, and re-submit.
// @see drupal_prepare_form()
- if (isset($old_form['#build_id']) && !empty($form_state['rebuild_info']['copy']['#build_id'])) {
+ $enforce_old_build_id = isset($old_form['#build_id']) && !empty($form_state['rebuild_info']['copy']['#build_id']);
+ $old_form_is_mutable_copy = isset($old_form['#build_id_old']);
+ if ($enforce_old_build_id || $old_form_is_mutable_copy) {
$form['#build_id'] = $old_form['#build_id'];
+ if ($old_form_is_mutable_copy) {
+ $form['#build_id_old'] = $old_form['#build_id_old'];
+ }
}
else {
+ if (isset($old_form['#build_id'])) {
+ $form['#build_id_old'] = $old_form['#build_id'];
+ }
$form['#build_id'] = 'form-' . drupal_random_key();
}
@@ -522,6 +536,15 @@ function form_get_cache($form_build_id, &$form_state) {
}
}
}
+ // Generate a new #build_id if the cached form was rendered on a cacheable
+ // page.
+ if (!empty($form_state['build_info']['immutable'])) {
+ $form['#build_id_old'] = $form['#build_id'];
+ $form['#build_id'] = 'form-' . drupal_random_key();
+ $form['form_build_id']['#value'] = $form['#build_id'];
+ $form['form_build_id']['#id'] = $form['#build_id'];
+ unset($form_state['build_info']['immutable']);
+ }
return $form;
}
}
@@ -534,15 +557,28 @@ function form_set_cache($form_build_id, $form, $form_state) {
// 6 hours cache life time for forms should be plenty.
$expire = 21600;
+ // Ensure that the form build_id embedded in the form structure is the same as
+ // the one passed in as a parameter. This is an additional safety measure to
+ // prevent legacy code operating directly with form_get_cache and
+ // form_set_cache from accidentally overwriting immutable form state.
+ if ($form['#build_id'] != $form_build_id) {
+ watchdog('form', 'Form build-id mismatch detected while attempting to store a form in the cache.', array(), WATCHDOG_ERROR);
+ return;
+ }
+
// Cache form structure.
if (isset($form)) {
if ($GLOBALS['user']->uid) {
$form['#cache_token'] = drupal_get_token();
}
+ unset($form['#build_id_old']);
cache_set('form_' . $form_build_id, $form, 'cache_form', REQUEST_TIME + $expire);
}
// Cache form state.
+ if (variable_get('cache', 0) && drupal_page_is_cacheable()) {
+ $form_state['build_info']['immutable'] = TRUE;
+ }
if ($data = array_diff_key($form_state, array_flip(form_state_keys_no_cache()))) {
cache_set('form_state_' . $form_build_id, $data, 'cache_form', REQUEST_TIME + $expire);
}