Skip to content

Srcdoc escaping - #425

Closed
vrana wants to merge 3 commits into
nette:masterfrom
vrana:srcdoc-escaping
Closed

vrana wants to merge 3 commits into
nette:masterfrom
vrana:srcdoc-escaping

Conversation

@vrana

@vrana vrana commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

dg and others added 3 commits July 31, 2026 19:48
…boolean attributes (BC break)

None of them is a boolean attribute in HTML; they are enumerated ones, so
rendering them as a bare name was wrong in three separate ways:

- a string 'false' is truthy in PHP, so contenteditable={$s} rendered the
  attribute present, i.e. the exact opposite of what was asked for
- the empty value is not a keyword of draggable, it maps to the invalid value
  default 'auto', so draggable=true could not be expressed at all
- 'attribute missing' and 'attribute false' are different states; the false
  value is what disables editing inside a contenteditable region, and dropping
  the attribute silently inherited from the parent instead

Two new categories replace them. 'tristate' (contenteditable, draggable,
spellcheck, writingsuggestions) maps bool to the true/false keywords, exactly
like aria-* already does, so formatAriaAttribute now delegates to it. The
'valuedBool' one (hidden, popover) stays a flag but keeps a non-empty value,
so hidden="until-found" survives. Falsy values are tested first there: hidden
with a '0' coming from a database must not render hidden="0", which is an
invalid value and would map back to the hidden state.
…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>
@dg

dg commented Oct 29, 2026

Copy link
Copy Markdown
Member

Thanks, merged!

@dg dg closed this Oct 29, 2026
dg pushed a commit that referenced this pull request Nov 17, 2026
…BC break) (#425)

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 twice as an
attribute value (HtmlHelpers::escapeHtmlAttr). Latte does not parse the document,
so the value may land in its text as well as in one of its attributes, as in
<iframe srcdoc="<p title=&quot;{$x}&quot;>">, where an unescaped quote would let
it add an event handler. 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).
dg pushed a commit that referenced this pull request Nov 17, 2026
…ly rendered as HTML (#425)

With Feature::MigrationWarnings, a plain srcdoc value that renders as a different
document now than before (it contains a tag or a character reference, also one
that browsers decode without a semicolon) triggers a warning suggesting to wrap
trusted HTML in Latte\Runtime\Html. Text such as 'a & b' or '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.
dg pushed a commit that referenced this pull request Nov 18, 2026
…BC break) (#425)

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 twice as an
attribute value (HtmlHelpers::escapeHtmlAttr). Latte does not parse the document,
so the value may land in its text as well as in one of its attributes, as in
<iframe srcdoc="<p title=&quot;{$x}&quot;>">, where an unescaped quote would let
it add an event handler. This covers the static attribute, the dynamic attribute
srcdoc={$x}, n:attr and blocks or templates included into srcdoc.

An Html object, and a value printed with |noescape, is encoded only once, so
trusted markup keeps its tags; previously an Html object lost them and
|noescape escaped only quotes, so an escaped <script> in it came alive.

A block defined in srcdoc becomes HTML by one attribute decoding, so it can be
included elsewhere and overridden by a block of an HTML template, and |stripHtml
and |stripTags accept it. Both work on the document, so |stripTags also removes
tags the attribute holds as entities. A block from an ordinary attribute is
included into srcdoc as text.

BC break: trusted HTML passed as a plain string is now displayed as text. Wrap
it in Latte\Runtime\Html (or use |noescape).
dg pushed a commit that referenced this pull request Nov 18, 2026
…ly rendered as HTML (#425)

With Feature::MigrationWarnings, a plain srcdoc value that renders as a different
document now than before (it contains a tag or a character reference, also one
that browsers decode without a semicolon) triggers a warning suggesting to wrap
trusted HTML in Latte\Runtime\Html. Text such as 'a & b' or '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.
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.

2 participants