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

Add debug to cors middleware #234

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var assign = require('object-assign');
var vary = require('vary');
var debug = require('debug')('express:cors');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this should just be the name of the npm module: cors.


var defaults = {
origin: '*',
Expand All @@ -17,50 +18,61 @@
}

function isOriginAllowed(origin, allowedOrigin) {
debug('test of origin %s. AllowedOrigin: %O', origin, allowedOrigin);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can likely simply this entire block by creating a new variable to store the true/false and then have a single debug call at the end with what was checked and the result. This would also fix the issue here where given an array, it will never log isAllowedOrigin false.

if (Array.isArray(allowedOrigin)) {
for (var i = 0; i < allowedOrigin.length; ++i) {
if (isOriginAllowed(origin, allowedOrigin[i])) {
debug('isAllowedOrigin true');
return true;
}
}
return false;
} else if (isString(allowedOrigin)) {
debug('isAllowedOrigin %s', origin === allowedOrigin);
return origin === allowedOrigin;
} else if (allowedOrigin instanceof RegExp) {
debug('isAllowedOrigin %s' , allowedOrigin.test(origin));
return allowedOrigin.test(origin);
} else {
debug('isAllowedOrigin %s',!!allowedOrigin)
return !!allowedOrigin;
}
}

function configureOrigin(options, req) {
debug('Configure Origin');
var requestOrigin = req.headers.origin,
headers = [],
isAllowed;

if (!options.origin || options.origin === '*') {
// allow any origin
debug('added header Access-Control-Allow-Origin *');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than duplicating this on every single header add line, you could just place a single call when the headers are populated to list them.

headers.push([{
key: 'Access-Control-Allow-Origin',
value: '*'
}]);
} else if (isString(options.origin)) {
// fixed origin
debug('added header Access-Control-Allow-Origin %s', options.origin);
headers.push([{
key: 'Access-Control-Allow-Origin',
value: options.origin
}]);
debug('added header Vary Origin');
headers.push([{
key: 'Vary',
value: 'Origin'
}]);
} else {
isAllowed = isOriginAllowed(requestOrigin, options.origin);
// reflect origin
debug('added header Access-Control-Allow-Origin %s', isAllowed ? requestOrigin : false);
headers.push([{
key: 'Access-Control-Allow-Origin',
value: isAllowed ? requestOrigin : false
}]);
debug('added header Vary Origin');
headers.push([{
key: 'Vary',
value: 'Origin'
Expand All @@ -71,18 +83,22 @@
}

function configureMethods(options) {
debug('Configure methods')
var methods = options.methods;
if (methods.join) {
methods = options.methods.join(','); // .methods is an array, so turn it into a string
}
debug('added header Access-Control-Allow-Methods %O', methods);
return {
key: 'Access-Control-Allow-Methods',
value: methods
};
}

function configureCredentials(options) {
debug('Configure Credentials')
if (options.credentials === true) {
debug('added header Access-Control-Allow-Credentials true')
return {
key: 'Access-Control-Allow-Credentials',
value: 'true'
Expand All @@ -92,11 +108,13 @@
}

function configureAllowedHeaders(options, req) {
debug('Configure Allowed Headers')
var allowedHeaders = options.allowedHeaders || options.headers;
var headers = [];

if (!allowedHeaders) {
allowedHeaders = req.headers['access-control-request-headers']; // .headers wasn't specified, so reflect the request headers
debug('added header Vary Access-Control-Request-Headers')
headers.push([{
key: 'Vary',
value: 'Access-Control-Request-Headers'
Expand All @@ -105,6 +123,7 @@
allowedHeaders = allowedHeaders.join(','); // .headers is an array, so turn it into a string
}
if (allowedHeaders && allowedHeaders.length) {
debug('added header Access-Control-Allow-Headers %s', allowedHeaders);
headers.push([{
key: 'Access-Control-Allow-Headers',
value: allowedHeaders
Expand All @@ -115,13 +134,15 @@
}

function configureExposedHeaders(options) {
debug('Configure Exposed Headers')
var headers = options.exposedHeaders;
if (!headers) {
return null;
} else if (headers.join) {
headers = headers.join(','); // .headers is an array, so turn it into a string
}
if (headers && headers.length) {
debug('added header Access-Control-Expose-Headers %s', headers);
return {
key: 'Access-Control-Expose-Headers',
value: headers
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"repository": "expressjs/cors",
"main": "./lib/index.js",
"dependencies": {
"debug": "^4.3.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would set this to the same version express uses, or at least one compatible with the version range of this module.

"object-assign": "^4",
"vary": "^1"
},
Expand Down