Skip to content

Escaper: blocks and includes in XML have the XML content type - #433

Open
vrana wants to merge 13 commits into
nette:masterfrom
vrana:xml-block-type
Open

vrana wants to merge 13 commits into
nette:masterfrom
vrana:xml-block-type

Conversation

@vrana

@vrana vrana commented Oct 29, 2026

Copy link
Copy Markdown
Member
  • bug fix
  • BC break? no except E_USER_DEPRECATED

dg and others added 13 commits October 27, 2026 11:34
extractIndentation() moved the indentation into the element but left
TemplateParser::$lastIndentation set, so a line like '  </ul>{/if}' was taken
for a line holding only a Latte tag and lost its newline.
A coroutine (Swoole) or a fiber can pass the class_exists() check in
Engine::loadTemplate() and then wait for the cache lock while another one
declares the class. After getting the lock it included the file again and
failed with "Cannot redeclare class".
…BC break)

The value of srcdoc is an HTML document: the browser attribute-decodes it and
then parses the result as HTML. Latte escaped it like any other attribute, so
<iframe srcdoc="<p>{$x}</p>"> with $x = '<script>...</script>' rendered
&lt;script&gt;, which decodes to a live <script>. An iframe without sandbox
shares the parent's origin, so this was an XSS.

srcdoc now gets its own html/attr/html context: a value is escaped as HTML text
first and as an attribute second (HtmlHelpers::escapeHtmlAttr). This covers the
static attribute, the dynamic attribute srcdoc={$x}, n:attr and blocks or
templates included into srcdoc.

An Html object is encoded only once, so trusted markup keeps its tags;
previously they were stripped.

BC break: trusted HTML passed as a plain string is now displayed as text. Wrap
it in Latte\Runtime\Html (or use |noescape).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ly rendered as HTML

With Feature::MigrationWarnings, a plain srcdoc value that renders as a different
document now than before (it contains '<' or a character reference) triggers
a warning suggesting to wrap trusted HTML in Latte\Runtime\Html. Text such as
'a & b' renders the same document as before and stays quiet; so does an Html
object, whose tags were stripped before and are kept now. |accept silences it.

It covers dynamic attributes (srcdoc={$x}, srcdoc="{$x}") and n:attr. A mixed
value like srcdoc="<p>{$x}</p>" is escaped by code the Escaper emits, which has
no access to feature flags, so it does not warn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…reak)

Since 3.1, a null value of an attribute means the attribute is omitted. URL
attributes were an exception: the checkUrls pass appends |checkUrl, which
converts null to '', so <a href={$url}> with null rendered href="". That
makes the element a link to the current page.

The pass now appends the filter as nullsafe ?|checkUrl, so null skips it and
the attribute is dropped. With migration warnings, the change is reported like
for other attributes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…locale, instead of NumberFormatter's default half-even
In {include}, {embed}, {extends}, {layout} and {import} the arguments are
bound to parameters by position and by name, so in {include block, name: $a, $b}
the positional $b takes position 0 and overrides $name. PHP forbids such a call;
Latte now triggers E_USER_DEPRECATED and will throw in the next minor version.
Arrays of n:attr and n:class are not affected.
Creating a formatter costs about 7 µs, formatting with an existing one 0.3 µs.
The rounding mode is now set in a single place.
Vue, petite-vue and AngularJS read the DOM after the browser has decoded
entities, so the &nette#123; that escapeText() used for a single brace did not
protect anything: {$a}{$b} with '{' and '{7*7}}' produced a live {{ }}, and so
did '{{{' because strtr() replaced only non-overlapping pairs. A brace followed
by another one or ending the string, and a leading one, is now separated by an
empty comment; a brace in the middle stays as it is.

escapeRawHtml() for <script type="text/template"> did not separate braces at
all, so Vue evaluated {{ }} from a variable there.
Their content is text that the browser does not parse as HTML, so the empty
comment separating braces was displayed and corrupted the value, e.g. a {{ }}
in a submitted form (nette#409). The content now gets the subType rcdata, printed
by HtmlHelpers::escapeRcData() without touching braces.

A block placed in <textarea> or <title> has the content type html/rcdata,
which contextual filters now see instead of html; |stripHtml and |stripTags
accept it.
The XML escaper shares the HTML states and export() named them html,
html/attr, ..., so a block in XML had the type html, an include into XML was
converted as into HTML (text kept control characters, which are invalid in
XML) and including XML into XML failed as incompatible. The Convertors
entries for xml and xml/attr, and the xml types accepted by |stripHtml and
|stripTags, were never reached.

export() now names them xml, xml/attr, xml/comment. Text, JS and CSS are
converted into XML by the XML escapers. Filters see the XML type, so
|spaceless minifies a block as XML.

XML and HTML converted into each other like HTML into HTML. That keeps
working, with E_USER_DEPRECATED. Including an XML template into HTML, which
used to throw, now works with the same deprecation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants