Skip to content
Merged
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
57 changes: 36 additions & 21 deletions web/src/components/console/utils/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,33 @@ export const ExternalLink: React.FC<ExternalLinkProps> = ({
additionalClassName = '',
dataTestID,
stopPropagation,
}) => (
<Button
variant="link"
component="a"
icon={
<Icon size="sm">
<ExternalLinkAltIcon />
</Icon>
}
className={additionalClassName}
href={href}
target="_blank"
iconPosition="end"
rel="noopener noreferrer"
data-test-id={dataTestID}
{...(stopPropagation ? { onClick: (e) => e.stopPropagation() } : {})}
isInline
>
{children || text}
</Button>
);
}) => {
if (!isSafeExternalURL(href)) {
return <>{children || text}</>;
}

return (
<Button
variant="link"
component="a"
icon={
<Icon size="sm">
<ExternalLinkAltIcon />
</Icon>
}
className={additionalClassName}
href={href}
target="_blank"
iconPosition="end"
rel="noopener noreferrer"
data-test-id={dataTestID}
{...(stopPropagation ? { onClick: (e) => e.stopPropagation() } : {})}
isInline
>
{children || text}
</Button>
);
};

// Open links in a new window and set noopener/noreferrer.
export const LinkifyExternal: React.FC<{ children: React.ReactNode }> = ({ children }) => (
Expand All @@ -45,3 +51,12 @@ type ExternalLinkProps = {
dataTestID?: string;
stopPropagation?: boolean;
};

const isSafeExternalURL = (value: string): value is string => {
if (!URL.canParse(value)) {
return false;
}

const { protocol } = new URL(value);
return protocol === 'http:' || protocol === 'https:';
};