-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-permission-sqlite.js
More file actions
146 lines (132 loc) Β· 3.71 KB
/
test-permission-sqlite.js
File metadata and controls
146 lines (132 loc) Β· 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Flags: --permission --allow-fs-read=* --allow-fs-write=* --allow-child-process
'use strict';
const common = require('../common');
common.skipIfSQLiteMissing();
const { isMainThread } = require('worker_threads');
if (!isMainThread) {
common.skip('This test only works on a main thread');
}
const assert = require('node:assert');
const { spawnSync } = require('node:child_process');
const fixtures = require('../common/fixtures');
const spawnOpts = {
encoding: 'utf8',
};
const sqliteCode = {
// Open database in read-only mode (should require fs.read)
readOnly: `const sqlite = require('node:sqlite');
const db = new sqlite.DatabaseSync(process.env.DB_PATH, { readOnly: true });
db.close();`,
// Open database in read-write mode (should require fs.write)
readWrite: `const sqlite = require('node:sqlite');
const db = new sqlite.DatabaseSync(process.env.DB_PATH);
db.close();`,
// Open database with options.open = false (no FS access expected at construction)
noOpen: `const sqlite = require('node:sqlite');
const db = new sqlite.DatabaseSync(process.env.DB_PATH, { open: false });
db.open();
db.close();`,
};
const dbPath = fixtures.path('permission', 'sqlite.db');
{
// Opening a database read-only should be blocked when fs.read is not granted
const code = sqliteCode.readOnly.replace(/\n/g, ' ');
const { status, stderr } = spawnSync(
process.execPath,
[
'--permission',
'--allow-fs-read=/nonexistent',
'--eval', code,
],
{
...spawnOpts,
env: {
...process.env,
DB_PATH: dbPath,
},
},
);
assert.strictEqual(status, 1, `stderr: ${stderr}`);
assert.match(stderr, /Access to this API has been restricted/);
}
{
// Opening a database read-write should be blocked when fs.write is not granted
const code = sqliteCode.readWrite.replace(/\n/g, ' ');
const { status, stderr } = spawnSync(
process.execPath,
[
'--permission',
'--allow-fs-write=/nonexistent',
'--eval', code,
],
{
...spawnOpts,
env: {
...process.env,
DB_PATH: dbPath,
},
},
);
assert.strictEqual(status, 1, `stderr: ${stderr}`);
assert.match(stderr, /Access to this API has been restricted/);
}
{
// Opening a database read-write should be allowed when permissions are granted
const code = sqliteCode.readWrite.replace(/\n/g, ' ');
const { status, stderr } = spawnSync(
process.execPath,
[
'--permission',
'--allow-fs-write=*',
'--allow-fs-read=*',
'--eval', code,
],
{
...spawnOpts,
env: {
...process.env,
DB_PATH: dbPath,
},
},
);
assert.strictEqual(status, 0, `stderr: ${stderr}`);
}
{
// Opening with open: false should not check permissions at construction,
// but db.open() should still check permissions
const code = sqliteCode.noOpen.replace(/\n/g, ' ');
const { status, stderr } = spawnSync(
process.execPath,
[
'--permission',
'--allow-fs-read=/nonexistent',
'--allow-fs-write=/nonexistent',
'--eval', code,
],
{
...spawnOpts,
env: {
...process.env,
DB_PATH: dbPath,
},
},
);
assert.strictEqual(status, 1, `stderr: ${stderr}`);
assert.match(stderr, /Access to this API has been restricted/);
}
{
// Memory database should not be restricted
const code = `const sqlite = require('node:sqlite');
const db = new sqlite.DatabaseSync(':memory:');
db.close();`.replace(/\n/g, ' ');
const { status, stderr } = spawnSync(
process.execPath,
[
'--permission',
'--allow-fs-read=/nonexistent',
'--eval', code,
],
spawnOpts,
);
assert.strictEqual(status, 0, `stderr: ${stderr}`);
}