twigphp / twigphp/Twig

Inconsistency with variable evaluation when autoescaping is enabled

Open
#3,917 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
8.4k
Forks
1.3k
Avg merge
1d 13h
Merged PRs (30d)
37

Description

Consider the following template:

{{ (br == "<br/>") ? "true" : "false" }}
{{ (include("partial.twig") == "<br/>") ? "true" : "false" }}

With partial.twig being:

{{ br }}

Executing this template with "<br/>" as value for the br variable outputs:

true
false

In both conditions, we test that the value of the variable br is equal to "<br/>". But in the second case only, it is considered as false.

https://twigfiddle.com/zdltz2

Why is a variable considered as safe for evaluation in one case and not the other? I understand the technical reason - "in the second case what is evaluated is the result of the inclusion", I'm more interested in the philosophical reasons: why is a template inclusion considered as less safe than a variable inclusion even though a variable value is more likely to be provided at runtime (and thus to be unsafe) than a template inclusion?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the two expressions with the provided template and twigfiddle example, then trace autoescaping and include evaluation in the implementation. No repository files or tests are named; done would require an agreed explanation of the differing safety behavior and a clearly defined follow-up change or documentation update.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
security, web-dev
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.