I have a very simple form function (see below). It just creates a text box and inserts that into a database.

When I use either basic or Mcrypt and #encrypt => TRUE on that field I get an error:

Error message
PDOException: in form_example_tutorial_2_submit() (line 97 of /home/greggles/workspace/d7/sites/all/modules/contrib/examples/form_example/form_example_tutorial.inc).

I think the problem is that Drupal's database API isn't able to handle some of the resulting characters from the encryption functions. If we add a uuencode and uudecode to the process then it works fine. I'm not sure if we should add this here or in the encryption_method plugins (e.g. mcrypt_rij_256.inc) but this seems like a good start: the problem only occurs when trying to save the data to the database so, at this point, it feels best to leave the encryption_method plugins "pure" while the fapi convenience tool gets the uuencoding.

Here's a simple module to demonstrate the problem:

function form_example_tutorial_2($form, &$form_state) {
  $fields = db_select('e2')
      ->fields('e2')
      ->orderBy('id', 'DESC')
      ->range(0,1)
      ->execute()
      ->fetchAll();
  if (!empty($fields[0]->text_value)) {
    $text_value = $fields[0]->text_value;
  }
  else {
    $text_value = convert_uuencode(encrypt('monkeypants')); 
  }

  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('A simple form with a submit button'),
  );

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $text_value,
    '#encrypt' => TRUE,
  );

  // Adds a simple submit button that refreshes the form and clears its
  // contents. This is the default behavior for forms.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

function form_example_tutorial_2_submit($form, &$form_state) {
  if (!empty($form_state['values']['name'])) {
    db_insert('e2')
      ->fields(array('text_value' => $form_state['values']['name']))
      ->execute();
  }
}

To create the table:
create table e2 (id int auto_increment, text_value varchar(2550), primary key (id));

Comments

greggles’s picture

Status: Active » Needs review
StatusFileSize
new1.43 KB
greggles’s picture

Title: #encrypt => TRUE causes fatal errors » #encrypt => TRUE can cause fatal errors
greggles’s picture

StatusFileSize
new2.38 KB

Ok, so I did a bit more review and see now:

1. base64 is way cooler than uuencoding
2. the MCRYPT_RIJNDAEL_256 has an option to base64encode, but that is not being used and missing from basic

So, the attached patch:
1. Tries to use the option when possible
2. Adds it as an option to the basic method

This seems to work pretty well for the MCRYPT_RIJNDAEL_256 encryption, but there's a bug somewhere in my logic for the basic encryption. If I set the site to use basic and then do drupal_set_message(decrypt(encrypt('asdf', array('base64' => TRUE)), array('base64' => TRUE))); I get back something beyond just the asdf.

Status: Needs review » Needs work

The last submitted patch, 1775734_encrypt_uuencode_2.patch, failed testing.

greggles’s picture

Status: Needs work » Needs review

#3: 1775734_encrypt_uuencode_2.patch queued for re-testing.

greggles’s picture

Title: #encrypt => TRUE can cause fatal errors » Add base64 option to the basic encryption (#encrypt => TRUE can cause fatal errors)

Status: Needs review » Needs work

The last submitted patch, 1775734_encrypt_uuencode_2.patch, failed testing.

greggles’s picture

Status: Needs work » Needs review
StatusFileSize
new4.32 KB

OK, maybe the basic method just doesn't need to base64 encode. I can no longer reproduce the issue I was initially having.

greggles’s picture

Title: Add base64 option to the basic encryption (#encrypt => TRUE can cause fatal errors) » Try to use base64 by default in ecryptfapi.module to store text in a more reliable form
Status: Needs review » Fixed

Better title for what's happening.

Given that this now passes and nobody's commented that this seemed like a bad idea I committed it. http://drupalcode.org/project/encrypt.git/commit/4bab066

I also created a new beta release from the 7.x-2.x branch with notes on how to deal with upgrading if people used this sub-module.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.