GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sqlite: enforce permission model on file access by mcollina · Pull Request #62957 · nodejs/node

sqlite: enforce permission model on file access #62957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
nodejs-github-bot merged 7 commits into nodejs:main from mcollina:sqlite-permission-checks
Nov 25, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

sqlite: enforce permission model on file access #62957

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
afb1792
sqlite: enforce permission model in Database
mcollina Apr 23, 2026
4d9a76c
sqlite: enforce permissions through a VFS shim
mcollina Jul 12, 2026
5d8218a
sqlite: fix permission VFS edge cases
mcollina Jul 12, 2026
b47af09
fixup! sqlite: fix permission VFS edge cases
araujogui Oct 23, 2026
5ca8342
fixup! sqlite: fix permission VFS edge cases
araujogui Oct 24, 2026
435bfbd
sqlite: disable URI filenames under permission
araujogui Nov 16, 2026
5a9c7a5
sqlite: report permission VFS denials
araujogui Nov 16, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

4 changes: 4 additions & 0 deletions 4 doc/api/permissions.md
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ There are constraints you need to know before using this system:
than vulnerabilities. Please report them through the regular issue tracker.
* Run-Time Loadable Extensions cannot be loaded when the Permission Model is
enabled, affecting the sqlite module.
* SQLite [URI filenames][] are disabled when the Permission Model is enabled.
The `node:sqlite` module rejects {URL} objects and paths starting with
`file:`, and `ATTACH DATABASE` treats such names as plain file paths.
* Using existing file descriptors via the `node:fs` module bypasses the
Permission Model.

Expand Down Expand Up @@ -548,6 +551,7 @@ Developers relying on --permission to sandbox untrusted code should be aware tha
been granted contain relative symbolic links.

[Security Policy]: https://github.com/nodejs/node/blob/main/SECURITY.md
[URI filenames]: https://www.sqlite.org/uri.html
[`--allow-addons`]: cli.md#--allow-addons
[`--allow-child-process`]: cli.md#--allow-child-process
[`--allow-env`]: cli.md#--allow-env
Expand Down
9 changes: 7 additions & 2 deletions 9 doc/api/sqlite.md
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ changes:
* `path` {string | Buffer | URL} The path of the database. A SQLite database can be
stored in a file or completely [in memory][]. To use a file-backed database,
the path should be a file path. To use an in-memory database, the path
should be the special name `':memory:'`.
should be the special name `':memory:'`. When the [Permission Model][] is
enabled, {URL} objects and [URI filenames][] (paths starting with `file:`)
are not supported and an exception is thrown.
* `options` {Object} Configuration options for the database connection. The
following options are supported:
* `open` {boolean} If `true`, the database is opened by the constructor. When
Expand Down Expand Up @@ -1840,7 +1842,8 @@ changes:

* `sourceDb` {Database} The database to backup. The source database must be open.
* `path` {string | Buffer | URL} The path where the backup will be created. If the file already exists,
the contents will be overwritten.
the contents will be overwritten. When the [Permission Model][] is enabled, {URL} objects and
[URI filenames][] (paths starting with `file:`) are not supported and an exception is thrown.
* `options` {Object} Optional configuration for the backup. The
following properties are supported:
* `source` {string} Name of the source database. This can be `'main'` (the default primary database) or any other
Expand Down Expand Up @@ -2144,9 +2147,11 @@ callback function to indicate what type of operation is being authorized.
[Constants Passed To The Conflict Handler]: https://www.sqlite.org/session/c_changeset_conflict.html
[Constants Returned From The Conflict Handler]: https://www.sqlite.org/session/c_changeset_abort.html
[Limit Constants]: https://www.sqlite.org/c3ref/c_limit_attached.html
[Permission Model]: permissions.md#permission-model
[Run-Time Limits]: https://www.sqlite.org/c3ref/limit.html
[SQL injection]: https://en.wikipedia.org/wiki/SQL_injection
[Type conversion between JavaScript and SQLite]: #type-conversion-between-javascript-and-sqlite
[URI filenames]: https://www.sqlite.org/uri.html
[Writing numbers and BigInts]: #writing-numbers-and-bigints
[`'sqlite.db.query'`]: diagnostics_channel.md#event-sqlitedbquery
[`ATTACH DATABASE`]: https://www.sqlite.org/lang_attach.html
Expand Down
2 changes: 2 additions & 0 deletions 2 node.gyp
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,10 @@
],
'node_sqlite_sources': [
'src/node_sqlite.cc',
'src/node_sqlite_vfs.cc',
'src/node_webstorage.cc',
'src/node_sqlite.h',
'src/node_sqlite_vfs.h',
'src/node_webstorage.h',
],
'node_ffi_sources': [
Expand Down
105 changes: 90 additions & 15 deletions 105 src/node_sqlite.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, Database* db) {
}
}

