From 5d2a08ad3e501e022f73e873353bfd01150af9d4 Mon Sep 17 00:00:00 2001 From: Zhuo Zhang Date: Wed, 26 Jul 2023 11:59:25 +0800 Subject: [PATCH] url: fix `isURL` detection by checking `path` Fixes: https://github.com/nodejs/node/issues/48921 PR-URL: https://github.com/nodejs/node/pull/48928 Fixes: https://github.com/getsentry/sentry-javascript/issues/8552 Fixes: https://github.com/request/request/issues/3458 Reviewed-By: Yagiz Nizipli Reviewed-By: Luigi Pinca Reviewed-By: Matthew Aitken Reviewed-By: Debadree Chatterjee --- lib/internal/url.js | 4 ++-- test/parallel/test-url-is-url.js | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index c4835c54e4abe3..7e44959ca2caad 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -747,13 +747,13 @@ ObjectDefineProperties(URLSearchParams.prototype, { * We use `href` and `protocol` as they are the only properties that are * easy to retrieve and calculate due to the lazy nature of the getters. * - * We check for auth attribute to distinguish legacy url instance with + * We check for `auth` and `path` attribute to distinguish legacy url instance with * WHATWG URL instance. * @param {*} self * @returns {self is URL} */ function isURL(self) { - return Boolean(self?.href && self.protocol && self.auth === undefined); + return Boolean(self?.href && self.protocol && self.auth === undefined && self.path === undefined); } class URL { diff --git a/test/parallel/test-url-is-url.js b/test/parallel/test-url-is-url.js index 0b42bb3b2f2d4a..6bb8a1595df2a0 100644 --- a/test/parallel/test-url-is-url.js +++ b/test/parallel/test-url-is-url.js @@ -9,3 +9,8 @@ const { isURL } = require('internal/url'); assert.strictEqual(isURL(new URL('https://www.nodejs.org')), true); assert.strictEqual(isURL(parse('https://www.nodejs.org')), false); +assert.strictEqual(isURL({ + href: 'https://www.nodejs.org', + protocol: 'https:', + path: '/', +}), false);