expressjs / expressjs/cors

Option preflightContinue not working with origin function

Open
#293 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
6.2k
Forks
512
PR merge metrics
No merged PRs in 30d

Description

When origin has a function logic it prevents to be overwritten on a next() by another cors policy when preflightContinue is set true.

Example:

```js
const allowedOrigins = [`${config.frontendUrl}`];
// enable cors
app.use(cors({
origin: function(origin, callback){
if(!origin) return callback(null, true);
if(allowedOrigins.indexOf(origin) === -1){
const msg = 'The CORS policy for this site does not ' +
'allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
},
credentials: true,
// Allow follow-up middleware to override this CORS for options
preflightContinue: true,
}));
```

This should be applied everywhere exept if redefined like

```js
const corsOptions = {
origin: true,
credentials: true
}
router.post('/test', cors(corsOptions), (req, res) => {});
```

In this example /test will always be blocked by cors policy, if instead I use origin: allowedOrigins (Array value) it works. Any ideas?

#40

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.