Skip to content

Commit

Permalink
fix: don't allow credentials when all origins allowed (#47)
Browse files Browse the repository at this point in the history
* fix(security): don't set access-control-allow-credentials header when access-control-allow-origin = *

* docs: fix error in sample usage

`access-control-allow-origin` header cannot return multiple origins.
Middleware doesn't do that either.
  • Loading branch information
domeq committed Aug 26, 2021
1 parent d3eed32 commit 19a37d1
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -49,7 +49,7 @@ handler({}, {}, (_, response) => {
expect(response).toEqual({
statusCode: 200,
headers: {
'access-control-allow-origin': 'https://www.vg.no, https://www.tek.no',
'access-control-allow-origin': 'https://www.vg.no',
},
body: JSON.stringify({ foo: 'bar' }),
})
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -22,7 +22,7 @@ const getCorsHeaders = (
}
}

if (credentials !== undefined) {
if (credentials && !R.includes('*', allowedOrigins)) {
headers['access-control-allow-credentials'] = credentials;
}

Expand Down
2 changes: 0 additions & 2 deletions index.test.js
Expand Up @@ -139,7 +139,6 @@ test('Adds CORS headers on success when all origins allowed', async () => {
expect(response).toEqual({
statusCode: 200,
headers: {
'access-control-allow-credentials': true,
'access-control-allow-headers': 'Content-Type, Accept, X-Forwarded-For',
'access-control-allow-methods': 'GET, POST',
'access-control-allow-origin': 'https://www.tek.no',
Expand Down Expand Up @@ -251,7 +250,6 @@ test('Adds CORS headers on error when all origins allowed', async () => {
)
).resolves.toMatchObject({
headers: {
'access-control-allow-credentials': true,
'access-control-allow-headers': 'Content-Type, Accept, X-Forwarded-For',
'access-control-allow-methods': 'GET, POST',
'access-control-allow-origin': 'https://www.tek.no',
Expand Down

0 comments on commit 19a37d1

Please sign in to comment.