NativeMailerHandlerTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. function mail($to, $subject, $message, $additional_headers = null, $additional_parameters = null)
  13. {
  14. $GLOBALS['mail'][] = \func_get_args();
  15. }
  16. class NativeMailerHandlerTest extends \Monolog\Test\MonologTestCase
  17. {
  18. protected function setUp(): void
  19. {
  20. $GLOBALS['mail'] = [];
  21. }
  22. protected function newNativeMailerHandler(... $args) : NativeMailerHandler
  23. {
  24. return new class(... $args) extends NativeMailerHandler {
  25. public $mail = [];
  26. protected function mail(
  27. string $to,
  28. string $subject,
  29. string $content,
  30. string $headers,
  31. string $parameters
  32. ) : void {
  33. $this->mail[] = \func_get_args();
  34. }
  35. };
  36. }
  37. public function testConstructorHeaderInjection()
  38. {
  39. $this->expectException(\InvalidArgumentException::class);
  40. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', "receiver@example.org\r\nFrom: faked@attacker.org");
  41. }
  42. public function testSetterHeaderInjection()
  43. {
  44. $this->expectException(\InvalidArgumentException::class);
  45. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  46. $mailer->addHeader("Content-Type: text/html\r\nFrom: faked@attacker.org");
  47. }
  48. public function testSetterArrayHeaderInjection()
  49. {
  50. $this->expectException(\InvalidArgumentException::class);
  51. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  52. $mailer->addHeader(["Content-Type: text/html\r\nFrom: faked@attacker.org"]);
  53. }
  54. public function testSetterContentTypeInjection()
  55. {
  56. $this->expectException(\InvalidArgumentException::class);
  57. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  58. $mailer->setContentType("text/html\r\nFrom: faked@attacker.org");
  59. }
  60. public function testSetterEncodingInjection()
  61. {
  62. $this->expectException(\InvalidArgumentException::class);
  63. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  64. $mailer->setEncoding("utf-8\r\nFrom: faked@attacker.org");
  65. }
  66. public function testSend()
  67. {
  68. $to = 'spammer@example.org';
  69. $subject = 'dear victim';
  70. $from = 'receiver@example.org';
  71. $mailer = $this->newNativeMailerHandler($to, $subject, $from);
  72. $mailer->setFormatter(new \Monolog\Formatter\LineFormatter);
  73. $mailer->handleBatch([]);
  74. // batch is empty, nothing sent
  75. $this->assertEmpty($mailer->mail);
  76. // non-empty batch
  77. $mailer->handle($this->getRecord(Level::Error, "Foo\nBar\r\n\r\nBaz"));
  78. $this->assertNotEmpty($mailer->mail);
  79. $this->assertIsArray($mailer->mail);
  80. $this->assertArrayHasKey('0', $mailer->mail);
  81. $params = $mailer->mail[0];
  82. $this->assertCount(5, $params);
  83. $this->assertSame($to, $params[0]);
  84. $this->assertSame($subject, $params[1]);
  85. $this->assertStringEndsWith(" test.ERROR: Foo Bar Baz [] []\n", $params[2]);
  86. $this->assertSame("From: $from\r\nContent-type: text/plain; charset=utf-8\r\n", $params[3]);
  87. $this->assertSame('', $params[4]);
  88. }
  89. public function testMessageSubjectFormatting()
  90. {
  91. $mailer = $this->newNativeMailerHandler('to@example.org', 'Alert: %level_name% %message%', 'from@example.org');
  92. $mailer->handle($this->getRecord(Level::Error, "Foo\nBar\r\n\r\nBaz"));
  93. $this->assertNotEmpty($mailer->mail);
  94. $this->assertIsArray($mailer->mail);
  95. $this->assertArrayHasKey('0', $mailer->mail);
  96. $params = $mailer->mail[0];
  97. $this->assertCount(5, $params);
  98. $this->assertSame('Alert: ERROR Foo Bar Baz', $params[1]);
  99. }
  100. public function testMail()
  101. {
  102. $mailer = new NativeMailerHandler('to@example.org', 'subject', 'from@example.org');
  103. $mailer->addParameter('foo');
  104. $mailer->handle($this->getRecord(Level::Error, "FooBarBaz"));
  105. $this->assertNotEmpty($GLOBALS['mail']);
  106. $this->assertIsArray($GLOBALS['mail']);
  107. $this->assertArrayHasKey('0', $GLOBALS['mail']);
  108. $params = $GLOBALS['mail'][0];
  109. $this->assertCount(5, $params);
  110. $this->assertSame('to@example.org', $params[0]);
  111. $this->assertSame('subject', $params[1]);
  112. $this->assertStringContainsString("FooBarBaz", $params[2]);
  113. $this->assertStringContainsString('From: from@example.org', $params[3]);
  114. $this->assertSame('foo', $params[4]);
  115. }
  116. }