Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): refuse a data URL with spaces before its media type (#7064)

Cindy Zhang19651c632db9
diff --git a/.changeset/safe-url-scheme-spaces.md b/.changeset/safe-url-scheme-spaces.md
new file mode 100644
index 0000000000..1bafe8b2eb
--- /dev/null
+++ b/.changeset/safe-url-scheme-spaces.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Navigation URL safety: refuse a data URL with spaces before its media type
+@cixzhang
+
+The shared URL safety check now ignores spaces after a URL's scheme before it compares the scheme, as a data URL's media type does, so `data: text/html,…` is refused like `data:text/html,…`. In Markdown this also refuses links, images, and angle autolinks whose destination decodes to that form, such as `data: text/html,…`. Accepted URLs are returned as before.
diff --git a/packages/core/src/Markdown/linkDestinations.test.tsx b/packages/core/src/Markdown/linkDestinations.test.tsx
index 56999b1d65..aee9ceae5b 100644
--- a/packages/core/src/Markdown/linkDestinations.test.tsx
+++ b/packages/core/src/Markdown/linkDestinations.test.tsx
@@ -84,3 +84,44 @@ describe('decoded destinations stay safe', () => {
expect(targets('![pic](javascript:alert(1))')).toEqual([]);
});
});
+
+describe('a data URL with spaces before its media type stays unsafe', () => {
+ const html = 'text/html;base64,PHNjcmlwdD4=';
+ it.each([
+ `[x](<data: ${html}>)`,
+ `[x](<data: ${html}>)`,
+ `[x](<DATA: TEXT/HTML,hi>)`,
+ `[x](data:&#32;${html})`,
+ `[x](data:&#x20;${html})`,
+ `[x](data:&#32;&#32;${html})`,
+ `[x][r]\n\n[r]: <data: ${html}>\n`,
+ ])('refuses the link %j', markdown => {
+ expect(targets(markdown)).toEqual([]);
+ const {container} = render(<Markdown>{markdown}</Markdown>);
+ expect(container.querySelector('a')).toBeNull();
+ });
+
+ it.each([`![x](<data: ${html}>)`, `![x](data:&#32;${html})`])(
+ 'refuses the image %j',
+ markdown => {
+ expect(targets(markdown)).toEqual([]);
+ },
+ );
+
+ it.each([`<data:&#32;${html}>`, `<data:&#x20;${html}>`])(
+ 'refuses the angle autolink %j',
+ markdown => {
+ const nodes = parseMarkdownAst(markdown, {autolink: 'gfm'})
+ .children as unknown as ReadonlyArray<Json>;
+ const links: string[] = [];
+ const visit = (node: Json) => {
+ if (node.type === 'link') {
+ links.push(node.url ?? '');
+ }
+ (node.children ?? []).forEach(visit);
+ };
+ nodes.forEach(visit);
+ expect(links).toEqual([]);
+ },
+ );
+});
diff --git a/packages/core/src/utils/safeUrl.test.ts b/packages/core/src/utils/safeUrl.test.ts
index b8bc767912..424960164a 100644
--- a/packages/core/src/utils/safeUrl.test.ts
+++ b/packages/core/src/utils/safeUrl.test.ts
@@ -16,6 +16,8 @@ const accepted = [
'custom:open/document',
'data:image/png;base64,iVBORw0KGgo=',
'data:text/plain,download',
+ 'data: text/plain,download',
+ 'mailto: [email protected]',
];
const rejected = [
'javascript:alert(1)',
@@ -28,6 +30,17 @@ const rejected = [
'java\nscript:alert(1)',
'\x00javascript:alert(1)',
'java\x7fscript:alert(1)',
+ // A data URL's media type ignores the spaces before it.
+ 'data: text/html,<script>alert(1)</script>',
+ 'data: text/html,<script>alert(1)</script>',
+ 'DATA: TEXT/HTML;base64,PHNjcmlwdD4=',
+ ' data: text/html,x',
+ 'data:\t text/html,x',
+ 'data: \ttext/html,x',
+ 'data:\x00 text/html,x',
+ 'data: \x7ftext/html,x',
+ 'javascript: alert(1)',
+ 'vbscript: MsgBox(1)',
];
describe('navigation strings', () => {
diff --git a/packages/core/src/utils/safeUrl.ts b/packages/core/src/utils/safeUrl.ts
index 3c6ea7d5e7..7cbae8b5c0 100644
--- a/packages/core/src/utils/safeUrl.ts
+++ b/packages/core/src/utils/safeUrl.ts
@@ -12,10 +12,19 @@ function normalizeUrl(url: string): string {
return url.replace(/[\x00-\x1f\x7f]/g, '').trim();
}
+/**
+ * The form a URL's scheme is compared in: lowercase, and without the spaces
+ * after the scheme's colon, which a data URL's media type ignores
+ * (`data: text/html` is HTML).
+ */
+function comparableUrl(normalized: string): string {
+ return normalized.toLowerCase().replace(/^([a-z][a-z\d+.-]*:) +/, '$1');
+}
+
/** Return a normalized navigation URL, or null for a blocked scheme. */
export function sanitizeUrl(url: string): string | null {
const normalized = normalizeUrl(url);
- const lower = normalized.toLowerCase();
+ const lower = comparableUrl(normalized);
if (
lower.startsWith('javascript:') ||
lower.startsWith('vbscript:') ||