Skip to content

Commit

Permalink
Hotfix for CORS issue when no Origin header is present.
Browse files Browse the repository at this point in the history
closes #986, #961
  • Loading branch information
sgress454 committed Oct 14, 2013
1 parent a003802 commit f42da3c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/hooks/cors/index.js
Expand Up @@ -79,8 +79,8 @@ module.exports = function(sails) {
routeCorsConfig = {};
}
return function(req, res, next) {
// If we can set headers (i.e. it's not a socket request), do so.
if (res.setHeader) {
// If we have an origin header, and we can set headers (i.e. it's not a socket request), do so.
if (res.setHeader && req.headers && req.headers.origin) {

// Get the allowed origins
var origins = (routeCorsConfig.origin || sails.config.cors.origin).split(',');
Expand Down
6 changes: 5 additions & 1 deletion lib/util/index.js
Expand Up @@ -253,7 +253,11 @@ exports.optional = function wrapOptionalCallback (cb) {

exports.isSameOrigin = function isSameOrigin (req) {
// Get the domain out of the origin header
var domain = req.headers.origin.match(/^https?:\/\/([^:]+)(:\d+)?$/)[1];
var matches = req.headers.origin.match(/^https?:\/\/([^:]+)(:\d+)?$/);
if (matches === null) {
return false;
}
var domain = matches[1];
// Compare it to the host
return (req.host == domain);
};
Expand Down

1 comment on commit f42da3c

@mikermcneil
Copy link
Member

Choose a reason for hiding this comment

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

@sgress454 would you mind merging into #associations? We can go ahead and move the cors config into the hook at the same time (good opt. for you to see how that's changed)

Please sign in to comment.