@zero2webmaster/starter-kit 0.22.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -54,7 +54,7 @@ function jsonLdHelpers() {
|
|
|
54
54
|
//
|
|
55
55
|
// Usage (Next.js App Router):
|
|
56
56
|
//
|
|
57
|
-
// import { articleJsonLd } from "@/seo/json-ld";
|
|
57
|
+
// import { articleJsonLd, serializeJsonLd } from "@/seo/json-ld";
|
|
58
58
|
// export default function Page() {
|
|
59
59
|
// const ld = articleJsonLd({
|
|
60
60
|
// headline: "Post title",
|
|
@@ -63,8 +63,47 @@ function jsonLdHelpers() {
|
|
|
63
63
|
// image: "https://example.com/og.png",
|
|
64
64
|
// url: "https://example.com/posts/post-title",
|
|
65
65
|
// });
|
|
66
|
-
// return <>{/* page body */}<script type="application/ld+json" dangerouslySetInnerHTML={{ __html:
|
|
66
|
+
// return <>{/* page body */}<script type="application/ld+json" dangerouslySetInnerHTML={{ __html: serializeJsonLd(ld) }} /></>;
|
|
67
67
|
// }
|
|
68
|
+
//
|
|
69
|
+
// 🔴 SERIALIZE WITH \`serializeJsonLd\`, NEVER WITH BARE \`JSON.stringify\`. These
|
|
70
|
+
// builders are fed CONTENT -- \`headline: post.title\`, an author name, a product
|
|
71
|
+
// description -- so they are fed DATABASE CONTENT, which makes them
|
|
72
|
+
// user-controlled by construction rather than by accident.
|
|
73
|
+
//
|
|
74
|
+
// \`JSON.stringify\` escapes what JSON needs escaped and does NOT escape \`<\`,
|
|
75
|
+
// correctly, because \`<\` is legal in a JSON string. But the HTML parser does not
|
|
76
|
+
// parse JSON: inside a <script> element it scans the raw bytes for \`</script\` and
|
|
77
|
+
// ENDS the element the moment it finds one, parsing the rest as markup. A headline
|
|
78
|
+
// containing that sequence closes the tag and runs whatever follows it.
|
|
79
|
+
// \`z2w-social\` found exactly this live on 2026-08-14 -- stored XSS reachable by
|
|
80
|
+
// any signed-in member against every visitor, admins included -- and it had passed
|
|
81
|
+
// a typecheck, a build, unit tests on the JSON-LD content, and a 200 response,
|
|
82
|
+
// because a serializer inside a script tag reads as the handled path.
|
|
83
|
+
//
|
|
84
|
+
// A CSP does not save you: \`'unsafe-inline'\` permits it by definition, and even a
|
|
85
|
+
// nonce does not, because the payload runs as MARKUP rather than as a script.
|
|
86
|
+
// Escaping is the control. See [[json-stringify-is-not-html-escaping]].
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Serialise a JSON-LD object for embedding in a <script> tag.
|
|
90
|
+
*
|
|
91
|
+
* Each replacement is an equivalent encoding of the same character inside a JSON
|
|
92
|
+
* string, so \`JSON.parse\` round-trips identically and crawlers, unfurlers and
|
|
93
|
+
* schema validators read exactly what they read before. There is no SEO cost,
|
|
94
|
+
* which is why there is no reason to defer it.
|
|
95
|
+
*
|
|
96
|
+
* U+2028/U+2029 are legal in JSON but are line terminators to a JavaScript
|
|
97
|
+
* parser. Written as escapes, never as literal characters: a raw U+2028 ends the
|
|
98
|
+
* line as far as esbuild is concerned, and the error it reports points at a line
|
|
99
|
+
* that looks completely fine because the character is invisible.
|
|
100
|
+
*/
|
|
101
|
+
export function serializeJsonLd(data: unknown): string {
|
|
102
|
+
return JSON.stringify(data)
|
|
103
|
+
.replace(/</g, "\\\\u003c")
|
|
104
|
+
.replace(/\\u2028/g, "\\\\u2028")
|
|
105
|
+
.replace(/\\u2029/g, "\\\\u2029");
|
|
106
|
+
}
|
|
68
107
|
|
|
69
108
|
export interface ArticleJsonLdInput {
|
|
70
109
|
headline: string;
|
|
@@ -713,7 +752,7 @@ Add the following to your \`package.json\` scripts:
|
|
|
713
752
|
${inputs.projectType === "nextjs"
|
|
714
753
|
? `\`\`\`tsx
|
|
715
754
|
// app/posts/[slug]/page.tsx
|
|
716
|
-
import { articleJsonLd } from "@/seo/json-ld";
|
|
755
|
+
import { articleJsonLd, serializeJsonLd } from "@/seo/json-ld";
|
|
717
756
|
|
|
718
757
|
export default async function PostPage({ params }: { params: { slug: string } }) {
|
|
719
758
|
const post = await loadPost(params.slug);
|
|
@@ -727,9 +766,12 @@ export default async function PostPage({ params }: { params: { slug: string } })
|
|
|
727
766
|
return (
|
|
728
767
|
<article>
|
|
729
768
|
{/* ... post body ... */}
|
|
769
|
+
{/* serializeJsonLd, never bare JSON.stringify: \`post.title\` is database
|
|
770
|
+
content, and JSON.stringify does not escape the \`</script\` sequence
|
|
771
|
+
that ends the element. See the note in src/seo/json-ld.ts. */}
|
|
730
772
|
<script
|
|
731
773
|
type="application/ld+json"
|
|
732
|
-
dangerouslySetInnerHTML={{ __html:
|
|
774
|
+
dangerouslySetInnerHTML={{ __html: serializeJsonLd(ld) }}
|
|
733
775
|
/>
|
|
734
776
|
</article>
|
|
735
777
|
);
|