Skip to content

Commit 72b080a

Browse files
andreituicutripodsan
authored andcommitted
fix: escape HTML characters in JSON LD
1 parent b6dd881 commit 72b080a

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

src/steps/render.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ function createElement(name, ...attrs) {
3636
}
3737

3838
function sanitizeJsonLd(jsonLd) {
39-
if (jsonLd.toLowerCase().indexOf('</script>') >= 0) {
40-
throw new Error('script tag not allowed');
41-
}
42-
return JSON.stringify(JSON.parse(jsonLd.trim()));
39+
const sanitizedJsonLd = jsonLd.replaceAll('<', '&#x3c;').replaceAll('>', '&#x3e;');
40+
return JSON.stringify(JSON.parse(sanitizedJsonLd.trim()));
4341
}
4442

4543
/**

test/fixtures/content/page-metadata-jsonld-xss.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<meta name="twitter:card" content="summary_large_image">
99
<meta name="twitter:title" content="Home | Helix Project Boilerplate">
1010
<meta name="twitter:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
11-
<script type="application/ld+json" data-error="error in json-ld: script tag not allowed"></script>
11+
<script type="application/ld+json">{"foo:":"&#x3c;/script&#x3e;alert('hello, world.')"}</script>
1212
<link id="favicon" rel="icon" type="image/svg+xml" href="/icons/spark.svg">
1313
<meta name="viewport" content="width=device-width, initial-scale=1">
1414
<script src="/scripts.js" type="module"></script>

0 commit comments

Comments
 (0)