Skip to content

Commit

Permalink
Allow use of ALLOWED_CORS_DESTINATIONS env var
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Dec 6, 2019
1 parent 8631d2e commit 647d24d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ if (module.hot) {
}
```

Configure the allowed_cors_destinations in config.settings
Configure the allowed_cors_destinations in config.settings or provide a system
environment variable named ALLOWED_CORS_DESTINATIONS, which is a list of comma
separated hostnames.
15 changes: 12 additions & 3 deletions src/server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@ const locales = {
};

const server = express();
const allowed_cors_destinations = settings.allowed_cors_destinations || [];
const env_destinations = String.split(
',',
process.env.ALLOWED_CORS_DESTINATIONS || '',
)
.map(s => String.trim(s))
.filter(s => s.length > 0);

const allowed_cors_destinations = [
...(settings.allowed_cors_destinations || []),
...env_destinations,
];

server
.disable('x-powered-by')
Expand All @@ -56,11 +66,10 @@ server
.all('/*', function(req, res, next) {
const match = req.path.match(/\/cors-proxy\/(.*)/);
if (match && match.length === 2) {
// console.log('CORS method on path', req.path);

const targetURL = match[1];
const parsed = url.parse(targetURL);

// TODO: use regex matching
if (allowed_cors_destinations.indexOf(parsed.host) === -1) {
res.set({
'Cache-Control': 'public, max-age=60, no-transform',
Expand Down

0 comments on commit 647d24d

Please sign in to comment.