Problem/Motivation

The Token API uses this regex to match and assemble a list of tokens in a string:

   // Matches tokens with the following pattern: [$type:$name]
    // $type and $name may not contain [ ] characters.
    // $type may not contain : or whitespace characters, but $name may.
    preg_match_all('/
      \[             # [ - pattern start
      ([^\s\[\]:]*)  # match $type not containing whitespace : [ or ]
      :              # : - separator
      ([^\[\]]*)     # match $name not containing [ or ]
      \]             # ] - pattern end
      /x', $text, $matches);

It uses the * character which will match zero or more characters, meaning it will match empty token types or tokens, which could lead to unexpected results.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because [:some_text] is not a valid token
Issue priority Normal because there is unlikely to be any code in core that actually hits this bug.
Prioritized changes The main goal of this issue is a bugfix, so it is a prioritized change for the beta phase.
Disruption Not disruptive for core or contributed modules unless they were somehow relying on matching empty tokens

Proposed resolution

Use the + repetition character (1 or more) rather than * (0 or more).

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards Novice Instructions

User interface changes

None.

API changes

None.

CommentFileSizeAuthor
token-not-empty-PASS.patch2.28 KBxjm
token-not-empty-FAIL.patch1.53 KBxjm
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

xjm’s picture

Issue summary: View changes

The last submitted patch, token-not-empty-FAIL.patch, failed testing.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Thank you seems straight forward so RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 62ba78d and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation for to the issue summary.

  • alexpott committed 62ba78d on 8.0.x
    Issue #2396793 by xjm: Token API will match tokens with empty types or...

Status: Fixed » Closed (fixed)

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