Skip to content

Commit 8250e47

Browse files
committed
fix(auth): add timeout handling for oauth fetch requests
1 parent 6ebd300 commit 8250e47

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

apps/dashboard-api/src/controllers/auth.controller.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,35 @@ const clearGithubStateCookie = (res) => {
8787
});
8888
};
8989

90+
const OAUTH_FETCH_TIMEOUT_MS = 10000;
91+
9092
const fetchJson = async (url, options, defaultMessage) => {
91-
const response = await fetch(url, options);
92-
const payload = await response.json().catch(() => null);
93-
94-
if (!response.ok) {
95-
const message =
96-
payload?.error_description ||
97-
payload?.error ||
98-
payload?.message ||
99-
defaultMessage;
100-
throw new Error(message);
101-
}
93+
// Prevent OAuth requests from hanging forever
94+
const controller = new AbortController();
95+
const timeout = setTimeout(() => controller.abort(), OAUTH_FETCH_TIMEOUT_MS);
96+
97+
try {
98+
const response = await fetch(url, { signal: controller.signal, ...options });
99+
const payload = await response.json().catch(() => null);
100+
101+
if (!response.ok) {
102+
const message =
103+
payload?.error_description ||
104+
payload?.error ||
105+
payload?.message ||
106+
defaultMessage;
107+
throw new Error(message);
108+
}
102109

103-
return payload;
110+
return payload;
111+
} catch (err) {
112+
if (err.name === 'AbortError') {
113+
throw new Error('OAuth request timed out.');
114+
}
115+
throw err;
116+
} finally {
117+
clearTimeout(timeout);
118+
}
104119
};
105120

106121
const exchangeGithubCodeForToken = async ({ code, req }) => {

0 commit comments

Comments
 (0)