Skip to content

Commit 695ce3a

Browse files
authored
Merge pull request DSpace#1850 from atmire/use-x-forwarded-for-redirect
Use values from x-forwarded headers in getOrigin server side
2 parents 77c38ad + 941e71a commit 695ce3a

7 files changed

Lines changed: 36 additions & 14 deletions

File tree

server.ts

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

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

src/app/core/services/browser-hard-redirect.service.spec.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@ import { TestBed } from '@angular/core/testing';
22
import { BrowserHardRedirectService } from './browser-hard-redirect.service';
33

44
describe('BrowserHardRedirectService', () => {
5-
const origin = 'https://test-host.com:4000';
6-
const mockLocation = {
7-
href: undefined,
8-
pathname: '/pathname',
9-
search: '/search',
10-
origin
11-
} as Location;
12-
13-
const service: BrowserHardRedirectService = new BrowserHardRedirectService(mockLocation);
5+
let origin: string;
6+
let mockLocation: Location;
7+
let service: BrowserHardRedirectService;
148

159
beforeEach(() => {
10+
origin = 'https://test-host.com:4000';
11+
mockLocation = {
12+
href: undefined,
13+
pathname: '/pathname',
14+
search: '/search',
15+
origin,
16+
replace: (url: string) => {
17+
mockLocation.href = url;
18+
}
19+
} as Location;
20+
spyOn(mockLocation, 'replace');
21+
22+
service = new BrowserHardRedirectService(mockLocation);
23+
1624
TestBed.configureTestingModule({});
1725
});
1826

@@ -28,8 +36,8 @@ describe('BrowserHardRedirectService', () => {
2836
service.redirect(redirect);
2937
});
3038

31-
it('should update the location', () => {
32-
expect(mockLocation.href).toEqual(redirect);
39+
it('should call location.replace with the new url', () => {
40+
expect(mockLocation.replace).toHaveBeenCalledWith(redirect);
3341
});
3442
});
3543

src/app/core/services/browser-hard-redirect.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class BrowserHardRedirectService extends HardRedirectService {
2424
* @param url
2525
*/
2626
redirect(url: string) {
27-
this.location.href = url;
27+
this.location.replace(url);
2828
}
2929

3030
/**

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
@@ -39,7 +39,10 @@ export class DefaultAppConfig implements AppConfig {
3939
rateLimiter: {
4040
windowMs: 1 * 60 * 1000, // 1 minute
4141
max: 500 // limit each IP to 500 requests per windowMs
42-
}
42+
},
43+
44+
// Trust X-FORWARDED-* headers from proxies
45+
useProxies: true,
4346
};
4447

4548
// 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)