From 93d85676950bb68845bbf412e561293e63ceba06 Mon Sep 17 00:00:00 2001 From: avallete Date: Mon, 28 Apr 2025 15:19:07 +0200 Subject: [PATCH] fix(studio): early fail query when x-connection-encrypted is invalid --- apps/studio/data/fetchers.ts | 21 +++++++++++++++++-- .../data/projects/project-detail-query.ts | 6 ++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/apps/studio/data/fetchers.ts b/apps/studio/data/fetchers.ts index b9e42e1097c..0ccd907f1b1 100644 --- a/apps/studio/data/fetchers.ts +++ b/apps/studio/data/fetchers.ts @@ -40,6 +40,24 @@ export async function constructHeaders(headersInit?: HeadersInit | undefined) { return headers } +function pgMetaGuard(request: Request) { + // Only check for /platform/pg-meta/ endpoints + if (request.url.includes('/platform/pg-meta/')) { + // If there is no valid `x-connection-encrypted`, pg-meta will necesseraly fail to connect to the target database + // in such case, we save the hops and throw a 421 response instead + if (!request.headers.has('x-connection-encrypted')) { + // TODO: Maybe here add a sentry warning to monitor how often this happen + // Simulate a 421 response by throwing an error + throw { + code: 421, + message: 'x-connection-encrypted header is required for /platform/pg-meta/ requests', + requestId: request.headers.get('X-Request-Id'), + } + } + } + return request +} + // Middleware client.use( { @@ -47,8 +65,7 @@ client.use( async onRequest({ request }) { const headers = await constructHeaders() headers.forEach((value, key) => request.headers.set(key, value)) - - return request + return pgMetaGuard(request) }, }, { diff --git a/apps/studio/data/projects/project-detail-query.ts b/apps/studio/data/projects/project-detail-query.ts index 67df9283bc2..ce14e256fdf 100644 --- a/apps/studio/data/projects/project-detail-query.ts +++ b/apps/studio/data/projects/project-detail-query.ts @@ -46,9 +46,11 @@ export const useProjectDetailQuery = ( enabled: enabled && typeof ref !== 'undefined', staleTime: 30 * 1000, // 30 seconds refetchInterval(data) { - const status = data && (data as unknown as ProjectDetailData).status + const result = data && (data as unknown as ProjectDetailData) + const status = result && result.status + const connectionString = result && result.connectionString - if (status === 'COMING_UP' || status === 'UNKNOWN') { + if (status === 'COMING_UP' || status === 'UNKNOWN' || !connectionString) { return 5 * 1000 // 5 seconds }