Skip to content

Commit

Permalink
fix: Vary: Origin header overwritten (#1407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum authored Jun 24, 2024
1 parent dc0fd2e commit ebbc85b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/real-bananas-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/server': patch
---

Vary: Access-Control-Request-Headers would overwrite Vary: Origin
3 changes: 2 additions & 1 deletion packages/server/src/plugins/useCors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ export function getCORSHeadersByRequestAndOptions(
headers['Access-Control-Allow-Headers'] = requestHeaders;
if (headers['Vary']) {
headers['Vary'] += ', Access-Control-Request-Headers';
} else {
headers['Vary'] = 'Access-Control-Request-Headers';
}
headers['Vary'] = 'Access-Control-Request-Headers';
}
}

Expand Down
17 changes: 17 additions & 0 deletions packages/server/test/useCors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,22 @@ describe('CORS', () => {
expect(headers?.['Access-Control-Allow-Origin']).toBeUndefined();
});
});
describe('Vary header', () => {
const corsOptionsWithMultipleOrigins: CORSOptions = {
origin: ['http://localhost:4000', 'http://localhost:4001'],
};
it('should return vary with multiple values', () => {
const request = new Request('http://localhost:4002/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
origin: 'http://localhost:4001',
'access-control-request-headers': 'x-foobar',
},
});
const headers = getCORSHeadersByRequestAndOptions(request, corsOptionsWithMultipleOrigins);
expect(headers?.Vary).toBe('Origin, Access-Control-Request-Headers');
});
});
});
});

0 comments on commit ebbc85b

Please sign in to comment.