永远不要相信用户提交的数据!
安全开发指导
参考ThinkPHP官方安全指导
过滤用户输入的内容,DolphinPHP从1.0.5版本开始提供了html安全过滤方法
htmlpurifier()
比如:$html = htmlpurifier(request()->post("content"))
或者:$html = request()->post("content", "", "htmlpurifier")
也可以加在TP的默认过滤规则,如何设置默认过滤规则请参考ThinkPHP官方文档前端过滤可以使用
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
以上方法仅作为安全辅助,没有一劳永逸的方法可以防止所有攻击。要做好数据检查,选用合适的过滤方法。
禁止访问敏感目录
由于框架将入口文件从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>