Skip to content

Commit 8da255e

Browse files
committed
sqlite: report all enforce-only failures in audit mode
Publish vfs= URIs and unverifiable ATTACH filenames to the fs channel, and keep temporary files in memory as in enforce mode, so an audit run predicts what --permission will reject. Assisted-by: Claude Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
1 parent 7e15b61 commit 8da255e

5 files changed

Lines changed: 35 additions & 11 deletions

File tree

‎doc/api/sqlite.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ against the granted file system permissions:
109109
* Temporary files are kept in memory unless file system access is granted for
110110
all paths.
111111

112+
In audit mode, the restrictions above are reported to the
113+
`node:permission-model:fs` diagnostics channel instead. An `ATTACH DATABASE`
114+
filename that is not a string literal is reported with an empty `resource`.
115+
112116
## Type conversion between JavaScript and SQLite
113117

114118
When Node.js writes to or reads from SQLite, it is necessary to convert between

‎src/node_sqlite.cc‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,8 +1915,12 @@ std::optional<std::string> ValidateDatabasePath(Environment* env,
19151915
};
19161916
const auto validate_uri_vfs =
19171917
[&](std::string value) -> std::optional<std::string> {
1918-
if (env->permission()->enabled() && !env->permission()->warning_only() &&
1919-
HasSQLiteVfsUriParameter(value)) {
1918+
if (env->permission()->enabled() && HasSQLiteVfsUriParameter(value)) {
1919+
if (env->permission()->warning_only()) {
1920+
env->permission()->PublishDenied(
1921+
env, permission::PermissionScope::kFileSystemRead, value);
1922+
return value;
1923+
}
19201924
THROW_ERR_INVALID_ARG_VALUE(
19211925
env->isolate(),
19221926
"The \"%s\" argument must not specify a SQLite VFS.",
@@ -3422,9 +3426,16 @@ int Database::AuthorizerCallback(void* user_data,
34223426
// SQLite only passes the ATTACH filename when it is a string literal. A
34233427
// bound or computed filename cannot be checked for a URI-selected VFS.
34243428
if (db->permission_vfs_ != nullptr && action_code == SQLITE_ATTACH &&
3425-
!db->env()->permission()->warning_only() &&
34263429
(param1 == nullptr || HasSQLiteVfsUriParameter(param1))) {
3427-
return SQLITE_DENY;
3430+
if (!db->env()->permission()->warning_only()) return SQLITE_DENY;
3431+
// Subscribers must not run while SQLite is preparing a statement. An
3432+
// unknown filename is reported as an empty resource.
3433+
db->env()->SetImmediate(
3434+
[resource = std::string(param1 == nullptr ? "" : param1)](
3435+
Environment* env) {
3436+
env->permission()->PublishDenied(
3437+
env, permission::PermissionScope::kFileSystemRead, resource);
3438+
});
34283439
}
34293440
if (!db->has_authorizer_) return SQLITE_OK;
34303441

‎src/node_sqlite_vfs.cc‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ int PermissionFileControl(sqlite3_file* file, int operation, void* argument) {
314314
const std::string_view proxy_path = static_cast<const char*>(argument);
315315
const bool allowed =
316316
proxy_path == ":auto:"
317-
? permission_file->vfs->AllowsTemporaryFile(true, true)
317+
? permission_file->vfs->AllowsTemporaryFile(true, true) ||
318+
permission_file->vfs->Denied("", true)
318319
: permission_file->vfs->AllowsPath(proxy_path, true, true);
319320
if (!allowed) return SQLITE_PERM;
320321
}
@@ -665,8 +666,6 @@ bool SQLitePermissionVFS::AllowsSidecarPath(std::string_view path,
665666
}
666667

667668
bool SQLitePermissionVFS::AllowsTemporaryFile(bool read, bool write) const {
668-
if (permission_->warning_only()) return true;
669-
670669
// SQLite does not provide a pathname for anonymous temporary files. Disk
671670
// storage is allowed only with global permissions. Otherwise xOpen uses
672671
// SQLite's memory VFS.

‎src/node_sqlite_vfs.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ class SQLitePermissionVFS {
7373
bool AllowsPath(std::string_view path, bool read, bool write) const;
7474
bool AllowsSidecarPath(std::string_view path, bool read, bool write) const;
7575
bool AllowsTemporaryFile(bool read, bool write) const;
76+
// Returns true in audit mode, after reporting the denial.
77+
bool Denied(std::string_view path, bool read) const;
7678

7779
private:
7880
bool AllowsCanonicalPath(std::string_view path, bool read, bool write) const;
79-
// Returns true in audit mode, after reporting the denial.
80-
bool Denied(std::string_view path, bool read) const;
8181

8282
sqlite3_vfs vfs_{};
8383
sqlite3_vfs* parent_ = nullptr;

‎test/parallel/test-permission-sqlite.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,11 @@ if (!common.isWindows) {
443443

444444
{
445445
// Audit mode reports SQLite file access without denying it, including
446-
// ATTACH filenames that the authorizer cannot inspect.
446+
// ATTACH filenames that the authorizer cannot inspect (reported as an
447+
// empty resource) and URI-selected VFSes.
447448
const auditDbPath = tmpdir.resolve('audit.sqlite');
448449
const auditAttachPath = tmpdir.resolve('audit-attach.sqlite');
450+
const vfsUri = `file:${tmpdir.resolve('audit-vfs.sqlite')}?vfs=memdb`;
449451
const code = `const dc = require('node:diagnostics_channel');
450452
const { DatabaseSync } = require('node:sqlite');
451453
const seen = new Set();
@@ -454,17 +456,25 @@ if (!common.isWindows) {
454456
db.prepare('ATTACH ? AS other').run(process.env.ATTACH_PATH);
455457
db.exec('CREATE TABLE other.data(value)');
456458
db.close();
459+
new DatabaseSync(process.env.VFS_URI).close();
457460
process.on('exit', () => console.log(JSON.stringify([...seen])));`;
458461
const { status, stdout, stderr } = spawnSync(
459462
process.execPath,
460463
['--permission-audit', '--eval', code],
461464
{
462465
encoding: 'utf8',
463-
env: { ...process.env, DB_PATH: auditDbPath, ATTACH_PATH: auditAttachPath },
466+
env: {
467+
...process.env,
468+
DB_PATH: auditDbPath,
469+
ATTACH_PATH: auditAttachPath,
470+
VFS_URI: vfsUri,
471+
},
464472
},
465473
);
466474
assert.strictEqual(status, 0, `stderr: ${stderr}`);
467475
const seen = JSON.parse(stdout);
468476
assert(seen.includes(auditDbPath), stdout);
469477
assert(seen.includes(auditAttachPath), stdout);
478+
assert(seen.includes(''), stdout);
479+
assert(seen.includes(vfsUri), stdout);
470480
}

0 commit comments

Comments
 (0)