Skip to content

Commit

Permalink
A-C-Expose-Headers: dont include the 7 CORS-safelisted response-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
bulk88 committed Sep 30, 2020
1 parent b453f56 commit 455cdb6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/cors-anywhere.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ function withCORS(headers, request) {
if (corsMaxAge) {
headers['access-control-max-age'] = corsMaxAge;
}
}
else {
headers['access-control-expose-headers'] = Object.keys(headers).join(',');
} else {
headers['access-control-expose-headers'] = Object.keys(headers).filter(
function(header){
if (header.match(/^cache-control|content-language|content-length|content-type|expires|last-modified|pragma$/)) {
return false;
} else {
return header;
}
}).join(',');
}

headers['access-control-allow-origin'] = '*';
Expand Down
10 changes: 9 additions & 1 deletion test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ function echoheaders(origin) {

nock('http://example.com')
.persist()
// replyContentLength() has no effect unless response body is JSON and will get
// stringified, use defaultReplyHeaders() instead
// https://github.com/nock/nock/blob/626021770b9b2fa52860c19f6b7a6033d64125e3/lib/interceptor.js#L176
.defaultReplyHeaders({
'Content-Length': function (req, res, body) {return body.length;},
})
.get('/')
.reply(200, 'Response from example.com')
.reply(200, 'Response from example.com', {
'Content-Type': 'text/plain',
})

.post('/echopost')
.reply(200, function(uri, requestBody) {
Expand Down
3 changes: 3 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ describe('Basic functionality', function() {
.get('/example.com')
.expect('Access-Control-Allow-Origin', '*')
.expect('x-request-url', 'http://example.com/')
.expect('Content-Length', '25')
.expectHeaderNotMatch('access-control-expose-headers', /content-length/)
.expectHeaderNotMatch('access-control-expose-headers', /content-type/)
.expectHeaderNotMatch('access-control-expose-headers', /access-control-allow-origin/)
.expect(200, 'Response from example.com', done);
});
Expand Down

0 comments on commit 455cdb6

Please sign in to comment.