Skip to content

Commit

Permalink
Filters: compatibility with JS binding II.
Browse files Browse the repository at this point in the history
- when {{ is used inside a <script type="text/template"> it can either be written as entity &#123; OR with comment
- when {{ is used in HTML it must be written with comment (which cannot be used in HTML attribute)
  • Loading branch information
dg committed Oct 27, 2021
1 parent 7964d02 commit 1adde0c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Latte/Runtime/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static function escapeHtmlAttr($s, bool $double = true): string
if (strpos($s, '`') !== false && strpbrk($s, ' <>"\'') === false) {
$s .= ' '; // protection against innerHTML mXSS vulnerability nette/nette#1496
}
return htmlspecialchars($s, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8', $double);
$s = htmlspecialchars($s, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8', $double);
$s = str_replace('{', '&#123;', $s);
return $s;
}


Expand Down
3 changes: 3 additions & 0 deletions tests/Latte/Filters.escapeHtmlAttr().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ Assert::same('`hello&apos;', Filters::escapeHtmlAttr("`hello'"));
// invalid UTF-8
Assert::same("foo \u{FFFD} bar", Filters::escapeHtmlAttr("foo \u{D800} bar")); // invalid codepoint high surrogates
Assert::same("foo \u{FFFD}&quot; bar", Filters::escapeHtmlAttr("foo \xE3\x80\x22 bar")); // stripped UTF

// JS
Assert::same('hello &#123; worlds }', Filters::escapeHtmlAttr('hello { worlds }'));
3 changes: 3 additions & 0 deletions tests/Latte/Filters.escapeHtmlAttrConv().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ Assert::same('`hello&apos;', Filters::escapeHtmlAttrConv("`hello'"));
// invalid UTF-8
Assert::same("foo \u{FFFD} bar", Filters::escapeHtmlAttrConv("foo \u{D800} bar")); // invalid codepoint high surrogates
Assert::same("foo \u{FFFD}&quot; bar", Filters::escapeHtmlAttrConv("foo \xE3\x80\x22 bar")); // stripped UTF

// JS
Assert::same('hello &#123; worlds }', Filters::escapeHtmlAttrConv('hello { worlds }'));
3 changes: 3 additions & 0 deletions tests/Latte/Filters.escapeHtmlAttrUnquoted().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ Assert::same('"`hello "', Filters::escapeHtmlAttrUnquoted('`hello'));
// invalid UTF-8
Assert::same("\"foo \u{FFFD} bar\"", Filters::escapeHtmlAttrUnquoted("foo \u{D800} bar")); // invalid codepoint high surrogates
Assert::same("\"foo \u{FFFD}&quot; bar\"", Filters::escapeHtmlAttrUnquoted("foo \xE3\x80\x22 bar")); // stripped UTF

// JS
Assert::same('"hello &#123; worlds }"', Filters::escapeHtmlAttrUnquoted('hello { worlds }'));

0 comments on commit 1adde0c

Please sign in to comment.