Token detection in email validation doesn't work if the token contains a comma

Created on 29 November 2024, 23 days ago

Problem/Motivation

In ✨ Co-Authors Token Active , we're working on providing a token with an array of all email addresses of the co-authors of a token. When trying to save [webform_submission:source-entity:co_authors_email:join:,] into the CC field of an email handler, the validation fails:

The email address [webform_submission:source-entity:co_authors_email:join is not valid.

Steps to reproduce

Add an email handler to a webform with [webform_submission:source-entity:co_authors_email:join:,] as CC field

Proposed resolution

The email validation contains a check to skip validation if the value contains a token. This check should happen before splitting the value on comma instead of after, because tokens can contain comma's.

πŸ› Bug report
Status

Active

Version

6.3

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

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

Merge Requests

Comments & Activities

  • Issue created by @dieterholvoet
  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels
  • Pipeline finished with Failed
    23 days ago
    Total: 468s
    #354063
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    I understand the issue but we need to support email addresses and tokens

    admin@example.com, [webform_submission:source-entity:co_authors_email:join:,]

    Maybe we need to remove all tokens before the value's email addresses are validated.

    Below is an untested POC.

          // If tokens are allowed, remove them before validating the email addresses.
          $values = preg_split(
            '/\s*,\s*/',
            (!empty($element['#allow_tokens']))
              ? preg_replace('/\[[^\]]+\]/', '', $value)
              : $value
          );
          foreach ($values as $value) {
            if ($value && !\Drupal::service('email.validator')->isValid($value)) {
              $form_state->setError($element, t('The email address %mail is not valid.', ['%mail' => $value]));
              return;
            }
    
  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    Wouldn't it be best if we actually replace the tokens before validating? I noticed there's cardinality validation after the email address validation. That cardinality validation can be bypassed when you use tokens, because one token can include multiple email addresses.

  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    Never mind, we're in context of a settings form. Of course there's no webform submission there to replace tokens with. I guess we can ignore the cardinality validation in this context.

  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    I applied your suggestion.

  • Pipeline finished with Failed
    23 days ago
    Total: 408s
    #354286
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    We need to fix the broken test.

    Can we please use snake case to internal variables?

    $emailAddresses use $email_addresses (or $emails)

  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    I updated the casing.

    About the test failure, there's a test that checks if the following is allowed:

    [token1]@[token2].com

    Which isn't anymore, because now we're stripping tokens before validating email addresses and looking at the test output, it's trying to validate @.com as an email address. Do we remove this test or update the implementation?

  • Pipeline finished with Failed
    18 days ago
    Total: 588s
    #358690
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    I think the [token1]@[token2].com test was added for a reason.

    We might have to take a different approach.

  • Pipeline finished with Canceled
    15 days ago
    Total: 415s
    #361898
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    Pleaser review the different approach, which escapes the tokens and then skips the validation. The MR includes test coverage for commas within tokens.

  • Pipeline finished with Canceled
    15 days ago
    Total: 201s
    #361899
  • Pipeline finished with Success
    15 days ago
    Total: 449s
    #361901
  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    Just tested and that works, thanks a lot for the help!

  • Pipeline finished with Skipped
    13 days ago
    #363584
  • Pipeline finished with Skipped
    13 days ago
    #363585
  • Pipeline finished with Skipped
    13 days ago
    #363586
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
Production build 0.71.5 2024