Fix sanitize URLs to prevent stored XSS in website#9537
Open
michaelstaib wants to merge 1 commit intomainfrom
Open
Fix sanitize URLs to prevent stored XSS in website#9537michaelstaib wants to merge 1 commit intomainfrom
michaelstaib wants to merge 1 commit intomainfrom
Conversation
| <TeaserImage> | ||
| <img | ||
| src={latestBlogPost.featuredImage} | ||
| src={sanitizeImageSrc(latestBlogPost.featuredImage)} |
|
|
||
| return isHash ? ( | ||
| <a href={to} {...rest}> | ||
| <a href={safeUrl} {...rest}> |
| ) : internal ? ( | ||
| prefetch === false ? ( | ||
| <a href={to} {...rest}> | ||
| <a href={safeUrl} {...rest}> |
| </a> | ||
| ) : ( | ||
| <NextLink href={to} {...rest}> | ||
| <NextLink href={safeUrl} {...rest}> |
| ) | ||
| ) : ( | ||
| <a href={to} target="_blank" rel="noopener noreferrer" {...rest}> | ||
| <a href={safeUrl} target="_blank" rel="noopener noreferrer" {...rest}> |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds URL sanitization utilities to reduce the risk of stored XSS through unsafe link/image protocols, and applies them in shared UI components.
Changes:
- Introduce
sanitizeUrlandsanitizeImageSrcinurl-helpers.tswith protocol allowlists. - Use
sanitizeUrlin the sharedLinkcomponent so rendered anchors don’t allow dangerous protocols. - Use
sanitizeImageSrcfor the latest blog post teaser image in the site header.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
website/src/utils/url-helpers.ts |
Adds URL/image-src sanitization helpers based on protocol allowlists. |
website/src/components/misc/link.tsx |
Routes all link targets through sanitizeUrl before rendering. |
website/src/components/layout/site/header.tsx |
Sanitizes the blog teaser <img src> value via sanitizeImageSrc. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+45
to
+63
| export function sanitizeImageSrc(src: string): string { | ||
| const trimmed = src.trim(); | ||
|
|
||
| if (trimmed === "" || trimmed.startsWith("/")) { | ||
| return trimmed; | ||
| } | ||
|
|
||
| try { | ||
| const parsed = new URL(trimmed, "http://placeholder.invalid"); | ||
|
|
||
| if (SAFE_IMG_PROTOCOLS.has(parsed.protocol)) { | ||
| return trimmed; | ||
| } | ||
| } catch { | ||
| // Malformed URL — reject. | ||
| } | ||
|
|
||
| return ""; | ||
| } |
Comment on lines
488
to
492
| {latestBlogPost.featuredImage && ( | ||
| <TeaserImage> | ||
| <img | ||
| src={latestBlogPost.featuredImage} | ||
| src={sanitizeImageSrc(latestBlogPost.featuredImage)} | ||
| alt={latestBlogPost.title} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.