Welcome to the Linux Foundation Forum!

10 - Block attacker IP with express

Refer to express code to get attacker IP, req.socket.remoteAddress === ''

The code cannot work as the actual value of req.socket.remoteAddress I get is :ffff:

I can process it to remove :ffff:. Anyone encounter this issue?


  • Just for local test, use the result when you request "req.socket.remoteAddress", use this value as a string and that's it.

    in your case you are getting the ipv6 and the ipv4 "ffff:" , in real world you could receive an array of ips in other ways. but this is just an exercise to evaluate.

  • Yes. I process "ffff:" to remove ":ffff:".

    So you mean I get ":ffff:" because of localhost?
    In real production env, I will get a array of ip without ":ffff:" ?

  • yeah exactly - if your system is setup slightly different you may get an ipv4 inside an ipv6 address. It doesn't matter, the principle is the same.

  • same issue here and i fixed like this:

    app.use(function (req, res, next) {
      const ipList = req.socket.remoteAddress.split(':')
      if (ipList.includes('')) {
        const err = new Error('Forbidden');
        err.status = 403;
  • xdxmxc
    xdxmxc Posts: 110


Upcoming Training