feat(cors): Enable to check origin header by a function. (#533)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Taku Amano · committed by GitHub · parent bbb9558 · Browse files · 3 files·+60 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+60 added−14 removed
- tests:+32 added−0 removed
- other:+28 added−14 removed
Provenance
- Commit
8eb2021- Tree
a3e4745- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
bbb9558
3 files changed+60 added−14 removed
modified:deno_dist/middleware/cors/index.ts+14 added-7 removed
12344567811 unmodified lines2021222324252627282930313233343536373839403132333435364142434445464746 unmodified linesimport type { MiddlewareHandler } from '../../hono.ts'
type CORSOptions = { origin: string | string[] origin: string | string[] | ((origin: string) => string | undefined | null) allowMethods?: string[] allowHeaders?: string[] maxAge?: number credentials?: boolean11 unmodified lines ...defaults, ...options, }
const findAllowOrigin = ((optsOrigin) => { if (typeof optsOrigin === 'string') { return () => optsOrigin } else if (typeof optsOrigin === 'function') { return optsOrigin } else { return (origin: string) => (optsOrigin.includes(origin) ? origin : optsOrigin[0]) } })(opts.origin)
return async (c, next) => { await next()
function set(key: string, value: string) { c.res.headers.append(key, value) }
if (typeof opts.origin === 'string') { set('Access-Control-Allow-Origin', opts.origin) } else if (opts.origin.length > 0) { const origin = c.req.headers.get('origin') const allowedOrigin = origin && opts.origin.includes(origin) ? origin : opts.origin[0] set('Access-Control-Allow-Origin', allowedOrigin) const allowOrigin = findAllowOrigin(c.req.headers.get('origin') || '') if (allowOrigin) { set('Access-Control-Allow-Origin', allowOrigin) }
// Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard). // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin46 unmodified linesmodified:src/middleware/cors/index.test.ts+32 added
22 unmodified lines23 origin: ['http://example.com', 'http://example.org', 'http://example.dev'],24 })25 )26 27+ app.use(28+ '/api4/*',29+ cors({30+ origin: (origin) => (origin.endsWith('.example.com') ? origin : 'http://example.com'),31+ })32+ )33+34 app.all('/api/abc', (c) => {35 return c.json({ success: true })36 })37 app.all('/api2/abc', (c) => {1 unmodified line39 })40 app.all('/api3/abc', (c) => {41 return c.json({ success: true })42 })43+ app.all('/api4/abc', (c) => {44+ return c.json({ success: true })45+ })46 47 it('GET default', async () => {48 const res = await app.request('http://localhost/api/abc')49 57 unmodified lines107 })108 res = await app.request(req)109 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')110 })111+112+ it('Allow origins by function', async () => {113+ let req = new Request('http://localhost/api4/abc', {114+ headers: {115+ Origin: 'http://subdomain.example.com',116+ },117+ })118+ let res = await app.request(req)119+ expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://subdomain.example.com')120+121+ req = new Request('http://localhost/api4/abc')122+ res = await app.request(req)123+ expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')124+125+ req = new Request('http://localhost/api4/abc', {126+ headers: {127+ Referer: 'http://evil-example.com/',128+ },129+ })130+ res = await app.request(req)131+ expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')132+ })133 })modified:src/middleware/cors/index.ts+14 added-7 removed
12344567811 unmodified lines2021222324252627282930313233343536373839403132333435364142434445464746 unmodified linesimport type { MiddlewareHandler } from '../../hono'
type CORSOptions = { origin: string | string[] origin: string | string[] | ((origin: string) => string | undefined | null) allowMethods?: string[] allowHeaders?: string[] maxAge?: number credentials?: boolean11 unmodified lines ...defaults, ...options, }
const findAllowOrigin = ((optsOrigin) => { if (typeof optsOrigin === 'string') { return () => optsOrigin } else if (typeof optsOrigin === 'function') { return optsOrigin } else { return (origin: string) => (optsOrigin.includes(origin) ? origin : optsOrigin[0]) } })(opts.origin)
return async (c, next) => { await next()
function set(key: string, value: string) { c.res.headers.append(key, value) }
if (typeof opts.origin === 'string') { set('Access-Control-Allow-Origin', opts.origin) } else if (opts.origin.length > 0) { const origin = c.req.headers.get('origin') const allowedOrigin = origin && opts.origin.includes(origin) ? origin : opts.origin[0] set('Access-Control-Allow-Origin', allowedOrigin) const allowOrigin = findAllowOrigin(c.req.headers.get('origin') || '') if (allowOrigin) { set('Access-Control-Allow-Origin', allowOrigin) }
// Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard). // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin46 unmodified lines