soul-cli 0.8.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.env.sample +11 -2
  2. package/.github/dependabot.yml +9 -0
  3. package/.github/workflows/ci.yml +33 -0
  4. package/.github/workflows/npm-publish.yml +19 -6
  5. package/.husky/pre-commit +0 -3
  6. package/README.md +14 -7
  7. package/ROADMAP.md +154 -0
  8. package/docs/extensions-examples.md +96 -16
  9. package/docs/ws-examples.md +4 -0
  10. package/eslint.config.js +29 -0
  11. package/package.json +40 -20
  12. package/scripts/dev.sh +64 -0
  13. package/src/cli.test.js +128 -0
  14. package/src/config/index.js +10 -2
  15. package/src/config/index.test.js +120 -0
  16. package/src/controllers/auth/common.js +1 -16
  17. package/src/controllers/auth/common.test.js +23 -0
  18. package/src/controllers/auth/tables.js +1 -1
  19. package/src/controllers/auth/token.js +30 -7
  20. package/src/controllers/auth/user.js +20 -20
  21. package/src/controllers/auth/user.test.js +232 -0
  22. package/src/controllers/auth.test.js +445 -0
  23. package/src/controllers/rows.js +159 -112
  24. package/src/controllers/rows.test.js +88 -1
  25. package/src/controllers/tables.js +63 -17
  26. package/src/controllers/tables.test.js +185 -0
  27. package/src/db/index.test.js +21 -0
  28. package/src/extensions.js +105 -36
  29. package/src/extensions.test.js +340 -0
  30. package/src/index.js +29 -20
  31. package/src/index.test.js +118 -0
  32. package/src/middlewares/api.js +1 -1
  33. package/src/middlewares/api.test.js +29 -0
  34. package/src/middlewares/auth.js +11 -48
  35. package/src/middlewares/auth.test.js +105 -0
  36. package/src/middlewares/validation.test.js +60 -0
  37. package/src/schemas/index.js +3 -1
  38. package/src/schemas/rows.js +15 -5
  39. package/src/schemas/tables.js +28 -4
  40. package/src/server.js +83 -1
  41. package/src/services/authService.js +33 -3
  42. package/src/services/authService.test.js +115 -0
  43. package/src/services/rowService.js +25 -18
  44. package/src/services/rowService.test.js +153 -0
  45. package/src/services/tableService.js +15 -17
  46. package/src/services/tableService.test.js +190 -0
  47. package/src/swagger/swagger.json +4 -7
  48. package/src/utils/index.js +25 -2
  49. package/src/utils/index.test.js +135 -0
  50. package/src/utils/logger.js +15 -0
  51. package/src/utils/sql.js +112 -0
  52. package/src/utils/sql.test.js +115 -0
  53. package/src/websocket.js +46 -8
  54. package/src/websocket.test.js +121 -0
  55. package/.eslintignore +0 -2
  56. package/.eslintrc.js +0 -24
  57. package/src/middlewares/logger.js +0 -13
@@ -25,6 +25,42 @@ describe('Tables Endpoints', () => {
25
25
  expect(res.body.data[0]).toHaveProperty('name');
26
26
  });
27
27
 
