LoginSignup
2
4

More than 5 years have passed since last update.

Smarty3でnl2brしつつXSSを避ける書き方 の処理について、自己解釈

Posted at

Smarty3でnl2brしつつXSSを避ける書き方をみて、なるほどって思ったので、どういう風に自己解釈したのか書く。

書いたコード

確認で、こんな感じのコードを書いた。

index.php
<?php
$smarty = new Smarty();
$smarty->default_modifier = ['escape'];

$smarty->assign('a', '<h1>title</h1>
this is summary
');

$smarty->display('index.tpl');
{{$a}|nl2br nofilter}
{*
  1. $a with default_modifire
  2. nl2br whithout default_modifier
*}
  1. 内側から処理されるっぽい。
  2. まず、{$a} を処理する。
    • このとき、nofilter はないので、普通にdefault_modifierが適用される。
    • 結果、<h1>とかはエスケープされる。
  3. 次に、nl2brされる。
    • この結果に対しては、nofilter が指定されているので、エスケープされない。
    • 結果、改行コードは<br />に置換される。
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4