Skip to content

Commit

Permalink
Merge pull request #1204 from prezly/feature/dev-18822-blockstream-vu…
Browse files Browse the repository at this point in the history
…lnerability-report-xss-reflected

[DEV-18822] Fix - Validate URL to prevent XSS vulnerability on untrusted param
  • Loading branch information
mohammadxali authored Dec 17, 2024
2 parents 27b2b36 + 749a071 commit 5c2fc33
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions modules/Header/ui/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ export function Header({
}, [props.logoSize, searchParams]);

const mainSiteUrl = useMemo(() => {
const mainSiteUrlPreview = searchParams.get('main_site_url');
const mainSiteUrlPreview = validateUrl(searchParams.get('main_site_url'));

if (mainSiteUrlPreview) {
return new URL(mainSiteUrlPreview);
return mainSiteUrlPreview;
}

if (props.mainSiteUrl) {
return new URL(props.mainSiteUrl);
return validateUrl(props.mainSiteUrl);
}

return null;
Expand Down Expand Up @@ -299,3 +300,22 @@ function humanizeUrl(url: URL) {
const string = url.hostname.replace(/^www\./, '');
return string.charAt(0).toUpperCase() + string.slice(1);
}

function validateUrl(url: string | null) {
if (!url) return null;

try {
const normalizedUrl =
url.startsWith('http://') || url.startsWith('https://') ? url : `https://${url}`;

const parsedUrl = new URL(normalizedUrl);

if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
return null;
}

return parsedUrl;
} catch {
return null;
}
}

0 comments on commit 5c2fc33

Please sign in to comment.