NativeMailerHandlerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Test\TestCase;
  12. use Monolog\Logger;
  13. use InvalidArgumentException;
  14. function mail($to, $subject, $message, $additional_headers = null, $additional_parameters = null)
  15. {
  16. $GLOBALS['mail'][] = func_get_args();
  17. }
  18. class NativeMailerHandlerTest extends TestCase
  19. {
  20. protected function setUp(): void
  21. {
  22. $GLOBALS['mail'] = [];
  23. }
  24. public function testConstructorHeaderInjection()
  25. {
  26. $this->expectException(\InvalidArgumentException::class);
  27. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', "receiver@example.org\r\nFrom: faked@attacker.org");
  28. }
  29. public function testSetterHeaderInjection()
  30. {
  31. $this->expectException(\InvalidArgumentException::class);
  32. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  33. $mailer->addHeader("Content-Type: text/html\r\nFrom: faked@attacker.org");
  34. }
  35. public function testSetterArrayHeaderInjection()
  36. {
  37. $this->expectException(\InvalidArgumentException::class);
  38. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  39. $mailer->addHeader(["Content-Type: text/html\r\nFrom: faked@attacker.org"]);
  40. }
  41. public function testSetterContentTypeInjection()
  42. {
  43. $this->expectException(\InvalidArgumentException::class);
  44. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  45. $mailer->setContentType("text/html\r\nFrom: faked@attacker.org");
  46. }
  47. public function testSetterEncodingInjection()
  48. {
  49. $this->expectException(\InvalidArgumentException::class);
  50. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  51. $mailer->setEncoding("utf-8\r\nFrom: faked@attacker.org");
  52. }
  53. public function testSend()
  54. {
  55. $to = 'spammer@example.org';
  56. $subject = 'dear victim';
  57. $from = 'receiver@example.org';
  58. $mailer = new NativeMailerHandler($to, $subject, $from);
  59. $mailer->setFormatter(new \Monolog\Formatter\LineFormatter);
  60. $mailer->handleBatch([]);
  61. // batch is empty, nothing sent
  62. $this->assertEmpty($GLOBALS['mail']);
  63. // non-empty batch
  64. $mailer->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
  65. $this->assertNotEmpty($GLOBALS['mail']);
  66. $this->assertIsArray($GLOBALS['mail']);
  67. $this->assertArrayHasKey('0', $GLOBALS['mail']);
  68. $params = $GLOBALS['mail'][0];
  69. $this->assertCount(5, $params);
  70. $this->assertSame($to, $params[0]);
  71. $this->assertSame($subject, $params[1]);
  72. $this->assertStringEndsWith(" test.ERROR: Foo Bar Baz [] []\n", $params[2]);
  73. $this->assertSame("From: $from\r\nContent-type: text/plain; charset=utf-8\r\n", $params[3]);
  74. $this->assertSame('', $params[4]);
  75. }
  76. public function testMessageSubjectFormatting()
  77. {
  78. $mailer = new NativeMailerHandler('to@example.org', 'Alert: %level_name% %message%', 'from@example.org');
  79. $mailer->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
  80. $this->assertNotEmpty($GLOBALS['mail']);
  81. $this->assertIsArray($GLOBALS['mail']);
  82. $this->assertArrayHasKey('0', $GLOBALS['mail']);
  83. $params = $GLOBALS['mail'][0];
  84. $this->assertCount(5, $params);
  85. $this->assertSame('Alert: ERROR Foo Bar Baz', $params[1]);
  86. }
  87. }