Repository navigation
Description
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
- Create a template
test/.inner:
body: {embed:body:length}
body2: {embed:body2:length}
- Embed it from
test/outer:
{embed="test/.inner" body="abc" body2="abcdefgh"}
Expected:
body: 3
body2: 8
Actual:
body: 3
body2: 3
- 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 on7.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
- This is the same kind of bug as get_pair_field() matches incorrectly if multiple pair fields have the same prefix #55 (
get_pair_field()matching pair fields with the same prefix). - {path} tag outputs incorrectly if {current_path:url_encode} exists within same template #3543 (
{path}output affected by{current_path:url_encode}) may be related. It couldn't be reproduced on a clean install at the time. - Found while testing modifiers from the third-party add-on https://github.com/ignetic/ee-extra-modifiers, then confirmed with core
:lengthand:limit.
Activity