Skip to content

Commit ceb5ef2

Browse files
authored
Merge pull request #244 from NIAEFEUP/feature/setup-heroku-netlify
Allow requests with cors specified in a list of regexes or urls and setup heroku
2 parents 2ddec2e + 342c7e5 commit ceb5ef2

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

.env

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ TEST_LOG_REQUESTS=false
3232
# Default admin credentials - ALSO OVERRIDE IN PRODUCTION
3333
ADMIN_EMAIL=ni@aefeup.pt
3434
ADMIN_PASSWORD=n1j0bs_ftw.12345
35-
#CORS allowed origin - OVERRIDE IN PRODUCTION
36-
ACCESS_CONTROL_ALLOW_ORIGIN=
35+
36+
# List of regexes or url's specifying allowed origins. Example:
37+
# ACCESS_CONTROL_ALLOW_ORIGINS=["https:\\/\\/deploy-preview-\\d+--nijobs\\.netlify\\.app", "https://nijobs.netlify.app"]
38+
ACCESS_CONTROL_ALLOW_ORIGINS=
3739

3840
# Mail service information. If you don't provide a MAIL_FROM, no emails will be sent. The app will execute no-ops and won't crash
3941
# However, if you want to send emails, you need to fill all of the following 2 fields
@@ -52,4 +54,4 @@ CLOUDINARY_URL=
5254
WEBSERVER_HOST=https://localhost:8087
5355

5456
# Path to save file uploads, the path must be relative to the root of the project - Defaults to static
55-
UPLOAD_FOLDER=
57+
UPLOAD_FOLDER=

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ Made with ❤️ by NIAEFEUP.
2727
To start developing, please check the [documentation](https://nijobs-docs.netlify.app/intro/getting-started) on how to configure your local development.
2828

2929
## License
30+
3031
[GNU General Public License v3.0](https://choosealicense.com/licenses/gpl-3.0/)

documentation/docs/intro/getting-started.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ To start developing, copy `.env` to `.env.local` (by running `cp .env .env.local
3434

3535
Then, you can override the variable's values, according to their explanation in `.env`.
3636

37+
Whenever the file is updated, the server needs to be restarted for changes to have effect.
38+
39+
40+
### Cors Setup
41+
42+
In order to allow requests from multiple clients, an array of URL's or regexes can be defined by `ACCESS_CONTROL_ALLOW_ORIGINS`. This is an important step regarding connecting the frontend and the backend of the project. It is crucial that all URL's have no trailing `/`. Example:
43+
44+
```
45+
ACCESS_CONTROL_ALLOW_ORIGINS=["https:\/\/localhost:3000", "https:\\/\\/deploy-preview-\\d+--nijobs\\.netlify\\.app", "https://nijobs.netlify.app"]
46+
```
3747

3848
## Usage
3949

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"type": "module",
77
"scripts": {
88
"start": "nodemon src/index.js",
9+
"heroku-start": "cp .env.public.heroku .env && node src/index.js",
910
"prod": "NODE_ENV=production node src/index.js",
1011
"lint": "eslint src test --max-warnings 0",
1112
"lint-fix": "npm run lint -- --fix",

src/config/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default Object.freeze({
2828
test_log_requests: JSON.parse(process.env.TEST_LOG_REQUESTS),
2929
admin_email: process.env.ADMIN_EMAIL,
3030
admin_password: process.env.ADMIN_PASSWORD,
31-
access_control_allow_origin: process.env.ACCESS_CONTROL_ALLOW_ORIGIN || "*",
31+
access_control_allow_origins: JSON.parse(process.env.ACCESS_CONTROL_ALLOW_ORIGINS || "[]"),
3232

3333
// Mail
3434
mail_from: process.env.MAIL_FROM,

src/loaders/express.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default (app) => {
4949

5050
// Set the API call rate limit only on production
5151
if (process.env.NODE_ENV === "production") {
52-
const api_rate_limiter = new RateLimit({
52+
const api_rate_limiter = RateLimit({
5353
store: new MongoStore({
5454
uri: config.db_uri,
5555
user: config.db_user,
@@ -76,9 +76,22 @@ export default (app) => {
7676
}
7777

7878
// Adding headers (CORS)
79-
app.use((_, res, next) => {
80-
// Allow connections for all origins
81-
res.setHeader("Access-Control-Allow-Origin", config.access_control_allow_origin);
79+
app.use((req, res, next) => {
80+
res.setHeader("Access-Control-Allow-Origin", "null");
81+
82+
// Allow requests from connections specified by the allow list regexes
83+
const origin = req.header("origin");
84+
if (origin) {
85+
const originAllowed = config.access_control_allow_origins.find(
86+
(allowOrigin) => origin.match(new RegExp(`^${allowOrigin}$`, "g")) // Match full string using ^ and $
87+
);
88+
89+
if (originAllowed) {
90+
res.setHeader("Access-Control-Allow-Origin", origin);
91+
}
92+
}
93+
94+
8295
// Allowed request methods
8396
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
8497
// Allowed request headers

0 commit comments

Comments
 (0)