This repository was archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Adds customizable CORS configurations #52
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0f28b48
Add new CORS functionality, confirm original tests still work, update…
TerryMooreII 981aaab
Add cors tests
TerryMooreII 8c99e24
Update readme and allow origin to be passed a function
TerryMooreII 33ad481
Code cleanup, remove semi colons, console log.
TerryMooreII dba641e
Remove extra space
TerryMooreII 0a38bce
Remove extra space
TerryMooreII f69bea6
Remove any type for APIGatewayProxyEvent['headers']
TerryMooreII e506e5a
Updates from PR comments
TerryMooreII 88b011c
Add example to docs, as per code comment
TerryMooreII File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,197 @@ | ||
| import { APIGatewayProxyEvent } from 'aws-lambda' | ||
|
|
||
| type HeaderKeyValue = { | ||
| key: string, | ||
| value: any | ||
| } | ||
|
|
||
| type HeaderObject = Array<HeaderKeyValue> | ||
|
|
||
| type CorsOrigin = string | boolean | RegExp | Array<RegExp | string> | Function| undefined | ||
|
|
||
| export interface CorsOptions { | ||
| origin?: CorsOrigin | ||
| methods?: string | string[] | ||
| allowedHeaders?: string | string[] | ||
| exposedHeaders?: string | string[] | ||
| maxAge?: number | ||
| credentials?: boolean | ||
| } | ||
|
|
||
| const defaults = { | ||
| origin: '*', | ||
| methods: 'GET,POST,PUT,DELETE,HEAD,PATCH', | ||
| allowedHeaders: 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token' | ||
| } | ||
|
|
||
| function isString(s: any) { | ||
| return typeof s === 'string' || s instanceof String | ||
| } | ||
|
|
||
| const isOriginAllowed = (origin: string, allowedOrigin: CorsOrigin): boolean => { | ||
| if (Array.isArray(allowedOrigin)) { | ||
| for (var i = 0; i < allowedOrigin.length; ++i) { | ||
| if (isOriginAllowed(origin, allowedOrigin[i])) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } else if (isString(allowedOrigin)) { | ||
| return origin === allowedOrigin | ||
| } else if (allowedOrigin instanceof RegExp) { | ||
| return allowedOrigin.test(origin) | ||
| } else { | ||
| return !!allowedOrigin | ||
| } | ||
| } | ||
|
|
||
| const configureOrigin = (options: CorsOptions, event: APIGatewayProxyEvent): HeaderObject => { | ||
| const { origin } = options | ||
| const headers: HeaderObject = [] | ||
|
|
||
| if (origin === true || origin === '*') { | ||
| headers.push({ | ||
| key: 'Access-Control-Allow-Origin', | ||
| value: '*' | ||
| }) | ||
| } else if(isString(origin)) { | ||
| headers.push({ | ||
| key: 'Access-Control-Allow-Origin', | ||
| value: origin | ||
| }, { | ||
| key: 'Vary', | ||
| value: 'Origin' | ||
| }) | ||
| } else if(typeof origin === 'function') { | ||
| headers.push({ | ||
| key: 'Access-Control-Allow-Origin', | ||
| value: origin(event) | ||
| }, { | ||
| key: 'Vary', | ||
| value: 'Origin' | ||
| }) | ||
| } else { | ||
| const requestOrigin: string = event.headers.origin | ||
| const isAllowed: boolean = isOriginAllowed(requestOrigin, origin) | ||
|
|
||
| headers.push({ | ||
| key: 'Access-Control-Allow-Origin', | ||
| value: isAllowed ? requestOrigin : false | ||
| }, { | ||
| key: 'Vary', | ||
| value: 'Origin' | ||
| }) | ||
| } | ||
|
|
||
| return headers | ||
| } | ||
|
|
||
| const configureMethods = (options: CorsOptions): HeaderObject => { | ||
| const { methods } = options | ||
|
|
||
| return [{ | ||
| key: 'Access-Control-Allow-Methods', | ||
| value: Array.isArray(methods) ? methods.join(',') : methods | ||
| }] | ||
| } | ||
|
|
||
| const configureAllowedHeaders = (options: CorsOptions, event: APIGatewayProxyEvent): HeaderObject => { | ||
| let { allowedHeaders } = options | ||
| const headers = [] | ||
|
|
||
| if (!allowedHeaders) { | ||
| allowedHeaders = event.headers['Access-Control-Request-Headers'] | ||
| headers.push({ | ||
| key: 'Vary', | ||
| value: 'Access-Control-Request-Headers' | ||
| }) | ||
| } else if(Array.isArray(allowedHeaders)) { | ||
| allowedHeaders = allowedHeaders.join(',') | ||
| } | ||
|
|
||
| if(allowedHeaders && allowedHeaders.length) { | ||
| headers.push({ | ||
| key: 'Access-Control-Allow-Headers', | ||
| value: allowedHeaders | ||
| }) | ||
| } | ||
|
|
||
| return headers | ||
| } | ||
|
|
||
| const configureExposedHeaders = (options: CorsOptions): HeaderObject => { | ||
| let { exposedHeaders } = options | ||
|
|
||
| if (!exposedHeaders) { | ||
| return [] | ||
| } else if(Array.isArray(exposedHeaders)){ | ||
| exposedHeaders = exposedHeaders.join(',') | ||
| } | ||
| if (exposedHeaders) { | ||
| return [{ | ||
| key: 'Access-Control-Expose-Headers', | ||
| value: exposedHeaders | ||
| }] | ||
| } | ||
| return [] | ||
| } | ||
|
|
||
|
|
||
| const configureAllowMaxAge = (options: CorsOptions): HeaderObject => { | ||
| const { maxAge } = options | ||
|
|
||
| return !maxAge ? [] : [ | ||
| { | ||
| key: 'Access-Control-Max-Age', | ||
| value: `${maxAge}` | ||
| } | ||
| ] | ||
| } | ||
|
|
||
|
|
||
| const configureCredentials = (options: CorsOptions): HeaderObject => { | ||
| const { credentials } = options | ||
|
|
||
| return credentials === true | ||
| ? [{ | ||
| key: 'Access-Control-Allow-Credentials', | ||
| value: 'true' | ||
| }] : [] | ||
| } | ||
|
|
||
| const generateHeaders = (headersArray: Array<HeaderObject> ) => { | ||
| const vary: string[] = [] | ||
| const headers: any = {} | ||
|
|
||
| headersArray.forEach((header: HeaderObject) => { | ||
| header.forEach((h: HeaderKeyValue) => { | ||
| if (h.key === 'Vary' && h.value) { | ||
| vary.push(h.value) | ||
| } else { | ||
| headers[h.key] = h.value | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| return { | ||
| ...headers, | ||
| ...(vary.length && { 'Vary': vary.join(',') }) | ||
| } | ||
| } | ||
|
|
||
| export const addCorsHeaders = (options: CorsOptions | boolean, event: APIGatewayProxyEvent) => { | ||
| if (options === false) { | ||
| return {} | ||
| } | ||
|
|
||
| const corsOptions = Object.assign({}, defaults, typeof options === 'object' ? options : {}) | ||
|
|
||
| return generateHeaders([ | ||
| configureOrigin(corsOptions, event), | ||
| configureExposedHeaders(corsOptions), | ||
| configureCredentials(corsOptions), | ||
| configureMethods(corsOptions), | ||
| configureAllowedHeaders(corsOptions, event), | ||
| configureAllowMaxAge(corsOptions) | ||
| ]) | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.