Problem/Motivation

If you implement an entity type update hook eg `my_module_node_update()`, you are unable to determine if an encrypted field value has changed by utilizing the `original` property as its fields are still encrypted.

Steps to reproduce

  1. Set up a `Test` content type with an encrypted text field `Test`
  2. Paste the example node update hook code below into a custom module
  3. Create a new `Test` node and then edit it and change the `Test` field value
  4. If you debug the node update hook code you will see the `$current_value` is correctly decrypted but the `$previous_value` is still encrypted.

Test code:

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function my_module_node_update(NodeInterface $entity) {
  $current_value = $entity->field_test->getString();
  $previous_value = $entity->original->field_test->getString();
}

Proposed resolution

Modify update hook code to also decrypt the `original` property if it exists.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tame4tex created an issue. See original summary.

tame4tex’s picture

Issue summary: View changes
tame4tex’s picture

Assigned: Unassigned » tame4tex

tame4tex’s picture

Status: Active » Needs review
StatusFileSize
new1.28 KB

Attached patch from MR for those that prefer a patch file.

ptmkenny’s picture

Version: 3.1.x-dev » 3.2.x-dev
Status: Needs review » Needs work

We should solve this issue but we need a test to confirm a change like this. Updating the development target since 3.2.x is now the active dev branch.

ptmkenny’s picture

Assigned: tame4tex » Unassigned

tame4tex’s picture

Status: Needs work » Needs review
StatusFileSize
new3.77 KB

I have added a new MR !72 for the 3.2.x branch which includes tests for the bug.

Let me know if there is anything else needed to get this committed. Thanks!

alexpott’s picture

Version: 3.2.x-dev » 4.x-dev

Confirmed this bug - we have to undo the work done in \Drupal\field_encrypt\ProcessEntities::encryptEntity()

alexpott changed the visibility of the branch 3463413-decrypt-original-3.2.x to hidden.

alexpott changed the visibility of the branch 3463413-decrypt-original to hidden.

alexpott changed the visibility of the branch 3.2.x to hidden.

alexpott changed the visibility of the branch 3.1.x to hidden.

alexpott’s picture

alexpott’s picture

Status: Needs review » Reviewed & tested by the community

It is a shame about the performance hit but there is nothing we can do and this is a bug.

  • alexpott committed 27d59146 on 4.x
    Issue #3463413 by tame4tex, alexpott, ptmkenny: Entity attached to "...

  • alexpott committed bd2c6947 on 3.2.x
    Issue #3463413 by tame4tex, alexpott, ptmkenny: Entity attached to "...
alexpott’s picture

Version: 4.x-dev » 3.2.x-dev

Fixed in 4.x and 3.2.x

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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