Skip to content

Commit 5613469

Browse files
FuturMixclaude
andauthored
fix: escape XML-unsafe characters in RSS feed output (#28)
The RSS feed interpolates author names directly into XML without escaping. An author name containing < > & or other XML metacharacters produces malformed XML that breaks RSS readers. Also encode slug in URLs with encodeURIComponent for safety. Add an escapeXml helper and apply it to the author field; use encodeURIComponent for slug in link/guid URLs. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 06d0d76 commit 5613469

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

apps/web/app/blog/rss.xml/route.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import { getDb } from '@/lib/db';
44

55
const BASE = 'https://c0upons.com';
66

7+
function escapeXml(str: string): string {
8+
return str
9+
.replace(/&/g, '&amp;')
10+
.replace(/</g, '&lt;')
11+
.replace(/>/g, '&gt;')
12+
.replace(/"/g, '&quot;')
13+
.replace(/'/g, '&apos;');
14+
}
15+
716
export async function GET() {
817
let posts: Array<{ slug: string; title: string; excerpt: string | null; published_at: string; author: string | null }> = [];
918

@@ -23,10 +32,10 @@ export async function GET() {
2332
const items = posts.map((p) => `
2433
<item>
2534
<title><![CDATA[${p.title}]]></title>
26-
<link>${BASE}/blog/${p.slug}</link>
27-
<guid isPermaLink="true">${BASE}/blog/${p.slug}</guid>
35+
<link>${BASE}/blog/${encodeURIComponent(p.slug)}</link>
36+
<guid isPermaLink="true">${BASE}/blog/${encodeURIComponent(p.slug)}</guid>
2837
${p.excerpt ? `<description><![CDATA[${p.excerpt}]]></description>` : ''}
29-
${p.author ? `<author>${p.author}</author>` : ''}
38+
${p.author ? `<author>${escapeXml(p.author)}</author>` : ''}
3039
<pubDate>${new Date(p.published_at).toUTCString()}</pubDate>
3140
</item>`).join('');
3241

0 commit comments

Comments
 (0)