Skip to content

Commit

Permalink
fix: improve get headers from request #2190 (#2271)
Browse files Browse the repository at this point in the history
* fix: improve request header handling

* chore: fix test

* chore: apply suggestion
  • Loading branch information
juanpicado committed May 26, 2021
1 parent b5ee703 commit 38ca095
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/api/endpoint/api/whoami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { $RequestExtend, $NextFunctionVer } from '../../../../types';

export default function (route: Router): void {
route.get('/whoami', (req: $RequestExtend, res: Response, next: $NextFunctionVer): void => {
if (req.headers.referer === 'whoami') {
if (req.get('referer') === 'whoami') {
next({ username: req.remote_user.name });
} else {
next('route');
Expand Down
8 changes: 4 additions & 4 deletions src/api/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function validatePackage(req: $RequestExtend, res: $ResponseExtend, next:
export function media(expect: string | null): any {
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
if (req.headers[HEADER_TYPE.CONTENT_TYPE] !== expect) {
next(ErrorCode.getCode(HTTP_STATUS.UNSUPPORTED_MEDIA, 'wrong content-type, expect: ' + expect + ', got: ' + req.headers[HEADER_TYPE.CONTENT_TYPE]));
next(ErrorCode.getCode(HTTP_STATUS.UNSUPPORTED_MEDIA, 'wrong content-type, expect: ' + expect + ', got: ' + req.get(HEADER_TYPE.CONTENT_TYPE)));
} else {
next();
}
Expand All @@ -134,7 +134,7 @@ export function expectJson(req: $RequestExtend, res: $ResponseExtend, next: $Nex

export function antiLoop(config: Config): Function {
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
if (req.headers.via != null) {
if (req?.headers?.via != null) {
const arr = req.headers.via.split(',');

for (let i = 0; i < arr.length; i++) {
Expand Down Expand Up @@ -232,7 +232,7 @@ export function log(config: Config) {
req.headers.authorization = '<Classified>';
}

const _cookie = req.headers.cookie;
const _cookie = req.get('cookie');
if (_.isNil(_cookie) === false) {
req.headers.cookie = '<Classified>';
}
Expand Down Expand Up @@ -277,7 +277,7 @@ export function log(config: Config) {
}
logHasBeenCalled = true;

const forwardedFor = req.headers['x-forwarded-for'];
const forwardedFor = req.get('x-forwarded-for');
const remoteAddress = req.connection.remoteAddress;
const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;
let message;
Expand Down
9 changes: 5 additions & 4 deletions src/lib/up-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ class ProxyStorage implements IProxy {
uri: options.req.url,
req: options.req,
headers: {
referer: options.req.headers.referer,
// query for search
referer: options.req.get('referer'),
},
});

Expand Down Expand Up @@ -570,14 +571,14 @@ class ProxyStorage implements IProxy {
// FIXME: proxy logic is odd, something is wrong here.
// @ts-ignore
if (!this.proxy) {
headers['X-Forwarded-For'] = (req.headers['x-forwarded-for'] ? req.headers['x-forwarded-for'] + ', ' : '') + req.connection.remoteAddress;
headers['x-forwarded-for'] = (req.get('x-forwarded-for') ? req.get('x-forwarded-for') + ', ' : '') + req.connection.remoteAddress;
}
}

// always attach Via header to avoid loops, even if we're not proxying
headers['Via'] = req && req.headers['via'] ? req.headers['via'] + ', ' : '';
headers['via'] = req && req.get('via') ? req.get('via') + ', ' : '';

headers['Via'] += '1.1 ' + this.server_id + ' (Verdaccio)';
headers['via'] += '1.1 ' + this.server_id + ' (Verdaccio)';
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const memoizedgetPublicUrl = memoizee(getPublicUrl);
* @return {String} a parsed url
*/
export function getLocalRegistryTarballUri(uri: string, pkgName: string, req: Request, urlPrefix: string | void): string {
const currentHost = req.headers.host;
const currentHost = req.get('host');

if (!currentHost) {
return uri;
Expand Down Expand Up @@ -646,7 +646,7 @@ export function getPublicUrl(url_prefix: string = '', req): string {
throw new Error('invalid host');
}
const protoHeader = process.env.VERDACCIO_FORWARDED_PROTO ?? HEADERS.FORWARDED_PROTO;
const protocol = getWebProtocol(req.get(protoHeader), req.protocol);
const protocol = getWebProtocol(req.get(protoHeader.toLowerCase()), req.protocol);
const combinedUrl = combineBaseUrl(protocol, host, url_prefix);
debug('public url by request %o', combinedUrl);
return combinedUrl;
Expand Down
15 changes: 15 additions & 0 deletions test/unit/modules/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,5 +887,20 @@ describe('Utilities', () => {
expect(getPublicUrl(undefined, req)).toEqual('http://some.com/');
delete process.env.VERDACCIO_FORWARDED_PROTO;
});

test('with the VERDACCIO_FORWARDED_PROTO undefined', () => {
process.env.VERDACCIO_FORWARDED_PROTO = undefined;
const req = httpMocks.createRequest({
method: 'GET',
headers: {
host: 'some.com',
[HEADERS.FORWARDED_PROTO]: 'https',
},
url: '/',
});

expect(getPublicUrl('/test/', req)).toEqual('http://some.com/test/');
delete process.env.VERDACCIO_FORWARDED_PROTO;
});
});
});

0 comments on commit 38ca095

Please sign in to comment.