Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(index): allow cors to be disabled at route level #332

Merged
merged 2 commits into from
Feb 22, 2025
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ fastify.register(async function (fastify) {
fastify.listen({ port: 3000 })
```

### Disabling CORS for a specific route

CORS can be disabled at the route level by setting the `cors` option to `false`.

```js
const fastify = require('fastify')()

fastify.register(require('@fastify/cors'), { origin: '*' })

fastify.get('/cors-enabled', (_req, reply) => {
reply.send('CORS headers')
})

fastify.get('/cors-disabled', { cors: false }, (_req, reply) => {
reply.send('No CORS headers')
})

fastify.listen({ port: 3000 })
```

### Custom Fastify hook name

By default, `@fastify/cors` adds a `onRequest` hook where the validation and header injection are executed. This can be customized by passing `hook` in the options. Valid values are `onRequest`, `preParsing`, `preValidation`, `preHandler`, `preSerialization`, and `onSend`.
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ function addCorsHeadersHandler (fastify, options, req, reply, next) {
return next()
}

// Allow routes to disable CORS individually
if (req.routeOptions.config?.cors === false) {
return next()
}

// Falsy values are invalid
if (!resolvedOriginOption) {
return next(new Error('Invalid CORS origin option'))
Expand Down
35 changes: 35 additions & 0 deletions test/cors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1015,3 +1015,38 @@ test('Should support wildcard config /2', async t => {
t.assert.strictEqual(res.payload, 'ok')
t.assert.strictEqual(res.headers['access-control-allow-origin'], '*')
})

test('Should allow routes to disable CORS individually', async t => {
t.plan(6)

const fastify = Fastify()
fastify.register(cors, { origin: '*' })

fastify.get('/cors-enabled', (_req, reply) => {
reply.send('ok')
})

fastify.get('/cors-disabled', { config: { cors: false } }, (_req, reply) => {
reply.send('ok')
})

// Test CORS enabled route
let res = await fastify.inject({
method: 'GET',
url: '/cors-enabled',
headers: { origin: 'example.com' }
})
t.assert.ok(res)
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.headers['access-control-allow-origin'], '*')

// Test CORS disabled route
res = await fastify.inject({
method: 'GET',
url: '/cors-disabled',
headers: { origin: 'example.com' }
})
t.assert.ok(res)
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.headers['access-control-allow-origin'], undefined)
})