Check in Origin header instead of Referer (#532)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* refactor(cors): Check in Origin header instead of Referer. * refactor(utils/url): Remove unused function getDomainFromURL. * chore: denoify
authored by Taku Amano · committed by GitHub · parent df64870 · Browse files · 6 files·+9 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+9 added−69 removed
- tests:+1 added−13 removed
- other:+8 added−56 removed
Provenance
- Commit
bbb9558- Tree
64aa2ab- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
df64870
6 files changed+9 added−69 removed
modified:deno_dist/middleware/cors/index.ts+4 added-20 removed
12234523 unmodified lines2930313234353637383940414243444546474849505152333435363738394046 unmodified linesimport type { MiddlewareHandler } from '../../hono.ts'import { getDomainFromURL } from '../../utils/url.ts'
type CORSOptions = { origin: string | string[] allowMethods?: string[]23 unmodified lines }
if (typeof opts.origin === 'string') { set('Access-Control-Allow-Origin', opts.origin) } else { const length = opts.origin.length if (length) { let origin = opts.origin[0] const referer = c.req.headers.get('referer') if (referer) { const domain = getDomainFromURL(referer) if (domain) { for (let i = 0; i < length; i++) { const optDomain = getDomainFromURL(opts.origin[i]) if (optDomain && optDomain === domain) { origin = opts.origin[i] break } } } } set('Access-Control-Allow-Origin', 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) }
// 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:deno_dist/utils/url.ts-8 removed
48 unmodified lines4950515253545556575859605354555641 unmodified lines48 unmodified lines
return result}
export const getDomainFromURL = (url: string): string | null => { const match = url.match(/^https?:\/\/([a-zA-Z0-9\-\.:]+)(:?\/|\#)?/) if (match) { return match[1] } return null}
export const getQueryStringFromURL = (url: string): string => { const queryIndex = url.indexOf('?') const result = queryIndex !== -1 ? url.substring(queryIndex) : '' return result41 unmodified linesmodified:src/middleware/cors/index.test.ts+1 added-1 removed
78 unmodified lines79 80 it('Allow multiple origins', async () => {81 let req = new Request('http://localhost/api3/abc', {82 headers: {83- Referer: 'http://example.org/',83+ Origin: 'http://example.org',84 },85 })86 let res = await app.request(req)87 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.org')14 unmodified linesmodified:src/middleware/cors/index.ts+4 added-20 removed
1 import type { MiddlewareHandler } from '../../hono'2-import { getDomainFromURL } from '../../utils/url'2 3 type CORSOptions = {4 origin: string | string[]5 allowMethods?: string[]23 unmodified lines29 }30 31 if (typeof opts.origin === 'string') {32 set('Access-Control-Allow-Origin', opts.origin)34- } else {35- const length = opts.origin.length36- if (length) {37- let origin = opts.origin[0]38- const referer = c.req.headers.get('referer')39- if (referer) {40- const domain = getDomainFromURL(referer)41- if (domain) {42- for (let i = 0; i < length; i++) {43- const optDomain = getDomainFromURL(opts.origin[i])44- if (optDomain && optDomain === domain) {45- origin = opts.origin[i]46- break47- }48- }49- }50- }51- set('Access-Control-Allow-Origin', origin)52- }33+ } else if (opts.origin.length > 0) {34+ const origin = c.req.headers.get('origin')35+ const allowedOrigin = origin && opts.origin.includes(origin) ? origin : opts.origin[0]36+ set('Access-Control-Allow-Origin', allowedOrigin)37 }38 39 // Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard).40 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin46 unmodified linesmodified:src/utils/url.test.ts-12 removed
3 unmodified lines4 getPathFromURL,5 isAbsoluteURL,6 mergePath,7 getQueryStringFromURL,8- getDomainFromURL,8 } from './url'9 10 describe('url', () => {11 it('splitPath', () => {53 unmodified lines65 expect(path).toBe('/hello')66 path = getPathFromURL('https://example.com/hello/hey/', false)67 expect(path).toBe('/hello/hey')68 })70-71- it('getDomainFromURL', () => {72- let domain = getDomainFromURL('https://example.com/hello/')73- expect(domain).toBe('example.com')74- domain = getDomainFromURL('https://example.com/')75- expect(domain).toBe('example.com')76- domain = getDomainFromURL('https://example.com')77- expect(domain).toBe('example.com')78- domain = getDomainFromURL('https://example.com#fragment')79- expect(domain).toBe('example.com')80- })69 })70 71 describe('getQueryStringFromURL', () => {72 it('should return strings of query params', () => {56 unmodified linesmodified:src/utils/url.ts-8 removed
48 unmodified lines49 50 return result51 }52 53-export const getDomainFromURL = (url: string): string | null => {54- const match = url.match(/^https?:\/\/([a-zA-Z0-9\-\.:]+)(:?\/|\#)?/)55- if (match) {56- return match[1]57- }58- return null59-}60-53 export const getQueryStringFromURL = (url: string): string => {54 const queryIndex = url.indexOf('?')55 const result = queryIndex !== -1 ? url.substring(queryIndex) : ''56 return result41 unmodified lines