ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
>[danger]永远不要相信用户提交的数据! ## 安全开发指导 1. 参考ThinkPHP官方[安全指导](https://www.kancloud.cn/manual/thinkphp5/268461) 2. 过滤用户输入的内容,DolphinPHP从1.0.5版本开始提供了html安全过滤方法`htmlpurifier()` 比如:`$html = htmlpurifier(request()->post('content'))` 或者:`$html = request()->post('content', '', 'htmlpurifier')` 也可以加在TP的默认过滤规则,如何设置默认过滤规则请参考ThinkPHP官方文档 3. 前端过滤可以使用`filterXSS()` ~~~ <script> // apply function filterXSS in the same way var html = filterXSS('<script>alert("xss");</scr' + 'ipt>'); alert(html); </script> ~~~ 更多用法,请参考:[https://github.com/leizongmin/js-xss/blob/master/README.zh.md](https://github.com/leizongmin/js-xss/blob/master/README.zh.md) >[info]以上方法仅作为安全辅助,没有一劳永逸的方法可以防止所有攻击。要做好数据检查,选用合适的过滤方法。 ## 禁止访问敏感目录 由于框架将入口文件从public移动到了应用目录,使一些不该被访问的目录也暴露了出来。如果将入口文件移动到public目录,需要改动的地方比较多,所以不建议这样做。 为了安全考虑,可以将一些敏感目录设置为禁止访问。 【Nginx】 在Nginx配置文件中,加入以下规则 ~~~ location ^~ /data { deny all; } location ^~ /runtime { deny all; } location ^~ /export { deny all; } location ^~ /application { deny all; } location ^~ /plugins { deny all; } location ^~ /thinkphp { deny all; } location ^~ /vendor { deny all; } ~~~ 【Apache】 在框架根目录的.htaccess文件加入以下规则 ~~~ RewriteRule ^data - [F,L] RewriteRule ^runtime - [F,L] RewriteRule ^export - [F,L] RewriteRule ^application - [F,L] RewriteRule ^plugins - [F,L] RewriteRule ^thinkphp - [F,L] RewriteRule ^vendor - [F,L] ~~~ 完整内容 ~~~ <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] RewriteRule ^data - [F,L] RewriteRule ^runtime - [F,L] RewriteRule ^export - [F,L] RewriteRule ^application - [F,L] RewriteRule ^plugins - [F,L] RewriteRule ^thinkphp - [F,L] RewriteRule ^vendor - [F,L] </IfModule> ~~~ ## 修复1.1.0之前的一处安全隐患 如果您的框架没有升级到1.1.0以上,请按以下方法修复一处文件上传隐患,主要是由于超级管理可以上传php文件,一般超级管理账号密码没有泄露是不会有问题的,但还是建议大家修复这个问题。 打开`application\admin\controller\Attachment.php` 大概217行,将 ![](https://box.kancloud.cn/a8e96c5a18a8cba6423b18357dace9eb_530x76.png) 修改为 ~~~ if (preg_grep("/php/i", $ext_limit)) { $error_msg = '禁止上传非法文件!'; } if (!preg_grep("/$file_ext/i", $ext_limit)) { $error_msg = '附件类型不正确!'; } ~~~