oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.11k stars 2.76k forks source link

Support behind-proxy rewriting of request based on X-Forwarded-* headers (similar to express's "trust proxy" feature) #11383

Open beorn opened 5 months ago

beorn commented 5 months ago

What is the problem this feature would solve?

When running behind proxies it is often necessary for code to know what the URL the client used to connect to the server was (for the purposes of creating/checking cookies, creating fully qualified URLs, etc), but this requires parsing the request's X-Forwarded-* headers and recreating the URL based on this information (mainly host + protocol).

What is the feature you are proposing to solve the problem?

Doing this kind of rewriting is most conveniently done once at the app server level, hence Express's "trust proxy" feature. This is a pretty common situation.

What alternatives have you considered?

(1) Application code can handle this but it is error prone and redundant, see for example: https://github.com/honojs/middleware/issues/537

(2) Wrapping fetch could be done - for example https://jsr.io/@hongminhee/x-forwarded-fetch/0.2.0/mod.ts#L24 - but Bun doesn't like this, e.g., websockets cease to work: https://github.com/oven-sh/bun/issues/11382 - In any case it would be nice to have a utility to do so "baked in".

Electroid commented 5 months ago

This would be a nice feature to add to Bun.serve