Skip to content

Commit 140d602

Browse files
authored
fix: update endpoint for cname check (supabase#37210)
1 parent 1b7137a commit 140d602

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

apps/studio/csp.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const STRIPE_SUBDOMAINS_URL = 'https://*.stripe.com'
4141
const STRIPE_JS_URL = 'https://js.stripe.com'
4242
const STRIPE_NETWORK_URL = 'https://*.stripe.network'
4343
const CLOUDFLARE_URL = 'https://www.cloudflare.com'
44-
const ONE_ONE_ONE_ONE_URL = 'https://one.one.one.one'
4544
const VERCEL_URL = 'https://vercel.com'
4645
const VERCEL_INSIGHTS_URL = 'https://*.vercel-insights.com'
4746
const GITHUB_API_URL = 'https://api.github.com'
@@ -81,7 +80,6 @@ module.exports.getCSP = function getCSP() {
8180
STRIPE_SUBDOMAINS_URL,
8281
STRIPE_NETWORK_URL,
8382
CLOUDFLARE_URL,
84-
ONE_ONE_ONE_ONE_URL,
8583
VERCEL_INSIGHTS_URL,
8684
GITHUB_API_URL,
8785
GITHUB_USER_CONTENT_URL,

apps/studio/data/custom-domains/check-cname-mutation.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useMutation, UseMutationOptions } from '@tanstack/react-query'
2-
import { fetchHandler } from 'data/fetchers'
2+
import { constructHeaders, fetchHandler, handleError } from 'data/fetchers'
3+
import { BASE_PATH } from 'lib/constants'
34
import { toast } from 'sonner'
45

56
import type { ResponseError } from 'types'
@@ -22,19 +23,24 @@ export type CheckCNAMERecordResponse = {
2223

2324
// [Joshen] Should tally with https://github.com/supabase/cli/blob/63790a1bd43bee06f82c4f510e709925526a4daa/internal/utils/api.go#L98
2425
export async function checkCNAMERecord({ domain }: CheckCNAMERecordVariables) {
25-
const res = await fetchHandler(`https://one.one.one.one/dns-query?name=${domain}&type=CNAME`, {
26-
method: 'GET',
27-
headers: { accept: 'application/dns-json' },
28-
})
29-
const verification = (await res.json()) as CheckCNAMERecordResponse
26+
try {
27+
const headers = await constructHeaders({ 'Content-Type': 'application/json' })
28+
const res: CheckCNAMERecordResponse = await fetchHandler(`${BASE_PATH}/api/check-cname`, {
29+
headers,
30+
method: 'POST',
31+
body: JSON.stringify({ domain }),
32+
}).then((res) => res.json())
3033

31-
if (verification.Answer === undefined) {
32-
throw new Error(
33-
`Your CNAME record for ${domain} cannot be found - if you've just added the record, do check back in a bit.`
34-
)
35-
}
34+
if (res.Answer === undefined) {
35+
throw new Error(
36+
`Your CNAME record for ${domain} cannot be found - if you've just added the record, do check back in a bit.`
37+
)
38+
}
3639

37-
return verification.Answer.some((x) => x.type === 5)
40+
return res.Answer.some((x) => x.type === 5)
41+
} catch (error) {
42+
handleError(error)
43+
}
3844
}
3945

4046
type CheckCNAMERecordData = Awaited<ReturnType<typeof checkCNAMERecord>>

apps/studio/data/fetchers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { API_URL } from 'lib/constants'
66
import { getAccessToken } from 'lib/gotrue'
77
import { uuidv4 } from 'lib/helpers'
88
import { ResponseError } from 'types'
9-
import type { paths } from './api' // generated from openapi-typescript
9+
// generated from openapi-typescript
10+
import type { paths } from './api'
1011

1112
const DEFAULT_HEADERS = { Accept: 'application/json' }
1213

@@ -15,6 +16,7 @@ export const fetchHandler: typeof fetch = async (input, init) => {
1516
return await fetch(input, init)
1617
} catch (err: any) {
1718
if (err instanceof TypeError && err.message === 'Failed to fetch') {
19+
console.error(err)
1820
throw new Error('Unable to reach the server. Please check your network or try again later.')
1921
}
2022
throw err

apps/studio/middleware.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const HOSTED_SUPPORTED_API_URLS = [
2424
'/ai/feedback/classify',
2525
'/get-ip-address',
2626
'/get-utc-time',
27+
'/check-cname',
2728
'/edge-functions/test',
2829
'/edge-functions/body',
2930
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { CheckCNAMERecordResponse } from 'data/custom-domains/check-cname-mutation'
2+
import { NextApiRequest, NextApiResponse } from 'next'
3+
4+
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
5+
const { domain } = req.body
6+
7+
try {
8+
const result: CheckCNAMERecordResponse = await fetch(
9+
`https://cloudflare-dns.com/dns-query?name=${domain}&type=CNAME`,
10+
{
11+
method: 'GET',
12+
headers: { Accept: 'application/dns-json' },
13+
}
14+
).then((res) => res.json())
15+
return res.status(200).json(result)
16+
} catch (error: any) {
17+
return res.status(400).json({ message: error.message })
18+
}
19+
}
20+
21+
export default handler

0 commit comments

Comments
 (0)