Skip to content

Commit 964b8bb

Browse files
feat(index): allow cors to be disabled at route level (#332)
1 parent 35ebc65 commit 964b8bb

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ fastify.register(async function (fastify) {
112112
fastify.listen({ port: 3000 })
113113
```
114114

115+
### Disabling CORS for a specific route
116+
117+
CORS can be disabled at the route level by setting the `cors` option to `false`.
118+
119+
```js
120+
const fastify = require('fastify')()
121+
122+
fastify.register(require('@fastify/cors'), { origin: '*' })
123+
124+
fastify.get('/cors-enabled', (_req, reply) => {
125+
reply.send('CORS headers')
126+
})
127+
128+
fastify.get('/cors-disabled', { cors: false }, (_req, reply) => {
129+
reply.send('No CORS headers')
130+
})
131+
132+
fastify.listen({ port: 3000 })
133+
```
134+
115135
### Custom Fastify hook name
116136

117137
By default, `@fastify/cors` adds an `onRequest` hook for validation and header injection. This can be customized by passing `hook` in the options. Valid values are `onRequest`, `preParsing`, `preValidation`, `preHandler`, `preSerialization`, and `onSend`.

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ function addCorsHeadersHandler (fastify, options, req, reply, next) {
171171
return next()
172172
}
173173

174+
// Allow routes to disable CORS individually
175+
if (req.routeOptions.config?.cors === false) {
176+
return next()
177+
}
178+
174179
// Falsy values are invalid
175180
if (!resolvedOriginOption) {
176181
return next(new Error('Invalid CORS origin option'))

test/cors.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,3 +1015,38 @@ test('Should support wildcard config /2', async t => {
10151015
t.assert.strictEqual(res.payload, 'ok')
10161016
t.assert.strictEqual(res.headers['access-control-allow-origin'], '*')
10171017
})
1018+
1019+
test('Should allow routes to disable CORS individually', async t => {
1020+
t.plan(6)
1021+
1022+
const fastify = Fastify()
1023+
fastify.register(cors, { origin: '*' })
1024+
1025+
fastify.get('/cors-enabled', (_req, reply) => {
1026+
reply.send('ok')
1027+
})
1028+
1029+
fastify.get('/cors-disabled', { config: { cors: false } }, (_req, reply) => {
1030+
reply.send('ok')
1031+
})
1032+
1033+
// Test CORS enabled route
1034+
let res = await fastify.inject({
1035+
method: 'GET',
1036+
url: '/cors-enabled',
1037+
headers: { origin: 'example.com' }
1038+
})
1039+
t.assert.ok(res)
1040+
t.assert.strictEqual(res.statusCode, 200)
1041+
t.assert.strictEqual(res.headers['access-control-allow-origin'], '*')
1042+
1043+
// Test CORS disabled route
1044+
res = await fastify.inject({
1045+
method: 'GET',
1046+
url: '/cors-disabled',
1047+
headers: { origin: 'example.com' }
1048+
})
1049+
t.assert.ok(res)
1050+
t.assert.strictEqual(res.statusCode, 200)
1051+
t.assert.strictEqual(res.headers['access-control-allow-origin'], undefined)
1052+
})

0 commit comments

Comments
 (0)