【发布时间】:2011-03-21 15:56:15
【问题描述】:
假设我们要发送微不足道的反馈并让这些字段动态化:
- 发件人姓名
- 发件人电子邮件
- 主题
- 消息正文
这个 PHP 代码是否足以保护我们免受各种邮件注入?
//sanitizing email address
if ($email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
//encoding subj according to RFC and thus protecting it from all kinds of injections
$subject = "=?UTF-8?B?".base64_encode($_POST['subject'])."?=";
//encoding name for same reasons, and using sanitized email
$from = "From: =?UTF-8?B?".base64_encode($_POST['name'])."?= <$email>\r\n";
//protecting body as it mentioned in http://php.net/mail
$message = str_replace("\n.", "\n .", $_POST['text']);
mail('me@example.com',$subject,$message,$from);
}
目前我正在使用像 "some@email.com, other@email.com," 这样的名称,但似乎所有可用的邮件客户端都正确处理它
【问题讨论】:
-
以我的理解有限,我认为这就足够了:)
-
很有趣——我敢肯定你几天前说过注射'.'靠它自己“永远行不通”......
-
嗯,我还在学习,就像我们一样
-
确实如此。我认为你因为暗示你错了而对我投了反对票……
-
感谢您的纠正 - 另请参阅 spotthevuln.com/2011/01/sleep-smtp-command-injection
标签: php security email spam-prevention