1、CKFinder的若干问题php
1.单独使用html
ckfinder从原fckeditor分离出来之后能够单独使用,一般我习惯于在工具栏中添加ckfinder.dll,这样之后要使用ckfinder直接从工具箱拖出来便可.windows
拖到页面中后,会造成这样一个控件实例:后端
<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( '.' );session
在下面加一行代码:app
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);*/}便可
2、与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名,技术上讲应该是能够修改实现的)