Skip to content

Commit

Permalink
refactor: callback uri cannot be cartridge.gg
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Aug 25, 2024
1 parent dc3e018 commit 6d4c56f
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/keychain/src/pages/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function CreateRemoteSession() {
// POST request. If the request is successful, then redirect to the
// success page. Else, redirect to the failure page.
const onCallback = useCallback(() => {
const url = new URL(decodeURIComponent(queries.callback_uri));
const url = decodeURIComponent(queries.callback_uri);
const session = controller.account.sessionJson();
if (!url || !session) {
router.replace(`/failure`);
Expand All @@ -45,7 +45,7 @@ export default function CreateRemoteSession() {
const headers = new Headers();
headers.append("Content-Type", "application/json");

fetch(url, {
fetch(sanitizeCallbackUrl(url), {
body: JSON.stringify({
username: controller.username,
credentials: {
Expand Down Expand Up @@ -121,3 +121,20 @@ export default function CreateRemoteSession() {
<CreateSessionComp onConnect={onConnect} />
);
}

/**
* Sanitize the callback url to ensure that it is a valid URL. Returns back the URL.
*/
function sanitizeCallbackUrl(url: string): URL | undefined {
try {
const parsed = new URL(url);

if (parsed.hostname.endsWith("cartridge.gg")) {
throw new Error(`Invalid callback url: ${url}`);
}

return parsed;
} catch (e) {
console.error(e);

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.
}
}

0 comments on commit 6d4c56f

Please sign in to comment.