soul-cli 0.8.1 → 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.
- package/.all-contributorsrc +9 -0
- package/.bruno/auth/folder.bru +8 -0
- package/.bruno/auth/obtain-access-token.bru +25 -0
- package/.bruno/bruno.json +6 -0
- package/.bruno/environments/local.bru +3 -0
- package/.bruno/rows/folder.bru +8 -0
- package/.bruno/rows/list-rows-with-ordering.bru +25 -0
- package/.bruno/tables/folder.bru +8 -0
- package/.bruno/tables/list-tables.bru +16 -0
- package/.env.sample +11 -2
- package/.github/dependabot.yml +9 -0
- package/.github/workflows/ci.yml +33 -0
- package/.github/workflows/npm-publish.yml +19 -6
- package/.husky/pre-commit +0 -3
- package/README.md +46 -7
- package/ROADMAP.md +154 -0
- package/assets/images/soul-RA-app.png +0 -0
- package/assets/images/soul-standalone.png +0 -0
- package/docs/extensions-examples.md +96 -16
- package/docs/self-hosting.md +54 -0
- package/docs/ws-examples.md +4 -0
- package/eslint.config.js +29 -0
- package/package.json +40 -20
- package/scripts/dev.sh +64 -0
- package/src/cli.test.js +128 -0
- package/src/config/index.js +10 -2
- package/src/config/index.test.js +120 -0
- package/src/controllers/auth/common.js +1 -16
- package/src/controllers/auth/common.test.js +23 -0
- package/src/controllers/auth/tables.js +1 -1
- package/src/controllers/auth/token.js +30 -7
- package/src/controllers/auth/user.js +20 -20
- package/src/controllers/auth/user.test.js +232 -0
- package/src/controllers/auth.test.js +445 -0
- package/src/controllers/rows.js +183 -120
- package/src/controllers/rows.test.js +119 -2
- package/src/controllers/tables.js +63 -17
- package/src/controllers/tables.test.js +185 -0
- package/src/db/index.test.js +21 -0
- package/src/extensions.js +105 -36
- package/src/extensions.test.js +340 -0
- package/src/index.js +29 -20
- package/src/index.test.js +118 -0
- package/src/middlewares/api.js +1 -1
- package/src/middlewares/api.test.js +29 -0
- package/src/middlewares/auth.js +11 -48
- package/src/middlewares/auth.test.js +105 -0
- package/src/middlewares/validation.test.js +60 -0
- package/src/schemas/index.js +3 -1
- package/src/schemas/rows.js +16 -6
- package/src/schemas/tables.js +28 -4
- package/src/server.js +83 -1
- package/src/services/authService.js +33 -3
- package/src/services/authService.test.js +115 -0
- package/src/services/rowService.js +39 -14
- package/src/services/rowService.test.js +153 -0
- package/src/services/tableService.js +15 -17
- package/src/services/tableService.test.js +190 -0
- package/src/swagger/swagger.json +4 -7
- package/src/tests/globalSetup.js +8 -1
- package/src/tests/index.js +23 -0
- package/src/utils/index.js +25 -2
- package/src/utils/index.test.js +135 -0
- package/src/utils/logger.js +15 -0
- package/src/utils/sql.js +112 -0
- package/src/utils/sql.test.js +115 -0
- package/src/websocket.js +46 -8
- package/src/websocket.test.js +121 -0
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -24
- package/src/middlewares/logger.js +0 -13
package/src/controllers/rows.js
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
const db = require('../db/index');
|
|
2
2
|
const { rowService } = require('../services');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
lte: '<=',
|
|
9
|
-
gte: '>=',
|
|
10
|
-
neq: '!=',
|
|
11
|
-
null: 'IS NULL',
|
|
12
|
-
notnull: 'IS NOT NULL',
|
|
13
|
-
};
|
|
3
|
+
const {
|
|
4
|
+
assertValidTableName,
|
|
5
|
+
assertValidColumnName,
|
|
6
|
+
assertValidOperator,
|
|
7
|
+
} = require('../utils/sql');
|
|
14
8
|
|
|
15
9
|
// Return paginated rows of a table
|
|
16
10
|
const listTableRows = async (req, res, next) => {
|
|
@@ -66,45 +60,58 @@ const listTableRows = async (req, res, next) => {
|
|
|
66
60
|
const page = parseInt(_page);
|
|
67
61
|
const limit = parseInt(_limit);
|
|
68
62
|
|
|
63
|
+
try {
|
|
64
|
+
assertValidTableName(db, tableName);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return res.status(error.status || 400).json({
|
|
67
|
+
message: error.message,
|
|
68
|
+
error: error,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
69
72
|
// if _filters are provided, filter rows by them
|
|
70
73
|
// _filters consists of fields to filter by
|
|
71
74
|
// filtering is case insensitive
|
|
72
75
|
// e.g. ?_filters=name:John,age:20
|
|
73
76
|
// will filter by name like '%John%' and age like '%20%'
|
|
74
|
-
let filters
|
|
77
|
+
let filters;
|
|
75
78
|
|
|
76
79
|
// split the filters by comma(,) except when in an array
|
|
77
80
|
const re = /,(?![^[]*?\])/;
|
|
78
81
|
try {
|
|
79
|
-
filters =
|
|
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
|
-
|
|
82
|
+
filters =
|
|
83
|
+
_filters === ''
|
|
84
|
+
? []
|
|
85
|
+
: _filters.split(re).map((filter) => {
|
|
86
|
+
//NOTE: When using the _filter parameter, the values are split using the ":" sign, like this (_filters=Total__eq:1). However, if the user sends a date value, such as (_filters=InvoiceDate__eq:2010-01-08 00:00:00), there will be additional colon (":") signs present.
|
|
87
|
+
let [key, ...value] = filter.split(':');
|
|
88
|
+
if (value.length === 1) {
|
|
89
|
+
value = value[0];
|
|
90
|
+
} else {
|
|
91
|
+
value = value.map((element) => element).join(':');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let field = key.split('__')[0];
|
|
95
|
+
let fieldOperator = key.split('__')[1] || 'eq';
|
|
96
|
+
|
|
97
|
+
assertValidColumnName(db, tableName, field);
|
|
98
|
+
let operator;
|
|
99
|
+
try {
|
|
100
|
+
operator = assertValidOperator(fieldOperator);
|
|
101
|
+
} catch {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Invalid field operator '${fieldOperator}' for field '${field}'. You can only use the following operators after the '${field}' field: __lt, __gt, __lte, __gte, __eq, __neq.`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (['null', 'notnull'].includes(fieldOperator)) {
|
|
108
|
+
value = null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { field, operator, value };
|
|
112
|
+
});
|
|
106
113
|
} catch (error) {
|
|
107
|
-
return res.status(400).json({
|
|
114
|
+
return res.status(error.status || 400).json({
|
|
108
115
|
message: error.message,
|
|
109
116
|
error: error,
|
|
110
117
|
});
|
|
@@ -120,11 +127,13 @@ const listTableRows = async (req, res, next) => {
|
|
|
120
127
|
if (filter.value) {
|
|
121
128
|
if (filter.value.startsWith('[') && filter.value.endsWith(']')) {
|
|
122
129
|
const arrayValues = filter.value.slice(1, -1).split(',');
|
|
130
|
+
whereStringValues.push(...arrayValues);
|
|
123
131
|
return `${tableName}.${filter.field} IN (${arrayValues
|
|
124
|
-
.map((
|
|
132
|
+
.map(() => '?')
|
|
125
133
|
.join(', ')})`;
|
|
126
134
|
} else {
|
|
127
|
-
|
|
135
|
+
whereStringValues.push(filter.value);
|
|
136
|
+
return `${tableName}.${filter.field} ${filter.operator} ?`;
|
|
128
137
|
}
|
|
129
138
|
} else {
|
|
130
139
|
return `${tableName}.${filter.field} ${filter.operator}`;
|
|
@@ -145,10 +154,15 @@ const listTableRows = async (req, res, next) => {
|
|
|
145
154
|
}
|
|
146
155
|
try {
|
|
147
156
|
// get all fields of the table
|
|
148
|
-
const fields = db
|
|
157
|
+
const fields = db
|
|
158
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
159
|
+
.all(tableName);
|
|
149
160
|
whereString += '(';
|
|
150
161
|
whereString += fields
|
|
151
|
-
.map((field) =>
|
|
162
|
+
.map((field) => {
|
|
163
|
+
whereStringValues.push(`%${_search}%`);
|
|
164
|
+
return `${tableName}.${field.name} LIKE ?`;
|
|
165
|
+
})
|
|
152
166
|
.join(' OR ');
|
|
153
167
|
whereString += ')';
|
|
154
168
|
params += `_search=${_search}&`;
|
|
@@ -163,24 +177,62 @@ const listTableRows = async (req, res, next) => {
|
|
|
163
177
|
// if _ordering is provided, order rows by it
|
|
164
178
|
// e.g. ?_ordering=name will order by name
|
|
165
179
|
// e.g. ?_ordering=-name will order by name descending
|
|
180
|
+
// e.g. ?_ordering=publisher.name will order by publisher name, considering that publisher is a foreign key to publishers table
|
|
166
181
|
let orderString = '';
|
|
182
|
+
let extendString = '';
|
|
183
|
+
let joins = {};
|
|
184
|
+
|
|
167
185
|
if (_ordering) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
186
|
+
const isDesc = _ordering.startsWith('-');
|
|
187
|
+
const cleanOrder = _ordering.replace('-', '');
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
if (cleanOrder.includes('.')) {
|
|
191
|
+
// Handle foreign key sort, e.g. publisher.name
|
|
192
|
+
const [relation, field] = cleanOrder.split('.');
|
|
193
|
+
|
|
194
|
+
const { foreignKey, joinedTableName, joinedTableFields } =
|
|
195
|
+
rowService.getForeignKeyInfo(tableName, relation);
|
|
196
|
+
|
|
197
|
+
assertValidColumnName(db, joinedTableName, field);
|
|
198
|
+
|
|
199
|
+
extendString += ` LEFT JOIN ${joinedTableName} ON ${joinedTableName}.${foreignKey.to} = ${tableName}.${relation}`;
|
|
200
|
+
joins[relation] = { foreignKey, joinedTableName, joinedTableFields };
|
|
201
|
+
orderString += ` ORDER BY ${joinedTableName}.${field} ${
|
|
202
|
+
isDesc ? 'DESC' : 'ASC'
|
|
203
|
+
}`;
|
|
204
|
+
} else {
|
|
205
|
+
// regular field sort
|
|
206
|
+
assertValidColumnName(db, tableName, cleanOrder);
|
|
207
|
+
orderString += ` ORDER BY ${cleanOrder} ${isDesc ? 'DESC' : 'ASC'}`;
|
|
208
|
+
}
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return res.status(error.status || 400).json({
|
|
211
|
+
message: error.message,
|
|
212
|
+
error: error,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
171
216
|
params += `_ordering=${_ordering}&`;
|
|
172
217
|
}
|
|
173
218
|
|
|
174
219
|
// if _schema is provided, return only those fields
|
|
175
220
|
// e.g. ?_schema=name,age will return only name and age fields
|
|
176
221
|
// if _schema is not provided, return all fields
|
|
177
|
-
|
|
178
222
|
let schemaString = '';
|
|
179
223
|
if (_schema) {
|
|
180
224
|
const schemaFields = _schema.split(',');
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
225
|
+
try {
|
|
226
|
+
schemaFields.forEach((field) => {
|
|
227
|
+
assertValidColumnName(db, tableName, field);
|
|
228
|
+
schemaString += `${tableName}.${field},`;
|
|
229
|
+
});
|
|
230
|
+
} catch (error) {
|
|
231
|
+
return res.status(error.status || 400).json({
|
|
232
|
+
message: error.message,
|
|
233
|
+
error: error,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
184
236
|
params += `_schema=${_schema}&`;
|
|
185
237
|
} else {
|
|
186
238
|
schemaString = `${tableName}.*`;
|
|
@@ -217,31 +269,27 @@ const listTableRows = async (req, res, next) => {
|
|
|
217
269
|
// }
|
|
218
270
|
|
|
219
271
|
let foreignKeyError = { error: '', message: '' };
|
|
220
|
-
let extendString = '';
|
|
221
272
|
|
|
222
273
|
if (_extend) {
|
|
223
274
|
const extendFields = _extend.split(',');
|
|
224
275
|
extendFields.forEach((extendedField) => {
|
|
225
276
|
try {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
.
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
277
|
+
let foreignKey, joinedTableName, joinedTableFields;
|
|
278
|
+
if (joins[extendedField]) {
|
|
279
|
+
foreignKey = joins[extendedField].foreignKey;
|
|
280
|
+
joinedTableName = joins[extendedField].joinedTableName;
|
|
281
|
+
joinedTableFields = joins[extendedField].joinedTableFields;
|
|
282
|
+
} else {
|
|
283
|
+
const foreignKeyInfo = rowService.getForeignKeyInfo(
|
|
284
|
+
tableName,
|
|
285
|
+
extendedField,
|
|
234
286
|
);
|
|
287
|
+
foreignKey = foreignKeyInfo.foreignKey;
|
|
288
|
+
joinedTableName = foreignKeyInfo.joinedTableName;
|
|
289
|
+
joinedTableFields = foreignKeyInfo.joinedTableFields;
|
|
290
|
+
extendString += ` LEFT JOIN ${joinedTableName} ON ${joinedTableName}.${foreignKey.to} = ${tableName}.${extendedField}`;
|
|
235
291
|
}
|
|
236
292
|
|
|
237
|
-
const { table: joinedTableName } = foreignKey;
|
|
238
|
-
|
|
239
|
-
const joinedTableFields = db
|
|
240
|
-
.prepare(`PRAGMA table_info(${joinedTableName})`)
|
|
241
|
-
.all();
|
|
242
|
-
|
|
243
|
-
extendString += ` LEFT JOIN ${joinedTableName} ON ${joinedTableName}.${foreignKey.to} = ${tableName}.${extendedField}`;
|
|
244
|
-
|
|
245
293
|
// joined fields will be returned in a new object called {field}_data e.g. author_id_data
|
|
246
294
|
const extendFieldsString =
|
|
247
295
|
'json_object( ' +
|
|
@@ -364,6 +412,8 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
364
412
|
);
|
|
365
413
|
|
|
366
414
|
try {
|
|
415
|
+
assertValidTableName(db, tableName);
|
|
416
|
+
|
|
367
417
|
const data = rowService.save({ tableName, fields });
|
|
368
418
|
|
|
369
419
|
/*
|
|
@@ -396,7 +446,7 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
396
446
|
}
|
|
397
447
|
}
|
|
398
448
|
*/
|
|
399
|
-
res.status(400).json({
|
|
449
|
+
res.status(error.status || 400).json({
|
|
400
450
|
message: error.message,
|
|
401
451
|
error: error,
|
|
402
452
|
});
|
|
@@ -449,19 +499,23 @@ const getRowInTableByPK = async (req, res, next) => {
|
|
|
449
499
|
|
|
450
500
|
let lookupField = _lookup_field;
|
|
451
501
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
502
|
+
try {
|
|
503
|
+
assertValidTableName(db, tableName);
|
|
504
|
+
|
|
505
|
+
if (!_lookup_field) {
|
|
506
|
+
// find the primary key of the table
|
|
455
507
|
lookupField = db
|
|
456
|
-
.prepare(
|
|
457
|
-
.all()
|
|
508
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
509
|
+
.all(tableName)
|
|
458
510
|
.find((field) => field.pk === 1).name;
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
message: error.message,
|
|
462
|
-
error: error,
|
|
463
|
-
});
|
|
511
|
+
} else {
|
|
512
|
+
assertValidColumnName(db, tableName, _lookup_field);
|
|
464
513
|
}
|
|
514
|
+
} catch (error) {
|
|
515
|
+
return res.status(error.status || 400).json({
|
|
516
|
+
message: error.message,
|
|
517
|
+
error: error,
|
|
518
|
+
});
|
|
465
519
|
}
|
|
466
520
|
|
|
467
521
|
// if _schema is provided, return only those fields
|
|
@@ -471,9 +525,17 @@ const getRowInTableByPK = async (req, res, next) => {
|
|
|
471
525
|
let schemaString = '';
|
|
472
526
|
if (_schema) {
|
|
473
527
|
const schemaFields = _schema.split(',');
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
528
|
+
try {
|
|
529
|
+
schemaFields.forEach((field) => {
|
|
530
|
+
assertValidColumnName(db, tableName, field);
|
|
531
|
+
schemaString += `${tableName}.${field},`;
|
|
532
|
+
});
|
|
533
|
+
} catch (error) {
|
|
534
|
+
return res.status(error.status || 400).json({
|
|
535
|
+
message: error.message,
|
|
536
|
+
error: error,
|
|
537
|
+
});
|
|
538
|
+
}
|
|
477
539
|
} else {
|
|
478
540
|
schemaString = `${tableName}.*`;
|
|
479
541
|
}
|
|
@@ -514,8 +576,8 @@ const getRowInTableByPK = async (req, res, next) => {
|
|
|
514
576
|
extendFields.forEach((extendedField) => {
|
|
515
577
|
try {
|
|
516
578
|
const foreignKey = db
|
|
517
|
-
.prepare(
|
|
518
|
-
.all()
|
|
579
|
+
.prepare('SELECT * FROM pragma_foreign_key_list(?)')
|
|
580
|
+
.all(tableName)
|
|
519
581
|
.find((fk) => fk.from === extendedField);
|
|
520
582
|
|
|
521
583
|
if (!foreignKey) {
|
|
@@ -525,8 +587,8 @@ const getRowInTableByPK = async (req, res, next) => {
|
|
|
525
587
|
const { table: joinedTableName } = foreignKey;
|
|
526
588
|
|
|
527
589
|
const joinedTableFields = db
|
|
528
|
-
.prepare(
|
|
529
|
-
.all();
|
|
590
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
591
|
+
.all(joinedTableName);
|
|
530
592
|
|
|
531
593
|
extendString += ` LEFT JOIN ${joinedTableName} ON ${joinedTableName}.${foreignKey.to} = ${tableName}.${extendedField}`;
|
|
532
594
|
|
|
@@ -635,33 +697,30 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
635
697
|
|
|
636
698
|
let lookupField = _lookup_field;
|
|
637
699
|
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
700
|
+
try {
|
|
701
|
+
assertValidTableName(db, tableName);
|
|
702
|
+
|
|
703
|
+
if (!_lookup_field) {
|
|
704
|
+
// find the primary key of the table
|
|
641
705
|
lookupField = db
|
|
642
|
-
.prepare(
|
|
643
|
-
.all()
|
|
706
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
707
|
+
.all(tableName)
|
|
644
708
|
.find((field) => field.pk === 1).name;
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
message: error.message,
|
|
648
|
-
error: error,
|
|
649
|
-
});
|
|
709
|
+
} else {
|
|
710
|
+
assertValidColumnName(db, tableName, _lookup_field);
|
|
650
711
|
}
|
|
651
|
-
}
|
|
652
712
|
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
.join(', ');
|
|
713
|
+
Object.keys(fields).forEach((field) =>
|
|
714
|
+
assertValidColumnName(db, tableName, field),
|
|
715
|
+
);
|
|
716
|
+
} catch (error) {
|
|
717
|
+
return res.status(error.status || 400).json({
|
|
718
|
+
message: error.message,
|
|
719
|
+
error: error,
|
|
720
|
+
});
|
|
721
|
+
}
|
|
663
722
|
|
|
664
|
-
if (
|
|
723
|
+
if (Object.keys(fields).length === 0) {
|
|
665
724
|
return res.status(400).json({
|
|
666
725
|
message: 'No fields provided',
|
|
667
726
|
error: 'no_fields_provided',
|
|
@@ -671,7 +730,7 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
671
730
|
try {
|
|
672
731
|
const data = rowService.update({
|
|
673
732
|
tableName,
|
|
674
|
-
|
|
733
|
+
fields,
|
|
675
734
|
lookupField,
|
|
676
735
|
pks,
|
|
677
736
|
});
|
|
@@ -727,19 +786,23 @@ const deleteRowInTableByPK = async (req, res, next) => {
|
|
|
727
786
|
|
|
728
787
|
let lookupField = _lookup_field;
|
|
729
788
|
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
789
|
+
try {
|
|
790
|
+
assertValidTableName(db, tableName);
|
|
791
|
+
|
|
792
|
+
if (!_lookup_field) {
|
|
793
|
+
// find the primary key of the table
|
|
733
794
|
lookupField = db
|
|
734
|
-
.prepare(
|
|
735
|
-
.all()
|
|
795
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
796
|
+
.all(tableName)
|
|
736
797
|
.find((field) => field.pk === 1).name;
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
message: error.message,
|
|
740
|
-
error: error,
|
|
741
|
-
});
|
|
798
|
+
} else {
|
|
799
|
+
assertValidColumnName(db, tableName, _lookup_field);
|
|
742
800
|
}
|
|
801
|
+
} catch (error) {
|
|
802
|
+
return res.status(error.status || 400).json({
|
|
803
|
+
message: error.message,
|
|
804
|
+
error: error,
|
|
805
|
+
});
|
|
743
806
|
}
|
|
744
807
|
|
|
745
808
|
try {
|
|
@@ -764,7 +827,7 @@ const deleteRowInTableByPK = async (req, res, next) => {
|
|
|
764
827
|
next();
|
|
765
828
|
}
|
|
766
829
|
} catch (error) {
|
|
767
|
-
res.status(400).json({
|
|
830
|
+
res.status(error.status || 400).json({
|
|
768
831
|
message: error.message,
|
|
769
832
|
error: error,
|
|
770
833
|
});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const { not } = require('joi');
|
|
2
1
|
const supertest = require('supertest');
|
|
3
2
|
|
|
4
3
|
const app = require('../index');
|
|
@@ -77,6 +76,35 @@ describe('Rows Endpoints', () => {
|
|
|
77
76
|
);
|
|
78
77
|
});
|
|
79
78
|
|
|
79
|
+
it('GET /tables/:name/rows?_ordering:userId.firstName&_extend=userId: should return the rows ordered by the related table field', async () => {
|
|
80
|
+
const accessToken = await generateToken(
|
|
81
|
+
{ username: 'John', isSuperuser: true },
|
|
82
|
+
config.tokenSecret,
|
|
83
|
+
'1H',
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
const params = {
|
|
87
|
+
_ordering: 'userId.firstName',
|
|
88
|
+
_extend: 'userId',
|
|
89
|
+
};
|
|
90
|
+
const query = queryString(params);
|
|
91
|
+
const res = await requestWithSupertest
|
|
92
|
+
.get(`/api/tables/posts/rows?${query}`)
|
|
93
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
94
|
+
|
|
95
|
+
expect(res.status).toEqual(200);
|
|
96
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
97
|
+
expect(res.body).toHaveProperty('data');
|
|
98
|
+
expect(res.body.data).toEqual(expect.any(Array));
|
|
99
|
+
|
|
100
|
+
const firstUserFirstName = res.body.data[1].userId_data.firstName;
|
|
101
|
+
const secondUserFirstName = res.body.data[2].userId_data.firstName;
|
|
102
|
+
const thirdUserFirstName = res.body.data[3].userId_data.firstName;
|
|
103
|
+
|
|
104
|
+
expect(firstUserFirstName <= secondUserFirstName).toBe(true);
|
|
105
|
+
expect(secondUserFirstName <= thirdUserFirstName).toBe(true);
|
|
106
|
+
});
|
|
107
|
+
|
|
80
108
|
it('GET /tables/:name/rows: should return a null field', async () => {
|
|
81
109
|
const accessToken = await generateToken(
|
|
82
110
|
{ username: 'John', isSuperuser: true },
|
|
@@ -268,8 +296,9 @@ describe('Rows Endpoints', () => {
|
|
|
268
296
|
);
|
|
269
297
|
|
|
270
298
|
const res = await requestWithSupertest
|
|
271
|
-
.delete('/api/tables/
|
|
299
|
+
.delete('/api/tables/posts/rows/1')
|
|
272
300
|
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
301
|
+
|
|
273
302
|
expect(res.status).toEqual(200);
|
|
274
303
|
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
275
304
|
});
|
|
@@ -329,4 +358,92 @@ describe('Rows Endpoints', () => {
|
|
|
329
358
|
expect(res.body.data).toEqual(expect.any(Array));
|
|
330
359
|
expect(res.body.data.length).toEqual(1);
|
|
331
360
|
});
|
|
361
|
+
|
|
362
|
+
describe('SQL injection regression', () => {
|
|
363
|
+
it('treats a quote-breaking _filters value as a literal, not SQL', async () => {
|
|
364
|
+
const accessToken = await generateToken(
|
|
365
|
+
{ username: 'John', isSuperuser: true },
|
|
366
|
+
config.tokenSecret,
|
|
367
|
+
'1H',
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
const res = await requestWithSupertest
|
|
371
|
+
.get(
|
|
372
|
+
`/api/tables/users/rows?_filters=${encodeURIComponent(
|
|
373
|
+
"firstName__eq:x' OR '1'='1",
|
|
374
|
+
)}`,
|
|
375
|
+
)
|
|
376
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
377
|
+
|
|
378
|
+
expect(res.status).toEqual(200);
|
|
379
|
+
expect(res.body.data).toEqual([]);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it('returns a 400 for a _filters field that does not exist on the table', async () => {
|
|
383
|
+
const accessToken = await generateToken(
|
|
384
|
+
{ username: 'John', isSuperuser: true },
|
|
385
|
+
config.tokenSecret,
|
|
386
|
+
'1H',
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
const res = await requestWithSupertest
|
|
390
|
+
.get('/api/tables/users/rows?_filters=bogus__eq:1')
|
|
391
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
392
|
+
|
|
393
|
+
expect(res.status).toEqual(400);
|
|
394
|
+
expect(res.body.message).toEqual(
|
|
395
|
+
expect.stringContaining("Column 'bogus' does not exist"),
|
|
396
|
+
);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it('returns a 404 (not a 500) for a crafted table name in the URL path, and leaves the table intact', async () => {
|
|
400
|
+
const accessToken = await generateToken(
|
|
401
|
+
{ username: 'John', isSuperuser: true },
|
|
402
|
+
config.tokenSecret,
|
|
403
|
+
'1H',
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
const res = await requestWithSupertest
|
|
407
|
+
.get(
|
|
408
|
+
`/api/tables/${encodeURIComponent(
|
|
409
|
+
'users; DROP TABLE users;--',
|
|
410
|
+
)}/rows`,
|
|
411
|
+
)
|
|
412
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
413
|
+
|
|
414
|
+
expect(res.status).toEqual(404);
|
|
415
|
+
|
|
416
|
+
const stillThere = await requestWithSupertest
|
|
417
|
+
.get('/api/tables/users/rows')
|
|
418
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
419
|
+
expect(stillThere.status).toEqual(200);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('does not let a PUT field value inject SQL to modify other columns', async () => {
|
|
423
|
+
const accessToken = await generateToken(
|
|
424
|
+
{ username: 'John', isSuperuser: true },
|
|
425
|
+
config.tokenSecret,
|
|
426
|
+
'1H',
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
const before = await requestWithSupertest
|
|
430
|
+
.get('/api/tables/users/rows/2')
|
|
431
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
432
|
+
const originalLastName = before.body.data[0].lastName;
|
|
433
|
+
|
|
434
|
+
const res = await requestWithSupertest
|
|
435
|
+
.put('/api/tables/users/rows/2')
|
|
436
|
+
.set('Cookie', [`accessToken=${accessToken}`])
|
|
437
|
+
.send({ fields: { firstName: "x', lastName='HACKED" } });
|
|
438
|
+
|
|
439
|
+
expect(res.status).toEqual(200);
|
|
440
|
+
|
|
441
|
+
const after = await requestWithSupertest
|
|
442
|
+
.get('/api/tables/users/rows/2')
|
|
443
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
444
|
+
|
|
445
|
+
expect(after.body.data[0].firstName).toEqual("x', lastName='HACKED");
|
|
446
|
+
expect(after.body.data[0].lastName).toEqual(originalLastName);
|
|
447
|
+
});
|
|
448
|
+
});
|
|
332
449
|
});
|