2014-10-08 82 views
7

我正在研究博客評論包,並且希望讓用戶使用Markdown發佈一些代碼。
我與Symfony2中,小枝和KNPMarkdownBundle工作分析使用TWIG Markdown退出HTML代碼

{{ post.content|markdown }} 

其實,內容是解析以及降價(<code><p> ...),但如果我有一些HTML代碼在我的內容,如:

Some content 
``` <script>alert("hello world");</script> ``` 

該代碼未被轉義,並且我有一條警告消息。 請有人解釋我該如何處理XSS問題? (foo|rawfoo|escape正在打破解析)

+0

所以要確認具體盟友,'{{post.content | escape('html')| markdown}}'不起作用?編輯:啊,你想讓某些HTML通過 – sjagr 2014-10-08 20:25:09

+0

沒有,因爲它轉義的'&'和我的輸出是'<腳本>警報(「」)< /腳本>' – 2014-10-08 20:28:06

回答

4

我正好有這個問題,但由於strip_tags是不夠的,保護的屬性變量值,我將提交我的答案。

我使用HTML Purifier刪除所有不需要的HTML元素和屬性。打開命令控制檯並執行以下命令進行安裝。

$ composer require ezyang/htmlpurifier "^4.6" 

然後你就可以創建自己的枝條延伸:

namespace AcmeBundle\Twig; 

class HTMLPurifierExtension extends \Twig_Extension 
{ 
    public function getFilters() 
    { 
     return array(
      new \Twig_SimpleFilter('html_purifier', array($this, 'purify'), array('is_safe' => array('html'))), 
     ); 
    } 

    public function purify($text) 
    { 
     $elements = array(
      'p', 
      'br', 
      'small', 
      'strong', 'b', 
      'em', 'i', 
      'strike', 
      'sub', 'sup', 
      'ins', 'del', 
      'ol', 'ul', 'li', 
      'h1', 'h2', 'h3', 
      'dl', 'dd', 'dt', 
      'pre', 'code', 'samp', 'kbd', 
      'q', 'blockquote', 'abbr', 'cite', 
      'table', 'thead', 'tbody', 'th', 'tr', 'td', 
      'a[href|target|rel|id]', 
      'img[src|title|alt|width|height|style]' 
     ); 

     $config = \HTMLPurifier_Config::createDefault(); 
     $config->set('HTML.Allowed', implode(',', $elements)); 

     $purifier = new \HTMLPurifier($config); 
     return $purifier->purify($text); 
    } 

    public function getName() 
    { 
     return 'html_purifier'; 
    } 
} 

打開services.yml和註冊擴展爲一個服務:

services: 
    acme.html_purifier_extension: 
     class: AcmeBundle\Twig\HTMLPurifierExtension 
     public: false 
     tags: 
      - { name: twig.extension } 

現在你可以用

{{ post.content|markdown|html_purifier }} 
使用
2

你可以用枝條的striptags filter就像你PHP's strip_tags function通過允許特定的HTML標籤:

{{ post.content|striptags('<code><p><br>')|markdown }} 

這將消除不必要的標籤完全,而不是分析它們來&lt;&gt;以及。

除此之外,您可能想要write your own Twig filter以防萬一您想每次輕鬆引用同一組「允許的標籤」。

+0

我想保持標籤,就像你SO可以發佈任何html內容,不管它是什麼 – 2014-10-08 20:32:13

+0

輸出:'code alert(1)'我想輸出的是'' – 2014-10-08 20:34:23

+0

@FabienPapet然後,你將不得不延長Twig並運行你自己的自定義逃生功能..沒有太多方法。告訴你如何做到這一點,這個問題太廣泛了,不能在幾段中簡潔地回答。你也可以在GitHub項目上打開一個問題,或者自己貢獻這個包來解決這個功能問題。 [看起來有一些已經以另一種形式完成了](https://github.com/KnpLabs/KnpMarkdownBundle/issues/63) – sjagr 2014-10-08 20:35:00

5

正如@sjagr暗示,你最好寫自己的樹枝延伸。曾幾何時,我遇到過類似的問題,並且只是寫了一個簡單的擴展來對它進行排序,這非常簡單和容易。

新嫩枝TAG:

{{ post.content|yourNewTag }} 

新嫩枝擴展類:

namespace Car\BrandBundle\Twig; 

    class YourNewTagExtension extends \Twig_Extension 
    { 
     public function yourNewTagFilter($param) 
     { 
      // Escape your content as you wish with some logic and return it 
      return $escaped; 
     } 

     public function getFilters() 
     { 
      return array(new \Twig_SimpleFilter('yourNewTag', array($this, 'yourNewTagFilter'))); 
     } 

     public function getName() 
     { 
      return 'yourNewTag_extension'; 
     } 
    } 

CONFIG:

services: 
    car.twig.yourNewTag_extension: 
     class: Car\BrandBundle\Twig\YourNewTagExtension 
     tags: 
      - { name: twig.extension } 

編輯:

嫩枝:

如果你不想逃避特定的標記,然後用allowable_tags標誌和用strip_tags()函數。

public function yourNewTagFilter($param) 
{ 
    $escaped = strip_tags($param); 
    // Do something else as well if you want 

    return $escaped; 
    // This will print alert("hello world"); as output in your webpage 
} 
+0

當我輸入該函數時,內容是否被轉義?如果是,我必須在之前指定'| raw'嗎? – 2014-10-12 12:14:52

+0

是的,你的邏輯在這個功能會爲你做所有事情。您不必使用任何其他內置樹枝擴展,因爲您實際上正在使用此示例創建一個。 – BentCoder 2014-10-12 12:55:31

+0

結帳**編輯**部分以獲取更多信息。 – BentCoder 2014-10-12 13:09:09