@galadrim/galadmin 0.2.7 → 0.2.8
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/dist/api.d.ts +3 -0
- package/dist/api.js +29 -5
- package/dist/api.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/api.ts +37 -7
- package/tests/column-roles.test.ts +86 -0
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -209,6 +209,21 @@ declare global {
|
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
+
// Columns of `view` the current role may not see (cf. the config middleware below).
|
|
213
|
+
export const hiddenColumnsOf = (view) => (view && view.__hiddenColumns) || [];
|
|
214
|
+
|
|
215
|
+
// Removes the hidden columns from a record before it is sent.
|
|
216
|
+
export const stripHiddenColumns = (view, record) => {
|
|
217
|
+
for (const name of hiddenColumnsOf(view)) delete record[name];
|
|
218
|
+
return record;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// Whether a write payload touches a hidden column.
|
|
222
|
+
export const touchesHiddenColumn = (view, data) =>
|
|
223
|
+
hiddenColumnsOf(view).some(
|
|
224
|
+
(name) => data && Object.prototype.hasOwnProperty.call(data, name),
|
|
225
|
+
);
|
|
226
|
+
|
|
212
227
|
export default (db, config) => {
|
|
213
228
|
const app = express();
|
|
214
229
|
|
|
@@ -243,14 +258,19 @@ export default (db, config) => {
|
|
|
243
258
|
})
|
|
244
259
|
.map((view) => {
|
|
245
260
|
if (view.type === "table") {
|
|
261
|
+
const allowed = (column) =>
|
|
262
|
+
!column.roles ||
|
|
263
|
+
(req.session.user &&
|
|
264
|
+
column.roles.includes(req.session.user.role));
|
|
246
265
|
return {
|
|
247
266
|
...view,
|
|
248
|
-
columns: view.columns.filter(
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
267
|
+
columns: view.columns.filter(allowed),
|
|
268
|
+
// SECURITY: a column's `roles` must hold server-side too. Rows are read with SELECT *, and
|
|
269
|
+
// writes take any column sent: the names of the columns this role may not see are kept
|
|
270
|
+
// here, stripped from every record sent back and refused in every write.
|
|
271
|
+
__hiddenColumns: view.columns
|
|
272
|
+
.filter((column) => !allowed(column))
|
|
273
|
+
.map((column) => column.name),
|
|
254
274
|
};
|
|
255
275
|
} else {
|
|
256
276
|
return view;
|
|
@@ -564,6 +584,7 @@ export default (db, config) => {
|
|
|
564
584
|
}
|
|
565
585
|
}
|
|
566
586
|
}
|
|
587
|
+
for (const record of records) stripHiddenColumns(view, record);
|
|
567
588
|
res.send({
|
|
568
589
|
records,
|
|
569
590
|
input,
|
|
@@ -678,6 +699,9 @@ export default (db, config) => {
|
|
|
678
699
|
}
|
|
679
700
|
}
|
|
680
701
|
}
|
|
702
|
+
if (columns.some((name) => hiddenColumnsOf(view).includes(name))) {
|
|
703
|
+
return res.sendStatus(403);
|
|
704
|
+
}
|
|
681
705
|
|
|
682
706
|
let queryString;
|
|
683
707
|
if (db.dbType === "POSTGRESQL") {
|
|
@@ -861,7 +885,7 @@ export default (db, config) => {
|
|
|
861
885
|
await column.formatValue(record);
|
|
862
886
|
}
|
|
863
887
|
}
|
|
864
|
-
res.send(record);
|
|
888
|
+
res.send(stripHiddenColumns(view, record));
|
|
865
889
|
} else {
|
|
866
890
|
res.sendStatus(404);
|
|
867
891
|
}
|
|
@@ -872,6 +896,9 @@ export default (db, config) => {
|
|
|
872
896
|
if (!areSafeColumnNames(req.body.data)) {
|
|
873
897
|
return res.status(400).send({ error: "Invalid column name" });
|
|
874
898
|
}
|
|
899
|
+
if (touchesHiddenColumn(view, req.body.data)) {
|
|
900
|
+
return res.sendStatus(403);
|
|
901
|
+
}
|
|
875
902
|
for (let key in req.body.data) {
|
|
876
903
|
if (
|
|
877
904
|
req.body.data[key] == "NULL" ||
|
|
@@ -1150,6 +1177,9 @@ export default (db, config) => {
|
|
|
1150
1177
|
const hasPanel =
|
|
1151
1178
|
req.body.panelIndex !== undefined && req.body.panelIndex !== null;
|
|
1152
1179
|
const panel = hasPanel ? view.recordViewPanels[req.body.panelIndex] : null;
|
|
1180
|
+
if (!hasPanel && touchesHiddenColumn(view, req.body.data)) {
|
|
1181
|
+
return res.sendStatus(403);
|
|
1182
|
+
}
|
|
1153
1183
|
try {
|
|
1154
1184
|
for (let key in req.body.data) {
|
|
1155
1185
|
if (
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import * as request from "supertest";
|
|
2
|
+
import { buildApiApp } from "./helpers/app";
|
|
3
|
+
import { FakeDb } from "./helpers/fakeDb";
|
|
4
|
+
import { baseConfig, fruitSchema, fruitView, schemaRow } from "./helpers/fixtures";
|
|
5
|
+
|
|
6
|
+
jest.mock("axios");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A column's `roles` must hold on the server, not only in the interface: rows are read with SELECT *, so a
|
|
10
|
+
* column hidden from a role must be stripped from what is sent back, and refused in writes.
|
|
11
|
+
*/
|
|
12
|
+
describe("column roles enforced server-side", () => {
|
|
13
|
+
let db: FakeDb;
|
|
14
|
+
const row = { id: 1, fruitName: "Pomme", acidity: 3, secret: "s3cr3t" };
|
|
15
|
+
const config = () =>
|
|
16
|
+
baseConfig({
|
|
17
|
+
views: [
|
|
18
|
+
fruitView({
|
|
19
|
+
roles: ["root", "user"],
|
|
20
|
+
columns: [
|
|
21
|
+
{ label: "Nom", name: "fruitName" },
|
|
22
|
+
{ label: "Acidité", name: "acidity" },
|
|
23
|
+
{ label: "Secret", name: "secret", roles: ["root"] },
|
|
24
|
+
],
|
|
25
|
+
}),
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
db = new FakeDb();
|
|
31
|
+
db.schemaRows = fruitSchema([schemaRow("fruits", "secret")]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("GET /api/record leaves the hidden column out for a role that may not see it", async () => {
|
|
35
|
+
db.queueRows([{ ...row }]);
|
|
36
|
+
const res = await request(buildApiApp(db, config(), { role: "user", userId: 7 }))
|
|
37
|
+
.get("/api/record")
|
|
38
|
+
.query({ viewIndex: 0, recordId: 1 })
|
|
39
|
+
.expect(200);
|
|
40
|
+
expect(res.body.fruitName).toBe("Pomme");
|
|
41
|
+
expect(res.body).not.toHaveProperty("secret");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("GET /api/record keeps it for a role that may see it", async () => {
|
|
45
|
+
db.queueRows([{ ...row }]);
|
|
46
|
+
const res = await request(buildApiApp(db, config(), { role: "root", userId: 1 }))
|
|
47
|
+
.get("/api/record")
|
|
48
|
+
.query({ viewIndex: 0, recordId: 1 })
|
|
49
|
+
.expect(200);
|
|
50
|
+
expect(res.body.secret).toBe("s3cr3t");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("POST /api/records leaves the hidden column out of every record", async () => {
|
|
54
|
+
db.defaultResult = { err: null, rows: [{ ...row, total: 1, count: 1 }] };
|
|
55
|
+
const res = await request(buildApiApp(db, config(), { role: "user", userId: 7 }))
|
|
56
|
+
.post("/api/records")
|
|
57
|
+
.send({ viewIndex: 0, page: 1, filters: [] })
|
|
58
|
+
.expect(200);
|
|
59
|
+
expect(res.body.records.length).toBeGreaterThan(0);
|
|
60
|
+
for (const record of res.body.records) expect(record).not.toHaveProperty("secret");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("POST /api/updateRecord refuses to write the hidden column", async () => {
|
|
64
|
+
await request(buildApiApp(db, config(), { role: "user", userId: 7 }))
|
|
65
|
+
.post("/api/updateRecord")
|
|
66
|
+
.send({ viewIndex: 0, primaryValue: 1, data: { secret: "x" } })
|
|
67
|
+
.expect(403);
|
|
68
|
+
expect(db.dataQueries().length).toBe(0);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("POST /api/updateRecord still writes the visible columns", async () => {
|
|
72
|
+
db.queueRows({ affectedRows: 1 });
|
|
73
|
+
await request(buildApiApp(db, config(), { role: "user", userId: 7 }))
|
|
74
|
+
.post("/api/updateRecord")
|
|
75
|
+
.send({ viewIndex: 0, primaryValue: 1, data: { fruitName: "Poire" } })
|
|
76
|
+
.expect((res) => expect(res.status).not.toBe(403));
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("POST /api/createRecord refuses to set the hidden column", async () => {
|
|
80
|
+
await request(buildApiApp(db, config(), { role: "user", userId: 7 }))
|
|
81
|
+
.post("/api/createRecord")
|
|
82
|
+
.send({ viewIndex: 0, data: { fruitName: "Kiwi", secret: "x" } })
|
|
83
|
+
.expect(403);
|
|
84
|
+
expect(db.dataQueries().length).toBe(0);
|
|
85
|
+
});
|
|
86
|
+
});
|