Ask Your Question
0

How can we retrieve the legitimate source hostname in Nodejs Express for safeguarding against spoofing?

asked 2022-07-07 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-06-02 04:00:00 +0000

lalupa gravatar image

To retrieve the legitimate source hostname in Nodejs Express, you can use the req.get('host') method, which returns the value of the Host header in the HTTP request.

Here's an example code snippet:

app.get('/example', function(req, res) {
  var clientHost = req.get('host');
  var serverHost = req.hostname;

  if (clientHost === serverHost) {
    // Request is legitimate
    res.send('Legitimate request');
  } else {
    // Spoofed request
    res.send('Spoofed request');
  }
});

In this example, we retrieve the client's hostname using the req.get('host') method and compare it with the server's hostname using the req.hostname method. If the two values match, the request is considered legitimate, and we can proceed with the requested action. If the values do not match, the request is considered spoofed, and we can reject or handle it accordingly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-07-07 11:00:00 +0000

Seen: 7 times

Last updated: Jun 02 '22