Skip to content

Commit

Permalink
Merge pull request #272 from etduroch/AllowCrossSiteOption
Browse files Browse the repository at this point in the history
Allow configurable cross site access, needed for client side REST access
  • Loading branch information
skrenek committed Feb 26, 2014
2 parents 3a6b2f5 + a380531 commit 0125cae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
"ignorePaths": {

}
},
"crossSiteAccess": {
"enabled": false,
"authorizedOrigins": {
"http://a.trusted.server": [
"^/_rest/",
"^/another/trusting/route"
]
}
}
},

Expand Down
24 changes: 22 additions & 2 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ exports.getMiddleware = function(options, cb) {

cache.getItemsWait([
"feather-files",
"feather-logger"
"feather-logger",
"feather-options"
], function(err, cacheItems) {
if (err) cb(err); else {
var files = cacheItems["feather-files"];
var logger = cacheItems['feather-logger'];
var appOptions = cacheItems['feather-options'];

router.getRouter(options, function(err, _router) {
if (err) cb(err); else {
Expand All @@ -81,7 +83,25 @@ exports.getMiddleware = function(options, cb) {
var middleware = [
Connect.cookieParser(options.connect.session.secret),
Connect.session(options.connect.session),


// Handle cross site access exceptions
function(req, res, next) {
var crossSiteAccess = appOptions.safeGet('connect.crossSiteAccess');
if (crossSiteAccess.enabled) {
var authorizedRoutes = crossSiteAccess.authorizedOrigins[req.headers.origin];
_.find(authorizedRoutes, function(routeRegex){
var pattern = new RegExp(routeRegex);
if (req.url.match(pattern)){
res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
return true;
}

return false;
});
}
next();
},

// bodyParser handling
function(req, res, next) {

Expand Down

0 comments on commit 0125cae

Please sign in to comment.