Skip to content

Commit

Permalink
feat(snap-keyring-bridge): sanitize redirect URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ccharly committed Oct 11, 2024
1 parent 1a7356e commit 3ed870e
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/keyring-snap-bridge/src/SnapKeyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,32 @@ export class SnapKeyring extends EventEmitter {
redirect: { message?: string; url?: string },
snapId: SnapId,
): Promise<void> {
const { message = '', url = '' } = redirect;
const { message = '', url: redirectUrl = '' } = redirect;
const url = this.#sanitizeRedirectUrl(redirectUrl);
if (url) {
this.#validateRedirectUrl(url, snapId);
}
await this.#callbacks.redirectUser(snapId, url, message);
}

/**
* Sanitize a redirect URL.
*
* @param url - The URL to sanitize.
* @returns The new sanitized redirect URL.
*/
#sanitizeRedirectUrl(url: string): string {
// We do check for this case since the Snap might not returns any URL at all.
if (!url) {
return url; // Nothing to sanitize in this case.
}

// For now, we only re-create the URL object which should take care of most of the sanitizing, like replacing
// upper-cased letters by their lower-cased counterparts in the "hostname" part.
const redirectUrl = new URL(url);
return redirectUrl.toString();
}

/**
* Validates if the redirect URL is in the Snap's allowed origins.
*
Expand Down

0 comments on commit 3ed870e

Please sign in to comment.