我的网站上有一个PHP驱动的HTML联系人表格。当前,我使用PHP mail()函数。因此,我必须进行许多用户输入验证,以避免email header injection attacks。我认为我很安全,但是我可能忘记了一些东西,我想转到一个可靠的PHP电子邮件库中。我选择的库是Swiftmailer

现在,我要检查Swiftmailer是否满足以下要求:

  • 删除或转义发送者名称中的<>字符。
  • 从发件人名称中删除换行符(\n\r\n ...)。
  • 从电子邮件主题中删除或转义换行符。
  • 标准化消息正文(电子邮件的内容)中的换行符。根据PHP文档,应在内容中使用\n,并在电子邮件标题分隔符中使用\r\n

  • PS:我尝试与我的问题联系Swiftmailer团队,但没有成功,所以我在这里尝试。

    编辑:

    我用Swiftmailer做了一些测试用例,到目前为止,这是我发现的结果:
  • 如果发件人名中包含<>,则会收到“无法投递”的电子邮件错误邮件。这可能会导致您的邮件服务器出现DOS attack(也许我错了)。这正常吗?
  • 换行符被转义,因此注入(inject)攻击失败。
  • 换行符被转义,因此注入(inject)攻击失败。
  • 经过测试,但是我无法查看Swiftmailer的功能(如果它可以执行某些操作)。所以我仍然在黑暗中。

  • 有人可以为我澄清#1和#4吗?我不确定这是否是正常行为...

    最佳答案

    编辑:此答案可能已过时。在我撰写本文时,SwiftMailer库存在一些问题。在这一点上,SwiftMailer一切正常,并且被认为是比PHPMailer更好的库,具有更多的功能。

    我建议您使用phpmailer。它是我使用过的最稳定的邮件库之一。这是应该正常工作的示例代码:

    include("./phpmailer/class.phpmailer.php");
    $mail = new PHPMailer(false); // the true param means it will throw exceptions on errors, which we need to catch
    $mail->IsSMTP();
    $mail->Host = "YourDomainName.com";
    $mail->SMTPDebug = 2;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "tls";
    $mail->Host = "YourSMTPMailServer.com";
    $mail->Port = 587;
    $mail->Username = "[email protected]";
    $mail->Password = "password"; // GMAIL password
    $mail->AddAddress("[email protected]", '<< >> ! " Receiver Name');
    $mail->SetFrom('[email protected]', '<< >> ! " Sender Name');
    $mail->Subject = "A testing subject";
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
    $mail->MsgHTML('This is my <b>html</b> testing email, sent '.time());
    $mail->Send();
    

    您需要对其进行配置,以便它可以连接到您的电子邮件服务器,但它应该可以正常工作。到目前为止,Phpmailer都摆脱了我尝试过的一切。我唯一要检查的是“[email protected]”。我用下面的代码来做到这一点:
    $email = "[email protected]";
    $email = filter_var(filter_var($email,FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL);
    
    if($email){
        echo "This email is valid!";
    } else {
        echo "This email is INVALID!";
    }
    

    我希望这有帮助 :)

    关于php - 使用Swiftmailer的PHP驱动的HTML联系人表单的安全性如何?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6601754/

    10-13 08:44