Skip to content

Commit

Permalink
fix: ensure edge case covered
Browse files Browse the repository at this point in the history
  • Loading branch information
willfarrell committed Nov 3, 2024
1 parent 0a668ae commit 99d893f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
9 changes: 3 additions & 6 deletions packages/http-cors/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ test('Should return default headers when { origin: "*" }', async (t) => {
deepEqual(response, {
statusCode: 204,
headers: {
'Access-Control-Allow-Origin': '*',
Vary: 'Origin'
'Access-Control-Allow-Origin': '*'
}
})
})
Expand Down Expand Up @@ -185,8 +184,7 @@ test('Access-Control-Allow-Origin header should be "*" when origin is "*"', asyn
deepEqual(response, {
statusCode: 204,
headers: {
'Access-Control-Allow-Origin': '*',
Vary: 'Origin'
'Access-Control-Allow-Origin': '*'
}
})
})
Expand Down Expand Up @@ -263,8 +261,7 @@ test('It should return whitelisted origin (any)', async (t) => {
deepEqual(response, {
statusCode: 204,
headers: {
'Access-Control-Allow-Origin': '*',
Vary: 'Origin'
'Access-Control-Allow-Origin': '*'
}
})
})
Expand Down
11 changes: 7 additions & 4 deletions packages/http-cors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const httpCorsMiddleware = (opts = {}) => {
// TODO: IDN -> puncycode not handled, add in if requested
const regExpStr = origin.replaceAll('.', '\\.').replaceAll('*', '[^.]*')
originDynamic.push(new RegExp(`^${regExpStr}$`))
console.log({ originDynamic })
}

const modifyHeaders = (headers, options, request) => {
Expand All @@ -94,10 +93,11 @@ const httpCorsMiddleware = (opts = {}) => {
headers['Access-Control-Allow-Methods'] = options.methods
}

let newOrigin
if (!existingHeaders.includes('Access-Control-Allow-Origin')) {
const eventHeaders = request.event.headers ?? {}
const incomingOrigin = eventHeaders.Origin ?? eventHeaders.origin
const newOrigin = options.getOrigin(incomingOrigin, options)
newOrigin = options.getOrigin(incomingOrigin, options)
if (newOrigin) {
headers['Access-Control-Allow-Origin'] = newOrigin
}
Expand All @@ -107,8 +107,11 @@ const httpCorsMiddleware = (opts = {}) => {
addHeaderPart(headers, 'Vary', options.vary)
}

// #1251
if (originAny || originMany) {
if (
originMany ||
(originAny && newOrigin !== '*') ||
(newOrigin === '*' && options.credentials)
) {
addHeaderPart(headers, 'Vary', 'Origin')
}

Expand Down

0 comments on commit 99d893f

Please sign in to comment.