Skip to content

Commit

Permalink
Merge pull request #64 from peter-at-work/feature/63-fix-x-forwarded-…
Browse files Browse the repository at this point in the history
…host

fix: Fix Issue #63 to properly set the x-forwarded-host header
  • Loading branch information
FlorianRappl committed Jan 30, 2024
2 parents 74ade1e + 64d9fcc commit 5c271cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.15.4 (tbd)

- Fixed the `x-forwarded-host` header value

## 0.15.3

- Added possibility to specify multiple headers or query parameters
Expand Down
12 changes: 11 additions & 1 deletion docs/proxy-injector.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ interface ProxyInjectorConfiguration {
active?: boolean;
agentOptions?: any;
proxy?: any;
xfwd?: boolean;
defaultHeaders?: Array<string>;
discardHeaders?: Array<string>;
permitHeaders?: Array<string>;
injectHeaders?: Record<string, string>;
followRedirect?: boolean;
}
```

The mapping is already a general configuration, as the targets need to be known as well as their usual counterparts (e.g., to identify the correct URLs in an HAR file). The `agentOptions` can be used to specify more sophisticated options for the proxyed request (e.g., which ciphers to use). The `proxy` option allows us to set a (corporate?) proxy to be used on the local machine (oh the irony - a proxy server that allows setting another proxy ...).

While the `defaultHeaders` provide a way to override the used default set of headers, `permitHeaders` are for explicitly allowing non-default headers and `discardHeaders` may be used to define which headers should never be considered.
While the `defaultHeaders` provide a way to override the used default set of headers, `permitHeaders` are for explicitly allowing non-default headers and `discardHeaders` may be used to define which headers should never be considered, and `injectHeaders` may be used to create or override headers with custom values.

If not explicitly specified the default headers are defined to be:

Expand All @@ -50,3 +52,11 @@ const defaultProxyHeaders = [
'range',
];
```

The `xfwd` option will create the following headers:
- x-forwarded-for
- x-forwarded-host
- x-forwarded-port
- x-forwarded-proto

and set the header values as derived from the original request. The `x-forwarded-host` header will contain the request's `host` header value, which may include a port number.
2 changes: 1 addition & 1 deletion src/server/injectors/proxy-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export default class ProxyInjector implements KrasInjector {

if (this.config.xfwd) {
integrateXfwd(headers, protocol, req);
headers['x-forwarded-host'] = headers['x-forwarded-host'] || headers.host || '';
headers['x-forwarded-host'] = headers['x-forwarded-host'] || req.headers.host || '';
}

return headers;
Expand Down

0 comments on commit 5c271cb

Please sign in to comment.