NativeMailerHandlerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Level;
  12. use Monolog\Test\TestCase;
  13. function mail($to, $subject, $message, $additional_headers = null, $additional_parameters = null)
  14. {
  15. $GLOBALS['mail'][] = func_get_args();
  16. }
  17. class NativeMailerHandlerTest extends TestCase
  18. {
  19. protected function setUp(): void
  20. {
  21. $GLOBALS['mail'] = [];
  22. }
  23. public function testConstructorHeaderInjection()
  24. {
  25. $this->expectException(\InvalidArgumentException::class);
  26. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', "receiver@example.org\r\nFrom: faked@attacker.org");
  27. }
  28. public function testSetterHeaderInjection()
  29. {
  30. $this->expectException(\InvalidArgumentException::class);
  31. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  32. $mailer->addHeader("Content-Type: text/html\r\nFrom: faked@attacker.org");
  33. }
  34. public function testSetterArrayHeaderInjection()
  35. {
  36. $this->expectException(\InvalidArgumentException::class);
  37. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  38. $mailer->addHeader(["Content-Type: text/html\r\nFrom: faked@attacker.org"]);
  39. }
  40. public function testSetterContentTypeInjection()
  41. {
  42. $this->expectException(\InvalidArgumentException::class);
  43. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  44. $mailer->setContentType("text/html\r\nFrom: faked@attacker.org");
  45. }
  46. public function testSetterEncodingInjection()
  47. {
  48. $this->expectException(\InvalidArgumentException::class);
  49. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  50. $mailer->setEncoding("utf-8\r\nFrom: faked@attacker.org");
  51. }
  52. public function testSend()
  53. {
  54. $to = 'spammer@example.org';
  55. $subject = 'dear victim';
  56. $from = 'receiver@example.org';
  57. $mailer = new NativeMailerHandler($to, $subject, $from);
  58. $mailer->setFormatter(new \Monolog\Formatter\LineFormatter);
  59. $mailer->handleBatch([]);
  60. // batch is empty, nothing sent
  61. $this->assertEmpty($GLOBALS['mail']);
  62. // non-empty batch
  63. $mailer->handle($this->getRecord(Level::Error, "Foo\nBar\r\n\r\nBaz"));
  64. $this->assertNotEmpty($GLOBALS['mail']);
  65. $this->assertIsArray($GLOBALS['mail']);
  66. $this->assertArrayHasKey('0', $GLOBALS['mail']);
  67. $params = $GLOBALS['mail'][0];
  68. $this->assertCount(5, $params);
  69. $this->assertSame($to, $params[0]);
  70. $this->assertSame($subject, $params[1]);
  71. $this->assertStringEndsWith(" test.ERROR: Foo Bar Baz [] []\n", $params[2]);
  72. $this->assertSame("From: $from\r\nContent-type: text/plain; charset=utf-8\r\n", $params[3]);
  73. $this->assertSame('', $params[4]);
  74. }
  75. public function testMessageSubjectFormatting()
  76. {
  77. $mailer = new NativeMailerHandler('to@example.org', 'Alert: %level_name% %message%', 'from@example.org');
  78. $mailer->handle($this->getRecord(Level::Error, "Foo\nBar\r\n\r\nBaz"));
  79. $this->assertNotEmpty($GLOBALS['mail']);
  80. $this->assertIsArray($GLOBALS['mail']);
  81. $this->assertArrayHasKey('0', $GLOBALS['mail']);
  82. $params = $GLOBALS['mail'][0];
  83. $this->assertCount(5, $params);
  84. $this->assertSame('Alert: ERROR Foo Bar Baz', $params[1]);
  85. }
  86. }