Skip to content

Modifiers on early-parsed variables use the wrong value when one variable name is a prefix of another (embed:body vs embed:body2) #5493

In review

Description

@ignetic

Description of the problem

When two early-parsed variables share a name prefix, such as embed parameters body and body2, a modifier on the longer one can output the shorter one's value. This happens with every modifier, core and add-on, with or without parameters.

It affects every early-parsed variable that goes through LegacyParser::parseModifiedVariables(): embed and layout variables, segment variables ({segment_1} vs {segment_10}), template route variables and global variables. The wrong value comes out with no error, and whether it happens depends on the order of the variables, so it's easy to miss.

How To Reproduce

  1. Create a template test/.inner:
body: {embed:body:length}
body2: {embed:body2:length}
  1. Embed it from test/outer:
{embed="test/.inner" body="abc" body2="abcdefgh"}

Expected:

body: 3
body2: 8

Actual:

body: 3
body2: 3
  1. Swap the order of the embed parameters: {embed="test/.inner" body2="abcdefgh" body="abc"}. The output is now correct:
body: 3
body2: 8

It's the same with parameterised core modifiers ({embed:body2:limit characters="3"} outputs body's value) and with modifiers registered by add-ons.

Error Messages

None. The wrong value is output with no error.

Screenshots / Videos / Template Code

The template code is above.

Environment Details:

  • Version: tested on 7.5.28. parseModifiedVariables() is unchanged in 7.6.0 and on 7.dev.
  • PHP Version: 8.3.30 (FPM)
  • OS: Linux
  • Web Server: nginx

Possible Solution

In system/ee/ExpressionEngine/Service/Template/Variables/LegacyParser.php, parseModifiedVariables() loops over each early variable $name and calls extractVariables($str, $name). That matches with /{(embed:body.*?)}/, which also catches {embed:body2:length}. The loop then applies the modifiers to embed:body's value and replaces {embed:body2:length} with the result. Nothing checks that the parsed field name is the variable being processed. If embed:body2 happens to be processed first, its tag is already gone by the time embed:body is processed, which is why the order matters.

A check after parseVariableProperties() fixes it:

foreach ($extracted_vars['var_single'] as $modified_var) {
    $content = $value;
    $var_props = $this->parseVariableProperties($modified_var, $prefix);

    // {embed:body2:...} also matches when processing embed:body; skip it so body2 is handled with its own value
    if ($var_props['field_name'] !== substr($name, strlen($prefix))) {
        continue;
    }
    ...

Additional context

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions