Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

fix: Mastodon preserves safe HTML (strong, em, a, p, br) instead of stripping everything

Paulo Castellano57ad4e2c7f8b
diff --git a/app/Services/Social/ContentSanitizer.php b/app/Services/Social/ContentSanitizer.php
index 9caf900f0..f5f73e506 100644
--- a/app/Services/Social/ContentSanitizer.php
+++ b/app/Services/Social/ContentSanitizer.php
@@ -12,6 +12,7 @@ class ContentSanitizer
{
return match ($platform) {
Platform::LinkedIn, Platform::LinkedInPage => $this->convertBoldAndStrip($content),
+ Platform::Mastodon => $this->stripUnsafeHtml($content),
default => $this->stripHtml($content),
};
}
@@ -41,6 +42,17 @@ class ContentSanitizer
return trim($content);
}
+ private function stripUnsafeHtml(string $content): string
+ {
+ // Mastodon accepts a subset of HTML: p, strong, em, a, br, span
+ $content = strip_tags($content, ['p', 'strong', 'em', 'b', 'i', 'a', 'br', 'span']);
+
+ // Decode HTML entities that aren't part of allowed tags
+ $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5, 'UTF-8');
+
+ return trim($content);
+ }
+
private function convertBoldAndStrip(string $content): string
{
// Convert <strong>/<b> to Unicode bold characters for LinkedIn
diff --git a/tests/Unit/Services/Social/ContentSanitizerTest.php b/tests/Unit/Services/Social/ContentSanitizerTest.php
index a2b073bd0..dd5084441 100644
--- a/tests/Unit/Services/Social/ContentSanitizerTest.php
+++ b/tests/Unit/Services/Social/ContentSanitizerTest.php
@@ -54,3 +54,25 @@ test('it converts list items to dashes', function () {
expect($result)->toContain('- Item one');
expect($result)->toContain('- Item two');
});
+
+test('it preserves safe html for mastodon', function () {
+ $sanitizer = new ContentSanitizer;
+ $result = $sanitizer->sanitize('<p>Hello <strong>world</strong> and <em>italic</em></p>', Platform::Mastodon);
+ expect($result)->toContain('<strong>world</strong>');
+ expect($result)->toContain('<em>italic</em>');
+ expect($result)->toContain('<p>');
+});
+
+test('it strips unsafe html for mastodon', function () {
+ $sanitizer = new ContentSanitizer;
+ $result = $sanitizer->sanitize('<p>Hello</p><script>alert("xss")</script><div>block</div>', Platform::Mastodon);
+ expect($result)->toContain('<p>Hello</p>');
+ expect($result)->not->toContain('<script>');
+ expect($result)->not->toContain('<div>');
+});
+
+test('it preserves links for mastodon', function () {
+ $sanitizer = new ContentSanitizer;
+ $result = $sanitizer->sanitize('<p>Check <a href="https://example.com">this</a></p>', Platform::Mastodon);
+ expect($result)->toContain('<a href="https://example.com">this</a>');
+});