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

Aperta Adatta ai principianti
#5,493 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php
Ambito
backend

Direzione di ricerca

Inizia in system/ee/ExpressionEngine/Service/Template/Variables/LegacyParser.php, in parseModifiedVariables(), e segui il modo in cui parseVariableProperties() analizza i risultati di extractVariables(). Riproduci l’esempio body/body2 dell’issue, poi verifica che i modificatori usino il valore del nome del campo corrispondente. Il lavoro è terminato quando entrambi gli ordini dei parametri producono le lunghezze previste per body e body2; l’issue non indica alcun file di test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

  • This is the same kind of bug as #55 (get_pair_field() matching pair fields with the same prefix).
  • #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 :length and :limit.
Lingua principale
PHP
Stelle
505
Fork
135
Merge medio
4g 21h
PR unite (30g)
23

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ExpressionEngine/ExpressionEngine

Tutte le issue di ExpressionEngine/ExpressionEngine

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.