Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/utils/safeJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Safely stringifies a JSON object for inclusion in a <script> tag.
* Replaces HTML control characters with their unicode equivalents to prevent XSS.
* This is especially useful for JSON-LD structured data.
* @param obj The object to stringify
* @param replacer A function that alters the behavior of the stringification process
* @param space A String or Number object that's used to insert white space into the output JSON string for readability purposes
* @returns The safely stringified JSON string
*/
export function safeJsonStringify(obj: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string {
const jsonString = JSON.stringify(obj, replacer, space);
return jsonString
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/&/g, '\\u0026');
}