From 25b205bd90eef0123b70588c8a388a9c24b4970b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilherme=20Ara=C3=BAjo?= Date: Fri, 18 Sep 2026 02:06:10 -0300 Subject: [PATCH] sqlite: throw on invalid URL path instead of abort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ValidateDatabasePath() treats any object exposing a string `href` as a URL, then asserts the parse result with CHECK(ada::can_parse(location)). Whether that string parses depends on user input rather than on an invariant the code guarantees, so a value such as { href: 'not a url' } aborted the process instead of throwing. Replace the CHECK with an ERR_INVALID_URL exception, matching how node_file.cc reports an unparsable URL. Both DatabaseSync() and backup() validate their path through this function, so both paths are covered. Signed-off-by: Guilherme Araújo Assisted-by: Claude Code PR-URL: https://github.com/nodejs/node/pull/66026 Reviewed-By: Xuguang Mei Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca Reviewed-By: Edy Silva Reviewed-By: Yagiz Nizipli --- src/node_sqlite.cc | 6 +++++- test/parallel/test-sqlite-backup.mjs | 10 ++++++++++ test/parallel/test-sqlite-database-sync.js | 6 ++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 372606eb6de..b24a94facb4 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -1296,7 +1296,11 @@ std::optional ValidateDatabasePath(Environment* env, Utf8Value location_value(env->isolate(), href.As()); auto location = location_value.ToStringView(); if (!has_null_bytes(location)) { - CHECK(ada::can_parse(location)); + if (!ada::can_parse(location)) { + THROW_ERR_INVALID_URL(env->isolate(), "Invalid URL"); + return std::nullopt; + } + if (!location.starts_with("file:")) { THROW_ERR_INVALID_URL_SCHEME(env->isolate()); return std::nullopt; diff --git a/test/parallel/test-sqlite-backup.mjs b/test/parallel/test-sqlite-backup.mjs index f995ae3ca72..ee5f54fd909 100644 --- a/test/parallel/test-sqlite-backup.mjs +++ b/test/parallel/test-sqlite-backup.mjs @@ -269,6 +269,16 @@ test('throws if URL is not file: scheme', (t) => { }); }); +test('throws if the URL-like path has an unparsable href', (t) => { + const database = new DatabaseSync(':memory:'); + + t.after(() => { database.close(); }); + + t.assert.throws(() => { + backup(database, { href: 'not a url' }); + }, { code: 'ERR_INVALID_URL' }); +}); + test('database backup fails when dest file is not writable', { skip: isRoot }, async (t) => { const readonlyDestDb = nextDb(); writeFileSync(readonlyDestDb, '', { mode: 0o444 }); diff --git a/test/parallel/test-sqlite-database-sync.js b/test/parallel/test-sqlite-database-sync.js index 08a636c9cbd..e570ffde779 100644 --- a/test/parallel/test-sqlite-database-sync.js +++ b/test/parallel/test-sqlite-database-sync.js @@ -51,6 +51,12 @@ suite('DatabaseSync() constructor', () => { }); }); + test('throws if the URL-like path has an unparsable href', (t) => { + t.assert.throws(() => { + new DatabaseSync({ href: 'not a url' }); + }, { code: 'ERR_INVALID_URL' }); + }); + test('throws if options is provided but is not an object', (t) => { t.assert.throws(() => { new DatabaseSync('foo', null);