This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
fix: handle non-url origins #623
Open
0xEdouardEth
wants to merge
4
commits 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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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,19 +1,51 @@ | ||
| import { METAMASK_ORIGIN } from '../constants/solana'; | ||
| import { | ||
| KNOWN_ORIGIN_LABELS, | ||
| WALLET_CONNECT_ORIGIN, | ||
|
Check warning on line 3 in packages/snap/src/core/utils/parseOrigin.ts
|
||
| } from '../constants/solana'; | ||
|
Comment on lines
+1
to
+4
|
||
|
|
||
| /** | ||
| * Parses the origin from a string. | ||
| * Parses the origin into a human-readable display label. | ||
| * | ||
| * @param origin - The origin to parse. | ||
| * @returns The parsed origin. | ||
| * @returns The display label: a known-origin label, the hostname for http(s) URLs. | ||
| */ | ||
| export function parseOrigin(origin: string) { | ||
| if (origin === METAMASK_ORIGIN) { | ||
| return 'MetaMask'; | ||
| const knownLabel = KNOWN_ORIGIN_LABELS[origin?.toLowerCase()]; | ||
| if (knownLabel) { | ||
| return knownLabel; | ||
| } | ||
|
|
||
| try { | ||
| return new URL(origin).hostname; | ||
| const url = new URL(origin); | ||
| if (!isHttpOrHttpsUrl(url)) { | ||
| throw new Error('Invalid url'); | ||
| } | ||
| return url.hostname; | ||
| } catch (error) { | ||
| throw new Error('Invalid URL'); | ||
| throw new Error( | ||
| `Invalid origin: ${origin}. Must be a valid URL or a known origin.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether an origin is a known origin. | ||
| * | ||
| * @param origin - The origin to check. | ||
| * @returns Whether the origin is a known origin. | ||
| */ | ||
| export function isKnownOrigin(origin: string | undefined): boolean { | ||
| return [...Object.keys(KNOWN_ORIGIN_LABELS)].includes( | ||
|
Check warning on line 38 in packages/snap/src/core/utils/parseOrigin.ts
|
||
| origin?.toLowerCase() ?? '', | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * 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:'; | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.