Skip to content
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

Only allow messages from the same IP address #592

Closed
grilme99 opened this issue Oct 16, 2019 · 2 comments
Closed

Only allow messages from the same IP address #592

grilme99 opened this issue Oct 16, 2019 · 2 comments

Comments

@grilme99
Copy link

grilme99 commented Oct 16, 2019

I don't think engine.io currently does this, but I want to prevent someone from sending messages to a socket when they don't have the same IP address as the initial request. How can I accomplish this?

@ghost
Copy link

ghost commented Jun 13, 2020

I don't believe that you can do this with vanilla Socket.io, but however, there is a package called ip that is a great source to use for this. You can run a user's IP address by saying 'ip.address()' and it should return the IP address of the user's WiFi router. In the backend such as Node.js, when the user requests to post something, you can compare an incoming IP to one stored in a database.
const ip = require('ip') console.log(ip.address())

Terminal:
Example: '127.0.0.1'

@darrachequesne
Copy link
Member

You can add a 'message' handler, and check the address (only in XHR polling, as this isn't needed for WebSocket):

server.on('connection', (socket) => {
  socket.on('message', () => {
    if (socket.transport.name === 'polling') {
      const initialAddress = socket.remoteAddress;
      const currentAddress = socket.transport.req.socket.remoteAddress;

      if (initialAddress !== currentAddress) {
        socket.close();
      }
    }
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants