Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions client/src/components/AppRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ interface AppRendererProps {
onNotification?: (notification: ServerNotification) => void;
}

const OAUTH_STATE_SESSION_KEY = "oauth_state";

const AppRenderer = ({
sandboxPath,
tool,
Expand Down Expand Up @@ -74,8 +76,22 @@ const AppRenderer = ({
const handleOpenLink = async ({ url }: { url: string }) => {
let isError = true;
if (url.startsWith("https://") || url.startsWith("http://")) {
window.open(url, "_blank");
isError = false;
try {
const nextUrl = new URL(url);
if (nextUrl.searchParams.get("response_type") === "code") {
const stateBytes = new Uint8Array(16);
window.crypto.getRandomValues(stateBytes);
const state = Array.from(stateBytes, (byte) =>
byte.toString(16).padStart(2, "0"),
).join("");
sessionStorage.setItem(OAUTH_STATE_SESSION_KEY, state);
nextUrl.searchParams.set("state", state);
}
window.open(nextUrl.toString(), "_blank");
isError = false;
} catch {
isError = true;
}
}
return { isError };
};
Expand Down
9 changes: 9 additions & 0 deletions client/src/components/OAuthCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface OAuthCallbackProps {
onConnect: (serverUrl: string) => void;
}

const OAUTH_STATE_SESSION_KEY = "oauth_state";

const OAuthCallback = ({ onConnect }: OAuthCallbackProps) => {
const { toast } = useToast();
const hasProcessedRef = useRef(false);
Expand All @@ -36,6 +38,13 @@ const OAuthCallback = ({ onConnect }: OAuthCallbackProps) => {
return notifyError(generateOAuthErrorDescription(params));
}

const callbackState = new URLSearchParams(window.location.search).get("state");
const storedState = sessionStorage.getItem(OAUTH_STATE_SESSION_KEY);
if (!callbackState || !storedState || callbackState !== storedState) {
return notifyError("Invalid OAuth state");
}
sessionStorage.removeItem(OAUTH_STATE_SESSION_KEY);

const serverUrl = sessionStorage.getItem(SESSION_KEYS.SERVER_URL);
if (!serverUrl) {
return notifyError("Missing Server URL");
Expand Down