Skip to content

Commit

Permalink
fix(CORS): add whitelist for trusted domains
Browse files Browse the repository at this point in the history
  • Loading branch information
g-div committed Oct 6, 2017
1 parent a81e439 commit eddf094
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 5 additions & 2 deletions config.tpl.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"admins": [],
"date_format": "MMMM DD, YYYY",
"port": 3000
}
"port": 3000,
"cors": {
"whitelist": ["http://localhost:8080"]
}
}
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const moment = require('moment');
Expand Down Expand Up @@ -51,8 +52,16 @@ function error(err, request, reply) {
function run(err, res) {
if (err) return console.error(err.message);

// todo: limit cors to trusted domains
app.use(require('cors')());
app.use(cors({
origin: (origin, callback) => {
if (typeof origin === 'undefined' || config.cors.whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
}
}));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

Expand Down

0 comments on commit eddf094

Please sign in to comment.