soul-cli 0.8.2 → 0.10.0
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/.env.sample +11 -2
- package/.github/dependabot.yml +9 -0
- package/.github/pull_request_template.md +8 -0
- package/.github/workflows/ci.yml +33 -0
- package/.github/workflows/pr-title.yml +27 -0
- package/.github/workflows/release.yml +44 -0
- package/.husky/pre-commit +0 -3
- package/.release-please-manifest.json +3 -0
- package/AGENTS.md +31 -0
- package/CHANGELOG.md +35 -0
- package/CLAUDE.md +1 -0
- package/README.md +16 -8
- package/ROADMAP.md +154 -0
- package/SECURITY.md +19 -0
- package/docs/extensions-examples.md +96 -16
- package/docs/ws-examples.md +4 -0
- package/eslint.config.js +29 -0
- package/package.json +44 -24
- package/release-please-config.json +10 -0
- 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 +159 -112
- package/src/controllers/rows.test.js +88 -1
- 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 +37 -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 +15 -5
- package/src/schemas/tables.js +28 -4
- package/src/server.js +85 -4
- package/src/services/authService.js +33 -3
- package/src/services/authService.test.js +115 -0
- package/src/services/rowService.js +25 -18
- 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/index.js +1 -1
- 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 +47 -8
- package/src/websocket.test.js +121 -0
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -24
- package/.github/workflows/npm-publish.yml +0 -23
- 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}&`;
|
|
@@ -172,21 +186,31 @@ const listTableRows = async (req, res, next) => {
|
|
|
172
186
|
const isDesc = _ordering.startsWith('-');
|
|
173
187
|
const cleanOrder = _ordering.replace('-', '');
|
|
174
188
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
189
|
+
try {
|
|
190
|
+
if (cleanOrder.includes('.')) {
|
|
191
|
+
// Handle foreign key sort, e.g. publisher.name
|
|
192
|
+
const [relation, field] = cleanOrder.split('.');
|
|
178
193
|
|
|
179
|
-
|
|
180
|
-
|
|
194
|
+
const { foreignKey, joinedTableName, joinedTableFields } =
|
|
195
|
+
rowService.getForeignKeyInfo(tableName, relation);
|
|
181
196
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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
|
+
});
|
|
190
214
|
}
|
|
191
215
|
|
|
192
216
|
params += `_ordering=${_ordering}&`;
|
|
@@ -198,9 +222,17 @@ const listTableRows = async (req, res, next) => {
|
|
|
198
222
|
let schemaString = '';
|
|
199
223
|
if (_schema) {
|
|
200
224
|
const schemaFields = _schema.split(',');
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
+
}
|
|
204
236
|
params += `_schema=${_schema}&`;
|
|
205
237
|
} else {
|
|
206
238
|
schemaString = `${tableName}.*`;
|
|
@@ -380,6 +412,8 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
380
412
|
);
|
|
381
413
|
|
|
382
414
|
try {
|
|
415
|
+
assertValidTableName(db, tableName);
|
|
416
|
+
|
|
383
417
|
const data = rowService.save({ tableName, fields });
|
|
384
418
|
|
|
385
419
|
/*
|
|
@@ -412,7 +446,7 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
412
446
|
}
|
|
413
447
|
}
|
|
414
448
|
*/
|
|
415
|
-
res.status(400).json({
|
|
449
|
+
res.status(error.status || 400).json({
|
|
416
450
|
message: error.message,
|
|
417
451
|
error: error,
|
|
418
452
|
});
|
|
@@ -465,19 +499,23 @@ const getRowInTableByPK = async (req, res, next) => {
|
|
|
465
499
|
|
|
466
500
|
let lookupField = _lookup_field;
|
|
467
501
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
502
|
+
try {
|
|
503
|
+
assertValidTableName(db, tableName);
|
|
504
|
+
|
|
505
|
+
if (!_lookup_field) {
|
|
506
|
+
// find the primary key of the table
|
|
471
507
|
lookupField = db
|
|
472
|
-
.prepare(
|
|
473
|
-
.all()
|
|
508
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
509
|
+
.all(tableName)
|
|
474
510
|
.find((field) => field.pk === 1).name;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
message: error.message,
|
|
478
|
-
error: error,
|
|
479
|
-
});
|
|
511
|
+
} else {
|
|
512
|
+
assertValidColumnName(db, tableName, _lookup_field);
|
|
480
513
|
}
|
|
514
|
+
} catch (error) {
|
|
515
|
+
return res.status(error.status || 400).json({
|
|
516
|
+
message: error.message,
|
|
517
|
+
error: error,
|
|
518
|
+
});
|
|
481
519
|
}
|
|
482
520
|
|
|
483
521
|
// if _schema is provided, return only those fields
|
|
@@ -487,9 +525,17 @@ const getRowInTableByPK = async (req, res, next) => {
|
|
|
487
525
|
let schemaString = '';
|
|
488
526
|
if (_schema) {
|
|
489
527
|
const schemaFields = _schema.split(',');
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
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
|
+
}
|
|
493
539
|
} else {
|
|
494
540
|
schemaString = `${tableName}.*`;
|
|
495
541
|
}
|
|
@@ -530,8 +576,8 @@ const getRowInTableByPK = async (req, res, next) => {
|
|
|
530
576
|
extendFields.forEach((extendedField) => {
|
|
531
577
|
try {
|
|
532
578
|
const foreignKey = db
|
|
533
|
-
.prepare(
|
|
534
|
-
.all()
|
|
579
|
+
.prepare('SELECT * FROM pragma_foreign_key_list(?)')
|
|
580
|
+
.all(tableName)
|
|
535
581
|
.find((fk) => fk.from === extendedField);
|
|
536
582
|
|
|
537
583
|
if (!foreignKey) {
|
|
@@ -541,8 +587,8 @@ const getRowInTableByPK = async (req, res, next) => {
|
|
|
541
587
|
const { table: joinedTableName } = foreignKey;
|
|
542
588
|
|
|
543
589
|
const joinedTableFields = db
|
|
544
|
-
.prepare(
|
|
545
|
-
.all();
|
|
590
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
591
|
+
.all(joinedTableName);
|
|
546
592
|
|
|
547
593
|
extendString += ` LEFT JOIN ${joinedTableName} ON ${joinedTableName}.${foreignKey.to} = ${tableName}.${extendedField}`;
|
|
548
594
|
|
|
@@ -651,33 +697,30 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
651
697
|
|
|
652
698
|
let lookupField = _lookup_field;
|
|
653
699
|
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
700
|
+
try {
|
|
701
|
+
assertValidTableName(db, tableName);
|
|
702
|
+
|
|
703
|
+
if (!_lookup_field) {
|
|
704
|
+
// find the primary key of the table
|
|
657
705
|
lookupField = db
|
|
658
|
-
.prepare(
|
|
659
|
-
.all()
|
|
706
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
707
|
+
.all(tableName)
|
|
660
708
|
.find((field) => field.pk === 1).name;
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
message: error.message,
|
|
664
|
-
error: error,
|
|
665
|
-
});
|
|
709
|
+
} else {
|
|
710
|
+
assertValidColumnName(db, tableName, _lookup_field);
|
|
666
711
|
}
|
|
667
|
-
}
|
|
668
712
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
.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
|
+
}
|
|
679
722
|
|
|
680
|
-
if (
|
|
723
|
+
if (Object.keys(fields).length === 0) {
|
|
681
724
|
return res.status(400).json({
|
|
682
725
|
message: 'No fields provided',
|
|
683
726
|
error: 'no_fields_provided',
|
|
@@ -687,7 +730,7 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
687
730
|
try {
|
|
688
731
|
const data = rowService.update({
|
|
689
732
|
tableName,
|
|
690
|
-
|
|
733
|
+
fields,
|
|
691
734
|
lookupField,
|
|
692
735
|
pks,
|
|
693
736
|
});
|
|
@@ -743,19 +786,23 @@ const deleteRowInTableByPK = async (req, res, next) => {
|
|
|
743
786
|
|
|
744
787
|
let lookupField = _lookup_field;
|
|
745
788
|
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
789
|
+
try {
|
|
790
|
+
assertValidTableName(db, tableName);
|
|
791
|
+
|
|
792
|
+
if (!_lookup_field) {
|
|
793
|
+
// find the primary key of the table
|
|
749
794
|
lookupField = db
|
|
750
|
-
.prepare(
|
|
751
|
-
.all()
|
|
795
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
796
|
+
.all(tableName)
|
|
752
797
|
.find((field) => field.pk === 1).name;
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
message: error.message,
|
|
756
|
-
error: error,
|
|
757
|
-
});
|
|
798
|
+
} else {
|
|
799
|
+
assertValidColumnName(db, tableName, _lookup_field);
|
|
758
800
|
}
|
|
801
|
+
} catch (error) {
|
|
802
|
+
return res.status(error.status || 400).json({
|
|
803
|
+
message: error.message,
|
|
804
|
+
error: error,
|
|
805
|
+
});
|
|
759
806
|
}
|
|
760
807
|
|
|
761
808
|
try {
|
|
@@ -780,7 +827,7 @@ const deleteRowInTableByPK = async (req, res, next) => {
|
|
|
780
827
|
next();
|
|
781
828
|
}
|
|
782
829
|
} catch (error) {
|
|
783
|
-
res.status(400).json({
|
|
830
|
+
res.status(error.status || 400).json({
|
|
784
831
|
message: error.message,
|
|
785
832
|
error: error,
|
|
786
833
|
});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const { not } = require('joi');
|
|
2
1
|
const supertest = require('supertest');
|
|
3
2
|
|
|
4
3
|
const app = require('../index');
|
|
@@ -359,4 +358,92 @@ describe('Rows Endpoints', () => {
|
|
|
359
358
|
expect(res.body.data).toEqual(expect.any(Array));
|
|
360
359
|
expect(res.body.data.length).toEqual(1);
|
|
361
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
|
+
});
|
|
362
449
|
});
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
const db = require('../db/index');
|
|
2
|
+
const { dbConstants } = require('../constants');
|
|
3
|
+
const {
|
|
4
|
+
assertValidTableName,
|
|
5
|
+
getValidatedTableName,
|
|
6
|
+
quoteIdentifier,
|
|
7
|
+
quoteLiteral,
|
|
8
|
+
} = require('../utils/sql');
|
|
9
|
+
|
|
10
|
+
const { reservedTableNames } = dbConstants;
|
|
2
11
|
|
|
3
12
|
const createTable = async (req, res) => {
|
|
4
13
|
/*
|
|
@@ -37,7 +46,7 @@ const createTable = async (req, res) => {
|
|
|
37
46
|
}) => {
|
|
38
47
|
let column = `${name} ${type}`;
|
|
39
48
|
if (defaultValue) {
|
|
40
|
-
column += ` DEFAULT ${defaultValue}`;
|
|
49
|
+
column += ` DEFAULT ${quoteLiteral(defaultValue)}`;
|
|
41
50
|
}
|
|
42
51
|
if (notNull) {
|
|
43
52
|
column += ' NOT NULL';
|
|
@@ -62,7 +71,7 @@ const createTable = async (req, res) => {
|
|
|
62
71
|
}
|
|
63
72
|
|
|
64
73
|
return column;
|
|
65
|
-
}
|
|
74
|
+
},
|
|
66
75
|
)
|
|
67
76
|
.join(', ');
|
|
68
77
|
|
|
@@ -107,7 +116,9 @@ const createTable = async (req, res) => {
|
|
|
107
116
|
db.prepare(indicesString).run();
|
|
108
117
|
}
|
|
109
118
|
|
|
110
|
-
const generatedSchema = db
|
|
119
|
+
const generatedSchema = db
|
|
120
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
121
|
+
.all(tableName);
|
|
111
122
|
|
|
112
123
|
/*
|
|
113
124
|
#swagger.responses[201] = {
|
|
@@ -163,26 +174,36 @@ const listTables = async (req, res) => {
|
|
|
163
174
|
const { _search, _ordering } = req.query;
|
|
164
175
|
|
|
165
176
|
let query = `SELECT name FROM sqlite_master WHERE type IN ('table', 'view')`;
|
|
177
|
+
const queryValues = [];
|
|
166
178
|
|
|
167
179
|
// if search is provided, search the tables
|
|
168
180
|
// e.g. ?_search=users
|
|
169
181
|
if (_search) {
|
|
170
|
-
query += ` AND name LIKE
|
|
182
|
+
query += ` AND name LIKE ?`;
|
|
183
|
+
queryValues.push(`%${_search}%`);
|
|
171
184
|
}
|
|
172
185
|
|
|
173
186
|
// if ordering is provided, order the tables
|
|
174
187
|
// e.g. ?_ordering=name (ascending) or ?_ordering=-name (descending)
|
|
188
|
+
// `name` is the only column this query returns, so it's the only valid
|
|
189
|
+
// ordering target -- there's no live schema to validate an arbitrary
|
|
190
|
+
// column against here the way row/column ordering does.
|
|
175
191
|
if (_ordering) {
|
|
176
|
-
|
|
192
|
+
const isDesc = _ordering.startsWith('-');
|
|
193
|
+
const cleanOrder = _ordering.replace('-', '');
|
|
194
|
+
|
|
195
|
+
if (cleanOrder !== 'name') {
|
|
196
|
+
return res.status(400).json({
|
|
197
|
+
message: `Invalid ordering field '${cleanOrder}'. Only 'name' can be used to order tables.`,
|
|
198
|
+
error: 'invalid_ordering_field',
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
query += ` ORDER BY name ${isDesc ? 'DESC' : 'ASC'}`;
|
|
177
203
|
}
|
|
178
204
|
|
|
179
205
|
try {
|
|
180
|
-
const tables = db.prepare(query).all(
|
|
181
|
-
searchQuery: `%${_search}%`,
|
|
182
|
-
ordering: `${_ordering?.replace('-', '')} ${
|
|
183
|
-
_ordering?.startsWith('-') ? 'DESC' : 'ASC'
|
|
184
|
-
}`,
|
|
185
|
-
});
|
|
206
|
+
const tables = db.prepare(query).all(...queryValues);
|
|
186
207
|
|
|
187
208
|
res.json({
|
|
188
209
|
data: tables,
|
|
@@ -210,15 +231,27 @@ const getTableSchema = async (req, res) => {
|
|
|
210
231
|
|
|
211
232
|
*/
|
|
212
233
|
const { name: tableName } = req.params;
|
|
213
|
-
const query = `PRAGMA table_info(${tableName})`;
|
|
214
234
|
try {
|
|
215
|
-
|
|
235
|
+
assertValidTableName(db, tableName);
|
|
236
|
+
|
|
237
|
+
const schema = db
|
|
238
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
239
|
+
.all(tableName);
|
|
240
|
+
|
|
241
|
+
// Additive alongside `data` -- lets clients (e.g. Soul Studio) render
|
|
242
|
+
// foreign-key-aware widgets without probing `_extend` on the rows
|
|
243
|
+
// endpoint, which throws on a non-FK column instead of reporting what
|
|
244
|
+
// the FK columns actually are.
|
|
245
|
+
const foreignKeys = db
|
|
246
|
+
.prepare('SELECT * FROM pragma_foreign_key_list(?)')
|
|
247
|
+
.all(tableName);
|
|
216
248
|
|
|
217
249
|
res.json({
|
|
218
250
|
data: schema,
|
|
251
|
+
foreignKeys,
|
|
219
252
|
});
|
|
220
253
|
} catch (error) {
|
|
221
|
-
res.status(400).json({
|
|
254
|
+
res.status(error.status || 400).json({
|
|
222
255
|
message: error.message,
|
|
223
256
|
error: error,
|
|
224
257
|
});
|
|
@@ -240,16 +273,29 @@ const deleteTable = async (req, res) => {
|
|
|
240
273
|
|
|
241
274
|
*/
|
|
242
275
|
const { name: tableName } = req.params;
|
|
243
|
-
const query = `DROP TABLE ${tableName}`;
|
|
244
276
|
try {
|
|
245
|
-
|
|
277
|
+
if (reservedTableNames.includes(tableName)) {
|
|
278
|
+
return res.status(409).json({
|
|
279
|
+
message: `Table '${tableName}' is reserved and cannot be deleted`,
|
|
280
|
+
error: 'reserved_table_name',
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const validatedTableName = getValidatedTableName(db, tableName);
|
|
285
|
+
|
|
286
|
+
// DROP TABLE cannot take a bound parameter for its identifier in any
|
|
287
|
+
// SQL dialect. Use the canonical name returned from sqlite_master
|
|
288
|
+
// (parameterized lookup) and quote it as an identifier before use.
|
|
289
|
+
const data = db
|
|
290
|
+
.prepare(`DROP TABLE ${quoteIdentifier(validatedTableName)}`)
|
|
291
|
+
.run();
|
|
246
292
|
|
|
247
293
|
res.json({
|
|
248
294
|
message: 'Table deleted',
|
|
249
295
|
data,
|
|
250
296
|
});
|
|
251
297
|
} catch (error) {
|
|
252
|
-
res.status(400).json({
|
|
298
|
+
res.status(error.status || 400).json({
|
|
253
299
|
message: error.message,
|
|
254
300
|
error: error,
|
|
255
301
|
});
|