<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>哇寶部落格 &#187; xss</title>
	<atom:link href="http://blog.wabow.com/archives/tag/xss/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.wabow.com</link>
	<description>Wabow Information Inc. Blog</description>
	<lastBuildDate>Fri, 20 Jan 2012 09:19:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>jQuery FCKEditor + HTML Purifier</title>
		<link>http://blog.wabow.com/archives/1502</link>
		<comments>http://blog.wabow.com/archives/1502#comments</comments>
		<pubDate>Tue, 23 Jun 2009 10:47:49 +0000</pubDate>
		<dc:creator>suzy</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[技術分享]]></category>
		<category><![CDATA[HTML Filtering]]></category>
		<category><![CDATA[standards compliant]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://blog.wabow.com/?p=1502</guid>
		<description><![CDATA[今天要介紹的是 wysiwyg + filter = wysiwyfter(?) 得知客戶需要一套 輕量型的 所見即所得(wysiwyg)編輯器. 要求是可以快速地載入編輯器, 進行 HTML 編輯, 但也需要嚴格防止駭客輸入不當的程式碼. 因此我們挑選了 jQuery FCKEditor (註1) 以及 HTML Purifier. HTML Purifier 是用來濾掉可疑的 XSS 程式碼, 且內容仍符合 W3C 的標準 (Standards Compliant). 註1: 曾試著裝 jWYSIWYG, 但發現許多小問題, 所以還是用回 FCKEditor 安裝與設定 1. 下載 jQuery FCKEditor 以及 HTML Purifier 2. 安裝 jQuery FCKEditor 1 2 3 &#60;!-- head --&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>今天要介紹的是 wysiwyg + filter = wysiwyfter(?)</p>
<p>得知客戶需要一套 輕量型的 所見即所得(wysiwyg)編輯器. 要求是可以快速地載入編輯器, 進行 HTML 編輯, 但也需要嚴格防止駭客輸入不當的程式碼.</p>
<p>因此我們挑選了 <a href="http://www.fyneworks.com/jquery/FCKEditor/">jQuery FCKEditor</a> (註1) 以及 <a href="http://htmlpurifier.org/">HTML Purifier</a>. </p>
<p>HTML Purifier 是用來濾掉可疑的 XSS 程式碼, 且內容仍符合 W3C 的標準 (Standards Compliant). </p>
<p><span id="more-1502"></span><br />
註1: 曾試著裝 jWYSIWYG, 但發現許多小問題, 所以還是用回 FCKEditor</p>
<h2>安裝與設定</h2>
<p>1. 下載 jQuery FCKEditor 以及 HTML Purifier<br />
2. 安裝 jQuery FCKEditor</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- head --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fck.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- body --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myEditor&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;content&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;</span>  
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;送出&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
$(function () {
    $('#myEditor').fck({
        path: 'http://localhost/test/fckeditor/', // fckeditor 資料夾的位置
        config: {
            ToolbarSet: 'MyToolbarSet', // 在 fckconfig.js 中自定 ToolbarSet
            EditorAreaCSS: 'layout.css' // css 設定檔
        },
        width:  650,
        height: 200
    });
});
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>3. 將下列設定<strong>新增</strong>在 /fckeditor/fckconfig.js 最下方(編輯器會顯示:粗體, 斜體, 底線, 連結, 及 文字大小)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;">FCKConfig.ToolbarSets[&quot;MyToolbarSet&quot;] = [
	['Bold','Italic','Underline','Link','Unlink','FontSize']
] ;</pre></td></tr></table></div>

<p>4. 其他 fckconfig.js 的設定</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;">FCKConfig.DefaultFontSizeLabel = '小' ;
FCKConfig.DefaultLinkTarget = '_blank' ;
&nbsp;
FCKConfig.FontSizes		= '12/小;15/中;18/大' ;
&nbsp;
FCKConfig.LinkDlgHideTarget		= true ;
FCKConfig.LinkDlgHideAdvanced	= true ;
&nbsp;
FCKConfig.LinkBrowser		= false ;
FCKConfig.LinkUpload		= false ;</pre></td></tr></table></div>

<p>樣式如圖:<br />
<a href="http://blog.wabow.com/wp-content/uploads/2009/06/fck1.jpg"><img src="http://blog.wabow.com/wp-content/uploads/2009/06/fck1.jpg" alt="fck1" class="alignnone size-full wp-image-1580" /></a></p>
<p>5. 在連結的區域, 我們也不需要多餘的 "超連結類型" 跟 "通訊協定". </p>
<p><a href="http://blog.wabow.com/wp-content/uploads/2009/06/fck2.jpg"><img src="http://blog.wabow.com/wp-content/uploads/2009/06/fck2.jpg" alt="fck2" class="alignnone size-full wp-image-1582" /></a></p>
<p>修改檔案: /fckeditor/editor/dialog/fck_link.html</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;">@ Line 37-40
<span style="color: #808080; font-style: italic;">&lt;!-- 註解掉</span>
<span style="color: #808080; font-style: italic;">&lt;option value=&quot;anchor&quot; fckLang=&quot;DlgLnkTypeAnchor&quot;&gt;Anchor in this page&lt;/option&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;option value=&quot;email&quot; fckLang=&quot;DlgLnkTypeEMail&quot;&gt;E-Mail&lt;/option&gt;</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
&nbsp;
@ Line 51-56
<span style="color: #808080; font-style: italic;">&lt;!-- 註解掉</span>
<span style="color: #808080; font-style: italic;">&lt;option value=&quot;https://&quot;&gt;https://&lt;/option&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;option value=&quot;ftp://&quot;&gt;ftp://&lt;/option&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;option value=&quot;news://&quot;&gt;news://&lt;/option&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;option value=&quot;&quot; fckLang=&quot;DlgLnkProtoOther&quot;&gt;&lt;other&gt;&lt;/option&gt;</span>
<span style="color: #808080; font-style: italic;">--&gt;</span></pre></td></tr></table></div>

<p>到這裡 FCKEditor 的部份就算完成了. <img src='http://blog.wabow.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>如果有需要更動到文字敘述可以修改語言檔: /fckeditor/editor/lang/zh.js</p>
<p>6. 再來就是安裝 HTML Purifier. 解壓縮後找到一個叫 library 的資料夾, 把它 copy 出來, 我將它重新命名為 HTMLPurifier</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HTMLPurifier/HTMLPurifier.auto.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 最快的安裝方式:</span>
<span style="color: #000088;">$purifier</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HTMLPurifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cleanContent</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$purifier</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">purify</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// -------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">// 如果要更改預設值, 可以照以下步驟設定:</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> HTMLPurifier_Config<span style="color: #339933;">::</span><span style="color: #004000;">createDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 設定編碼, 預設是 UTF-8</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Core'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Encoding'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ISO-8859-1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 設定 Doctype, 預設是 XHTML Transitional</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HTML'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Doctype'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'HTML 4.01 Transitional'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;!--</span>
    <span style="color: #339933;">*</span> HTML <span style="color:#800080;">4.01</span> Strict
    <span style="color: #339933;">*</span> HTML <span style="color:#800080;">4.01</span> Transitional
    <span style="color: #339933;">*</span> XHTML <span style="color:#800080;">1.0</span> Strict
    <span style="color: #339933;">*</span> XHTML <span style="color:#800080;">1.0</span> Transitional
    <span style="color: #339933;">*</span> XHTML <span style="color:#800080;">1.1</span>
<span style="color: #339933;">--&gt;</span>
<span style="color: #666666; font-style: italic;">// 選擇允許的 HTML Tags, 除了 以下的 Tags[Attributes], 其他的一律都會被過濾掉</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HTML'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Allowed'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'p,br,strong,em,u,span[style],a[href|target]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Attr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AllowedFrameTargets'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_blank, _self'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 將 $config 帶入 object </span>
<span style="color: #000088;">$purifier</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HTMLPurifier<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cleanContent</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$purifier</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">purify</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>到這裡就大功告成啦 <img src='http://blog.wabow.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h2>使用報告</h2>
<p>這裡有 <a href="http://htmlpurifier.org/live/smoketests/xssAttacks.php">HTMLPurifier 的 xssAttack List</a>, 代表它已通過這些xss測試.</p>
<p>HTML Purifier 還有以下功能: </p>
<ul>
<li>缺 closing tag 會補</li>
<li>輸入 &lt;img src=""&gt; 會變成 &lt;img src="" alt="" /&gt;</li>
<li>form, input, script 自動消失 (除非設定 allow)</li>
<li>&lt;p onclick="alert();"&gt;x&lt;/p&gt; 會過濾成 &lt;p&gt;x&lt;/p&gt;</li>
</ul>
<p>其他的大家可以來 <a href="http://www.dodoke.com/testground/wysiwyfter/purifier.php">這裡</a> 跟 <a href="http://www.dodoke.com/testground/wysiwyfter/fckpurifier.php">這裡</a> 試玩..</p>
<p>HTML Purifier 滿強大的, 應該可以再多加運用 <img src='http://blog.wabow.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.wabow.com/archives/1502/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

