Skip to content

Extension: added getAttributeContexts() for escaping custom attributes as JS, CSS or HTML - #426

Open
vrana wants to merge 14 commits into
nette:masterfrom
vrana:attribute-contexts
Open

vrana wants to merge 14 commits into
nette:masterfrom
vrana:attribute-contexts

Conversation

@vrana

@vrana vrana commented Sep 28, 2026

Copy link
Copy Markdown
Member

dg and others added 8 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
@dg

dg commented Oct 28, 2026

Copy link
Copy Markdown
Member

I'd like to take this, with one change in shape: let the built-in contexts be the default entries of the same table instead of the hard-coded checks, i.e. 'on*' => JavaScript, 'style' => Css, 'srcdoc' => Html, and extensions only add to it (overriding a built-in stays an exception). Escaper::enterHtmlAttribute() and classifyAttributeType() then ask one function and the special cases for on*, style and srcdoc disappear. Could you rework it that way and rebase it on master? #425 is merged now.

dg and others added 6 commits October 29, 2026 00:08
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.
…s as JS, CSS or HTML

An extension can map attribute names, or prefixes ending with *, to
ContentType::JavaScript, Css or Html, e.g. 'x-on:*' => JavaScript or Tippy's
'data-tippy-content' => Html. A registered attribute is then escaped exactly like
on*, style or srcdoc: static values via the Escaper subType, dynamic attributes
(name={$x}) and n:attr via classifyAttributeType(), which now takes the map.
n:attr receives it as a compiled argument only when some context is registered.

Built-in contexts can't be overridden (on*, style, srcdoc and prefixes like s*
are rejected). The map is part of the configuration signature, so extensions
don't have to include it in getCacheKey().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tributes as JS, CSS or HTML

The built-in contexts are the default entries of the same table
(HtmlHelpers::AttributeContexts: on*, style, srcdoc) and extensions only add
to it. enterHtmlAttribute() and classifyAttributeType() ask
matchAttributeContext() alone; the hard-coded checks of on*, style and srcdoc
are gone. Overriding a built-in stays an exception.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vrana
vrana force-pushed the attribute-contexts branch from 3bd7dfa to bdb1f41 Compare October 29, 2026 06:34
@vrana
vrana changed the base branch from srcdoc-escaping to master October 29, 2026 06:34
@vrana

vrana commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

Makes sense, done.

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