28
+ it('GET /tables succeeds even with an unrelated cookie present alongside accessToken', async () => {
29
+ // regression: Joi rejects unknown object keys by default. The browser
30
+ // sends every cookie for the domain, including ones unrelated to Soul
31
+ // (e.g. `localhost` cookies from other local apps aren't scoped by
32
+ // port) -- without `.unknown(true)` on the cookies schema, a request
33
+ // riding alongside a stray cookie like this would 400 with
34
+ // `"cookies.csrftoken" is not allowed`, even though the actual auth
35
+ // cookie is perfectly valid.
36
+ const accessToken = await generateToken(
37
+ { username: 'John', isSuperuser: true },
38
+ config.tokenSecret,
39
+ '1H',
40
+ );
41
+
42
+ const res = await requestWithSupertest
43
+ .get('/api/tables')
44
+ .set('Cookie', [`accessToken=${accessToken}`, 'csrftoken=some-value']);
45
+
46
+ expect(res.status).toEqual(200);
47
+ });
48
+
49
+ it('POST /tables should reject creating a table with a reserved name', async () => {
50
+ const accessToken = await generateToken(
51
+ { username: 'John', isSuperuser: true },
52
+ config.tokenSecret,
53
+ '1H',
54
+ );
55
+
56
+ const res = await requestWithSupertest
57
+ .post('/api/tables')
58
+ .set('Cookie', [`accessToken=${accessToken}`])
59
+ .send({ name: '_users', schema: [{ name: 'x', type: 'TEXT' }] });
60
+
61
+ expect(res.status).toEqual(409);
62
+ });
63
+
28
64
  it('POST /tables should create a new table and return generated schema', async () => {
29
65
  const accessToken = await generateToken(
30
66
  { username: 'John', isSuperuser: true },
@@ -89,4 +125,153 @@ describe('Tables Endpoints', () => {
89
125
  expect(res.body).toHaveProperty('data');
90
126
  expect(res.body.data).toEqual(expect.any(Array));
91
127
  });
128
+
129
+ it('GET /tables/:name includes foreignKeys for a table with a foreign key column', async () => {
130
+ const accessToken = await generateToken(
131
+ { username: 'John', isSuperuser: true },
132
+ config.tokenSecret,
133
+ '1H',
134
+ );
135
+
136
+ // "pets" is created earlier in this file with an `owner` column
137
+ // referencing users(id).
138
+ const res = await requestWithSupertest
139
+ .get('/api/tables/pets')
140
+ .set('Cookie', [`accessToken=${accessToken}`]);
141
+
142
+ expect(res.status).toEqual(200);
143
+ expect(res.body).toHaveProperty('foreignKeys');
144
+ expect(res.body.foreignKeys).toEqual(expect.any(Array));
145
+
146
+ const ownerForeignKey = res.body.foreignKeys.find(
147
+ (fk) => fk.from === 'owner',
148
+ );
149
+ expect(ownerForeignKey).toMatchObject({
150
+ table: 'users',
151
+ to: 'id',
152
+ });
153
+ });
154
+
155
+ it('GET /tables/:name returns an empty foreignKeys array for a table with no foreign keys', async () => {
156
+ const accessToken = await generateToken(
157
+ { username: 'John', isSuperuser: true },
158
+ config.tokenSecret,
159
+ '1H',
160
+ );
161
+
162
+ const res = await requestWithSupertest
163
+ .get('/api/tables/users')
164
+ .set('Cookie', [`accessToken=${accessToken}`]);
165
+
166
+ expect(res.status).toEqual(200);
167
+ expect(res.body.foreignKeys).toEqual([]);
168
+ });
169
+
170
+ describe('SQL injection regression', () => {
171
+ it('safely escapes a malicious column default value instead of executing it', async () => {
172
+ const accessToken = await generateToken(
173
+ { username: 'John', isSuperuser: true },
174
+ config.tokenSecret,
175
+ '1H',
176
+ );
177
+
178
+ const res = await requestWithSupertest
179
+ .post('/api/tables')
180
+ .set('Cookie', [`accessToken=${accessToken}`])
181
+ .send({
182
+ name: 'evil_defaults',
183
+ schema: [
184
+ {
185
+ name: 'myfield',
186
+ type: 'TEXT',
187
+ default: "x'); DROP TABLE users; --",
188
+ },
189
+ ],
190
+ });
191
+
192
+ expect(res.status).toEqual(201);
193
+
194
+ const usersStillExist = await requestWithSupertest
195
+ .get('/api/tables/users')
196
+ .set('Cookie', [`accessToken=${accessToken}`]);
197
+ expect(usersStillExist.status).toEqual(200);
198
+ });
199
+
200
+ it('rejects a table name containing SQL metacharacters with a 400 before it reaches the query', async () => {
201
+ const accessToken = await generateToken(
202
+ { username: 'John', isSuperuser: true },
203
+ config.tokenSecret,
204
+ '1H',
205
+ );
206
+
207
+ const res = await requestWithSupertest
208
+ .get(`/api/tables/${encodeURIComponent('nope; DROP TABLE users;--')}`)
209
+ .set('Cookie', [`accessToken=${accessToken}`]);
210
+
211
+ expect(res.status).toEqual(400);
212
+ });
213
+
214
+ it('returns a 404 (not a 500) for a syntactically valid but nonexistent table name on GET /tables/:name', async () => {
215
+ const accessToken = await generateToken(
216
+ { username: 'John', isSuperuser: true },
217
+ config.tokenSecret,
218
+ '1H',
219
+ );
220
+
221
+ const res = await requestWithSupertest
222
+ .get('/api/tables/totally-nonexistent-table')
223
+ .set('Cookie', [`accessToken=${accessToken}`]);
224
+
225
+ expect(res.status).toEqual(404);
226
+ });
227
+
228
+ it('rejects deleting a reserved system table', async () => {
229
+ const accessToken = await generateToken(
230
+ { username: 'John', isSuperuser: true },
231
+ config.tokenSecret,
232
+ '1H',
233
+ );
234
+
235
+ const res = await requestWithSupertest
236
+ .delete('/api/tables/_users')
237
+ .set('Cookie', [`accessToken=${accessToken}`]);
238
+
239
+ expect(res.status).toEqual(409);
240
+
241
+ const usersStillExist = await requestWithSupertest
242
+ .get('/api/tables/_users')
243
+ .set('Cookie', [`accessToken=${accessToken}`]);
244
+ expect(usersStillExist.status).toEqual(200);
245
+ });
246
+
247
+ it('rejects a crafted table name on DELETE with a 400 before it reaches the query', async () => {
248
+ const accessToken = await generateToken(
249
+ { username: 'John', isSuperuser: true },
250
+ config.tokenSecret,
251
+ '1H',
252
+ );
253
+
254
+ const res = await requestWithSupertest
255
+ .delete(
256
+ `/api/tables/${encodeURIComponent('nope; DROP TABLE users;--')}`,
257
+ )
258
+ .set('Cookie', [`accessToken=${accessToken}`]);
259
+
260
+ expect(res.status).toEqual(400);
261
+ });
262
+
263
+ it('returns a 404 (not a 500) when deleting a syntactically valid but nonexistent table name', async () => {
264
+ const accessToken = await generateToken(
265
+ { username: 'John', isSuperuser: true },
266
+ config.tokenSecret,
267
+ '1H',
268
+ );
269
+
270
+ const res = await requestWithSupertest
271
+ .delete('/api/tables/totally-nonexistent-table')
272
+ .set('Cookie', [`accessToken=${accessToken}`]);
273
+
274
+ expect(res.status).toEqual(404);
275
+ });
276
+ });
92
277
  });
@@ -0,0 +1,21 @@
1
+ // This module's export is the raw better-sqlite3 connection (a singleton
2
+ // built from `config.db.filename`), not the `Database` wrapper class -- so
3
+ // these tests exercise the module's actual public contract rather than
4
+ // reaching into its internals.
5
+ const db = require('./index');
6
+
7
+ describe('db', () => {
8
+ it('exports a usable better-sqlite3 connection', () => {
9
+ expect(typeof db.prepare).toBe('function');
10
+ expect(typeof db.exec).toBe('function');
11
+ expect(typeof db.close).toBe('function');
12
+
13
+ const result = db.prepare('SELECT 1 + 1 AS total').get();
14
+ expect(result.total).toBe(2);
15
+ });
16
+
17
+ it('closes without throwing', () => {
18
+ expect(() => db.close()).not.toThrow();
19
+ expect(db.open).toBe(false);
20
+ });
21
+ });
package/src/extensions.js CHANGED
@@ -1,45 +1,114 @@
1
1
  const fs = require('fs');
2
+ const path = require('path');
2
3
 
3
- const { extensions: extensionsConfig } = require('./config');
4
+ const config = require('./config');
5
+ const { decodeToken, toBoolean } = require('./utils');
6
+ const logger = require('./utils/logger');
7
+ const { responseMessages } = require('./constants');
8
+ const { authService } = require('./services');
4
9
 
5
- const { path: extensionsPath } = extensionsConfig;
10
+ const { path: extensionsPath } = config.extensions;
11
+ const { errorMessage } = responseMessages;
6
12
 
7
- const setupExtensions = async (app, db) => {
8
- if (extensionsPath) {
9
- const extensions = fs.readdirSync(extensionsPath);
10
- extensions.forEach((extension) => {
11
- if (extension === 'api.js') {
12
- const apiExtensions = require(`${extensionsPath}/${extension}`);
13
-
14
- console.log('API extensions loaded');
15
-
16
- Object.keys(apiExtensions).forEach((key) => {
17
- const api = apiExtensions[key];
18
- switch (api.method) {
19
- case 'GET':
20
- app.get(api.path, (req, res) => api.handler(req, res, db));
21
- break;
22
- case 'POST':
23
- app.post(api.path, api.handler);
24
- break;
25
- case 'PUT':
26
- app.put(api.path, api.handler);
27
- break;
28
- case 'DELETE':
29
- app.delete(api.path, api.handler);
30
- break;
31
-
32
- default:
33
- break;
34
- }
35
- console.log(' >', api.path);
36
- });
37
- console.log('\n');
38
- }
13
+ // Wraps an extension handler with an auth check, reusing the same
14
+ // decodeToken/hasTablePermission primitives the core hasAccess middleware
15
+ // uses -- not hasAccess itself, since that assumes a `:name` route param
16
+ // for the table, which extension routes don't have.
17
+ //
18
+ // `auth` accepts:
19
+ // true -- must be authenticated, no further check
20
+ // { superuserOnly } -- must be authenticated and a superuser
21
+ // { table } -- must be authenticated and have permission on
22
+ // `table` for the request's HTTP method (via
23
+ // authService.hasTablePermission, which already
24
+ // grants superusers everything)
25
+ const withAuth = (auth, handler, db) => async (req, res) => {
26
+ if (!config.auth) {
27
+ return handler(req, res, db);
28
+ }
29
+
30
+ let payload;
31
+ try {
32
+ payload = await decodeToken(req.cookies?.accessToken, config.tokenSecret);
33
+ req.user = payload;
34
+ } catch {
35
+ return res
36
+ .status(401)
37
+ .send({ message: errorMessage.INVALID_ACCESS_TOKEN_ERROR });
38
+ }
39
+
40
+ if (auth === true) {
41
+ return handler(req, res, db);
42
+ }
43
+
44
+ if (auth.superuserOnly && !toBoolean(payload.isSuperuser)) {
45
+ return res.status(403).send({ message: errorMessage.NOT_AUTHORIZED_ERROR });
46
+ }
47
+
48
+ if (auth.table) {
49
+ const hasPermission = authService.hasTablePermission({
50
+ payload,
51
+ tableName: auth.table,
52
+ verb: req.method,
39
53
  });
40
- } else {
41
- console.log('No extensions directory provided');
54
+
55
+ if (!hasPermission) {
56
+ return res
57
+ .status(403)
58
+ .send({ message: errorMessage.NOT_AUTHORIZED_ERROR });
59
+ }
60
+ }
61
+
62
+ return handler(req, res, db);
63
+ };
64
+
65
+ const loadExtensionRoutes = (dirPath) => {
66
+ const files = fs.readdirSync(dirPath).filter((file) => file.endsWith('.js'));
67
+
68
+ return files.reduce((routes, file) => {
69
+ const exported = require(path.join(dirPath, file));
70
+ return { ...routes, ...exported };
71
+ }, {});
72
+ };
73
+
74
+ const setupExtensions = async (app, db) => {
75
+ if (!extensionsPath) {
76
+ logger.info('No extensions directory provided');
77
+ return;
42
78
  }
79
+
80
+ const routes = loadExtensionRoutes(extensionsPath);
81
+ const routeCount = Object.keys(routes).length;
82
+
83
+ if (routeCount === 0) {
84
+ logger.info(`No extension routes found in ${extensionsPath}`);
85
+ return;
86
+ }
87
+
88
+ logger.info(
89
+ `Loading ${routeCount} extension route(s) from ${extensionsPath}`,
90
+ );
91
+
92
+ Object.entries(routes).forEach(([key, route]) => {
93
+ const method = String(route.method || '').toLowerCase();
94
+ const registerRoute = app[method];
95
+
96
+ if (typeof registerRoute !== 'function') {
97
+ logger.warn(
98
+ `Skipping extension route '${key}': unsupported method '${route.method}'`,
99
+ );
100
+ return;
101
+ }
102
+
103
+ const handler = route.auth
104
+ ? withAuth(route.auth, route.handler, db)
105
+ : (req, res) => route.handler(req, res, db);
106
+
107
+ registerRoute.call(app, route.path, handler);
108
+ logger.info(
109
+ ` > ${route.method} ${route.path}${route.auth ? ' (auth required)' : ''}`,
110
+ );
111
+ });
43
112
  };
44
113
 
45
114
  module.exports = {