Node: Check if URL is safe to request

I am collecting URLs from visitors of my site that I then (on-demand) request from my backend. However, I have gotten stuck on the validation of the URL.

A URL is safe to request when:

What I have found so far:

However, none of these seems to do what I need it to - not even a combination of them does.

Is there anything I have overlooked?

Upvotes: 0

Views: 1262

Answers (1)

I solved my own problem. I used:

First, I validate the input with new URL(input), and then I use require('hostname-is-private').isPrivate (this is an async function) on that URL.hostname. This also works on IP addresses (even if the name does not suggest that).

The code looks something like this:

let isPrivate = require('hostname-is-private').isPrivate;

function validateURL(urlstr, cb) {
  try {
    let url = new URL(urlstr);
    isPrivate(url.hostname, (err, res) => {
      if (err) {cb(false)}
      cb(!res);
    })
  } catch (e) {
    return cb(false);
  }
}

Upvotes: 1

Related Questions