.NET 輕鬆實現HTML的絕對過濾之SafeHelper

來源:互聯網
上載者:User

標籤:des   style   blog   class   code   c   

     當今網頁中經常使用到網頁編輯器,因為人們需要在網頁中插入圖片,視頻,樣式等html代碼內容,這使得網頁的資訊更加豐富。隨之而來的,也給程式開發人員帶來了不少麻煩,因為提交的html中難免會出現不安全標記和非法標記,比如script,比如未知標籤。這需要我們編寫大量的程式碼去分析指定使用者提交的html資訊安全性,標準性。

    方法1:

    今天我要給大家推薦一個組件,他可以智能的分析出代碼的出錯部份和清除出錯部份,並且配置比較簡單。他的名字叫SafeHelper,通過設定檔設定的標記外,他將清楚和檢查出不允許出現的標記。使用方法相當簡單,只需要調用一個靜態方法即可。

    第一步,建立一個檔案名稱為“wuxiu.HtmlAnalyserConfig.xml”的xml檔案到網站跟目錄,並添寫以下代碼:

<?xml version="1.0" encoding="utf-8" ?><HtmlAnylyser >  <AllowTags>    <div attrs="class|style"/>    <ul attrs="class"/>    <li/>    <table attrs="class|cellpadding|cellspacing|border|width"/>    <tr attrs="class"/>    <th attrs="class"/>    <td attrs="class"/>    <span attrs="style|class"/>    <object attrs="classid|codebase|width|height"/>    <param attrs="name|value"/>    <embed attrs="src|width|height|quality|pluginspage|type|wmode"/>    <a attrs="href|target|title"/>    <h1 attrs="class"/>    <h2 attrs="class"/>    <h3 attrs="class"/>    <h4 attrs="class"/>    <h5 attrs="class"/>    <h6 attrs="class"/>    <strong attrs="class"/>    <b attrs="class"/>    <i attrs="class"/>    <em attrs="class"/>    <u attrs="class"/>    <hr attrs="class"/>    <br attrs="class"/>    <img attrs="class|src|width|height|alt"/>    <p attrs="class"/>    <ol attrs="class"/>    <dl attrs="class"/>    <dt attrs="class"/>    <dd attrs="class"/>  </AllowTags></HtmlAnylyser>

第二步,添加dll引用,safehelper官網:http://www.wuxiu.org/downloads.html

第三步,調用如下代碼可以實現對html中未知標記清除(wuxiu.HtmlAnalyserConfig.xml中未定義的所有標記):

string html = "<script>alert(‘yes‘);</script><p>content</p>";html = wuxiu.SafeHelper.HtmlSafer.HtmlSaferAnalyser.ToSafeHtml(html);Response.Write(html);

或檢查所有未知標記

string html = "<script>alert(‘yes‘);</script><p>myhtmlcontent</p>";string [] dangers = wuxiu.SafeHelper.HtmlSafer.HtmlSaferAnalyser.ValidHtml(html,false);foreach (string danger_tag in dangers){    Response.Write(danger_tag+"<br/>");}

方法二,通過Regex匹配出script危險標記:

public static string StripHTML(string strHtml){    string[]aryReg =    {      @"<script[^>]*?>.*?</script>",      @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""‘])(\\["        "‘tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", @"([\r\n])[\s]+", @        "&(quot|#34);", @"&(amp|#38);", @"&(lt|#60);", @"&(gt|#62);", @        "&(nbsp|#160);", @"&(iexcl|#161);", @"&(cent|#162);", @"&(pound|#163);",        @"&(copy|#169);", @"&#(\d+);", @"-->", @"<!--.*\n"    };    string[]aryRep =    {      "", "", "", "\"", "&", "<", ">", "   ", "\xa1", //chr(161),      "\xa2", //chr(162),      "\xa3", //chr(163),      "\xa9", //chr(169),      "", "\r\n", ""    };    string newReg = aryReg[0];    string strOutput = strHtml;    for (int i = 0; i < aryReg.Length; i++)    {      Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);      strOutput = regex.Replace(strOutput, aryRep[i]);    }    strOutput.Replace("<", "");    strOutput.Replace(">", "");    strOutput.Replace("\r\n", "");    return strOutput;}

 

更多0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.