Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle Chrome CORS preflight private network header #308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ app.listen(80, function () {
* `maxAge`: Configures the **Access-Control-Max-Age** CORS header. Set to an integer to pass the header, otherwise it is omitted.
* `preflightContinue`: Pass the CORS preflight response to the next handler.
* `optionsSuccessStatus`: Provides a status code to use for successful `OPTIONS` requests, since some legacy browsers (IE11, various SmartTVs) choke on `204`.
* `allowPrivateNetwork`: Provides **Access-Control-Allow-Private-Network: true** if **Access-Control-Request-Private-Network: true** is presented.

The default configuration is the equivalent of:

Expand Down
14 changes: 14 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@
}
}

function configureAllowPrivateNetwork(options, req) {
if (
options.allowPrivateNetwork &&
req.headers['access-control-request-private-network'] === 'true'
) {
return {
key: 'Access-Control-Allow-Private-Network',
value: 'true',
};
}
return null;
}

function cors(options, req, res, next) {
var headers = [],
method = req.method && req.method.toUpperCase && req.method.toUpperCase();
Expand All @@ -168,6 +181,7 @@
headers.push(configureAllowedHeaders(options, req));
headers.push(configureMaxAge(options))
headers.push(configureExposedHeaders(options))
headers.push(configureAllowPrivateNetwork(options, req));
applyHeaders(headers, res);

if (options.preflightContinue) {
Expand Down
33 changes: 33 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,39 @@ var util = require('util')
})
});

it('allows private network if explicitly enabled', function (done) {
var cb = after(1, done)
var req = new FakeRequest('OPTIONS', {
'access-control-request-private-network': 'true'
})
var res = new FakeResponse()

res.on('finish', function () {
assert.equal(res.getHeader('Access-Control-Allow-Private-Network'), 'true')
cb()
})

cors({ allowPrivateNetwork: true })(req, res, function (err) {
cb(err || new Error('should not be called'))
})
});


it('not allows private network if explicitly enabled but access-control-request-private-network is missing', function (done) {
var cb = after(1, done)
var req = new FakeRequest('OPTIONS')
var res = new FakeResponse()

res.on('finish', function () {
assert.equal(res.getHeader('Access-Control-Allow-Private-Network'), undefined)
cb()
})

cors({ allowPrivateNetwork: true })(req, res, function (err) {
cb(err || new Error('should not be called'))
})
});

it('does not includes credentials unless explicitly enabled', function (done) {
// arrange
var req, res, next;
Expand Down