-
-
Notifications
You must be signed in to change notification settings - Fork 279
feat: controller-utils: Add Sign-in-with-Ethereum origin validation #1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
472fa4b
feat: controller-utils: Add SIWE origin validation functionality
legobeat d253c78
rename parseAuthorityParts to parseOriginParts
legobeat 63f9513
fix: make scheme and host comparisons case-insensitive
legobeat aea4892
fix: log and return false instead of throwing on isValidSIWEOrigin er…
legobeat 6f9e009
test: remove dulpicate case
legobeat 3b7cb0a
test: add test for mismatching localhost port
legobeat 79bb1fa
refactor: replace parseOriginParts() with URL()`
legobeat 2a06bc1
refactor: replace parseDomainParts logic with URL()`
legobeat 40e32bd
Merge branch 'main' into siwe-origin-validation
legobeat fe3cdd5
Merge branch 'main' into siwe-origin-validation
legobeat 5833715
Merge branch 'main' into siwe-origin-validation
legobeat 4ab1f02
add comment summary of what's validated and not in isValidSIWEOrigin
legobeat 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,10 +34,108 @@ function msgHexToText(hex: string): string { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * @type WrappedSIWERequest | ||
| * | ||
| * Sign-In With Ethereum (SIWE)(EIP-4361) message with request metadata | ||
| * @property {string} from - Subject account address | ||
| * @property {string} origin - The RFC 3986 originating authority of the signing request, including scheme | ||
| * @property {ParsedMessage} siwe - The data parsed from the message | ||
| */ | ||
| export interface WrappedSIWERequest { | ||
| from: string; | ||
| origin: string; | ||
|
legobeat marked this conversation as resolved.
Outdated
|
||
| siwe: SIWEMessage; | ||
| } | ||
|
|
||
| interface DomainParts { | ||
| username?: string; | ||
| hostname: string; | ||
| port?: string; | ||
| } | ||
|
|
||
| const DEFAULT_PORTS_BY_PROTOCOL = { | ||
| 'http:': '80', | ||
| 'https:': '443', | ||
| } as Record<string, string>; | ||
|
|
||
| /** | ||
| * Parses parts from RFC 3986 authority from EIP-4361 `domain` field. | ||
| * | ||
| * @param domain - input string | ||
| * @param originProtocol - implied protocol from origin | ||
| * @returns parsed parts | ||
| */ | ||
| export const parseDomainParts = ( | ||
| domain: string, | ||
| originProtocol: string, | ||
| ): DomainParts => { | ||
| if (domain.match(/^[^/:]*:\/\//u)) { | ||
| return new URL(domain); | ||
| } | ||
|
legobeat marked this conversation as resolved.
Outdated
|
||
| return new URL(`${originProtocol}//${domain}`); | ||
| }; | ||
|
|
||
| /** | ||
| * Validates origin of a Sign-In With Ethereum (SIWE)(EIP-4361) request. | ||
| * As per spec: | ||
| * hostname must match. | ||
| * port and username must match iff specified. | ||
|
legobeat marked this conversation as resolved.
|
||
| * Protocol binding and full same-origin are currently not performed. | ||
| * | ||
|
digiwand marked this conversation as resolved.
|
||
| * @param req - Signature request | ||
| * @returns true if origin matches domain; false otherwise | ||
| */ | ||
| export const isValidSIWEOrigin = (req: WrappedSIWERequest): boolean => { | ||
| try { | ||
| const { origin, siwe } = req; | ||
|
|
||
| // origin = scheme://[user[:password]@]domain[:port] | ||
| // origin is supplied by environment and must match domain claim in message | ||
| if (!origin || !siwe?.parsedMessage?.domain) { | ||
| return false; | ||
| } | ||
|
|
||
| const originParts = new URL(origin); | ||
| const domainParts = parseDomainParts( | ||
| siwe.parsedMessage.domain, | ||
| originParts.protocol, | ||
| ); | ||
|
|
||
| if ( | ||
| domainParts.hostname.localeCompare(originParts.hostname, undefined, { | ||
| sensitivity: 'accent', | ||
| }) !== 0 | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| if (domainParts.port !== '' && domainParts.port !== originParts.port) { | ||
| // If origin port is not specified, protocol default is implied | ||
| return ( | ||
| originParts.port === '' && | ||
| domainParts.port === DEFAULT_PORTS_BY_PROTOCOL[originParts.protocol] | ||
| ); | ||
| } | ||
|
|
||
| if ( | ||
| domainParts.username !== '' && | ||
| domainParts.username !== originParts.username | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } catch (e) { | ||
| log(e); | ||
| return false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * A locally defined object used to provide data to identify a Sign-In With Ethereum (SIWE)(EIP-4361) message and provide the parsed message | ||
| * | ||
| * @typedef localSIWEObject | ||
| * @typedef SIWEMessage | ||
| * @param {boolean} isSIWEMessage - Does the intercepted message conform to the SIWE specification? | ||
| * @param {ParsedMessage} parsedMessage - The data parsed out of the message | ||
| */ | ||
|
|
||
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.