This repository was archived by the owner on Jul 31, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
fix: handle non-url origins #340
Open
0xEdouardEth
wants to merge
1
commit into
main
Choose a base branch
from
fix/validate-origin-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+46
−22
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,49 @@ | ||
| /** | ||
| * Maps known non-URL origins to their display labels (case-insensitive lookup). | ||
| * In-app requests use 'MetaMask' (capitalized), so the lookup is case-insensitive. | ||
| */ | ||
| const KNOWN_ORIGIN_LABELS: Record<string, string> = { | ||
| metamask: 'MetaMask', | ||
| 'wallet-connect': 'WalletConnect', | ||
| }; | ||
|
|
||
| /** | ||
| * Formats an origin for display purposes. | ||
| * | ||
| * Returns 'Unknown' for undefined/empty origins. Returns a friendly label for | ||
| * known origins ('metamask' to 'MetaMask', 'wallet-connect' to 'WalletConnect'), | ||
| * matched case-insensitively. Returns the hostname for http(s) URLs. Returns an | ||
| * empty string for everything else (channelIds, non-http URLs, invalid strings) | ||
| * so display guards `{origin ? (...) : null}` hide the row. | ||
| * | ||
| * @param origin - The origin string to format (e.g., 'metamask', 'https://example.com'). | ||
| * @returns The formatted origin string (e.g., 'MetaMask', 'example.com'). | ||
| * @returns The formatted origin string (e.g., 'MetaMask', 'example.com', ''). | ||
| */ | ||
| export function formatOrigin(origin: string | undefined): string { | ||
| if (!origin) { | ||
| return 'Unknown'; | ||
| } | ||
|
|
||
| // Special case: format 'metamask' as 'MetaMask' (case-insensitive) | ||
| if (origin.toLowerCase() === 'metamask') { | ||
| return 'MetaMask'; | ||
| const knownLabel = KNOWN_ORIGIN_LABELS[origin.toLowerCase()]; | ||
| if (knownLabel) { | ||
| return knownLabel; | ||
| } | ||
|
|
||
| // Try to extract hostname from URL | ||
| try { | ||
| return new URL(origin).hostname; | ||
| const url = new URL(origin); | ||
| return isHttpOrHttpsUrl(url) ? url.hostname : ''; | ||
| } catch { | ||
| // If not a valid URL, return the original value | ||
| // This shouldn't happen if validation is working correctly | ||
| return origin; | ||
| return ''; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a parsed URL uses an HTTP(S) protocol. | ||
| * | ||
| * @param url - The parsed URL to check. | ||
| * @returns Whether the URL uses HTTP or HTTPS. | ||
| */ | ||
| function isHttpOrHttpsUrl(url: URL): boolean { | ||
| return url.protocol === 'http:' || url.protocol === 'https:'; | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unknown origin shows blank label
Medium Severity
Sign-transaction confirmation sets missing origins to the literal
'Unknown', then runsformatOriginon that value. After this change,formatOriginreturns an empty string for non-URL strings, so the origin row still renders (the prop is truthy) but the value is blank instead ofUnknown.Reviewed by Cursor Bugbot for commit 548af0c. Configure here.