-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #509 from P4sca1/fix/regexp-origin
feat: support using regular expressions as CORS origin
- Loading branch information
Showing
14 changed files
with
208 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { fileURLToPath } from 'node:url' | ||
import { setup, fetch } from '@nuxt/test-utils' | ||
|
||
describe('[nuxt-security] CORS', async () => { | ||
await setup({ | ||
rootDir: fileURLToPath(new URL('./fixtures/cors', import.meta.url)), | ||
}) | ||
|
||
it ('should allow requests from serverUrl by default', async () => { | ||
const res = await fetch('/', { headers: { origin: 'http://localhost:3000' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://localhost:3000') | ||
}) | ||
|
||
it ('should block requests from other origins by default', async () => { | ||
const res = await fetch('/', { headers: { origin: 'http://example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBeNull() | ||
}) | ||
|
||
it('should allow requests from all origins when * is set', async () => { | ||
let res = await fetch('/star', { headers: { origin: 'http://example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*') | ||
|
||
res = await fetch('/star', { headers: { origin: 'http://a.b.c.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*') | ||
}) | ||
|
||
it('should allow requests if origin matches', async () => { | ||
const res = await fetch('/single', { headers: { origin: 'https://example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://example.com') | ||
}) | ||
|
||
it('should block requests when origin does not match', async () => { | ||
const res = await fetch('/single', { headers: { origin: 'https://foo.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBeNull() | ||
}) | ||
|
||
it('should support multiple origins', async () => { | ||
let res = await fetch('/multi', { headers: { origin: 'https://a.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://a.example.com') | ||
|
||
res = await fetch('/multi', { headers: { origin: 'https://b.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://b.example.com') | ||
|
||
res = await fetch('/multi', { headers: { origin: 'https://c.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBeNull() | ||
}) | ||
|
||
it('should support regular expressions', async () => { | ||
let res = await fetch('/regexp-single', { headers: { origin: 'https://a.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://a.example.com') | ||
|
||
res = await fetch('/regexp-single', { headers: { origin: 'https://b.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://b.example.com') | ||
|
||
res = await fetch('/regexp-single', { headers: { origin: 'https://c.example.com' } }) | ||
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') | ||
|
||
res = await fetch('/regexp-multi', { headers: { origin: 'https://b.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://b.example.com') | ||
|
||
res = await fetch('/regexp-multi', { headers: { origin: 'https://c.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBeNull() | ||
|
||
res = await fetch('/regexp-multi', { headers: { origin: 'https://c.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBeNull() | ||
|
||
res = await fetch('/regexp-multi', { headers: { origin: 'https://foo.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBeNull() | ||
|
||
res = await fetch('/regexp-multi', { headers: { origin: 'https://1.foo.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://1.foo.example.com') | ||
|
||
res = await fetch('/regexp-multi', { headers: { origin: 'https://a.b.c.foo.example.com' } }) | ||
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('https://a.b.c.foo.example.com') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
imports.autoImport=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<template> | ||
<div> | ||
<NuxtPage /> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
export default defineNuxtConfig({ | ||
modules: [ | ||
'../../../src/module' | ||
], | ||
security: { | ||
}, | ||
routeRules: { | ||
'/empty': { | ||
security: { | ||
corsHandler: { | ||
origin: '' | ||
} | ||
} | ||
}, | ||
'/star': { | ||
security: { | ||
corsHandler: { | ||
origin: '*' | ||
} | ||
} | ||
}, | ||
'/single': { | ||
security: { | ||
corsHandler: { | ||
origin: 'https://example.com' | ||
} | ||
} | ||
}, | ||
'/multi': { | ||
security: { | ||
corsHandler: { | ||
origin: ['https://a.example.com', 'https://b.example.com'] | ||
} | ||
} | ||
}, | ||
'/regexp-single': { | ||
security: { | ||
corsHandler: { | ||
// eslint-disable-next-line no-useless-escape -- This is parsed as a regular expression, so the escape is required. | ||
origin: '(a|b)\\.example\\.com', | ||
useRegExp: true | ||
} | ||
} | ||
}, | ||
'/regexp-multi': { | ||
security: { | ||
corsHandler: { | ||
// eslint-disable-next-line no-useless-escape -- This is parsed as a regular expression, so the escape is required. | ||
origin: ['(a|b)\.example\.com', '(.*)\\.foo.example\\.com'], | ||
useRegExp: true | ||
} | ||
} | ||
}, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"private": true, | ||
"name": "basic", | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div>basic</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div>multi</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div>regexp-multi</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div>regexp-single</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div>single</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div>star</div> | ||
</template> |