Skip to content

Commit 78af539

Browse files
committed
fix: handle invalid url in oauth modal
1 parent cb41d4b commit 78af539

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

packages/insomnia/src/main/mcp/transport-streamable-http.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,6 @@ const wrappedFetch = async (
283283
fetchFn: authFetchFn,
284284
});
285285
}
286-
// Close the oauth authorization modal after authorization is complete
287-
BrowserWindow.getAllWindows().forEach(window => {
288-
window.webContents.send('hide-oauth-authorization-modal');
289-
});
290286
if (authResult !== 'AUTHORIZED') {
291287
throw new UnauthorizedError();
292288
}
@@ -295,6 +291,10 @@ const wrappedFetch = async (
295291
// Wrap and throw MCPAuthError for better identification, some of the errors thrown by sdk are generic Error which is hard to identify
296292
throw new MCPAuthError(e.message || 'Authentication failed', { cause: e });
297293
} finally {
294+
// Close the oauth authorization modal after authorization is complete
295+
BrowserWindow.getAllWindows().forEach(window => {
296+
window.webContents.send('hide-oauth-authorization-modal');
297+
});
298298
unsubscribe();
299299
}
300300
return await wrappedFetch(url, init, options, calledByAuth);

packages/insomnia/src/ui/components/modals/oauth-authorization-status-modal.tsx

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const OAuthAuthorizationStatusModal: FC = () => {
1515
const [authCodeUrlStr, setAuthCodeUrlStr] = useState<string | undefined>();
1616
const [submitting, setSubmitting] = useState<boolean>(false);
1717
const { submit: redirectToDefaultBrowserSubmit } = useDefaultBrowserRedirectActionFetcher();
18+
const [error, setError] = useState<string | null>(null);
1819

1920
useEffect(() => {
2021
const unsubscribe = window.main.on('show-oauth-authorization-modal', (_, authCodeUrlStr: string) => {
@@ -65,6 +66,7 @@ export const OAuthAuthorizationStatusModal: FC = () => {
6566
} else if (status === 'getting_code') {
6667
modalRef.current?.show();
6768
setSubmitting(false);
69+
setError(null);
6870
}
6971
}, [status]);
7072

@@ -84,7 +86,7 @@ export const OAuthAuthorizationStatusModal: FC = () => {
8486
{status === 'getting_code' && (
8587
<>
8688
<p className="text-[rgba(var(--color-font-rgb),0.8))] text-start">
87-
See your browser to finish authorization, if the browser didnt open automatically, copy and paste this
89+
See your browser to finish authorization, if the browser didn't open automatically, copy and paste this
8890
URL into your browser to authorize.
8991
</p>
9092
<div className="form-control form-control--outlined no-pad-top flex">
@@ -110,29 +112,36 @@ export const OAuthAuthorizationStatusModal: FC = () => {
110112
</p>
111113
<form
112114
onSubmit={e => {
113-
e.preventDefault();
114-
const form = e.currentTarget;
115-
const data = new FormData(form);
115+
try {
116+
e.preventDefault();
117+
const form = e.currentTarget;
118+
const data = new FormData(form);
116119

117-
const url = data.get('url');
118-
invariant(typeof url === 'string', 'Expected code to be a string');
119-
if (url.length === 0) {
120-
return;
121-
}
122-
setSubmitting(true);
123-
const parsedUrl = new URL(url);
124-
const params = Object.fromEntries(parsedUrl.searchParams);
125-
const { encryptedUrl: encryptedRedirectUrl, encryptedKey, iv } = params;
126-
if (encryptedRedirectUrl && encryptedKey && iv) {
120+
const url = data.get('url');
121+
invariant(typeof url === 'string', 'Expected code to be a string');
122+
if (url.length === 0) {
123+
return;
124+
}
125+
setError(null);
126+
setSubmitting(true);
127+
const parsedUrl = new URL(url);
128+
const params = Object.fromEntries(parsedUrl.searchParams);
129+
const { encryptedUrl: encryptedRedirectUrl, encryptedKey, iv } = params;
130+
if (encryptedRedirectUrl && encryptedKey && iv) {
131+
return redirectToDefaultBrowserSubmit({
132+
encryptedRedirectUrl,
133+
encryptedKey,
134+
iv,
135+
});
136+
}
127137
return redirectToDefaultBrowserSubmit({
128-
encryptedRedirectUrl,
129-
encryptedKey,
130-
iv,
138+
redirectUrl: url,
131139
});
140+
} catch (error) {
141+
setError(error instanceof Error ? error.message : String(error));
142+
setSubmitting(false);
143+
return;
132144
}
133-
return redirectToDefaultBrowserSubmit({
134-
redirectUrl: url,
135-
});
136145
}}
137146
>
138147
<div className="form-control form-control--outlined no-pad-top" style={{ display: 'flex' }}>
@@ -151,6 +160,7 @@ export const OAuthAuthorizationStatusModal: FC = () => {
151160
Proceed
152161
</button>
153162
</div>
163+
{error && <p className="text-danger">Error: {error}</p>}
154164
</form>
155165
</>
156166
)}

0 commit comments

Comments
 (0)