GithubHelp home page GithubHelp logo

Comments (3)

dougwilson avatar dougwilson commented on April 27, 2024

Just to answer the latter part, you can use any mechanism you need to get the proxy IP, you are not limited to x-forwarded-for only. Just define your own custom req.ip to do what you need https://expressjs.com/en/guide/overriding-express-api.html

from express.

chriswilty avatar chriswilty commented on April 27, 2024

Thanks for the rapid response! That looks really promising, if I am allowed to overwrite the req.ip and req.secure flags. I couldn't find any reference to which Request properties are in which category (assigned or getters), but I'll see if those work for me....

from express.

chriswilty avatar chriswilty commented on April 27, 2024

Works an absolute treat 🥳

secure-yessss

For anyone else coming here, I overrode request.protocol rather than request.secure. Code works for my use case - I can trust the left-most entry in Forwarded header, and can ignore X-Forwarded headers - but adapt as necessary.

const parseForwardedHeader = (request: typeof app.request) =>
	request.header('Forwarded')
		?.split(",")
		.flatMap((proxy) => proxy.split(';'))
		.reduce((result, proxyProps) => {
			const [key, value] = proxyProps.split('=');
			if (key && value) {
				result[key] = (result[key] || []).concat(value);
			}
			return result;
		}, {} as Record<string, string[]>);

Object.defineProperties(app.request, {
	'ip': {
		configurable: true,
		enumerable: true,
		get() {
			const proxies = parseForwardedHeader(this as typeof app.request);
			return proxies?.['for']?.[0] ?? this.socket.remoteAddress;
		},
	},
	'protocol': {
		configurable: true,
		enumerable: true,
		get() {
			const proxies = parseForwardedHeader(this as typeof app.request);
			return proxies?.['proto']?.[0] ?? this.socket.encrypted ? 'https' : 'http';
		},
	},
});

Importantly, for this use case don't enable trust proxy in case any in-between proxies are adding X-Forwarded- headers.

from express.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.