Skip to content

Commit

Permalink
fix: removeHeader throws an error
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso committed Apr 13, 2023
1 parent 7d2d41b commit 9a30295
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/gitlab-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { IncomingMessage } from 'http';

export interface RouterOptions {
logger: Logger;
Expand All @@ -26,11 +27,20 @@ export async function createRouter(

const router = Router();

// We are filtering the proxy request headers here rather than in
// `onProxyReq` because when global-agent is enabled then `onProxyReq`
// fires _after_ the agent has already sent the headers to the proxy
// target, causing a ERR_HTTP_HEADERS_SENT crash
const filter = (_pathname: string, req: IncomingMessage): boolean => {
if (req.headers['authorization']) delete req.headers['authorization'];
return req.method === 'GET';
};

for (const { host, apiBaseUrl, token } of gitlabIntegrations) {
const apiUrl = new URL(apiBaseUrl);
router.use(
`/${host}`,
createProxyMiddleware((_pathname, req) => req.method === 'GET', {
createProxyMiddleware(filter, {
target: apiUrl.origin,
changeOrigin: true,
headers: {
Expand All @@ -40,13 +50,6 @@ export async function createRouter(
pathRewrite: {
[`^/api/gitlab/${host}`]: apiUrl.pathname,
},
onProxyReq: (proxyReq) => {
try {
proxyReq.removeHeader('Authorization');
} catch (e) {
console.log((e as Error).message);
}
},
})
);
}
Expand Down

1 comment on commit 9a30295

@mbenson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not make more sense to determine that the filter will return true before you modify the request?

Please sign in to comment.