Skip to content

Commit

Permalink
fix(dev): don't overwrite proxy headers if already set (#896)
Browse files Browse the repository at this point in the history
* fix: don't overwrite proxy headers if already set

* perf: only call `getHeaders()` once

---------

Co-authored-by: Mike Bellika <mike@altinget.dk>
  • Loading branch information
MikeBellika and MikeAltinget authored Feb 1, 2023
1 parent c784b87 commit f7eed65
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/dev/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ export function createDevServer(nitro: Nitro): NitroDevServer {
// Main worker proxy
const proxy = createProxy();
proxy.proxy.on("proxyReq", (proxyReq, req) => {
// TODO: Avoid overwriting headers if already set
if (req.socket.remoteAddress) {
const proxyRequestHeaders = proxyReq.getHeaders();
if (req.socket.remoteAddress && !proxyRequestHeaders["x-forwarded-for"]) {
proxyReq.setHeader("X-Forwarded-For", req.socket.remoteAddress);
}
if (req.socket.remotePort) {
if (req.socket.remotePort && !proxyRequestHeaders["x-forwarded-port"]) {
proxyReq.setHeader("X-Forwarded-Port", req.socket.remotePort);
}
if (req.socket.remoteFamily) {
if (req.socket.remoteFamily && !proxyRequestHeaders["x-forwarded-proto"]) {
proxyReq.setHeader("X-Forwarded-Proto", req.socket.remoteFamily);
}
});
Expand Down

0 comments on commit f7eed65

Please sign in to comment.