fix: Escape special characters in schema.org JSON-LD metadata#305
fix: Escape special characters in schema.org JSON-LD metadata#305tylere wants to merge 2 commits into
Conversation
Prevents XSS via injected `</script>` or HTML-entity sequences in account/product fields by encoding `<`, `>`, and `&` as Unicode escapes within the JSON-LD payload. https://claude.ai/code/session_01UCrzLXdCiDYw7WKmZ6Ueot
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Claude finished @tylere's task in 1m 53s —— View job PR ReviewSummary: This is a correct, well-scoped security fix. The escaping approach — replacing Security ✅
Code Quality — One NoteThe escaping chain is identical in both files (see inline comment). Extracting it into a small shared utility ( Other observations
Verdict: Looks good to merge. The fix is correct and the only meaningful suggestion (extracting the shared helper) is a cosmetic follow-up, not a blocker. |
| __html: JSON.stringify(schemaData) | ||
| .replace(/</g, "\\u003c") | ||
| .replace(/>/g, "\\u003e") | ||
| .replace(/&/g, "\\u0026"), |
There was a problem hiding this comment.
The identical escaping chain is duplicated in both ProductSchemaMetadata.tsx and AccountSchemaMetadata.tsx. If the escaping logic ever needs to change (e.g., adding ' for ', or switching to a single-pass regex), both files must be updated in sync.
Consider extracting to a shared helper — e.g. src/lib/jsonLd.ts:
export function safeJsonLd(data: unknown): string {
return JSON.stringify(data)
.replace(/</g, "\\u003c")
.replace(/>/g, "\\u003e")
.replace(/&/g, "\\u0026");
}Then both components call __html: safeJsonLd(schemaData). Low urgency — the current approach is correct — but worth doing while the logic is fresh. Fix this →
What I'm changing
This PR escapes special HTML characters (
<,>,&) in schema.org JSON-LD metadata to prevent potential XSS vulnerabilities and ensure proper rendering of structured data on product and account pages.How I did it
Updated both
ProductSchemaMetadata.tsxandAccountSchemaMetadata.tsxto escape special characters in the JSON-LD script content:<is replaced with\u003c>is replaced with\u003e&is replaced with\u0026This follows security best practices for embedding JSON-LD in HTML documents and prevents any user-supplied data within the schema from breaking out of the script tag.
Also bumped the version to 1.3.0 in
package.jsonto reflect the release.How you can test it
<script type="application/ld+json">tag contains escaped characters where applicablehttps://claude.ai/code/session_01UCrzLXdCiDYw7WKmZ6Ueot