简体   繁体   English

Codeigniter-清理输入的最佳实践

[英]Codeigniter - best practice to sanitize input

I would like to know what's the best practice to sanitize user input using Codeigniter. 我想知道使用Codeigniter清理用户输入的最佳实践是什么。

I understands that CI offers form_validation, such as set_rules. 我知道CI提供form_validation,例如set_rules。

'set_rules'=>'trim|xss_clean|alpha_numeric|htmlspecialchars'

"Any native PHP function that accepts one parameter can be used as a rule, like htmlspecialchars, trim, MD5, etc." “任何可以接受一个参数的本地PHP函数都可以用作规则,例如htmlspecialchars,trim,MD5等。”

My question now is, 我的问题是

is this enough to protect us from xss, sql injection attacks etc? 这足以保护我们免受xss,sql注入攻击等影响吗?

what other rules are there that I can apply? 还有哪些其他规则可以应用?

in term of performance, is it costly for us to apply all these rules for all the inputs? 在性能方面,对所有输入应用所有这些规则是否对我们造成成本?

I understand MD5 is a hash funciton, but what happens if you set MD5 as part of the rule? 我了解MD5是哈希函数,但是如果将MD5设置为规则的一部分,会发生什么呢?

above that I've added javascript validation as well. 上面我还添加了JavaScript验证。 Am I on the right track on sanitizing inputs and validating user inputs? 我在清理输入和验证用户输入方面是否走上了正确的轨道? Please advice. 请指教。

Sanitizing is more that just running your input through all sorts of filters. 消毒不仅仅是通过各种过滤器运行您的输入。

Sanitizing your input is about not polluting your application with user data you don't want. 清理您的输入意味着不要用不需要的用户数据污染应用程序。
The big question, though, what is it you don't want? 不过,最大的问题是,您不想要什么?

First example 第一个例子

You've made a page, allowing a user to send a text message. 您已经制作了一个页面,允许用户发送短信。 Your expected input would be a phone number and a text message. 您希望输入的是电话号码和短信。
Looking at the Rule reference in the manual, I would probably go for these rules: 查看手册中的“ 规则”参考 ,我可能会选择以下规则:

numeric|exact_length[8]

These rules as I would like to make sure that the input is nummeric and that the input matches the length of phonenumbers in my region. 我想确保输入规则为数字,并且输入规则与我所在地区的电话号码长度匹配。 Since I already validate that the input is nummeric, I can assume that XSS and SQL injection attempts should fail (as these attacks contain non-nummeric characters). 由于我已经确认输入是数字,所以我可以假设XSS和SQL注入尝试应该失败(因为这些攻击包含非数字字符)。

For the text message field, I would use trim and required: trim|required as I don't wan't an empty message sent. 对于文本消息字段,我将使用trim和required: trim|required因为我不想发送空消息。

Second example 第二个例子

Allowing users to comment, is a good way to allow users to spam your site or inject malicious code. 允许用户发表评论是允许用户向您的网站发送垃圾邮件或注入恶意代码的好方法。

Basically, what you wan't is a name, an email and the comment. 基本上,您所不需要的是名称,电子邮件和评论。

All input needs to be required. 需要所有输入。 The e-mail needs to validate. 电子邮件需要验证。 But the comment and name needs to have some cleaning of XSS and overhead spaces/line feeds. 但是注释和名称需要对XSS和开销空格/换行进行一些清理。

My validation with sanitazion would look like this: 我对sanitazion的验证如下所示:

$this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('comment', 'Comment', 'required|trim|xss_clean');

Sanitize what you must - not what you can - and do the sanitaziton for what you need. 对必须要做的事情(而不是可以做的事情)进行消毒,并根据需要进行消毒。
Make sure, when you insert the data to your backend to use the Active Record/Query Builder for escaping your input correctly or that your are using Query Bindings which does the same for you. 确保在将数据插入后端时使用Active Record / Query Builder正确转义输入,或者确保您正在使用与您相同的查询绑定

A private function if you're looking for, 私人功能,如果您正在寻找,

function sanitizeString($value = ''){

    $value = trim($value);
    if (get_magic_quotes_gpc()) { $value = stripslashes($value); }

    $value = strtr($value,array_flip(get_html_translation_table(HTML_ENTITIES)));
    $value = strip_tags($value);
    $value = mysqli_real_escape_string(get_mysqli(), $value);
    $value = htmlspecialchars($value);

    return $value;
}

function get_mysqli() { 
    $db = (array)get_instance()->db;
    return mysqli_connect('localhost', $db['username'], $db['password'], $db['databse']);
} 

I'm using this as a custom function to sanitize each parameter passed in a form,further to this we can add up more custom functions, i hope. 我将其用作自定义函数来清理以表格形式传递的每个参数,我希望可以进一步添加更多自定义函数。 Always to have a custom function is an advantage array_map or array_walk can also be employed to simplify it further for arrays like $_GET, $_POST etc 始终具有自定义功能是一个优势array_maparray_walk也可以用于简化$ _GET,$ array_map等数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM