Skip to content

Commit 941e71a

Browse files
committed
add useProxies config to support x-forwarded headers in express
1 parent 241816e commit 941e71a

5 files changed

Lines changed: 16 additions & 2 deletions

File tree

server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export function app() {
7575
*/
7676
const server = express();
7777

78+
// Tell Express to trust X-FORWARDED-* headers from proxies
79+
// See https://expressjs.com/en/guide/behind-proxies.html
80+
server.set('trust proxy', environment.ui.useProxies);
81+
7882
/*
7983
* If production mode is enabled in the environment file:
8084
* - Enable Angular's production mode

src/config/config.util.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('Config Util', () => {
1010
expect(appConfig.cache.msToLive.default).toEqual(15 * 60 * 1000); // 15 minute
1111
expect(appConfig.ui.rateLimiter.windowMs).toEqual(1 * 60 * 1000); // 1 minute
1212
expect(appConfig.ui.rateLimiter.max).toEqual(500);
13+
expect(appConfig.ui.useProxies).toEqual(true);
1314

1415
expect(appConfig.submission.autosave.metadata).toEqual([]);
1516

@@ -25,6 +26,8 @@ describe('Config Util', () => {
2526
};
2627
appConfig.ui.rateLimiter = rateLimiter;
2728

29+
appConfig.ui.useProxies = false;
30+
2831
const autoSaveMetadata = [
2932
'dc.author',
3033
'dc.title'
@@ -44,6 +47,7 @@ describe('Config Util', () => {
4447
expect(environment.cache.msToLive.default).toEqual(msToLive);
4548
expect(environment.ui.rateLimiter.windowMs).toEqual(rateLimiter.windowMs);
4649
expect(environment.ui.rateLimiter.max).toEqual(rateLimiter.max);
50+
expect(environment.ui.useProxies).toEqual(false);
4751
expect(environment.submission.autosave.metadata[0]).toEqual(autoSaveMetadata[0]);
4852
expect(environment.submission.autosave.metadata[1]).toEqual(autoSaveMetadata[1]);
4953

src/config/default-app-config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export class DefaultAppConfig implements AppConfig {
3737
rateLimiter: {
3838
windowMs: 1 * 60 * 1000, // 1 minute
3939
max: 500 // limit each IP to 500 requests per windowMs
40-
}
40+
},
41+
42+
// Trust X-FORWARDED-* headers from proxies
43+
useProxies: true,
4144
};
4245

4346
// The REST API server settings

src/config/ui-server-config.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ export class UIServerConfig extends ServerConfig {
1111
max: number;
1212
};
1313

14+
// Trust X-FORWARDED-* headers from proxies
15+
useProxies: boolean;
1416
}

src/environments/environment.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const environment: BuildConfig = {
2525
rateLimiter: {
2626
windowMs: 1 * 60 * 1000, // 1 minute
2727
max: 500 // limit each IP to 500 requests per windowMs
28-
}
28+
},
29+
useProxies: true,
2930
},
3031

3132
// The REST API server settings.

0 commit comments

Comments
 (0)