-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
🐛 BUG: impossible to get remote client IP address from incoming Astro.request #3708
Comments
Is there a property of a standard |
Sadly no, it's not available on a standard Web But it seems like Deno has thought of this, because a I had hoped that there would be a generic solution across adapters to this problem, but I don't think there is. Eg. Netlify Functions exposes the remote address by adding the So maybe the solution here is to grab it from the Deno To match the Deno |
Thanks for digging into how Deno handles this, @JReinhold! Adding a second param similar to |
Yep Cloudflare also adds IncomingRequestCfProperties. I was also trying to find a way around this limitation when implementing the Cloudflare adaptor, but I think this is a dev server only issue, I'm certainly able to access those properties in prod. In development right now I've got an ugly workaround using globalThis to make the objects consistant across environments. |
Notes from Triage Call
|
Sounds good! Here's the PR in SvelteKit that adds the And it doesn't look like Remix does anything in particular, just relying on userland to get the remote IP from whichever header matches their deployment target. Here's a (userland) utility to get the address by just brute forcing any sensible header: https://github.com/sergiodxa/remix-utils/blob/main/src/server/get-client-id-address.ts |
I wonder if a second param was to be added that contained the additional metadata wether it could be dual purpose and be used for other features too such as accessing additional properties added by plugins. Arguably though thats a different problem entirely. |
Looking at other frameworks
SvelteKit seems closest to how we work, So I'd propose either: ---
Astro.clientAddress; // getter that throws if the adapter doesn't provide it.
--- or ---
import { getClientAddress } from 'astro';
let address = getClientAddress(Astro.request);
--- It would probably work the same way under the hood either way. Slight preference for the first option since we don't have many things you import from astro. |
I agree the first option is more appealing. It's simpler to use and easier to discover. |
Draft PR is out: #3973 Just need to update docs. |
What version of
astro
are you using?1.0.0-beta.56
Are you using an SSR adapter? If so, which one?
Deno
What package manager are you using?
pnpm
What operating system are you using?
Mac
Describe the Bug
The Request object that can be read via
Astro.request
during SSR only contains a subset of thehttp.IncomingMessage
. This means that properties likesocket
are not available and thus it's impossible to get the remote IP address of the client.Reading the request IP is common for geo-related topics like geofencing content, adhering to local laws (eg. EU cookie consent), etc., and I can't currently come up with a valid workaround for this.
It looks like the
createRequest
function is responsible for this:astro/packages/astro/src/core/request.ts
Line 17 in 4b0f27d
This is both an issue when using the vite dev server as well as running a production server with the Deno adapter (and most likely all the other SSR adapters as well, but I might be wrong here).
I'm unsure if there are other valuable properties that are being stripped from the
IncomingMessage
, all I've noticed is thesocket
property.Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-ufjx2c?file=src/pages/index.astro
Participation
The text was updated successfully, but these errors were encountered: