Skip to content
Myr

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

6 files+9 added−69 removed

modified:deno_dist/middleware/cors/index.ts+4 added-20 removed
modified:deno_dist/utils/url.ts-8 removed
modified:src/middleware/cors/index.test.ts+1 added-1 removed
78 unmodified lines
7980 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 lines
modified:src/middleware/cors/index.ts+4 added-20 removed
1import type { MiddlewareHandler } from '../../hono'2import { getDomainFromURL } from '../../utils/url'23type CORSOptions = {4 origin: string | string[]5 allowMethods?: string[]
23 unmodified lines
29 }3031 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 }3839 // 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-Origin
46 unmodified lines
modified:src/utils/url.test.ts-12 removed
3 unmodified lines
4 getPathFromURL,5 isAbsoluteURL,6 mergePath,7 getQueryStringFromURL,8 getDomainFromURL,8} from './url'910describe('url', () => {11 it('splitPath', () => {
53 unmodified lines
65 expect(path).toBe('/hello')66 path = getPathFromURL('https://example.com/hello/hey/', false)67 expect(path).toBe('/hello/hey')68 })7071 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 })7071 describe('getQueryStringFromURL', () => {72 it('should return strings of query params', () => {
56 unmodified lines
modified:src/utils/url.ts-8 removed
48 unmodified lines
4950 return result51}5253export 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}6053export const getQueryStringFromURL = (url: string): string => {54 const queryIndex = url.indexOf('?')55 const result = queryIndex !== -1 ? url.substring(queryIndex) : ''56 return result
41 unmodified lines