Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/lib/faucet-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ describe('faucet API', () => {
})

it('allows docs and Vercel origins for CORS', async () => {
const docsResponse = await OPTIONS(requestWithOrigin('https://tempo.xyz'))
const tempoResponse = await OPTIONS(requestWithOrigin('https://tempo.xyz'))
const docsResponse = await OPTIONS(requestWithOrigin('https://docs.tempo.xyz'))
const vercelResponse = await OPTIONS(requestWithOrigin('https://docs-git-branch.vercel.app'))

expect(docsResponse.headers.get('Access-Control-Allow-Origin')).toBe('https://tempo.xyz')
expect(tempoResponse.headers.get('Access-Control-Allow-Origin')).toBe('https://tempo.xyz')
expect(docsResponse.headers.get('Access-Control-Allow-Origin')).toBe('https://docs.tempo.xyz')
expect(vercelResponse.headers.get('Access-Control-Allow-Origin')).toBe(
'https://docs-git-branch.vercel.app',
)
})

it('does not allow arbitrary CORS origins', async () => {
const response = await OPTIONS(requestWithOrigin('https://example.com'))
const prefixResponse = await OPTIONS(requestWithOrigin('https://tempo.xyz.example.com'))

expect(response.headers.get('Access-Control-Allow-Origin')).toBeNull()
expect(prefixResponse.headers.get('Access-Control-Allow-Origin')).toBeNull()
})
})

Expand Down
4 changes: 2 additions & 2 deletions src/pages/_api/api/faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function fund(address: `0x${string}`, headers: Record<string, string>): Pr
}

function cors(origin: string | null): Record<string, string> {
const allowedOrigins = ['https://tempo.xyz']
const allowedOrigins = ['https://tempo.xyz', 'https://docs.tempo.xyz']

if (origin?.includes('vercel.app')) allowedOrigins.push(origin)
if (process.env.NODE_ENV === 'development') allowedOrigins.push('http://localhost:5173')
Expand All @@ -64,7 +64,7 @@ function cors(origin: string | null): Record<string, string> {
'Access-Control-Allow-Headers': 'Content-Type, x-api-token',
}

if (origin && allowedOrigins.some((allowed) => origin.startsWith(allowed)))
if (origin && allowedOrigins.includes(origin))
headers['Access-Control-Allow-Origin'] = origin

return headers
Expand Down
Loading