From e5d79ce8f00a18f0c0646b761f22dc608b5baf87 Mon Sep 17 00:00:00 2001 From: Juan Picado Date: Tue, 21 Dec 2021 23:09:31 +0100 Subject: [PATCH] feat: user agent is the client by default (#2793) * feat: user agent is the client by default * chore: get user agent fix * chore: disable x-powered-by --- src/api/index.ts | 12 ++++++++---- src/lib/config.ts | 10 +++------- src/lib/up-storage.ts | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index c2115f9e8..8f6bd638e 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -30,10 +30,14 @@ const defineAPI = function (config: IConfig, storage: IStorageHandler): any { // Router setup app.use(log(config)); app.use(errorReportingMiddleware); - app.use(function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void { - res.setHeader('X-Powered-By', config.user_agent); - next(); - }); + if (config.user_agent) { + app.use(function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void { + res.setHeader('X-Powered-By', config.user_agent); + next(); + }); + } else { + app.disable('x-powered-by'); + } app.use(compression()); diff --git a/src/lib/config.ts b/src/lib/config.ts index dc067da5f..f0bb1638b 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -4,12 +4,7 @@ import _ from 'lodash'; import { PackageList, Config as AppConfig, Security, Logger } from '@verdaccio/types'; import { MatchedPackage, StartUpConfig } from '../../types'; import { generateRandomHexString } from './crypto-utils'; -import { - getMatchedPackagesSpec, - normalisePackageAccess, - sanityCheckUplinksProps, - uplinkSanityCheck -} from './config-utils'; +import { getMatchedPackagesSpec, normalisePackageAccess, sanityCheckUplinksProps, uplinkSanityCheck } from './config-utils'; import { getUserAgent, isObject } from './utils'; import { APP_ERROR } from './constants'; @@ -22,6 +17,7 @@ const allowedEnvConfig = ['http_proxy', 'https_proxy', 'no_proxy']; */ class Config implements AppConfig { public logger: Logger; + // @ts-ignore public user_agent: string; // @ts-ignore public secret: string; @@ -49,7 +45,7 @@ class Config implements AppConfig { } // @ts-ignore - if (_.isNil(this.user_agent)) { + if (config?.user_agent) { this.user_agent = getUserAgent(); } diff --git a/src/lib/up-storage.ts b/src/lib/up-storage.ts index ad3160456..eb1db8fe8 100644 --- a/src/lib/up-storage.ts +++ b/src/lib/up-storage.ts @@ -278,7 +278,7 @@ class ProxyStorage implements IProxy { headers[accept] = headers[accept] || contentTypeAccept; headers[acceptEncoding] = headers[acceptEncoding] || 'gzip'; // registry.npmjs.org will only return search result if user-agent include string 'npm' - headers[userAgent] = headers[userAgent] || `npm (${this.userAgent})`; + headers[userAgent] = this.userAgent ? `npm (${this.userAgent})` : options.req?.get('user-agent'); return this._setAuth(headers); }