Skip to content

Commit

Permalink
Latte: security fix for escaping inside <script>, prevents "Script da…
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 5, 2013
1 parent 4d40622 commit 4a1e82d
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Nette/Templating/Helpers.php
Expand Up @@ -118,7 +118,7 @@ public static function escapeCss($s)




/** /**
* Escapes string for use inside JavaScript template. * Escapes variables for use inside <script>.
* @param mixed UTF-8 encoding * @param mixed UTF-8 encoding
* @return string * @return string
*/ */
Expand All @@ -127,7 +127,7 @@ public static function escapeJs($s)
if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) { if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
$s = $s->__toString(TRUE); $s = $s->__toString(TRUE);
} }
return str_replace(']]>', ']]\x3E', Nette\Utils\Json::encode($s)); return str_replace(array(']]>', '<!'), array(']]\x3E', '\x3C!'), Nette\Utils\Json::encode($s));
} }




Expand Down
8 changes: 4 additions & 4 deletions tests/Nette/Latte/expected/macros.general.html.html
Expand Up @@ -45,7 +45,7 @@
<li id="item-3" class="odd">Paul</li> <li id="item-3" class="odd">Paul</li>




<li id="item-4" class="even">]]&gt;</li> <li id="item-4" class="even">]]&gt; &lt;!--</li>
</ul> </ul>




Expand All @@ -64,7 +64,7 @@
<!-- <!--
alert('</div>'); alert('</div>');


var prop = ["John","Mary","Paul","]]\x3E"]; var prop = ["John","Mary","Paul","]]\x3E \x3C!--"];


document.getElementById("some&<>\"'\/chars").style.backgroundColor = 'red'; document.getElementById("some&<>\"'\/chars").style.backgroundColor = 'red';


Expand All @@ -76,7 +76,7 @@
<script> <script>
/* <![CDATA[ */ /* <![CDATA[ */


var prop2 = ["John","Mary","Paul","]]\x3E"]; var prop2 = ["John","Mary","Paul","]]\x3E \x3C!--"];


/* ]]> */ /* ]]> */
</script> </script>
Expand Down Expand Up @@ -123,7 +123,7 @@
<li>John</li> <li>John</li>
<li>Mary</li> <li>Mary</li>
<li>Paul</li> <li>Paul</li>
<li>]]&gt;</li> <li>]]&gt; &lt;!--</li>
</ul> </ul>


<ul title="for"> <ul title="for">
Expand Down
8 changes: 4 additions & 4 deletions tests/Nette/Latte/expected/macros.general.xhtml.html
Expand Up @@ -45,7 +45,7 @@
<li id="item-3" class="odd">Paul</li> <li id="item-3" class="odd">Paul</li>




<li id="item-4" class="even">]]&gt;</li> <li id="item-4" class="even">]]&gt; &lt;!--</li>
</ul> </ul>




Expand All @@ -64,7 +64,7 @@
<!-- <!--
alert('</div>'); alert('</div>');


var prop = ["John","Mary","Paul","]]\x3E"]; var prop = ["John","Mary","Paul","]]\x3E \x3C!--"];


document.getElementById("some&<>\"'\/chars").style.backgroundColor = 'red'; document.getElementById("some&<>\"'\/chars").style.backgroundColor = 'red';


Expand All @@ -76,7 +76,7 @@
<script> <script>
/* <![CDATA[ */ /* <![CDATA[ */


var prop2 = ["John","Mary","Paul","]]\x3E"]; var prop2 = ["John","Mary","Paul","]]\x3E \x3C!--"];


/* ]]> */ /* ]]> */
</script> </script>
Expand Down Expand Up @@ -123,7 +123,7 @@
<li>John</li> <li>John</li>
<li>Mary</li> <li>Mary</li>
<li>Paul</li> <li>Paul</li>
<li>]]&gt;</li> <li>]]&gt; &lt;!--</li>
</ul> </ul>


<ul title="for"> <ul title="for">
Expand Down
2 changes: 1 addition & 1 deletion tests/Nette/Latte/expected/macros.xml.html
Expand Up @@ -47,7 +47,7 @@
<li>John</li> <li>John</li>
<li>Mary</li> <li>Mary</li>
<li>Paul</li> <li>Paul</li>
<li>]]&gt;</li> <li>]]&gt; &lt;!--</li>
</ul> </ul>


<p> <p>
Expand Down
2 changes: 1 addition & 1 deletion tests/Nette/Latte/macros.general.html.phpt
Expand Up @@ -28,7 +28,7 @@ $template->registerHelperLoader('Nette\Templating\Helpers::loader');


$template->hello = '<i>Hello</i>'; $template->hello = '<i>Hello</i>';
$template->xss = 'some&<>"\'/chars'; $template->xss = 'some&<>"\'/chars';
$template->people = array('John', 'Mary', 'Paul', ']]>'); $template->people = array('John', 'Mary', 'Paul', ']]> <!--');
$template->menu = array('about', array('product1', 'product2'), 'contact'); $template->menu = array('about', array('product1', 'product2'), 'contact');
$template->el = Html::el('div')->title('1/2"'); $template->el = Html::el('div')->title('1/2"');


Expand Down
2 changes: 1 addition & 1 deletion tests/Nette/Latte/macros.general.xhtml.phpt
Expand Up @@ -26,7 +26,7 @@ $template->registerHelperLoader('Nette\Templating\Helpers::loader');


$template->hello = '<i>Hello</i>'; $template->hello = '<i>Hello</i>';
$template->xss = 'some&<>"\'/chars'; $template->xss = 'some&<>"\'/chars';
$template->people = array('John', 'Mary', 'Paul', ']]>'); $template->people = array('John', 'Mary', 'Paul', ']]> <!--');
$template->menu = array('about', array('product1', 'product2'), 'contact'); $template->menu = array('about', array('product1', 'product2'), 'contact');
$template->el = Html::el('div')->title('1/2"'); $template->el = Html::el('div')->title('1/2"');


Expand Down
2 changes: 1 addition & 1 deletion tests/Nette/Latte/macros.xml.phpt
Expand Up @@ -27,7 +27,7 @@ $template->registerHelperLoader('Nette\Templating\Helpers::loader');


$template->hello = '<i>Hello</i>'; $template->hello = '<i>Hello</i>';
$template->id = ':/item'; $template->id = ':/item';
$template->people = array('John', 'Mary', 'Paul', ']]>'); $template->people = array('John', 'Mary', 'Paul', ']]> <!--');
$template->comment = 'test -- comment'; $template->comment = 'test -- comment';
$template->netteHttpResponse = new Nette\Http\Response; $template->netteHttpResponse = new Nette\Http\Response;
$template->el = Html::el('div')->title('1/2"'); $template->el = Html::el('div')->title('1/2"');
Expand Down

0 comments on commit 4a1e82d

Please sign in to comment.