-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adapter-node: Make remoteAddress of requesting client available #3993
Conversation
This makes available to endpoints the IP address of client requesting the resource via platform object
|
Is this necessary? I would think that you could get the client address from an existing HTTP header. E.g. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For It feels weird to me to expose |
This header only is set when a proxy is used because in this case "remoteAddress" would only show the IP address of the proxy (who in fact send the request). I already proposed to show the IP-address as a additional header information to the request object, but Rich Harris suggested it should be an adapter/platform relevant information. Whether the whole request (raw request) or just the remoteAddress ist exposed, in any case the IP address of the remote client is an important information for security purposes and "it feels weird" to me that to date nobody thought about this so far. |
Yeah, I wouldn't make the assumption apps are always behind a proxy, at least not with node and custom adapters support. I don't know about exposing the raw request, but even if it made sense in the end, I wonder if it also makes sense to lift common features so that they work across adapters. While not all adapters may have the remote address available, as pointed out in the other pr, most will, each exposing it in their own way (eg. ps. it might be worth it to make it |
I'm a bit worried about lulling people into a false sense of security by having similar APIs in I agree that we don't want to assume that certain headers are present from a proxy in front of the Node app. But also, |
I agree with the feedback on the earlier PR that we shouldn't make this a header because it's not a header. But it does seem reasonable to expose in some way Making it a field on the SvelteKit request is a somewhat reasonable idea. Vercel and the other serverless paltforms would mostly populate it using I'd want some other folks to weigh in on this as well since it's adding new API surface |
The regular case is that the client is directly connected to the server, especially in a node-server context, as proposed here. The special case, that someone uses an internal proxy, making remoteAddress always returning that proxy address should be addressed in a later commit and/or by the user of the proxy with a header solution. Generally it would be logical to have the remoteAddress in the request object because every request has a remote origin. Maybe making it optional so that it could be set by the relevant adapter or not of some adapter does not have a remote origin of the request. |
Doing |
A best-effort I wouldn't be worried about a false sense of API compatibility because, alternatively, users would have used some adapter-specific API and they would've encountered a similar error when migrating. If that is a prevalent worry, we could also go with a Proxy or accessor API so that useful errors are possible, at a (small?) performance cost, eg. platform.get('remoteAddress') === new Error(`adapter-x has no support for "${property}". see https://adapter-docs/ for details.`) |
I believe |
I suppose we'd need to add options to |
Having the requesting clients IP address available is something we very much need in order to use it to query the ipregistry.org API in order to get information about whether this IP belongs to a botnet, or the latitude/longitute for mapping onto a map. I've been using Sapper and a firebase SSR cloud function in the past, running nodejs with some express middleware ( From
|
Maybe using That said, I reckon it would be very useful for either adapter to provide the requesting IP address so it can be used for things like geolocation and whatnot. The |
This is what we already do for |
Good point but then, just from looking through svk issues and discord channels, there are actually quite a bunch of people who'd need/want the IP address of a remote client... meaning I don't think that feature is niche 🤗
Agreed... from a developers point of view that's no big deal I guess and much easier compared to the current situation where in fact it's impossible to do it in case you don't want to use a import { handler } from '../build/handler.js'
import express from 'express'
import requestIP from 'request-ip'
let IPAddress
const getUserIPAddressMiddleware = async ( request, response, next ) => {
try {
IPAddress = await requestIP.getClientIp( request ) // empty string or null if IP address couldn't be retrieved
console.log( 'IPAddress: ', IPAddress )
next()
} catch ( error ) {
console.error( '🔴 error in getUserIPAddressMiddleware: ', error )
}
}
const app = express()
// add a route that lives separately from the SvelteKit app
app.get('/nonsvkroute', (request, response, next) => { response.locals = {duck: 'ente'}, next('non-svk route...'), next() })
// let SvelteKit handle everything else, including serving prerendered pages and static assets
app.use(
getUserIPAddressMiddleware,
handler
)
app.listen(3000, () => { console.log('Listening on port 3000') }) So here you're able to get the IP address but then how do I get it to svk easily so I could maybe pass it down to the client for display using I would much rather like to use |
The horrible war in 🇺🇦 and the impossed sanctions on 🇷🇺 create another use case for having the remote clients IP address available in Using the remote IP of a client we are going to call the ipregistry.org API whether this is a russian user or not. If so, he/she is redirected to a dedicated page, telling him/her that they can visist our website, but that payments are disabled. |
@benmccann @mrkishi @TheRealThor @Rich-Harris @Conduitry what do you think? |
Ok, recapping some discussion between the maintainers:
|
going to close this in favour of #4289 — thanks for getting the ball rolling |
This makes available to endpoints the IP address of client requesting the resource via platform object.
Improved solution to #3972
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpx changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0