Skip to content

A free open source IT asset/license management system

Block markdown images in emails

snipe836c4a20c745
diff --git a/app/Mail/BlockImagesMarkdownExtension.php b/app/Mail/BlockImagesMarkdownExtension.php
new file mode 100644
index 0000000000..d4db1674e1
--- /dev/null
+++ b/app/Mail/BlockImagesMarkdownExtension.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace App\Mail;
+
+use League\CommonMark\Environment\EnvironmentBuilderInterface;
+use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
+use League\CommonMark\Extension\ExtensionInterface;
+use League\CommonMark\Node\Node;
+use League\CommonMark\Renderer\ChildNodeRendererInterface;
+use League\CommonMark\Renderer\NodeRendererInterface;
+
+/**
+ * CommonMark extension that neutralizes markdown image syntax in outbound
+ * mail. Registered globally on the Illuminate\Mail\Markdown singleton so
+ * every parser instance picks it up. That includes the hardcoded
+ * Illuminate\Mail\Markdown::parse calls inside vendor mail views (mail::table,
+ * mail::panel, mail::subcopy, mail::footer, mail::layout), which a container
+ * subclass would otherwise miss.
+ *
+ * The image renderer is replaced at priority 100, which beats the priority-0
+ * default from CommonMarkCoreExtension. Rather than dropping the content
+ * entirely we render the image node's alt text as plain text. A note like
+ * `![My photo](/etc/passwd)` becomes literal "My photo" in the email instead
+ * of vanishing silently.
+ *
+ * Why this matters. laravel-mail-auto-embed walks outbound emails and, for
+ * every <img src> it finds, either file_get_contents()s the path or curl-
+ * fetches the URL and attaches the result. Without this extension, any
+ * user-controlled string that reaches a markdown mail template can be used
+ * to exfiltrate arbitrary server-readable files or issue arbitrary internal
+ * HTTP requests. See GHSA advisory for the full attack chain.
+ */
+class BlockImagesMarkdownExtension implements ExtensionInterface
+{
+ public function register(EnvironmentBuilderInterface $environment): void
+ {
+ $environment->addRenderer(Image::class, new class implements NodeRendererInterface
+ {
+ public function render(Node $node, ChildNodeRendererInterface $childRenderer): string
+ {
+ return htmlspecialchars(
+ $childRenderer->renderNodes($node->children()),
+ ENT_QUOTES | ENT_HTML5,
+ 'UTF-8'
+ );
+ }
+ }, 100);
+ }
+}
diff --git a/config/mail.php b/config/mail.php
index 08eaa366be..28639b260d 100755
--- a/config/mail.php
+++ b/config/mail.php
@@ -272,6 +272,17 @@ return [
'paths' => [
resource_path('views/vendor/mail'),
],
+
+ // CommonMark extensions to register on Laravel's mail Markdown parser.
+ // BlockImagesMarkdownExtension replaces the default Image renderer
+ // with a no-op that emits the alt text as plain text. Without it,
+ // user-controlled fields that reach a markdown mail template can
+ // produce <img src="/any/path"> tags. laravel-mail-auto-embed then
+ // resolves each src via file_get_contents() or curl, exfiltrating
+ // arbitrary local files or issuing internal HTTP requests.
+ 'extensions' => [
+ \App\Mail\BlockImagesMarkdownExtension::class,
+ ],
],
];
diff --git a/tests/Feature/CheckoutAcceptances/Ui/AssetAcceptanceTest.php b/tests/Feature/CheckoutAcceptances/Ui/AssetAcceptanceTest.php
index 6deca9920d..2bdc2ab8e5 100644
--- a/tests/Feature/CheckoutAcceptances/Ui/AssetAcceptanceTest.php
+++ b/tests/Feature/CheckoutAcceptances/Ui/AssetAcceptanceTest.php
@@ -246,6 +246,58 @@ class AssetAcceptanceTest extends TestCase
);
}
+ public function test_acceptance_note_cannot_inject_markdown_image_tag(): void
+ {
+ // Regression for GHSA (F41 / Adam Nurudini). A low-privilege user
+ // could submit an acceptance note like `![x](/var/www/html/.env)`,
+ // which Blade's HTML escape leaves intact (the chars are not HTML
+ // metacharacters). CommonMark then expanded it into a real <img>
+ // tag inside the outbound notification, and laravel-mail-auto-embed
+ // resolved the src via file_get_contents() or curl, exfiltrating
+ // arbitrary local files or issuing SSRF requests. The fix registers
+ // a CommonMark extension that overrides the Image renderer so no
+ // <img> tag survives markdown parsing.
+ Event::fake([CheckoutAccepted::class]);
+ Notification::fake();
+
+ $checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
+
+ $lfrPayload = '![x](/etc/hostname)';
+ $ssrfPayload = '![y](http://169.254.169.254/latest/meta-data/)';
+ $filePayload = '![z](file:///var/www/html/.env)';
+
+ $this->actingAs($checkoutAcceptance->assignedTo)
+ ->post(route('account.store-acceptance', $checkoutAcceptance), [
+ 'asset_acceptance' => 'accepted',
+ 'note' => "{$lfrPayload} {$ssrfPayload} {$filePayload}",
+ ])
+ ->assertRedirectToRoute('account.accept')
+ ->assertSessionHas('success');
+
+ Notification::assertSentTo(
+ $checkoutAcceptance,
+ function (AcceptanceItemAcceptedNotification $notification) {
+ $rendered = $notification->toMail()->render();
+
+ // None of the payload targets should survive as an <img src>
+ // in the rendered email. laravel-mail-auto-embed resolves any
+ // surviving img src via file_get_contents() (LFR) or curl (SSRF).
+ // Legitimate header/logo <img> tags with app-controlled src
+ // paths are unaffected. This test targets only user-controlled
+ // src values that Adam's F41 report demonstrated.
+ foreach (['/etc/hostname', '169.254.169.254', '/var/www/html/.env', 'file://'] as $payload) {
+ $this->assertStringNotContainsString(
+ $payload,
+ $rendered,
+ "Rendered mail contains attacker-controlled path \"{$payload}\" - CommonMark image markdown was not blocked."
+ );
+ }
+
+ return true;
+ }
+ );
+ }
+
public function test_admin_can_complete_sign_in_place_acceptance_and_is_redirected_to_selected_destination()
{
Event::fake([CheckoutAccepted::class]);