e-alfred / ocdownloader

ocDownloader - AGPL-licensed multi-protocol download manager for Nextcloud using ARIA2, youtube-dl and Curl (supports Youtube, BitTorrent, HTTP, FTP)
https://github.com/e-alfred/ocdownloader
GNU Affero General Public License v3.0
375 stars 85 forks source link

Fix URL check #239

Open Jwiggiff opened 2 years ago

Jwiggiff commented 2 years ago

This pull request changes the URL check for http downloads. Rather than using the previous REGEX, it uses the URL constructor built in to JavaScript. The previous REGEX had some weaknesses, including any URLs that contained square brackets (and probably some other characters as well). The new method uses the URL constructor in JavaScript. Rather than checking with a REGEX, it attempts to construct a new URL object using the provided URL string within a try/catch statement. This will throw an error on a malformed URL, which then returns false. This process is illustrated below.

try {
  new URL(URLString);
  return true; // Is a valid url since it was able to construct a URL object
} catch (_) {
  return false; // Invalid url since an error was thrown when constructing the object
}