<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 上下文編碼(Contextual Escaping) # 上下文編碼(Contextual Escaping) 網站及其它B/S應用極易受到 [XSS](https://www.owasp.org/index.php/XSS) 攻擊,盡管PHP提供了轉義功能,在某些情況下依然不夠安全。在Phalcon中 [*Phalcon\\Escaper*](#) 提供了上下文轉義功能,這個模塊是由C語言實現的,這在進行轉義時可以有更好的性能。 Phalcon的上下文轉義組件基于 [OWASP](https://www.owasp.org) 提供的`XSS (Cross Site Scripting) 預防作弊表`\_ 另外,這個組件依賴于 [mbstring](http://php.net/manual/en/book.mbstring.php) 擴展,以支持幾乎所有的字符集。 下面的例子中展示了這個組件是如何工作的: ``` <pre class="calibre14">``` <?php // 帶有額外的html標簽的惡意的文檔標題 $maliciousTitle = '</title><script>alert(1)</script>'; // 惡意的css類名 $className = ';`('; // 惡意的css字體名 $fontName = 'Verdana"</style>'; // 惡意的Javascript文本 $javascriptText = "';</script>Hello"; // 創建轉義實例對象 $e = new Phalcon\Escaper(); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title><?php echo $e->escapeHtml($maliciousTitle) ?></title> <style type="text/css"> .<?php echo $e->escapeCss($className) ?> { font-family : "<?php echo $e->escapeCss($fontName) ?>"; color: red; } </style> </head> <body> <div class='<?php echo $e->escapeHtmlAttr($className) ?>'>hello</div> <script>var some = '<?php echo $e->escapeJs($javascriptText) ?>'</script> </body> </html> ``` ``` 結果如下: ![](https://box.kancloud.cn/2015-12-30_5683412b9377a.jpeg) Phalcon會根據文本所處的上下文進行轉義。 恰當的上下文環境對防范XSS攻擊來說是非常重要的。 ### HTML 編碼(Escaping HTML) 最不安全的情形即是在html標簽中插入非安全的數據。 ``` <pre class="calibre14">``` <div class="comments"><!-- Escape untrusted data here! --></div> ``` ``` 我們可以使用escapeHtml方法對這些文本進行轉義: ``` <pre class="calibre14">``` <div class="comments"><?php echo $e->escapeHtml('></div><h1>myattack</h1>'); ?></div> ``` ``` 結果如下: ``` <pre class="calibre14">``` <div class="comments">&gt;&lt;/div&gt;&lt;h1&gt;myattack&lt;/h1&gt;</div> ``` ``` ### HTML 屬性編碼(Escaping HTML Attributes) 對html屬性進行轉義和對html內容進行轉義略有不同。對html的屬性進行轉義是通過對所有的非字母和數字轉義來實現的。類例的轉義都會如此進行的,除了一些復雜的屬性外如:href和url: ``` <pre class="calibre14">``` <table width="Escape untrusted data here!"><tr><td>Hello</td></tr></table> ``` ``` 我們這里使用escapeHtmlAttr方法對html屬性進行轉義: ``` <pre class="calibre14">``` <table width="<?php echo $e->escapeHtmlAttr('"><h1>Hello</table'); ?>"><tr><td>Hello</td></tr></table> ``` ``` 結果如下: ``` <pre class="calibre14">``` <table width="&#x22;&#x3e;&#x3c;h1&#x3e;Hello&#x3c;&#x2f;table"><tr><td>Hello</td></tr></table> ``` ``` ### URL 編碼(Escaping URLs) 一些html的屬性如href或url需要使用特定的方法進行轉義: ``` <pre class="calibre14">``` <a href="Escape untrusted data here!">Some link</a> ``` ``` 我們這里使用escapeUrl方法進行url的轉義: ``` <pre class="calibre14">``` <a href="<?php echo $e->escapeUrl('"><script>alert(1)</script><a href="#'); ?>">Some link</a> ``` ``` 結果如下: ``` <pre class="calibre14">``` <a href="%22%3E%3Cscript%3Ealert%281%29%3C%2Fscript%3E%3Ca%20href%3D%22%23">Some link</a> ``` ``` ### CSS 編碼(Escaping CSS) CSS標識/值也可以進行轉義: ``` <pre class="calibre14">``` <a style="color: Escape untrusted data here">Some link</a> ``` ``` 這里我們使用escapeCss方法進行轉義: ``` <pre class="calibre14">``` <a style="color: <?php echo $e->escapeCss('"><script>alert(1)</script><a href="#'); ?>">Some link</a> ``` ``` 結果: ``` <pre class="calibre14">``` <a style="color: \22 \3e \3c script\3e alert\28 1\29 \3c \2f script\3e \3c a\20 href\3d \22 \23 ">Some link</a> ``` ``` ### Javascript 編碼(Escaping Javascript) 插入Javascript代碼的字符串也需要進行適當的轉義: ``` <pre class="calibre14">``` <script>document.title = 'Escape untrusted data here'</script> ``` ``` 這里我們使用escapeJs進行轉義: ``` <pre class="calibre14">``` <script>document.title = '<?php echo $e->escapejs("'; alert(100); var x='"); ?>'</script> ``` ``` ``` <pre class="calibre14">``` <script>document.title = '\x27; alert(100); var x\x3d\x27'</script> ``` ``` | - [索引](# "總目錄") - [下一頁](# "驗證(Validation)") | - [上一頁](# "過濾與清理(Filtering and Sanitizing)") |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看