A bad URL from a user crashed your code? URL.parse returns null instead

new URL throws if the URL is invalid, so it needs try and catch. URL.parse returns null instead of throwing, so you check a URL with one if line, and it is Baseline 2024 in browsers.

Published 2 min read#Web development#Developer tools

You have a form with a "your website" field, and a user typed htps//mysite instead of a full URL. If your code runs new URL() on it without a try, the whole request falls over. This article shows how URL.parse() turns that check into one line.

What is URL.parse()?

URL.parse() is a static method on URL that creates a new URL object from the URL you give it, and returns null if the URL is not valid. So it does not throw like new URL() does.

According to the MDN page on URL.parse, it is an alternative to new URL() inside a try...catch, or to checking with URL.canParse() and returning null yourself.

Why does new URL() crash your code?

new URL() throws if the URL is not valid, so any bad URL from a user becomes an exception you have to catch. This was the shape:

let site = null;
try {
  site = new URL(input);
} catch {
  site = null;
}

And with URL.parse(), the same thing:

const site = URL.parse(input);

if (!site) {
  return res.status(400).json({ error: 'Invalid website URL' });
}

And there is no empty catch left that could swallow other errors by mistake.

How does the base work with relative URLs?

The base is the second argument, and it is used only when the first URL is relative: URL.parse(url, base). If the URL is absolute, the base does not change it.

The page also warns that the final URL is not a concatenation of the two strings. A URL that starts with / is resolved from the base origin: in its example, /different/place with a base that has a path and a query takes only the scheme and the domain from the base.

const page = URL.parse('/different/place', siteBase);
const broken = URL.parse('en-US/docs', badBase);

And if the base itself is invalid, like the page's example that is missing the colon after https, the result is null.

Where does it work?

URL.parse() is Baseline 2024 according to the same page: it works in the latest browser versions since September 2024, and it may not work in older devices or browsers. It also works inside Web Workers.

If you have to support older browsers, the page itself checks that it exists first: if ("parse" in URL).

Where do you start now?

The lesson for us as developers: a URL from a user is input like any other, and checking it should be a normal condition, not an exception that brings down the request.

  1. Search your code for new URL( that takes a value from the user.

  2. Replace every empty try around it with URL.parse() and an if.

  3. If you have a relative URL, give it a fixed base from your settings, not from the request.

Checking a URL before you redirect the user to it is a security story of its own: the open redirect article. If you fetch the URL after checking it, see the fetch timeout article. And if you are building your JavaScript basics, there is the 2026 developer roadmap.

Where in your code is there a new URL() on a value that comes from a form?

FAQ

What is the difference between URL.parse and URL.canParse?

URL.canParse only tells you whether the URL is valid. URL.parse gives you a ready URL object if it is valid, and null if not, so you do not build it twice.

Does it work in every browser?

In the latest browser versions, yes. The MDN page says it is Baseline 2024, working since September 2024, but it may not work in older devices or browsers.

If the URL is absolute, does the base change it?

No. If the URL is absolute, the base is not used. The base is only for relative URLs.

Share

More articles