open-next icon indicating copy to clipboard operation
open-next copied to clipboard

Middleware's request.ip doesn't provide the client's IP

Open FearlessSlug opened this issue 1 year ago • 1 comments

In Vercel hosted NextJS app in middleware.ts you can get the client's IP from request.ip (might also be in route.ts file in the app directory but I'm not sure and haven't tested there), but in SST deployed NextJS app there's nothing in the request.ip.

FearlessSlug avatar Jul 24 '23 21:07 FearlessSlug

request.remoteAddress

masterbater avatar Aug 10 '23 05:08 masterbater

Both of these no longer exist on NextRequest

daveycodez avatar Dec 15 '24 07:12 daveycodez

We can probably close this. Today you would get the IP address from the request headers. either cloudfront-viewer-address or x-forwarded-for

sommeeeer avatar Dec 15 '24 10:12 sommeeeer

As @sommeeeer pointed out there is other ways to get the IP address.

conico974 avatar Dec 15 '24 13:12 conico974

For sure I was just noting it if anyone stumbles here from Google tbh.

Best way to get IP is

req.headers['x-forwarded-for'] || '').split(',').pop().trim()

This always gets the last entry, gets around XFF spoofing for certain environments.

daveycodez avatar Dec 15 '24 18:12 daveycodez