Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix xss vulnerability in the seo component #1839

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/popular-moose-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Fix XSS vulnerability in the SEO component
52 changes: 50 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/hydrogen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"dependencies": {
"@shopify/hydrogen-react": "2024.1.1",
"content-security-policy-builder": "^2.1.1",
"type-fest": "^4.5.0"
"type-fest": "^4.5.0",
"xss": "^1.0.15"
},
"peerDependencies": {
"@remix-run/react": "^2.1.0",
Expand Down
11 changes: 10 additions & 1 deletion packages/hydrogen/src/seo/generate-seo-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type {ComponentPropsWithoutRef} from 'react';
import type {Maybe} from '@shopify/hydrogen-react/storefront-api-types';
import type {Thing, WithContext} from 'schema-dts';

import xss from 'xss';

const ERROR_PREFIX = 'Error in SEO input: ';

// TODO: Refactor this into more reusable validators or use a library like zod to do this if we decide to use it in
Expand Down Expand Up @@ -503,7 +505,14 @@ export function generateSeoTags<
'script',
{
type: 'application/ld+json',
children: JSON.stringify(block),
children: JSON.stringify(block, (k, value) => {
return typeof value === 'string'
? xss(value, {
stripIgnoreTag: true,
stripIgnoreTagBody: true,
})
: value;
}),
},
// @ts-expect-error
`json-ld-${block?.['@type'] || block?.name || index++}`,
Expand Down
29 changes: 29 additions & 0 deletions packages/hydrogen/src/seo/seo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,35 @@ describe('seo', () => {
</DocumentFragment>
`);
});

it('escapes script content', async () => {
vi.mocked(useMatches).mockReturnValueOnce([
fillMatch({
data: {
seo: {
jsonLd: {
'@context': 'https://schema.org',
'@type': 'Organization',
name: 'Hydrogen Root',
description: '</script><script>alert("hacked")</script>shows up',
},
},
},
}),
]);

const {asFragment} = render(createElement(Seo));

expect(asFragment()).toMatchInlineSnapshot(`
<DocumentFragment>
<script
type="application/ld+json"
>
{"@context":"https://schema.org","@type":"Organization","name":"Hydrogen Root","description":"shows up"}
blittle marked this conversation as resolved.
Show resolved Hide resolved
</script>
</DocumentFragment>
`);
});
});

function fillMatch(partial: Partial<UIMatch<any>> = {}) {
Expand Down
Loading