Database exception if honeypot_time form element is tampered with

Created on 23 October 2018, over 6 years ago
Updated 29 August 2024, 8 months ago

The hidden 'honeypot_time' field can be altered client side (which is an issue on it's own I think???). This can result in database exceptions to be thrown and as a result fill up your watchdog table if a lot of submissions are attempted.

This is the code:

// Get the time value.
  $identifier = $form_state->getValue('honeypot_time', FALSE);
  $honeypot_time = \Drupal::service('keyvalue.expirable')->get('honeypot_time_restriction')->get($identifier, 0);

  // Get the honeypot_time_limit.
  $time_limit = honeypot_get_time_limit($form_state->getValues());

  // Make sure current time - (time_limit + form time value) is greater than 0.
  // If not, throw an error.
  if (!$honeypot_time || \Drupal::time()->getRequestTime() < ($honeypot_time + $time_limit)) {
    _honeypot_log($form_state->getValue('form_id'), 'honeypot_time');
    $time_limit = honeypot_get_time_limit();
    \Drupal::service('keyvalue.expirable')->get('honeypot_time_restriction')->setWithExpire($identifier, \Drupal::time()->getRequestTime(), 3600*24);
    $form_state->setErrorByName('', t('There was a problem with your form submission. Please wait @limit seconds and try again.', ['@limit' => $time_limit]));
  }

If $identifier is replaced with a string which is longer than 128 characters, we get an exception:
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'name' at row 1: INSERT INTO {key_value_expire} (name, collection, value) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => /../../../../../../../../../windows/win.ini............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................. [:db_insert_placeholder_1] => honeypot_time_restriction [:db_insert_placeholder_2] => i:1540294474; ) in _honeypot_time_restriction_validate()

Suggested solution:
- truncate $identifier to 128 chars before writing it to the database.

πŸ› Bug report
Status

Needs review

Version

2.2

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium kriboogh

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024