An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown): apply the URL safety rule to every image path (#5522)
Baraa Hamodi05d8d42d6935
diff --git a/.changeset/markdown-image-url-parity.md b/.changeset/markdown-image-url-parity.mdnew file mode 100644index 0000000000..4628739643--- /dev/null+++ b/.changeset/markdown-image-url-parity.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown now applies the same URL safety rule to every image path it parses: reference-style images (`![alt][label]` and the shortcut form) and standalone block images pass through the check inline images and links already used, and the render-side guard normalizes control characters before testing so both layers see a URL the way a browser will.++@bhamodidiff --git a/packages/core/src/Markdown/Markdown.tsx b/packages/core/src/Markdown/Markdown.tsxindex ea918b4946..813e44eb1a 100644--- a/packages/core/src/Markdown/Markdown.tsx+++ b/packages/core/src/Markdown/Markdown.tsx@@ -561,14 +561,19 @@ const headingStyles = {const DANGEROUS_URL_PATTERN = /^(javascript|data|vbscript):/i;function sanitizeUrl(url: string): string | null {- const trimmed = url.trim();- if (trimmed.length === 0) {+ // Strip control characters before testing, the same normalization the+ // parser's isSafeUrl applies — the anchored pattern must see the URL the+ // way a browser will. Return the normalized value so the stripped+ // characters don't ride along into an attribute or component override.+ // eslint-disable-next-line no-control-regex -- control chars are the bypass+ const normalized = url.replace(/[\x00-\x1f\x7f]/g, '').trim();+ if (normalized.length === 0) {return null;}- if (DANGEROUS_URL_PATTERN.test(trimmed)) {+ if (DANGEROUS_URL_PATTERN.test(normalized)) {return null;}- return trimmed;+ return normalized;}// ---------------------------------------------------------------------------diff --git a/packages/core/src/Markdown/parser.test.ts b/packages/core/src/Markdown/parser.test.tsindex 2236923c2e..65c6f89fc3 100644--- a/packages/core/src/Markdown/parser.test.ts+++ b/packages/core/src/Markdown/parser.test.ts@@ -454,6 +454,14 @@ describe('parseMarkdown', () => {}});+ it('rejects a standalone image with an unsafe scheme as literal text (XSS prevention)', () => {+ // Same rule as inline images: the line falls through to the paragraph+ // path and stays literal text instead of becoming an image node.+ const result = parseMarkdown('');+ expect(result[0].type).toBe('paragraph');+ expect(JSON.stringify(result)).not.toContain('"type":"image"');+ });+it('parses complex AI response', () => {const input = ['# Analysis',@@ -1150,6 +1158,31 @@ describe('link reference definitions', () => {expect(links).toEqual([{type: 'image', src: '/logo.png', text: 'logo'}]);});+ it('rejects a reference image whose definition has an unsafe scheme (XSS prevention)', () => {+ const {links} = paragraphLinks('![logo][l]\n\n[l]: <javascript:alert(1)>');+ expect(links).toEqual([]);+ });++ it('rejects a shortcut reference image with an unsafe definition (XSS prevention)', () => {+ const {links} = paragraphLinks('![logo]\n\n[logo]: <javascript:alert(1)>');+ expect(links).toEqual([]);+ });++ it('rejects definitions that hide the scheme behind control chars (XSS prevention)', () => {+ // isSafeUrl strips control characters before testing; the angle-bracket+ // destination form is the only definition shape that can contain them.+ // Both consumers of a definition — reference images and reference links —+ // must apply it.+ const {links: imageLinks} = paragraphLinks(+ '![logo][l]\n\n[l]: <java\tscript:alert(1)>',+ );+ expect(imageLinks).toEqual([]);+ const {links: refLinks} = paragraphLinks(+ '[click][l]\n\n[l]: <java\tscript:alert(1)>',+ );+ expect(refLinks).toEqual([]);+ });+it('does not treat a whitespace-only second label as collapsed', () => {// `[foo][ ]` is a full reference to the empty (normalized) label and matches// nothing; `[foo]` still resolves as a shortcut and `[ ]` stays literal.diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex 774d34fd22..a92ca9f82c 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -369,7 +369,7 @@ function matchReferenceImage(const rawLabel = text.slice(altClose + 2, labelClose);const label = rawLabel === '' ? alt : rawLabel;const src = linkDefs.get(normalizeLinkLabel(label));- if (src != null) {+ if (src != null && isSafeUrl(src)) {return {node: {type: 'image', src, alt}, end: labelClose + 1};}// No match — fall back to a shortcut `![alt]`.@@ -379,7 +379,7 @@ function matchReferenceImage(return null;}const src = linkDefs.get(normalizeLinkLabel(alt));- if (src == null) {+ if (src == null || !isSafeUrl(src)) {return null;}return {node: {type: 'image', src, alt}, end: altClose + 1};@@ -1416,8 +1416,14 @@ function parseMarkdownImpl(}// --- Standalone image ---+ // An unsafe src falls through to the paragraph path and renders as+ // literal text, the same rule the inline image path applies.const imageMatch = line.match(/^!\[([^\]]*)\]\(([^)]+)\)/);- if (imageMatch && line.trim() === imageMatch[0]) {+ if (+ imageMatch &&+ line.trim() === imageMatch[0] &&+ isSafeUrl(imageMatch[2])+ ) {pushBlock({type: 'image', alt: imageMatch[1], src: imageMatch[2]});index++;continue;