NativeMailerHandlerTest.php 4.9 KB

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