diff --git a/apps/studio/csp.js b/apps/studio/csp.js index f4ddabfcc76..775afd742fa 100644 --- a/apps/studio/csp.js +++ b/apps/studio/csp.js @@ -41,7 +41,6 @@ const STRIPE_SUBDOMAINS_URL = 'https://*.stripe.com' const STRIPE_JS_URL = 'https://js.stripe.com' const STRIPE_NETWORK_URL = 'https://*.stripe.network' const CLOUDFLARE_URL = 'https://www.cloudflare.com' -const ONE_ONE_ONE_ONE_URL = 'https://one.one.one.one' const VERCEL_URL = 'https://vercel.com' const VERCEL_INSIGHTS_URL = 'https://*.vercel-insights.com' const GITHUB_API_URL = 'https://api.github.com' @@ -81,7 +80,6 @@ module.exports.getCSP = function getCSP() { STRIPE_SUBDOMAINS_URL, STRIPE_NETWORK_URL, CLOUDFLARE_URL, - ONE_ONE_ONE_ONE_URL, VERCEL_INSIGHTS_URL, GITHUB_API_URL, GITHUB_USER_CONTENT_URL, diff --git a/apps/studio/data/custom-domains/check-cname-mutation.ts b/apps/studio/data/custom-domains/check-cname-mutation.ts index 97c59858cb5..dd8c2003e05 100644 --- a/apps/studio/data/custom-domains/check-cname-mutation.ts +++ b/apps/studio/data/custom-domains/check-cname-mutation.ts @@ -1,5 +1,6 @@ import { useMutation, UseMutationOptions } from '@tanstack/react-query' -import { fetchHandler } from 'data/fetchers' +import { constructHeaders, fetchHandler, handleError } from 'data/fetchers' +import { BASE_PATH } from 'lib/constants' import { toast } from 'sonner' import type { ResponseError } from 'types' @@ -22,19 +23,24 @@ export type CheckCNAMERecordResponse = { // [Joshen] Should tally with https://github.com/supabase/cli/blob/63790a1bd43bee06f82c4f510e709925526a4daa/internal/utils/api.go#L98 export async function checkCNAMERecord({ domain }: CheckCNAMERecordVariables) { - const res = await fetchHandler(`https://one.one.one.one/dns-query?name=${domain}&type=CNAME`, { - method: 'GET', - headers: { accept: 'application/dns-json' }, - }) - const verification = (await res.json()) as CheckCNAMERecordResponse + try { + const headers = await constructHeaders({ 'Content-Type': 'application/json' }) + const res: CheckCNAMERecordResponse = await fetchHandler(`${BASE_PATH}/api/check-cname`, { + headers, + method: 'POST', + body: JSON.stringify({ domain }), + }).then((res) => res.json()) - if (verification.Answer === undefined) { - throw new Error( - `Your CNAME record for ${domain} cannot be found - if you've just added the record, do check back in a bit.` - ) + if (res.Answer === undefined) { + throw new Error( + `Your CNAME record for ${domain} cannot be found - if you've just added the record, do check back in a bit.` + ) + } + + return res.Answer.some((x) => x.type === 5) + } catch (error) { + handleError(error) } - - return verification.Answer.some((x) => x.type === 5) } type CheckCNAMERecordData = Awaited> diff --git a/apps/studio/data/fetchers.ts b/apps/studio/data/fetchers.ts index 9c369ee5504..b2c3d4aaa6f 100644 --- a/apps/studio/data/fetchers.ts +++ b/apps/studio/data/fetchers.ts @@ -6,7 +6,8 @@ import { API_URL } from 'lib/constants' import { getAccessToken } from 'lib/gotrue' import { uuidv4 } from 'lib/helpers' import { ResponseError } from 'types' -import type { paths } from './api' // generated from openapi-typescript +// generated from openapi-typescript +import type { paths } from './api' const DEFAULT_HEADERS = { Accept: 'application/json' } @@ -15,6 +16,7 @@ export const fetchHandler: typeof fetch = async (input, init) => { return await fetch(input, init) } catch (err: any) { if (err instanceof TypeError && err.message === 'Failed to fetch') { + console.error(err) throw new Error('Unable to reach the server. Please check your network or try again later.') } throw err diff --git a/apps/studio/middleware.ts b/apps/studio/middleware.ts index 37f16b6a41d..122e48a481d 100644 --- a/apps/studio/middleware.ts +++ b/apps/studio/middleware.ts @@ -24,6 +24,7 @@ const HOSTED_SUPPORTED_API_URLS = [ '/ai/feedback/classify', '/get-ip-address', '/get-utc-time', + '/check-cname', '/edge-functions/test', '/edge-functions/body', ] diff --git a/apps/studio/pages/api/check-cname.ts b/apps/studio/pages/api/check-cname.ts new file mode 100644 index 00000000000..276491bcd2a --- /dev/null +++ b/apps/studio/pages/api/check-cname.ts @@ -0,0 +1,21 @@ +import { CheckCNAMERecordResponse } from 'data/custom-domains/check-cname-mutation' +import { NextApiRequest, NextApiResponse } from 'next' + +const handler = async (req: NextApiRequest, res: NextApiResponse) => { + const { domain } = req.body + + try { + const result: CheckCNAMERecordResponse = await fetch( + `https://cloudflare-dns.com/dns-query?name=${domain}&type=CNAME`, + { + method: 'GET', + headers: { Accept: 'application/dns-json' }, + } + ).then((res) => res.json()) + return res.status(200).json(result) + } catch (error: any) { + return res.status(400).json({ message: error.message }) + } +} + +export default handler