Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxyReq.removeHeader throws an error on tab load #143

Closed
zjpersc opened this issue Mar 16, 2023 · 8 comments · Fixed by #184
Closed

proxyReq.removeHeader throws an error on tab load #143

zjpersc opened this issue Mar 16, 2023 · 8 comments · Fixed by #184

Comments

@zjpersc
Copy link
Contributor

zjpersc commented Mar 16, 2023

Running Backstage v1.12.0 and v5.0.0 of the plugin.

When loading the tab associated with the plugin's content, the following error is thrown:

 node:_http_outgoing:771
[1]     throw new ERR_HTTP_HEADERS_SENT('remove');
[1]     ^
[1]
[1] Error [ERR_HTTP_HEADERS_SENT]: Cannot remove headers after they are sent to the client
[1]     at new NodeError (node:internal/errors:400:5)
[1]     at ClientRequest.removeHeader (node:_http_outgoing:771:11)
[1]     at ProxyServer.onProxyReq (C:\dev\van-1-12-0\node_modules\@immobiliarelabs\backstage-plugin-gitlab-backend\dist\index.cjs.js:36:20)
[1]     at ProxyServer.emit (C:\dev\van-1-12-0\node_modules\eventemitter3\index.js:184:35)
[1]     at ClientRequest.<anonymous> (C:\dev\van-1-12-0\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:133:16)
[1]     at ClientRequest.emit (node:events:513:28)
[1]     at ClientRequest.emit (node:domain:489:12)
[1]     at tickOnSocket (node:_http_client:834:7)
[1]     at onSocketNT (node:_http_client:897:5)
[1]     at process.processTicksAndRejections (node:internal/process/task_queues:83:21) {
[1]   code: 'ERR_HTTP_HEADERS_SENT'
[1] }
[1]
[1] Node.js v18.13.0

I have been able to mitigate the error by commenting out the following code:

//@immobilarielabs/backstage-plugin-gitlab-backend/dist/index.cjs/js (L35-37)
        onProxyReq: (proxyReq) => {
          proxyReq.removeHeader("Authorization");
        }

I assume the code is necessary, so I don't view that as likely a long term fix.

@antoniomuso
Copy link
Contributor

antoniomuso commented Mar 17, 2023

Hi @zjpersc, I searched for the error and found this issue related to our proxying plugin:chimurai/http-proxy-middleware#343 http-party/node-http-proxy#1287

I have to find a way to reproduce locally the same error. Could you let me know if the plugin works if you catch the error?

@zjpersc
Copy link
Contributor Author

zjpersc commented Mar 17, 2023

@antoniomuso I will say that I validated adding the 'proxyTimeout: 20000' to the httpProxyMiddleware options works for me. If you would like I can submit that as a PR to this project, but it is just one line here:

https://github.com/immobiliare/backstage-plugin-gitlab/blob/main/packages/gitlab-backend/src/service/router.ts

After Line 35:
changeOrigin: true,

Add:
proxyTimeout: 20000,

@zjpersc
Copy link
Contributor Author

zjpersc commented Mar 18, 2023

@antoniomuso Catching the error w/o doing the proxyTimeout works as well.

My code:

        onProxyReq: (proxyReq) => {
          try {
            proxyReq.removeHeader("Authorization");
          } catch (e) {
            console.log((e instanceof Error).message);
          }

@antoniomuso
Copy link
Contributor

Hi, catching seems to me a good solution, we can also add a custom timeout as optional parameter in plugin configuration.

zjpersc added a commit to zjpersc/backstage-plugin-gitlab that referenced this issue Mar 20, 2023
zjpersc added a commit to zjpersc/backstage-plugin-gitlab that referenced this issue Mar 21, 2023
zjpersc added a commit to zjpersc/backstage-plugin-gitlab that referenced this issue Mar 21, 2023
zjpersc added a commit to zjpersc/backstage-plugin-gitlab that referenced this issue Mar 23, 2023
Refs: immobiliare#143
Signed-off-by: zjpersc <[email protected]>

fix: catching the error from http-proxy-middleware

Refs: immobiliare#143
Signed-off-by: zjpersc <[email protected]>
zjpersc added a commit to zjpersc/backstage-plugin-gitlab that referenced this issue Mar 23, 2023
@fmenesesg
Copy link

The catch didn't fix the problem, I just get 401 on every request, I think that something similar to what backstage did with the problem should be implemented: https://github.com/backstage/backstage/blob/f1c2ff2cdb01fa8c7d02e4d224ae5513e4a6b542/plugins/proxy-backend/src/service/router.ts#L144

@antoniomuso
Copy link
Contributor

LGTM! Good catch @fmenesesg

@antoniomuso
Copy link
Contributor

closed by #184

@mbenson
Copy link

mbenson commented Jun 13, 2023

Please see my comment 9a30295#commitcomment-117894397

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants