Skip to content

Commit

Permalink
fix: use safe URL for repository icon (#3247)
Browse files Browse the repository at this point in the history
Fixes #3246.
  • Loading branch information
leafty authored Jul 23, 2024
1 parent b5d223a commit e6ba253
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,16 @@ interface RepositoryIconProps {
}

function RepositoryIcon({ className, provider }: RepositoryIconProps) {
if (provider == null) {
const iconUrl = useMemo(
// eslint-disable-next-line spellcheck/spell-checker
() => (provider != null ? safeNewUrl("/favicon.ico", provider) : null),
[provider]
);

if (iconUrl == null) {
return null;
}

// eslint-disable-next-line spellcheck/spell-checker
const iconUrl = new URL("/favicon.ico", provider);
return (
<img
className={className}
Expand Down
7 changes: 5 additions & 2 deletions client/src/utils/helpers/safeNewUrl.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
*/

/** Returns a new URL instance or null if `url` is not a valid URL string. */
export function safeNewUrl(url: string | undefined | null): URL | null {
export function safeNewUrl(
url: URL | string | undefined | null,
base?: URL | string | undefined
): URL | null {
if (url == null) {
return null;
}
try {
return new URL(url);
return new URL(url, base);
} catch (error) {
if (error instanceof TypeError) {
return null;
Expand Down

0 comments on commit e6ba253

Please sign in to comment.