From 79bf6fb431617268e594af021cb67df2019c4fae Mon Sep 17 00:00:00 2001 From: Omar Hamza Date: Sun, 14 Apr 2024 15:53:20 +0000 Subject: [PATCH 1/2] feat(options): added follow redirects with max body length and max redirects --- src/configuration.ts | 10 ++++++++++ src/errors.ts | 1 + src/http-proxy-middleware.ts | 26 ++++++++++++++++++++++++++ src/types.ts | 11 +++++++++++ 4 files changed, 48 insertions(+) diff --git a/src/configuration.ts b/src/configuration.ts index 1a88ec4f..69f513d0 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -5,4 +5,14 @@ export function verifyConfig(options: Options): void { if (!options.target && !options.router) { throw new Error(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING); } + if (options.followRedirects && options.followRedirectsOpts) { + if ( + (options.followRedirectsOpts.maxRedirects !== undefined && + options.followRedirectsOpts.maxRedirects <= 0) || + (options.followRedirectsOpts.maxBodyLength !== undefined && + options.followRedirectsOpts.maxBodyLength <= 0) + ) { + throw new Error(ERRORS.ERR_FOLLOW_REDIRECT_INVALID_OPTIONS); + } + } } diff --git a/src/errors.ts b/src/errors.ts index 73dcdd81..7c0199f7 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -3,4 +3,5 @@ export enum ERRORS { ERR_CONTEXT_MATCHER_GENERIC = '[HPM] Invalid context. Expecting something like: "/api" or ["/api", "/ajax"]', ERR_CONTEXT_MATCHER_INVALID_ARRAY = '[HPM] Invalid pathFilter. Expecting something like: ["/api", "/ajax"] or ["/api/**", "!**.html"]', ERR_PATH_REWRITER_CONFIG = '[HPM] Invalid pathRewrite config. Expecting object with pathRewrite config or a rewrite function', + ERR_FOLLOW_REDIRECT_INVALID_OPTIONS = '[HPM] Invalid followRedirectsOpts config', } diff --git a/src/http-proxy-middleware.ts b/src/http-proxy-middleware.ts index 583b5a1f..80f2bab5 100644 --- a/src/http-proxy-middleware.ts +++ b/src/http-proxy-middleware.ts @@ -29,6 +29,8 @@ export class HttpProxyMiddleware { this.pathRewriter = PathRewriter.createPathRewriter(this.proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided + this.setFollowRedirectOptions(options); + // https://github.com/chimurai/http-proxy-middleware/issues/19 // expose function to upgrade externally this.middleware.upgrade = (req, socket, head) => { @@ -84,6 +86,30 @@ export class HttpProxyMiddleware { }); } + /** + Sets follow-redirects module global options used internally by http-proxy. When followRedirects is true, http-proxy uses the http and https agents of the follow-redirects module https://github.com/http-party/node-http-proxy/blob/9b96cd725127a024dabebec6c7ea8c807272223d/lib/http-proxy/passes/web-incoming.js#L105 + */ + private setFollowRedirectOptions = (options: Options) => { + if (!options.followRedirectsOpts) { + return; + } + // eslint-disable-next-line @typescript-eslint/no-var-requires + const followRedirects = require('follow-redirects'); // Since there is no way to access the module from http-proxy and also given that http-proxy is unmaintained. + if (!followRedirects) { + return; + } + // Sets follow-redirects global options according to https://github.com/follow-redirects/follow-redirects?tab=readme-ov-file#global-options + // This is a workaround, the options are set globally, ideally follow redirect options should be set through the http-proxy. + if (options.followRedirectsOpts.maxRedirects) { + followRedirects.maxRedirects = options.followRedirectsOpts.maxRedirects; + debug('set followRedirects.maxRedirects globally', followRedirects.maxRedirects); + } + if (options.followRedirectsOpts.maxBodyLength) { + followRedirects.maxBodyLength = options.followRedirectsOpts.maxBodyLength; + debug('set followRedirects.maxBodyLength globally', followRedirects.maxBodyLength); + } + }; + private catchUpgradeRequest = (server: https.Server) => { if (!this.wsInternalSubscribed) { debug('subscribing to server upgrade event'); diff --git a/src/types.ts b/src/types.ts index 5087decb..dc95d15c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -139,4 +139,15 @@ export interface Options Date: Sun, 14 Apr 2024 16:09:29 +0000 Subject: [PATCH 2/2] fix(typo): typo fix --- src/http-proxy-middleware.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/http-proxy-middleware.ts b/src/http-proxy-middleware.ts index 80f2bab5..15664a44 100644 --- a/src/http-proxy-middleware.ts +++ b/src/http-proxy-middleware.ts @@ -29,7 +29,7 @@ export class HttpProxyMiddleware { this.pathRewriter = PathRewriter.createPathRewriter(this.proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided - this.setFollowRedirectOptions(options); + this.setFollowRedirectsOptions(options); // https://github.com/chimurai/http-proxy-middleware/issues/19 // expose function to upgrade externally @@ -89,7 +89,7 @@ export class HttpProxyMiddleware { /** Sets follow-redirects module global options used internally by http-proxy. When followRedirects is true, http-proxy uses the http and https agents of the follow-redirects module https://github.com/http-party/node-http-proxy/blob/9b96cd725127a024dabebec6c7ea8c807272223d/lib/http-proxy/passes/web-incoming.js#L105 */ - private setFollowRedirectOptions = (options: Options) => { + private setFollowRedirectsOptions = (options: Options) => { if (!options.followRedirectsOpts) { return; }