0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

feat: custom protocol header (#2156)

This commit is contained in:
Juan Picado 2021-04-02 10:48:51 +02:00 committed by GitHub
parent 5b1aa87b76
commit 9c803993d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View file

@ -33,3 +33,11 @@ url_prefix: '/second_prefix'
// url -> https://somedomain.org/second_prefix/' // url -> https://somedomain.org/second_prefix/'
``` ```
#### VERDACCIO_FORWARDED_PROTO
The default header to identify the protocol is `X-Forwarded-Proto`, but there are some environments which [uses something different](https://github.com/verdaccio/verdaccio/issues/990), to change it use the variable `VERDACCIO_FORWARDED_PROTO`
```
$ VERDACCIO_FORWARDED_PROTO=CloudFront-Forwarded-Proto verdaccio --listen 5000
```

View file

@ -641,7 +641,8 @@ export function getPublicUrl(url_prefix: string = '', req): string {
if (!isHost(host)) { if (!isHost(host)) {
throw new Error('invalid host'); throw new Error('invalid host');
} }
const protocol = getWebProtocol(req.get(HEADERS.FORWARDED_PROTO), req.protocol); const protoHeader = process.env.VERDACCIO_FORWARDED_PROTO ?? HEADERS.FORWARDED_PROTO;
const protocol = getWebProtocol(req.get(protoHeader), req.protocol);
const combinedUrl = combineBaseUrl(protocol, host, url_prefix); const combinedUrl = combineBaseUrl(protocol, host, url_prefix);
debug('public url by request %o', combinedUrl); debug('public url by request %o', combinedUrl);
return combinedUrl; return combinedUrl;

View file

@ -871,5 +871,21 @@ describe('Utilities', () => {
expect(getPublicUrl(undefined, req)).toEqual('http://some/'); expect(getPublicUrl(undefined, req)).toEqual('http://some/');
delete process.env.VERDACCIO_PUBLIC_URL; delete process.env.VERDACCIO_PUBLIC_URL;
}); });
test('with the VERDACCIO_FORWARDED_PROTO to override valid X-Forwarded-Proto https', () => {
process.env.VERDACCIO_FORWARDED_PROTO = 'http';
const req = httpMocks.createRequest({
method: 'GET',
headers: {
host: 'some.com',
'CloudFront-Forwarded-Proto': 'http',
[HEADERS.FORWARDED_PROTO]: 'https',
},
url: '/',
});
expect(getPublicUrl(undefined, req)).toEqual('http://some.com/');
delete process.env.VERDACCIO_FORWARDED_PROTO;
});
}); });
}); });