一、CKFinder的若干問題
1.單獨使用
ckfinder從原fckeditor分離出來以后可以單獨使用,通常我習慣於在工具欄中添加ckfinder.dll,這樣以后要使用ckfinder直接從工具箱拖出來即可.
拖到頁面中后,會形成這樣一個控件實例:
<CKFinder:FileBrowser ID="FileBrowser1" runat="server"></CKFinder:FileBrowser>
2.上傳文件自動重命名
修改ckfinder的源文件,找到Connector\CommandHandlers\FileUploadCommandHandler.cs這個文件,定位到:
string sExtension = System.IO.Path.GetExtension( oFile.FileName );
sExtension = sExtension.TrimStart( '.' );
在下面加一行代碼:
sFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + sExtension;
即強制把文件名改為時間格式字符串.
3.上傳安全問題
3.1 跟fckeditor類似,默認情況下ckfinder是不允許上傳的,找到config.ascx這個文件,定位到
public override bool CheckAuthentication()
{
return false;
}
在這里加入自己需要的判斷邏輯,千萬不要直接改成return true;這樣相當於免費把自己的服務器變成一個網絡硬盤+肉雞,任何人都可以直接上傳任何文件(包括木馬),起碼也得類似下面這樣:
3.2 文件擴展名校驗
默認情況下,ckfinder幾乎能上傳任何文件,所以設置允許上傳的文件擴展名是必需的,ckfinder采用了黑白名單的做法,即同時可以設置"允許上傳的擴展名"及"禁止上傳的擴展名",config.ascx中可參考下面這樣設置:
ResourceType type;
type = ResourceType.Add("Zip");
...
type.AllowedExtensions = new string[] { "zip" };
type.DeniedExtensions = new string[] {"asp","aspx","jsp","php","ashx","js","html","htm" };
...
這一段設置相當於只允許.zip文件上傳,同時禁止.asp,.aspx...之類的服務端文件上傳
3.3 MIME類型/ContentType校驗
光有擴展名校驗是遠遠不夠的,比如在asp時代就有一種經典的攻擊方式:
a.先把asp木馬文件擴展名改成.jpeg之類(這樣就能繞過擴展名檢驗)
b.然后利用其它發包工具(或直接用ckfinder的上傳功能),上傳"偽jpeg"文件
c.如果網站還支持html代碼的留言(或產品編碼,個人簡介編輯等),寫上這樣一行代碼
<!--inlude file = "xxx.jpeg"-->
這里xxx.jpeg即上傳后的"偽jpeg"木馬,如果服務端允許包含文件的話,瀏覽包含這行代碼的頁面,木馬就能運行了!
為了防止這類攻擊,必須要在服務端做MIME/ContentType校驗,因為文件的擴展名不管改成什么,其內在的MIME/ContentType是不會變的,修改方法:
定位到Settings\ResourceType.cs,找到
public string[] AllowedExtensions;
public string[] DeniedExtensions;
再增加二個數組
public string[] AllowedMIMETypes;
public string[] DeniedMIMETypes;
相應的構造函數也加初始化代碼:
AllowedMIMETypes = new string[0];
DeniedMIMETypes = new string[0];
然后再增加一個方法:
public bool CheckMIMEType(string mimeType)
{
mimeType = mimeType.Trim().ToLower();
if (DeniedMIMETypes.Length > 0)
{
if (Array.IndexOf(this.DeniedMIMETypes, mimeType) >= 0)
{
return false;
}
}
if (AllowedMIMETypes.Length > 0)
{
return (Array.IndexOf(this.AllowedMIMETypes, mimeType) >= 0);
}
else
{
return true;
}
}
然后定位到Connector\CommandHandlers\FileUploadCommandHandler.cs,找到:
if (!this.CurrentFolder.ResourceTypeInfo.CheckExtension(sExtension))
{
ConnectorException.Throw(Errors.InvalidExtension);
}
然后加上:
string sFileMIME = oFile.ContentType;
if (!this.CurrentFolder.ResourceTypeInfo.CheckMIMEType(sFileMIME))//檢測上傳文件的MIME類型
{
ConnectorException.Throw(Errors.InvalidMIMEType);
}
最后再修改config.ascx,加上MIME類型的黑白名單:
ResourceType type;
type = ResourceType.Add("Zip");
...
type.AllowedExtensions = new string[] { "zip" };
type.DeniedExtensions = new string[] {"asp","aspx","jsp","php","ashx","js","html","htm" };
type.AllowedMIMETypes = new string[] { "application/x-zip-compressed" };
type.DeniedMIMETypes = new string[] {"text/plain" };
這樣就相對就安全一些了(當然服務器端還可以進一步做安全處理,不過這個話題再展開就變成"服務器安全設置"專題了,不在本文的討論范圍,暫不深入)
4.上傳文件大小限制
默認情況下ResourceType的構造函數里,MaxSize=0即不對上傳文件大小做限制,所以只要在config.ascx里加上限制就行了
type = ResourceType.Add("Zip");
...
type.MaxSize = 0;
即把這里的MaxSize改成想要的值即可(以字節為單位計算),注意:ResourceType雖然有MaxSize成員,但其實上傳代碼中並未對上傳文件大小做判斷,而是在上傳完成后生成縮略圖時,才做了一次判斷,如果需要在上傳文件SaveAs以前就做判斷處理,自行加一條if語句,比較oFile.ContentLength與MaxSize即可
5.上傳后縮略圖無法正常顯示
這是ckFinder在windows系統中的一個小bug,定位到Settings\Thumbnails.cs,找到public string GetTargetDirectory()方法,改成下面這樣:
if (Dir.Length == 0 || Dir.Substring(0,1)!="/") //如果Dir為空,或者只是相對路徑
{
return HttpContext.Current.Server.MapPath(Url);
}
else
{
if (Dir.IndexOf(":\\") == -1)//如果不是物理路徑
{
return HttpContext.Current.Server.MapPath(Dir);
}
else
{
return Dir;
}
}
6.動態指定上傳路徑
默認情況下無法用cs代碼修改config.ascx中的BaseUrl設置,因為其后端代碼ConfigFile中並沒有提供修改BaseUrl的方法,這里我借用了fckeditor以前的用法:利用session來動態處理
public string DynamicBaseUrl
{
get
{
object _baseUrl = HttpContext.Current.Session["CKFinder:DynamicBaseUrl"];
if (_baseUrl == null || string.IsNullOrEmpty(_baseUrl.ToString()))
{
_baseUrl = "/ckfinder/userfiles/";
}
this.BaseUrl = _baseUrl.ToString();
return this.BaseUrl;
}
}
如上,在Settings\ConfigFile.cs中增加一個屬性,讓其從session中取值,然后再把config.ascx中的BaseUrl改成下面這樣
//BaseUrl = "/ckfinder/userfiles/";
BaseUrl = DynamicBaseUrl;
最后在嵌入ckFinder的頁面中類似這樣處理:
protected void Page_Load(object sender, EventArgs e)
{
Session["CKFinder:DynamicBaseUrl"] = "/upload/";
}
7.CKfinder免費版本如何去掉“那啥”的提示
打開core\js\ckfinder_ie.js,找到 {en.call(window,qo);},改成{/*en.call(window,qo);*/}即可
二、與CKeditor的整合
1.CKeditor的設置
window.onload = function () {
CKEDITOR.replace("editor1", {
filebrowserBrowseUrl: '/ckfinder/ckfinder.html', //啟用瀏覽功能
filebrowserImageBrowseUrl: '/ckfinder/ckfinder.html?Type=Image',
filebrowserFlashBrowseUrl: '/ckfinder/ckfinder.html?Type=Flash',
filebrowserUploadUrl: '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Zip',
filebrowserImageUploadUrl: '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Image',
filebrowserFlashUploadUrl: '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash'
});
}
這樣就可以了,需要說明的"ckfinder.html?Type=Image"上的Type=XXX,即對應CKFinder中Config.ascx的ResourceType設置,而且ResourceType的名稱不能用中文名,否則在快速上傳時無法上傳到服務端。(很多地方是在html中以js方式接收參數的,改成中文后會導致亂碼,從而無法正確定位目錄,熟悉js的朋友如果想讓其支持中文Type名,技術上講應該是可以修改實現的)