if (db->ThrowIfPermissionDenied()) return;

Local<Object> e;
if (CreateSQLiteError(isolate, db->Connection()).ToLocal(&e)) {
isolate->ThrowException(e);
Expand Down Expand Up @@ -620,6 +622,7 @@ class BackupJob : public ThreadPoolWork {
std::string source_db,
std::string destination_name,
std::string dest_db,
std::shared_ptr<SQLitePermissionVFS> permission_vfs,
int pages,
Local<Function> progressFunc)
: ThreadPoolWork(env, "node_sqlite3.BackupJob"),
Expand All @@ -628,7 +631,8 @@ class BackupJob : public ThreadPoolWork {
pages_(pages),
source_db_(std::move(source_db)),
destination_name_(std::move(destination_name)),
dest_db_(std::move(dest_db)) {
dest_db_(std::move(dest_db)),
permission_vfs_(std::move(permission_vfs)) {
resolver_.Reset(env->isolate(), resolver);
progressFunc_.Reset(env->isolate(), progressFunc);
}
Expand All @@ -640,13 +644,16 @@ class BackupJob : public ThreadPoolWork {
backup_status_ = sqlite3_open_v2(
destination_name_.c_str(),
&dest_raw,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI,
nullptr);
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
(permission_vfs_ == nullptr ? SQLITE_OPEN_URI : 0),
permission_vfs_ == nullptr ? nullptr : permission_vfs_->name().c_str());
dest_.reset(dest_raw);
Local<Promise::Resolver> resolver =
Local<Promise::Resolver>::New(env()->isolate(), resolver_);
if (backup_status_ != SQLITE_OK) {
Settle(resolver, CreateSQLiteError(isolate, dest_.get()), {});
Settle(resolver,
PermissionErrorOr(CreateSQLiteError(isolate, dest_.get())),
{});
return;
}

Expand Down Expand Up @@ -677,7 +684,9 @@ class BackupJob : public ThreadPoolWork {

if (!(backup_status_ == SQLITE_OK || backup_status_ == SQLITE_DONE ||
backup_status_ == SQLITE_BUSY || backup_status_ == SQLITE_LOCKED)) {
Settle(resolver, CreateSQLiteError(isolate, backup_status_), {});
Settle(resolver,
PermissionErrorOr(CreateSQLiteError(isolate, backup_status_)),
{});
return;
}

Expand Down Expand Up @@ -744,9 +753,21 @@ class BackupJob : public ThreadPoolWork {
backup_status_ = sqlite3_errcode(dest_.get());
dest_.reset();
}

permission_vfs_.reset();
}

private:
MaybeLocal<Value> PermissionErrorOr(MaybeLocal<Object> sqlite_error) {
if (permission_vfs_ != nullptr) {
if (auto denied = permission_vfs_->TakeDenied(backup_status_)) {
return permission::CreateAccessDeniedError(
env(), denied->scope, denied->path);
}
}
return sqlite_error;
}

// The single exit point of a backup. Rejects with `error` if it is set,
// otherwise resolves with `value` if it is set. Neither being set means an
// exception is already pending. Deletes the job.
Expand Down Expand Up @@ -776,6 +797,7 @@ class BackupJob : public ThreadPoolWork {
std::string source_db_;
std::string destination_name_;
std::string dest_db_;
std::shared_ptr<SQLitePermissionVFS> permission_vfs_;
};

UserDefinedFunction::UserDefinedFunction(Environment* env,
Expand Down Expand Up @@ -1615,12 +1637,47 @@ void Database::MemoryInfo(MemoryTracker* tracker) const {
"open_config", sizeof(open_config_), "DatabaseOpenConfiguration");
}

bool Database::ThrowIfPermissionDenied() {
if (permission_vfs_ == nullptr) return false;
auto denied = permission_vfs_->TakeDenied(sqlite3_errcode(connection_.get()));
if (!denied) return false;
permission::Permission::ThrowAccessDenied(env(), denied->scope, denied->path);
return true;
}

bool Database::Open() {
if (IsOpen()) {
THROW_ERR_INVALID_STATE(env(), "database is already open");
return false;
}

// An anonymous temporary database has no path for the VFS to check.
if (open_config_.location().empty()) {
THROW_IF_INSUFFICIENT_PERMISSIONS(
env(), permission::PermissionScope::kFileSystemRead, "", false);
if (!open_config_.get_read_only()) {
THROW_IF_INSUFFICIENT_PERMISSIONS(
env(), permission::PermissionScope::kFileSystemWrite, "", false);
}
}

if (env()->permission()->enabled()) {
// Connections of the same Environment share one permission VFS.
BindingData* binding =
env()->principal_realm()->GetBindingData<BindingData>();
if (binding != nullptr) permission_vfs_ = binding->permission_vfs.lock();
if (permission_vfs_ == nullptr) {
permission_vfs_ = std::make_shared<SQLitePermissionVFS>(env());
if (!permission_vfs_->Register()) {
THROW_ERR_SQLITE_ERROR(env()->isolate(),
"Unable to register the SQLite permission VFS");
permission_vfs_.reset();
return false;
}
if (binding != nullptr) binding->permission_vfs = permission_vfs_;
}
}

// sqlite3_open_v2() assigns a database handle even when it fails. Such a
// handle is in a "sick" state and may only be used to retrieve the error
// and must then be released with sqlite3_close(). Close and reset the
Expand All @@ -1630,19 +1687,23 @@ bool Database::Open() {
auto reset_connection_on_failure = OnScopeLeave([&]() {
if (!opened) {
connection_.reset();
permission_vfs_.reset();
}
});

// TODO(cjihrig): Support additional flags.
int default_flags = SQLITE_OPEN_URI;
// TODO(araujogui): Support URI filenames with the permission model. A URI
// could select another VFS, bypassing the permission VFS.
int default_flags = permission_vfs_ == nullptr ? SQLITE_OPEN_URI : 0;
int flags = open_config_.get_read_only()
? SQLITE_OPEN_READONLY
: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
sqlite3* raw_conn = nullptr;
int r = sqlite3_open_v2(open_config_.location().c_str(),
&raw_conn,
flags | default_flags,
nullptr);
int r = sqlite3_open_v2(
open_config_.location().c_str(),
&raw_conn,
flags | default_flags,
permission_vfs_ == nullptr ? nullptr : permission_vfs_->name().c_str());
connection_.reset(raw_conn);
CHECK_ERROR_OR_THROW(env()->isolate(), this, r, SQLITE_OK, false);

Expand Down Expand Up @@ -1869,10 +1930,22 @@ std::optional<std::string> ValidateDatabasePath(Environment* env,
constexpr auto has_null_bytes = [](std::string_view str) {
return str.find('\0') != std::string_view::npos;
};
// Without SQLITE_OPEN_URI, SQLite would open a file named "file:...".
const auto reject_uri = [&](std::string value) -> std::optional<std::string> {
if (env->permission()->enabled() && value.starts_with("file:")) {
THROW_ERR_INVALID_ARG_VALUE(
env->isolate(),
"The \"%s\" argument must not be a URL or SQLite URI filename when "
"the permission model is enabled.",
field_name);
return std::nullopt;
}
return value;
};
if (path->IsString()) {
Utf8Value location(env->isolate(), path.As<String>());
if (!has_null_bytes(location.ToStringView())) {
return location.ToString();
return reject_uri(location.ToString());
}
} else if (path->IsUint8Array()) {
Local<Uint8Array> buffer = path.As<Uint8Array>();
Expand All @@ -1881,7 +1954,8 @@ std::optional<std::string> ValidateDatabasePath(Environment* env,
auto data =
static_cast<const uint8_t*>(buffer->Buffer()->Data()) + byteOffset;
if (std::find(data, data + byteLength, 0) == data + byteLength) {
return std::string(reinterpret_cast<const char*>(data), byteLength);
return reject_uri(
std::string(reinterpret_cast<const char*>(data), byteLength));
}
} else if (path->IsObject()) { // When is URL
auto url = path.As<Object>();
Expand All @@ -1901,7 +1975,7 @@ std::optional<std::string> ValidateDatabasePath(Environment* env,
return std::nullopt;
}

return location_value.ToString();
return reject_uri(location_value.ToString());
}
}
}
Expand Down Expand Up @@ -2108,6 +2182,7 @@ void Database::Close(const FunctionCallbackInfo<Value>& args) {
int r = sqlite3_close_v2(db->connection_.get());
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
db->connection_.release();
db->permission_vfs_.reset();
// Backups can defer SQLite destruction until after the connection is closed.
db->user_defined_functions_.clear();
}
Expand Down Expand Up @@ -2951,6 +3026,7 @@ void Backup(const FunctionCallbackInfo<Value>& args) {
std::move(source_db),
dest_path.value(),
std::move(dest_db),
db->PermissionVFS(),
rate,
progressFunc);
db->AddBackup(job);
Expand Down Expand Up @@ -3237,9 +3313,8 @@ void Database::SetAuthorizer(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = env->isolate();

if (args[0]->IsNull()) {
// Clear the authorizer
sqlite3_set_authorizer(db->connection_.get(), nullptr, nullptr);
db->object()->SetInternalField(kAuthorizerCallback, Null(isolate));
sqlite3_set_authorizer(db->connection_.get(), nullptr, nullptr);
return;
}

Expand Down
9 changes: 9 additions & 0 deletions 9 src/node_sqlite.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#include "base_object.h"
#include "lru_cache-inl.h"
#include "node_mem.h"
#include "node_sqlite_vfs.h"
#include "sqlite3.h"
#include "util.h"

#include <algorithm>
#include <array>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <string_view>
#include <unordered_set>
Expand Down Expand Up @@ -212,6 +214,7 @@ class BindingData : public BaseObject {
SET_SELF_SIZE(BindingData)

std::unordered_set<Database*> open_databases;
std::weak_ptr<SQLitePermissionVFS> permission_vfs;

static void CreatePerContextProperties(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
Expand Down Expand Up @@ -290,6 +293,11 @@ class Database : public BaseObject {
}
sqlite3* Connection();
void SetLimit(int sqlite_limit_id, int value);
std::shared_ptr<SQLitePermissionVFS> PermissionVFS() const {
return permission_vfs_;
}
// Throws the permission error if the permission VFS caused the last failure.
bool ThrowIfPermissionDenied();

// In some situations, such as when using custom functions, it is possible
// that SQLite reports an error while JavaScript already has a pending
Expand Down Expand Up @@ -356,6 +364,7 @@ class Database : public BaseObject {
int authorizer_depth_ = 0;
int trace_suppression_depth_ = 0;
std::vector<sqlite3_stmt*> stepping_statements_;
std::shared_ptr<SQLitePermissionVFS> permission_vfs_;

// SQLite owns these scalar and aggregate/window function registrations. Its
// destroy callbacks untrack replaced functions and failed registrations.
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.