Skip to content

Commit

Permalink
add trust proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
youngbryanyu committed Feb 13, 2024
1 parent c0702f0 commit 8b975cf
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
8 changes: 7 additions & 1 deletion backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ REFRESH_TOKEN_SECRET = TODO
PASSWORD_SECRET = TODO

# MongoDB URI (Developer User, Fitnesse Database only)
MONGO_CONNECTION_URL = TODO
MONGO_CONNECTION_URL = TODO

# environment
NODE_ENV = TODO

# IPs for trust proxy
TRUSTED_IPS = TODO
8 changes: 8 additions & 0 deletions backend/.env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ PASSWORD_SECRET = password_secret

# MongoDB URI (Developer User, Fitnesse Database only)
MONGO_CONNECTION_URL = test_url

# environment
NODE_ENV = testing

# IPs for trust proxy
TRUSTED_IPS = ["0.0.0.0"]


3 changes: 2 additions & 1 deletion backend/config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"FAILED_LOGIN_ATTEMPTS_WINDOW": 900,
"ACCESS_TOKEN_LIFETIME": "15m",
"IDLE_TIMEOUT": 604800
}
},
"TRUSTED_IPS": "$TRUSTED_IPS::set:string"
}
3 changes: 2 additions & 1 deletion backend/config/testing.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"FAILED_LOGIN_ATTEMPTS_WINDOW": 5,
"ACCESS_TOKEN_LIFETIME": "1d",
"IDLE_TIMEOUT": 5000
}
},
"TRUSTED_IPS": "$TRUSTED_IPS::set:string"
}
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"scripts": {
"build": "pnpm install && tsc",
"start": "node dist/index.js",
"dev": "nodemon src/index.ts --env=development",
"dev": "nodemon src/index.ts",
"lint": "eslint .",
"test": "jest --verbose",
"prettier": "prettier --config .prettierrc 'src/**/*.ts' 'tests/**/*.ts' --write"
Expand Down
11 changes: 11 additions & 0 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class App {
this.expressApp = express();
this.initializeMiddleWares();
this.mountRoutes();
this.setNetworkConfigs();
this.serverPool = new Map<number, http.Server>();
}

Expand All @@ -39,6 +40,16 @@ class App {
this.expressApp.use(helmet());
}

/**
* Set network communication configurations.
*/
private setNetworkConfigs() {
/* Set trust proxy IPs */
const trustedIps: Set<string> = Config.get('TRUSTED_IPS');
const trustedIpList = Array.from(trustedIps);
this.expressApp.set('trust proxy', trustedIpList);
}

/**
* Mounts the routes for the backend API endpoints.
*/
Expand Down
2 changes: 0 additions & 2 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import App from './app';
import logger from './logging/logger';
import Config from 'simple-app-config';
logger.info('starting the app');
logger.debug('debug');

/* Get the server port from configuration object */
const PORT: number = Config.get('PORT');
Expand Down
15 changes: 9 additions & 6 deletions backend/tests/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ describe('App Tests', () => {
jest.spyOn(mongoose, 'connect').mockImplementation(() => {
return { close: jest.fn() } as unknown as Promise<typeof import('mongoose')>;
});
jest.spyOn(Config, 'get').mockImplementation(() => {
return 'dummy connection string';
});
jest.spyOn(Config, 'get').mockImplementation(() => {
return 2;
});

jest
.spyOn(Config, 'get')
.mockImplementationOnce(() => {
return 'dummy connection string';
})
.mockImplementationOnce(() => {
return 2;
});

/* Connect to DB */
await appInstance.connectToDatabase();
Expand Down

0 comments on commit 8b975cf

Please sign in to comment.