Skip to content

A free open source IT asset/license management system

Notifications: Fixed #19998 - force base64 encoding on emails to prevent double-dots

snipe2567e5d36ae9
diff --git a/app/Listeners/ForceBase64MailEncoding.php b/app/Listeners/ForceBase64MailEncoding.php
new file mode 100644
index 0000000000..250550dcc6
--- /dev/null
+++ b/app/Listeners/ForceBase64MailEncoding.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace App\Listeners;
+
+use Illuminate\Mail\Events\MessageSending;
+use Symfony\Component\Mime\Part\AbstractMultipartPart;
+use Symfony\Component\Mime\Part\AbstractPart;
+use Symfony\Component\Mime\Part\DataPart;
+use Symfony\Component\Mime\Part\TextPart;
+
+/*
+ * Why base64 and not QP: quoted-printable soft-wraps at col 76 end up inside long
+ * markdown-table URLs (issue #19998). A compliant MTA strips "=\n" per
+ * RFC 2045 §6.7, but some intermediates (SafeLinks, older Exchange)
+ * rewrite that soft-break into a literal "." or similar joining
+ * char, producing user-visible double-dots in wrapped links. Base64 has no
+ * soft-wrap at all so the URL stays normal no matter what downstream mangling happens.
+ */
+class ForceBase64MailEncoding
+{
+ public function handle(MessageSending $event): void
+ {
+ $this->switchTextPartsToBase64($event->message->getBody());
+ }
+
+ private function switchTextPartsToBase64(AbstractPart $part): void
+ {
+ if ($part instanceof AbstractMultipartPart) {
+ foreach ($part->getParts() as $child) {
+ $this->switchTextPartsToBase64($child);
+ }
+
+ return;
+ }
+
+ // DataPart (attachments / inline images) already defaults to base64;
+ // leave it untouched to avoid re-encoding binary payloads.
+ if (! $part instanceof TextPart || $part instanceof DataPart) {
+ return;
+ }
+
+ if ($part->getMediaType() !== 'text') {
+ return;
+ }
+
+ // TextPart::$encoding is private and has no public setter. Reflection
+ // is the only way to flip the content-transfer-encoding on an
+ // already-constructed part without rebuilding the whole MIME tree
+ // (which would otherwise require re-doing CID replacement, inline
+ // image detection, and nested Multipart/Alternative/Related/Mixed
+ // bookkeeping that Symfony's Email::generateBody() does for us).
+ $reflection = new \ReflectionProperty(TextPart::class, 'encoding');
+ $reflection->setValue($part, 'base64');
+ }
+}
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 7c9b2007ee..a9e261ccd8 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -4,6 +4,7 @@ namespace App\Providers;
use App\Listeners\CheckoutableListener;
use App\Listeners\CheckoutablesCheckedOutInBulkListener;
+use App\Listeners\ForceBase64MailEncoding;
use App\Listeners\FulfillCheckoutRequestListener;
use App\Listeners\LogFailedLogin;
use App\Listeners\LogListener;
@@ -11,6 +12,7 @@ use App\Listeners\LogSuccessfulLogin;
use App\Listeners\RegenerateIdeHelperAfterMigrate;
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
+use Illuminate\Mail\Events\MessageSending;
class EventServiceProvider extends ServiceProvider
{
@@ -36,6 +38,10 @@ class EventServiceProvider extends ServiceProvider
MigrationsEnded::class => [
RegenerateIdeHelperAfterMigrate::class,
],
+
+ MessageSending::class => [
+ ForceBase64MailEncoding::class,
+ ],
];
/**
diff --git a/tests/Feature/Notifications/Email/ForceBase64MailEncodingTest.php b/tests/Feature/Notifications/Email/ForceBase64MailEncodingTest.php
new file mode 100644
index 0000000000..3fcc8f0545
--- /dev/null
+++ b/tests/Feature/Notifications/Email/ForceBase64MailEncodingTest.php
@@ -0,0 +1,146 @@
+<?php
+
+namespace Tests\Feature\Notifications\Email;
+
+use App\Listeners\ForceBase64MailEncoding;
+use Illuminate\Mail\Events\MessageSending;
+use PHPUnit\Framework\Attributes\Group;
+use Symfony\Component\Mime\Email;
+use Symfony\Component\Mime\Part\AbstractMultipartPart;
+use Symfony\Component\Mime\Part\DataPart;
+use Symfony\Component\Mime\Part\TextPart;
+use Tests\TestCase;
+
+#[Group('notifications')]
+class ForceBase64MailEncodingTest extends TestCase
+{
+ /*
+ * Regression test for issue #19998:
+ */
+ public function test_html_and_text_parts_use_base64_encoding(): void
+ {
+ $email = (new Email)
+ ->from('[email protected]')
+ ->subject('t')
+ ->html('<p>Hello <a href="https://snipe.school.k12.blah.us/hardware/1">asset</a></p>')
+ ->text("Hello https://snipe.school.k12.blah.us/hardware/1\n");
+
+ (new ForceBase64MailEncoding)->handle(new MessageSending($email));
+
+ $raw = $email->toString();
+
+ preg_match_all('/Content-Transfer-Encoding: (\S+)/', $raw, $m);
+ $encodings = array_filter($m[1], fn($e) => in_array($e, ['base64', 'quoted-printable', '8bit', '7bit'], true));
+
+ $this->assertNotEmpty($encodings, 'Expected at least one Content-Transfer-Encoding header on text/html parts.');
+ foreach ($encodings as $encoding) {
+ $this->assertSame('base64', $encoding, 'All text parts must be base64, found: ' . $encoding);
+ }
+ }
+
+ public function test_no_url_host_is_split_across_a_soft_wrap(): void
+ {
+ $longLink = str_repeat('X', 50) . ' https://snipe.school.k12.blah.us/hardware/' . str_repeat('9', 20);
+ $email = (new Email)
+ ->from('[email protected]')
+ ->subject('t')
+ ->html('<p>' . e($longLink) . '</p>')
+ ->text($longLink);
+
+ (new ForceBase64MailEncoding)->handle(new MessageSending($email));
+
+ $raw = $email->toString();
+
+ // Base64 output has no soft-wrap continuation. It only emits "=" as
+ // terminal padding. Confirm no QP soft-wrap artifact sits inside.
+ $this->assertStringNotContainsString('=3D', $raw, 'Encoded body should contain no QP escapes (=3D).');
+ $this->assertDoesNotMatchRegularExpression(
+ '/=\r?\n[A-Za-z0-9][a-z0-9]/',
+ $raw,
+ 'No QP soft-wrap continuations should appear in a base64-encoded body.',
+ );
+ }
+
+ public function test_url_survives_round_trip_through_base64(): void
+ {
+ $host = 'snipe.school.k12.blah.us';
+ $html = '<a href="https://' . $host . '/hardware/42">asset</a>';
+
+ $email = (new Email)
+ ->from('[email protected]')
+ ->subject('t')
+ ->html($html);
+
+ (new ForceBase64MailEncoding)->handle(new MessageSending($email));
+
+ $raw = $email->toString();
+
+ $this->assertTrue(
+ (bool) preg_match('/Content-Transfer-Encoding: base64\r?\n\r?\n([A-Za-z0-9+\/\r\n=]+?)(?=\r?\n--|$)/s', $raw, $m),
+ 'Expected to find a base64-encoded body section.',
+ );
+
+ $decoded = base64_decode($m[1]);
+ $this->assertStringContainsString($host, $decoded);
+ $this->assertStringNotContainsString('snipe..', $decoded);
+ $this->assertStringNotContainsString('..school', $decoded);
+ }
+
+ public function test_data_part_attachments_are_not_touched(): void
+ {
+ /*
+ * DataPart (attachments / inline images) extends TextPart but holds
+ * binary payloads already encoded as base64. The listener must skip
+ * them so we don't double-encode or disturb inline-image CIDs.
+ */
+ $email = (new Email)
+ ->from('[email protected]')
+ ->subject('t')
+ ->html('<p>has attachment</p>')
+ ->attach('binary-payload', 'file.bin', 'application/octet-stream');
+
+ (new ForceBase64MailEncoding)->handle(new MessageSending($email));
+
+ $textParts = $this->collectTextParts($email->getBody());
+
+ $sawHtml = false;
+ $sawAttachment = false;
+ foreach ($textParts as $part) {
+ if ($part instanceof DataPart) {
+ $sawAttachment = true;
+
+ continue;
+ }
+ if ($part->getMediaType() === 'text' && $part->getMediaSubtype() === 'html') {
+ $sawHtml = true;
+ $header = $part->getPreparedHeaders()->get('Content-Transfer-Encoding');
+ $this->assertNotNull($header);
+ $this->assertSame('base64', $header->getBodyAsString());
+ }
+ }
+
+ $this->assertTrue($sawHtml, 'Expected to find an html TextPart.');
+ $this->assertTrue($sawAttachment, 'Expected to find a DataPart attachment.');
+ }
+
+ /**
+ * @return list<TextPart>
+ */
+ private function collectTextParts($part): array
+ {
+ if ($part instanceof AbstractMultipartPart) {
+ $out = [];
+ foreach ($part->getParts() as $child) {
+ $out = array_merge($out, $this->collectTextParts($child));
+ }
+
+ return $out;
+ }
+
+ return $part instanceof TextPart ? [$part] : [];
+ }
+}