使用 Swiftmailer 的 PHP 驱动的 HTML 联系表单的安全性如何?
我的网站上有一个 PHP 驱动的 HTML 联系表单。目前我使用 PHP mail() 函数。因此,我必须进行许多用户输入验证,以避免电子邮件标头注入攻击。我认为我是安全的,但我可能忘记了一些东西,我想迁移到一个可靠的 PHP 电子邮件库。我选择的库是 Swiftmailer。
现在我想检查 Swiftmailer 是否解决了以下问题:
- 删除或转义发件人姓名中的
<
和>
字符。 - 从发件人姓名中删除换行符(
\n
、\r\n
...)。 - 从电子邮件主题中删除或转义换行符。
- 规范邮件正文(电子邮件的内容)中的换行符。根据 PHP 文档,应在内容中使用
\n
,并应使用\r\n
作为电子邮件标头分隔符。
PS:我尝试联系 Swiftmailer 团队提出我的问题,但没有成功,所以我在这里尝试。
编辑:
我用 Swiftmailer 做了一些测试用例,这是我到目前为止发现的:
- 当你在发件人的姓名,您会收到无法送达电子邮件错误邮件。这可能会在某种程度上导致您的邮件服务器遭受 DOS 攻击(也许我错了)。这正常吗?!
- 换行符被转义,因此注入攻击失败。
- 换行符被转义,因此注入攻击失败。
- 经过测试,但我无法看到 Swiftmailer 做了什么(如果它做了什么)。所以我在这里还处于黑暗之中。
有人可以帮我澄清#1 和#4 吗?我不确定这是否是正常行为......
I have a PHP driven HTML contact form on my site. Currently I use the PHP mail() function. Because of that I have to do many user input validation to avoid email header injection attacks. I think I'm secure, but I probably forgot something and I want to move to a solid PHP email library. The library I selected is Swiftmailer.
Now I want to check if Swiftmailer address the following:
- Removes or escape
<
and>
characters in sender names. - Removes newlines (
\n
,\r\n
...) from sender names. - Removes or escape newlines from email subject.
- Normalize newlines in message body (the content of the email). As per the PHP docs,
\n
should be used in content and\r\n
as email headers separator.
PS: I tried to contact the Swiftmailer team with my questions without success so I'm trying here.
Edit:
I did some test cases with Swiftmailer and this is what I found so far:
- When you have a
<
or>
in the name of a sender, you get a Undeliverable email error mail. This can somewhat lead in a DOS attack of your mail server (maybe I'm wrong). Is this normal?! - The newlines are escaped so the injection attack fails.
- The newlines are escaped so the injection attack fails.
- Tested but I'm unable to see what Swiftmailer do (if it does something). So I'm still in the dark here.
Can someone clarify #1 and #4 for me? I'm not sure if it's normal behavior...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:这个答案可能已过时。在我写这篇文章时,SwiftMailer 库存在一些问题。此时,
SwiftMailer
一切工作正常,并且被认为是更好的库,比PHPMailer
提供更多功能。我建议你使用 phpmailer。它是我用过的最稳定的邮件库之一。下面是一个应该可以工作的示例代码:
您需要对其进行配置,以便它连接到您的电子邮件服务器,但它应该可以工作。 Phpmailer 逃脱了到目前为止我尝试过的一切。我唯一要检查的是“[电子邮件受保护]”。我用这段代码来做:
我希望这有帮助:)
EDIT: This answer may be obsolete. At the time I wrote this, there were some problems with the SwiftMailer library. At this point, everything is working fine with the
SwiftMailer
and is considered to be the better library with a lot more to offer thanPHPMailer
.I would suggest you use phpmailer. It is one of the most stable mailing libraries I've ever used. Here's an example code that should be working:
You'll need to configure this so that it connects to your email server but it should be working. Phpmailer escapes so far everything I've tried. The only I'm checking is "[email protected]". I do it with this code:
I hope this helps :)