fix(studio): early fail query when x-connection-encrypted is invalid

This commit is contained in:
avallete committed 2025-04-28 15:19:07 +02:00
1 parent 36d3847f74
commit 93d8567695
2 files changed
+23 -4

No files matched your search

+19 -2
View File
@@ -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)
},
},
{
@@ -46,9 +46,11 @@ export const useProjectDetailQuery = <TData = ProjectDetailData>(
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
}