Codeigniter 中的安全性
下午好,
我对CodeIgniter的安全性有一些疑问,首先是:
我有一个控制器:news.php,其中我有一个名为view的方法
示例:
class News extends CI_Controller{
public function view( $id )
{
$this->load->model('news_model');
$this->news_model->get_by_id( $id );
// ...
}
}
这种工作形式安全吗?没有通过 URL 进行 SQL 注入的风险吗?考虑到此页面被访问,因此 mywebpage/news/number_id。通过intval()过滤会很有趣还是没有必要?
我的第二个问题是:
默认情况下CodeIgniter xss过滤器可以发布和获取,但不知道CodeIgniter过滤HTML的方法,我在CodeIgniter中创建了一个助手,有一些类似于本机CodeIgniter的方法吗?
function remove_xss_html($string){
if( is_array( $string ) ){
$return_array = array();
foreach( $string as $item )
{
if(!get_magic_quotes_gpc())
{
$return_array[] = addslashes( htmlspecialchars( strip_tags( $item ) ) );
}
else
{
$return_array[] = htmlspecialchars( strip_tags( $item ) );
}
}
return $return_array;
}
else
{
return htmlspecialchars( strip_tags( $string ) );
}
}
第三个也是最后一个问题是:
如果我发送一个变量 $ this->输入-> post('my_var') 直接到数据库而不经过过滤器,我会面临sql注入的风险吗? CodeIgniter或过滤器那么安全吗?
重要提示:我的英语不是很好,我使用谷歌翻译并修复了我能做的。
谢谢大家...
Good afternoon,
I'm having some doubts about the safety in CodeIgniter, the first is:
I have a controller: news.php, and in it I have a method called view
Example:
class News extends CI_Controller{
public function view( $id )
{
$this->load->model('news_model');
$this->news_model->get_by_id( $id );
// ...
}
}
This form of work is safe? no risk of SQL injection by URL? taking into consideration that this page is accessed so mywebpage / news / number_id. It would be interesting to filter through intval () or unnecessary?
My second question is:
By default CodeIgniter xss filter can post and get, but unknown a way to filter HTML by CodeIgniter, I created a helper down in CodeIgniter, there is some way similar to that in native CodeIgniter?
function remove_xss_html($string){
if( is_array( $string ) ){
$return_array = array();
foreach( $string as $item )
{
if(!get_magic_quotes_gpc())
{
$return_array[] = addslashes( htmlspecialchars( strip_tags( $item ) ) );
}
else
{
$return_array[] = htmlspecialchars( strip_tags( $item ) );
}
}
return $return_array;
}
else
{
return htmlspecialchars( strip_tags( $string ) );
}
}
and the third and last question is:
If I send a variable $ this-> input-> post ('my_var') directly to the database without the filter, I run the risk of a sql injection? CodeIgniter or filters so safely?
IMPORTANTE: My English is not very good, I used google translate and fix what I could.
Thank you all ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 Active Record 类进行数据库交互,数据将自动转义:
如果不是,并且您正在手动运行查询,则需要自己转义它。
关于您的函数的一些建议:
如果 URL 中不存在
$id
,您将收到错误通知。设置默认值:然后检查控制器中的值。示例:
此外,请确保在继续之前获得结果(如果未找到记录,我假设您的模型在此处返回
false
):您可以验证
$id
的类型或完整性如您所愿,但为了简单起见,我将其传递给模型,如果没有找到记录,则返回 false。If you're using the Active Record class for DB interaction the data will be escaped automatically:
If not and you are manually running queries, you'll need to escape it yourself.
Some advice on your function:
If
$id
is not present in the URL, you will get error notices. Set a default value:Then check the value in your controller. Example:
Also, make sure you get a result before continuing (I assume your model returns
false
here if no record is found):You can validate the
$id
s type or integrity as much as you want, but for simplicity I would just pass it over to the model and returnfalse
if no record was found.即使您没有运行活动记录,也会提供自动转义。您只需要像这样查询数据库:
Even if you not running active records automatic escaping is provided. You just need to query the db like this: