在用户提交文本的情况下,当输出到页面时,您在输入和输出中使用什么文本过滤器?
据我所知,使用$this->input->post('something',true)
将从输入数据中清除XSS内容,因此没有其他事情可以做以确保安全?比如htmlspecialchars()
, strip_tags()
,等等?
我也想知道,如果例如htmlspecialchars()是很好的使用,为什么CI安全库不默认应用htmlspecialchars()传递的字符串?
您应该使用form_validation库。您可以执行基于规则的检查和过滤。这是一种更健壮的验证输入数据的方法。
下面是内置的规则,任何带有一个参数的函数都可以用作过滤器/规则。
required
matches
min_length
max_length
exact_length
greater_than
less_than
alpha
alpha_numeric
alpha_dash
numeric
integer
decimal
is_natural
is_natural_no_zeroetc
valid_email
valid_emails
valid_ip
valid_base64
这取决于你对这个输入所做的事情,但最有可能的是你也想通过htmlspecialchars()
运行字符串
根据我的理解,您希望将用户提交的文本存储在数据库中,然后在页面上显示它——有点像基本的评论系统或其他东西。你只是不希望任何调皮/不完整的HTML字符破坏你的页面输出。
每当有用户提交的数据时,您都希望利用form_validation库尽可能地清理和消毒它,这是一种良好的安全措施。如果它转到数据库,则应该使用活动记录或查询绑定从Codeigniter获得额外的安全性,例如转义字符串等。
让我展示一下在网站上提交和输出用户输入的解决方案。也许还有更好的方法,但是这样就可以完成任务了。
<?php
/*Controller
**************************************************/
class Something extends CI_Controller {
function comments_or_whatever() {
//Required -> trim value -> max_length of 100 -> strip HTML tags -> remove additional HTML entities missed by strip tags
$this->form_validation->set_rules('input_1', 'The First User Input', 'required|trim|max_length[100]|xss_clean|strip_tags|callback__remove_html_entities');
$this->form_validation->set_rules('input_2', 'The Second User Input', 'trim|exact_length[11]|xss_clean|strip_tags|callback__remove_html_entities');
if ($this->form_validation->run() == FALSE) {
//form didn't validate.. try again display error messages
$this->load->view('your_view');
}
} else {
$input_1 = $this->input->post('input_1');
$input_2 = $this->input->post('input_2');
$submission_array = array(
'db_field_1' => $input_1,
'db_field_2' => $input_2
);
$this->load->model('comments');
$result = $this->comments->submit_comments_or_whatever($submission_array);
if ($result['is_true'] == TRUE) {
//creates a temporary flash message and redirects to current page
//if on a windows server use 'refresh' instead of 'location'
$this->session->set_flashdata('message', '<div class="message">'.$result['message'].'</div>');
redirect('something', 'location');
} else {
$data['message'] = $result['message'];
$this->load->view('your_view', $data);
}
}
}
// Very important to get rid calling HTML Entities via HTML number codes such as < etc. Strip_tags does not do this.
// This is privately called during validation from the callback__remove_html_entities custom callback
function _remove_html_entities($submission) {
$submission = preg_replace("/&#?[a-z0-9]{2,8};/i","",$submission);
return $submission;
}
}
/* Model
****************************************/
class Comments extends CI_Model {
function submit_comments_or_whatever($submission_array) {
// Active record escapes string and does additional security
$query = $this->db->insert('comments', $submission_array);
if ($query == TRUE) {
$data['is_true'] = TRUE;
$data['message'] = 'Your message has been successfully shared!';
return $data;
} else {
$data['is_true'] = FALSE;
$data['message'] = 'Sorry, but there was an error dude inserting your message into the database.';
return $data;
}
}
}
/* View -> your_view.php
****************************************/
<?php echo validation_errors('<div class="message">', '</div>'); ?>
<?php echo $this->session->flashdata('message'); ?>
<?php if (!empty($message)) echo '<div class="message">'.$message.'</div>'; ?>
<?php echo form_open('something/comments_or_whatever'); ?>
<?php echo form_label('The First User Input', 'input_1'); ?><br>
<?php $input_1_form = array('name' => 'input_1', 'id' => 'input_1', 'value' => set_value('input_1')); ?>
<?php echo form_input($input_1_form); ?><br>
<?php echo form_label('The Second User Input', 'input_2'); ?><br>
<?php $input_2_form = array('name' => 'input_2', 'id' => 'input_2', 'value' => set_value('input_2')); ?>
<?php echo form_input($input_2_form); ?><br>
<?php echo form_submit('submit', 'Dude, submit my user inputed text!'); ?>
<?php echo form_close(); ?>
这段代码假设您自动加载了表单验证、会话、数据库库和表单助手。现在,在表单验证期间使用自定义正则表达式回调将所有用户输入的数据剥离为最少的纯文本。所有不正经的HTML字符都被彻底清除了。你现在可以放心地输出提交的数据在任何地方,你想在一个网页上没有破坏或成为一个安全问题。
只是做HTMLSpecialChars()和html解码的问题是它不考虑不完整的html标签。希望这能帮到你,祝你好运,一如既往,没有什么是绝对安全的。