Skip to content

Commit

Permalink
origin matching should be case insensitive
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Sthamer <[email protected]>
  • Loading branch information
P4sca1 committed Aug 9, 2024
1 parent 2151b7d commit 6a04128
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/runtime/server/middleware/corsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default defineEventHandler((event) => {
}

if (origin && origin !== '*' && corsHandler.useRegExp) {
origin = origin.map((o) => new RegExp(o))
origin = origin.map((o) => new RegExp(o, 'i'))
}

handleCors(event, {
Expand Down
5 changes: 5 additions & 0 deletions test/cors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ describe('[nuxt-security] CORS', async () => {
expect(res.headers.get('Access-Control-Allow-Origin')).toBeNull()
})

it('should match origins with regular expressions in a case-insensitive way', async () => {
const res = await fetch('/regexp-single', { headers: { origin: 'https://A.EXAMPLE.COM' } })
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://A.EXAMPLE.COM')
})

it('should support multiple regular expressions', async () => {
let res = await fetch('/regexp-multi', { headers: { origin: 'https://a.example.com' } })
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://a.example.com')
Expand Down

0 comments on commit 6a04128

Please sign in to comment.