nuxt-alt / proxy

An alternative module to @nuxtjs/proxy
MIT License
24 stars 5 forks source link

proxyReq.removeHeader does not work with Node V20+ (LTS) #21

Closed TimGuendel closed 9 months ago

TimGuendel commented 9 months ago

In my proxy, this used to work with Node 18.18.2:

proxyReq.getHeaderNames().forEach((headerName) => {
    proxyReq.removeHeader(headerName);
});

When using Node 20.10.0 (LTS), the call times out ⚠️. If I comment out this line:

proxyReq.removeHeader(headerName);

it works again 👀

Denoder commented 9 months ago

That code example is too vague for me to find where the issue is.

TimGuendel commented 9 months ago

I narrowed it down to this line. Here is the whole thing:

proxy: {
        proxies: {
            '/api': {
                target: process.env.BACKEND_URL,
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
                xfwd: true,
                configure: (proxy) => {
                    proxy.on('proxyReq', (proxyReq) => {
                        function getCookieFromString(cookiesString: string, cookieName: string): string | null {
                            return (cookiesString ?? '').match(`(^|;)\\s*${cookieName}\\s*=\\s*([^;]+)`)?.pop() ?? null;
                        }

                        const cookie = proxyReq.getHeader('cookie') as string;
                        const token = getCookieFromString(cookie, 'token');

                        const ALLOWED_HEADERS = ['host', 'x-forwarded-for', 'x-real-ip'];
                        proxyReq.getHeaderNames().forEach((headerName) => {
                            if (!ALLOWED_HEADERS.includes(headerName)) {
                                proxyReq.removeHeader(headerName); // This line here causes the error with Node 20+
                            }
                        });

                        if (token) {
                            proxyReq.setHeader('token', token);
                        }
                    });
                },
            },
        },
    },
Denoder commented 9 months ago

can you try to change the ALLOWED_HEADERS to:

const ALLOWED_HEADERS = ['Host', 'X-Forwarded-For', 'X-Real-Ip'];

and tell me if that works for you, if not then it might be another issue on my end. Other than that what error exactly pops up?

TimGuendel commented 9 months ago

can you try to change the ALLOWED_HEADERS to:

const ALLOWED_HEADERS = ['Host', 'X-Forwarded-For', 'X-Real-Ip'];

and tell me if that works for you, if not then it might be another issue on my end. Other than that what error exactly pops up?

This causes an error 400 right away 👀 (with node 18 AND node 20)

[nuxt] [request error] [fatal] [400] Error fetching data: AxiosError: Request failed with status code 400

Denoder commented 9 months ago

I've pushed an update for the proxy package this module uses. update the module.

TimGuendel commented 9 months ago

The error still occurs with the new version (with both Node Versions)

[nuxt] [request error] [fatal] [400] Error fetching data: AxiosError: Request failed with status code 400

Denoder commented 9 months ago

are you using the captialized headers?

TimGuendel commented 9 months ago

are you using the captialized headers?

Yes. As soon as I do, I get an error 400

Denoder commented 9 months ago

try the non-capitalized ones too?

TimGuendel commented 9 months ago

I did, see here:

The error still occurs with the new version (with both Node Versions)

[nuxt] [request error] [fatal] [400] Error fetching data: AxiosError: Request failed with status code 400

Denoder commented 9 months ago

Can you check in whatever lockfile you're utilizing what version @refactorjs/http-proxy is?

TimGuendel commented 9 months ago

I have 2 entries with "@refactorjs/http-proxy":

"@nuxt-alt/proxy@npm:^2.4.9":
  version: 2.4.9
  resolution: "@nuxt-alt/proxy@npm:2.4.9"
  dependencies:
    "@nuxt/kit": ^3.8.1
    "@refactorjs/http-proxy": latest
    defu: ^6.1.3
    picocolors: ^1.0.0
  checksum: f48536b22b4e904ad26362caaec02b2170c4e890f0cbc1871323f4596ed6a96df7d1fbdba3492d72b362318759acd0b37c184785ceec679242ad2e32fb676ab4
  languageName: node
  linkType: hard
"@refactorjs/http-proxy@npm:latest":
  version: 0.2.5
  resolution: "@refactorjs/http-proxy@npm:0.2.5"
  dependencies:
    requires-port: ^1.0.0
  checksum: 7e9a81cdb08ff9f2597714d30346f342b052602f323992596a42eee6d6e0c06d45b32aa5f751a2ab70b00fbdffe75276e74de00e45c3469c0cef3ad6d0303b34
  languageName: node
  linkType: hard
Denoder commented 9 months ago

I don't know what else to tell you. I'm unable to replicate the issue you're facing. I assumed it's a 'Headers already sent issue' but even that I am unable to replicate.

TimGuendel commented 9 months ago

Ok. I'll keep you updated if I find anything new.

TimGuendel commented 9 months ago

Quick update: I switched to a proxy inside a server eventHandler and I am having the same problem when using Node 20. So your library is not at fault.