@terreno/admin-backend 57.2.0 → 57.4.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/.ai/guidelines/core.md +12 -4
- package/.claude/CLAUDE.local.md +17 -2
- package/.cursor/rules/00-root.mdc +17 -2
- package/.github/copilot-instructions.md +17 -2
- package/AGENTS.md +17 -2
- package/README.md +3 -1
- package/bunfig.toml +9 -1
- package/dist/adminApp.d.ts +20 -2
- package/dist/adminApp.js +480 -474
- package/dist/adminConfigIdentity.d.ts +1 -0
- package/dist/adminConfigIdentity.js +1 -0
- package/dist/adminMigrations.d.ts +10 -0
- package/dist/adminMigrations.js +205 -0
- package/dist/adminScriptJob.d.ts +32 -0
- package/dist/adminScriptJob.js +360 -0
- package/dist/adminTextSearch.d.ts +1 -0
- package/dist/adminTextSearch.js +1 -0
- package/dist/adminUiV2.d.ts +2 -0
- package/dist/aggregateAdmin.js +6 -1
- package/dist/documentStorageApp.js +1 -4
- package/dist/enrichAdminListFilters.d.ts +7 -0
- package/dist/enrichAdminListFilters.js +37 -0
- package/dist/filterParser.js +139 -12
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -1
- package/dist/legacy.js +4 -2
- package/dist/resolvedAdminModel.js +2 -1
- package/package.json +8 -8
- package/src/adminApp.migrations.test.ts +232 -0
- package/src/adminApp.models.test.ts +647 -5
- package/src/adminApp.syncMeta.test.ts +237 -0
- package/src/adminApp.test.ts +92 -0
- package/src/adminApp.ts +354 -240
- package/src/adminConfigIdentity.ts +1 -0
- package/src/adminHttpGoldens.test.ts +337 -0
- package/src/adminMigrations.ts +157 -0
- package/src/adminScriptJob.test.ts +297 -0
- package/src/adminScriptJob.ts +271 -0
- package/src/adminSyncMutationAuthMismatch.test.ts +650 -0
- package/src/adminTextSearch.test.ts +28 -0
- package/src/adminTextSearch.ts +1 -0
- package/src/adminUiV2.test.ts +71 -1
- package/src/adminUiV2.ts +2 -0
- package/src/aggregateAdmin.test.ts +125 -0
- package/src/aggregateAdmin.ts +7 -4
- package/src/documentStorageApp.ts +1 -3
- package/src/enrichAdminListFilters.test.ts +44 -0
- package/src/enrichAdminListFilters.ts +29 -0
- package/src/filterParser.test.ts +95 -0
- package/src/filterParser.ts +156 -10
- package/src/index.ts +2 -0
- package/src/legacy.ts +5 -1
- package/src/resolvedAdminModel.ts +2 -1
package/src/adminUiV2.test.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import {describe, expect, it} from "bun:test";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
buildAdminModelQueryFields,
|
|
5
|
+
defaultBulkPatchAllowlistFrom,
|
|
6
|
+
normalizeAdminHome,
|
|
7
|
+
normalizeSidebarRecentLast,
|
|
8
|
+
SYSTEM_ADMIN_FIELDS,
|
|
9
|
+
} from "./adminUiV2";
|
|
4
10
|
|
|
5
11
|
describe("buildAdminModelQueryFields", () => {
|
|
6
12
|
it("includes _id, list fields, listDisplay, searchFields, and filter fields", () => {
|
|
@@ -27,3 +33,67 @@ describe("buildAdminModelQueryFields", () => {
|
|
|
27
33
|
expect(new Set(fields).size).toBe(fields.length);
|
|
28
34
|
});
|
|
29
35
|
});
|
|
36
|
+
|
|
37
|
+
describe("normalizeSidebarRecentLast", () => {
|
|
38
|
+
it("returns empty and missing sidebars unchanged", () => {
|
|
39
|
+
expect(normalizeSidebarRecentLast(undefined)).toBeUndefined();
|
|
40
|
+
expect(normalizeSidebarRecentLast([])).toEqual([]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("leaves a sidebar without recentActivity as-is", () => {
|
|
44
|
+
expect(normalizeSidebarRecentLast(["modelsGrid"])).toEqual(["modelsGrid"]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("moves recentActivity to the end", () => {
|
|
48
|
+
expect(normalizeSidebarRecentLast(["recentActivity", "modelsGrid"])).toEqual([
|
|
49
|
+
"modelsGrid",
|
|
50
|
+
"recentActivity",
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("normalizeAdminHome", () => {
|
|
56
|
+
it("defaults to modelsGrid when home is omitted", () => {
|
|
57
|
+
expect(normalizeAdminHome()).toEqual({
|
|
58
|
+
slots: {main: ["modelsGrid"]},
|
|
59
|
+
title: "Administration",
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("uses provided slots and normalizes the sidebar", () => {
|
|
64
|
+
expect(
|
|
65
|
+
normalizeAdminHome({
|
|
66
|
+
slots: {sidebar: ["recentActivity", "nav"]},
|
|
67
|
+
title: "Ops",
|
|
68
|
+
})
|
|
69
|
+
).toEqual({
|
|
70
|
+
slots: {sidebar: ["nav", "recentActivity"]},
|
|
71
|
+
title: "Ops",
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("promotes legacy widgets into main and prepends modelsGrid when missing", () => {
|
|
76
|
+
expect(normalizeAdminHome({widgets: ["recentActivity"]})).toEqual({
|
|
77
|
+
slots: {main: ["modelsGrid", "recentActivity"]},
|
|
78
|
+
title: "Administration",
|
|
79
|
+
});
|
|
80
|
+
expect(normalizeAdminHome({widgets: ["modelsGrid", "recentActivity"]})).toEqual({
|
|
81
|
+
slots: {main: ["modelsGrid", "recentActivity"]},
|
|
82
|
+
title: "Administration",
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("defaultBulkPatchAllowlistFrom", () => {
|
|
88
|
+
it("keeps writable schema fields from the list", () => {
|
|
89
|
+
expect(
|
|
90
|
+
defaultBulkPatchAllowlistFrom({
|
|
91
|
+
hiddenFieldSet: new Set(["secret"]),
|
|
92
|
+
listFields: ["_id", "title", "secret", "created", "notes"],
|
|
93
|
+
readonlyFields: ["notes"],
|
|
94
|
+
schemaPaths: new Set(["title", "secret", "notes"]),
|
|
95
|
+
})
|
|
96
|
+
).toEqual(["title"]);
|
|
97
|
+
expect(SYSTEM_ADMIN_FIELDS.has("_id")).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
});
|
package/src/adminUiV2.ts
CHANGED
|
@@ -78,6 +78,8 @@ export interface AdminListFilterBoolean {
|
|
|
78
78
|
|
|
79
79
|
export interface AdminListFilterChoice {
|
|
80
80
|
choices: {label: string; value: string}[];
|
|
81
|
+
/** When true, filter UI offers Empty and parser accepts `__empty__` sentinel. */
|
|
82
|
+
allowEmpty?: boolean;
|
|
81
83
|
field: string;
|
|
82
84
|
kind: "choice";
|
|
83
85
|
label?: string;
|
|
@@ -2,11 +2,13 @@ import {describe, expect, it} from "bun:test";
|
|
|
2
2
|
import type {AdminConfig, AdminContribution, TerrenoPlugin} from "@terreno/api";
|
|
3
3
|
import {modelRouter, Permissions} from "@terreno/api";
|
|
4
4
|
import {FoodModel} from "@terreno/api/testing";
|
|
5
|
+
import {assert} from "chai";
|
|
5
6
|
|
|
6
7
|
import type {AdminModelConfig} from "./adminApp";
|
|
7
8
|
import {
|
|
8
9
|
aggregateAdminContributions,
|
|
9
10
|
aggregateFromTerrenoApp,
|
|
11
|
+
collectPluginAdminContributions,
|
|
10
12
|
collectRegisteredAdminModels,
|
|
11
13
|
} from "./aggregateAdmin";
|
|
12
14
|
import {resetLegacyDeprecationWarningsForTests} from "./legacy";
|
|
@@ -75,6 +77,56 @@ describe("aggregateAdminContributions", () => {
|
|
|
75
77
|
expect(aggregated.widgetIds).toEqual(["feature-flags-overrides"]);
|
|
76
78
|
});
|
|
77
79
|
|
|
80
|
+
it("forwards custom screen group metadata from plugin contributions", () => {
|
|
81
|
+
const aggregated = aggregateAdminContributions({
|
|
82
|
+
pluginContributions: [
|
|
83
|
+
{
|
|
84
|
+
customScreens: [
|
|
85
|
+
{
|
|
86
|
+
displayName: "Overview",
|
|
87
|
+
group: "Announcements",
|
|
88
|
+
icon: "bullhorn",
|
|
89
|
+
name: "announcements",
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
assert.equal(aggregated.customScreens[0]?.group, "Announcements");
|
|
97
|
+
assert.equal(aggregated.customScreens[0]?.icon, "bullhorn");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("merges unique scripts, screens, and widget ids and skips duplicates", () => {
|
|
101
|
+
const runner = async (): Promise<void> => {};
|
|
102
|
+
const aggregated = aggregateAdminContributions({
|
|
103
|
+
pluginContributions: [
|
|
104
|
+
{
|
|
105
|
+
customScreens: [{displayName: "Docs", name: "documents"}],
|
|
106
|
+
homeWidgets: [{displayName: "A", id: "w1"}],
|
|
107
|
+
scripts: [{description: "one", name: "seed", runner}],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
customScreens: [
|
|
111
|
+
{displayName: "Docs again", name: "documents"},
|
|
112
|
+
{displayName: "Flags", name: "flags"},
|
|
113
|
+
],
|
|
114
|
+
homeWidgets: [
|
|
115
|
+
{displayName: "A", id: "w1"},
|
|
116
|
+
{displayName: "B", id: "w2"},
|
|
117
|
+
],
|
|
118
|
+
scripts: [
|
|
119
|
+
{description: "dup", name: "seed", runner},
|
|
120
|
+
{description: "two", name: "wipe", runner},
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
});
|
|
125
|
+
expect(aggregated.customScreens.map((screen) => screen.name)).toEqual(["documents", "flags"]);
|
|
126
|
+
expect(aggregated.scripts.map((script) => script.name)).toEqual(["seed", "wipe"]);
|
|
127
|
+
expect(aggregated.widgetIds).toEqual(["w1", "w2"]);
|
|
128
|
+
});
|
|
129
|
+
|
|
78
130
|
it("forwards populatePaths from plugin model contributions", () => {
|
|
79
131
|
const aggregated = aggregateAdminContributions({
|
|
80
132
|
pluginContributions: [
|
|
@@ -127,6 +179,57 @@ describe("aggregateAdminContributions", () => {
|
|
|
127
179
|
})
|
|
128
180
|
).toThrow(/Duplicate admin modelRouter routePath/);
|
|
129
181
|
});
|
|
182
|
+
|
|
183
|
+
it("merges plugin scripts and custom screens, skipping duplicate names", () => {
|
|
184
|
+
const pluginContribution: AdminContribution = {
|
|
185
|
+
customScreens: [
|
|
186
|
+
{displayName: "Flags", name: "feature-flags"},
|
|
187
|
+
{displayName: "Flags again", name: "feature-flags"},
|
|
188
|
+
],
|
|
189
|
+
scripts: [
|
|
190
|
+
{
|
|
191
|
+
description: "Seed foods",
|
|
192
|
+
name: "seed-foods",
|
|
193
|
+
runner: async () => ({results: [], success: true}),
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
description: "Duplicate seed",
|
|
197
|
+
name: "seed-foods",
|
|
198
|
+
runner: async () => ({results: [], success: true}),
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const aggregated = aggregateAdminContributions({
|
|
204
|
+
pluginContributions: [pluginContribution],
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
assert.deepEqual(
|
|
208
|
+
aggregated.customScreens.map((screen) => screen.name),
|
|
209
|
+
["feature-flags"]
|
|
210
|
+
);
|
|
211
|
+
assert.equal(aggregated.customScreens[0]?.displayName, "Flags");
|
|
212
|
+
assert.deepEqual(
|
|
213
|
+
aggregated.scripts.map((script) => script.name),
|
|
214
|
+
["seed-foods"]
|
|
215
|
+
);
|
|
216
|
+
assert.equal(aggregated.scripts[0]?.description, "Seed foods");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("skips duplicate home widget ids", () => {
|
|
220
|
+
const aggregated = aggregateAdminContributions({
|
|
221
|
+
pluginContributions: [
|
|
222
|
+
{
|
|
223
|
+
homeWidgets: [
|
|
224
|
+
{displayName: "Overrides", id: "feature-flags-overrides"},
|
|
225
|
+
{displayName: "Overrides again", id: "feature-flags-overrides"},
|
|
226
|
+
],
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
assert.deepEqual(aggregated.widgetIds, ["feature-flags-overrides"]);
|
|
232
|
+
});
|
|
130
233
|
});
|
|
131
234
|
|
|
132
235
|
describe("aggregateFromTerrenoApp", () => {
|
|
@@ -153,6 +256,16 @@ describe("aggregateFromTerrenoApp", () => {
|
|
|
153
256
|
expect(aggregated.models).toHaveLength(1);
|
|
154
257
|
expect(aggregated.models[0]?.routePath).toBe("/feature-flags");
|
|
155
258
|
});
|
|
259
|
+
|
|
260
|
+
it("ignores plugins without an admin contribution", () => {
|
|
261
|
+
expect(
|
|
262
|
+
collectPluginAdminContributions({
|
|
263
|
+
getPlugins: () => [{register() {}}],
|
|
264
|
+
getRegistrations: () => [],
|
|
265
|
+
} as never)
|
|
266
|
+
).toEqual([]);
|
|
267
|
+
expect(collectPluginAdminContributions()).toEqual([]);
|
|
268
|
+
});
|
|
156
269
|
});
|
|
157
270
|
|
|
158
271
|
const anyPermissions = {
|
|
@@ -164,6 +277,18 @@ const anyPermissions = {
|
|
|
164
277
|
};
|
|
165
278
|
|
|
166
279
|
describe("collectRegisteredAdminModels", () => {
|
|
280
|
+
it("returns an empty list without a TerrenoApp", () => {
|
|
281
|
+
expect(collectRegisteredAdminModels()).toEqual([]);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("skips non-modelRouter registrations", () => {
|
|
285
|
+
const registered = collectRegisteredAdminModels({
|
|
286
|
+
getPlugins: () => [],
|
|
287
|
+
getRegistrations: () => [{__type: "plugin", register() {}} as never],
|
|
288
|
+
} as never);
|
|
289
|
+
expect(registered).toEqual([]);
|
|
290
|
+
});
|
|
291
|
+
|
|
167
292
|
it("does not copy the public queryFilter onto admin CRUD", () => {
|
|
168
293
|
const registered = collectRegisteredAdminModels({
|
|
169
294
|
getPlugins: () => [],
|
package/src/aggregateAdmin.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
TerrenoApp,
|
|
8
8
|
TerrenoPlugin,
|
|
9
9
|
} from "@terreno/api";
|
|
10
|
-
import {logger} from "@terreno/api";
|
|
10
|
+
import {APIError, logger} from "@terreno/api";
|
|
11
11
|
|
|
12
12
|
import type {AdminCustomScreenConfig, AdminModelConfig, AdminScriptConfig} from "./adminApp";
|
|
13
13
|
import {convertLegacyModelConfig} from "./legacy";
|
|
@@ -37,6 +37,8 @@ const contributionScriptToConfig = (script: AdminScriptContribution): AdminScrip
|
|
|
37
37
|
const contributionScreenToConfig = (screen: AdminCustomScreen): AdminCustomScreenConfig => ({
|
|
38
38
|
adminAccess: screen.adminAccess,
|
|
39
39
|
displayName: screen.displayName,
|
|
40
|
+
group: screen.group,
|
|
41
|
+
icon: screen.icon,
|
|
40
42
|
name: screen.name,
|
|
41
43
|
});
|
|
42
44
|
|
|
@@ -133,9 +135,10 @@ export const aggregateAdminContributions = ({
|
|
|
133
135
|
const path = registered.routePath;
|
|
134
136
|
const existingRegistered = registeredByPath.get(path);
|
|
135
137
|
if (existingRegistered) {
|
|
136
|
-
throw new
|
|
137
|
-
|
|
138
|
-
|
|
138
|
+
throw new APIError({
|
|
139
|
+
status: 500,
|
|
140
|
+
title: `Duplicate admin modelRouter routePath "${path}" (${existingRegistered.sourceLabel} and ${registered.sourceLabel})`,
|
|
141
|
+
});
|
|
139
142
|
}
|
|
140
143
|
registeredByPath.set(path, registered);
|
|
141
144
|
const existing = modelsByPath.get(path);
|
|
@@ -192,9 +192,7 @@ export class DocumentStorageApp implements TerrenoPlugin {
|
|
|
192
192
|
app.post(
|
|
193
193
|
`${basePath}/`,
|
|
194
194
|
...adminGuard,
|
|
195
|
-
|
|
196
|
-
// biome-ignore lint/suspicious/noExplicitAny: multer's RequestHandler type is incompatible with Express 5's middleware signature.
|
|
197
|
-
upload.single("file") as any,
|
|
195
|
+
upload.single("file") as unknown as express.RequestHandler,
|
|
198
196
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
199
197
|
const file = (req as unknown as {file?: Express.Multer.File}).file;
|
|
200
198
|
if (!file) {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
import type {AdminListFilter} from "./adminUiV2";
|
|
4
|
+
import {enrichAdminListFilters} from "./enrichAdminListFilters";
|
|
5
|
+
|
|
6
|
+
const prioritySchema = new mongoose.Schema({
|
|
7
|
+
name: {required: true, type: String},
|
|
8
|
+
priority: {enum: ["low", "high"], type: String},
|
|
9
|
+
status: {enum: ["open", "closed"], required: true, type: String},
|
|
10
|
+
});
|
|
11
|
+
const PriorityModel =
|
|
12
|
+
mongoose.models.AdminEnrichPriority ?? mongoose.model("AdminEnrichPriority", prioritySchema);
|
|
13
|
+
|
|
14
|
+
describe("enrichAdminListFilters", () => {
|
|
15
|
+
it("sets allowEmpty for optional choice filters from the schema", () => {
|
|
16
|
+
const filters: AdminListFilter[] = [
|
|
17
|
+
{
|
|
18
|
+
choices: [{label: "High", value: "high"}],
|
|
19
|
+
field: "priority",
|
|
20
|
+
kind: "choice",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
choices: [{label: "Open", value: "open"}],
|
|
24
|
+
field: "status",
|
|
25
|
+
kind: "choice",
|
|
26
|
+
},
|
|
27
|
+
];
|
|
28
|
+
const enriched = enrichAdminListFilters(filters, PriorityModel);
|
|
29
|
+
expect(enriched?.[0]).toEqual({...filters[0], allowEmpty: true});
|
|
30
|
+
expect(enriched?.[1]).toEqual(filters[1]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("preserves explicit allowEmpty", () => {
|
|
34
|
+
const filters: AdminListFilter[] = [
|
|
35
|
+
{
|
|
36
|
+
allowEmpty: false,
|
|
37
|
+
choices: [{label: "High", value: "high"}],
|
|
38
|
+
field: "priority",
|
|
39
|
+
kind: "choice",
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
expect(enrichAdminListFilters(filters, PriorityModel)).toEqual(filters);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type {Model} from "mongoose";
|
|
2
|
+
|
|
3
|
+
import type {AdminListFilter} from "./adminUiV2";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Derives `allowEmpty` for optional choice filters from the Mongoose schema when not
|
|
7
|
+
* set explicitly on admin config.
|
|
8
|
+
*/
|
|
9
|
+
export const enrichAdminListFilters = (
|
|
10
|
+
filters: AdminListFilter[] | undefined,
|
|
11
|
+
model: Model<unknown>
|
|
12
|
+
): AdminListFilter[] | undefined => {
|
|
13
|
+
if (!filters) {
|
|
14
|
+
return filters;
|
|
15
|
+
}
|
|
16
|
+
return filters.map((filter) => {
|
|
17
|
+
if (filter.kind !== "choice") {
|
|
18
|
+
return filter;
|
|
19
|
+
}
|
|
20
|
+
if (filter.allowEmpty !== undefined) {
|
|
21
|
+
return filter;
|
|
22
|
+
}
|
|
23
|
+
const path = model.schema.path(filter.field);
|
|
24
|
+
if (!path || path.isRequired) {
|
|
25
|
+
return filter;
|
|
26
|
+
}
|
|
27
|
+
return {...filter, allowEmpty: true};
|
|
28
|
+
});
|
|
29
|
+
};
|
package/src/filterParser.test.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {describe, expect, it} from "bun:test";
|
|
2
2
|
|
|
3
|
+
import {ADMIN_LIST_CHOICE_EMPTY_VALUE} from "@terreno/api";
|
|
4
|
+
|
|
3
5
|
import type {AdminListFilter} from "./adminUiV2";
|
|
4
6
|
import {parseAdminListFilters} from "./filterParser";
|
|
5
7
|
|
|
@@ -53,6 +55,59 @@ describe("parseAdminListFilters", () => {
|
|
|
53
55
|
expect(errors.$where).toBeDefined();
|
|
54
56
|
});
|
|
55
57
|
|
|
58
|
+
it("parses choice $in and text $regex filters", () => {
|
|
59
|
+
const {errors, filter} = parseAdminListFilters(
|
|
60
|
+
{
|
|
61
|
+
name: {$options: "i", $regex: "ali"},
|
|
62
|
+
role: {$in: ["staff", "admin"]},
|
|
63
|
+
},
|
|
64
|
+
[
|
|
65
|
+
{
|
|
66
|
+
choices: [
|
|
67
|
+
{label: "Staff", value: "staff"},
|
|
68
|
+
{label: "Admin", value: "admin"},
|
|
69
|
+
],
|
|
70
|
+
field: "role",
|
|
71
|
+
kind: "choice",
|
|
72
|
+
},
|
|
73
|
+
{field: "name", kind: "text"},
|
|
74
|
+
]
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
expect(errors).toEqual({});
|
|
78
|
+
expect(filter.role).toEqual({$in: ["staff", "admin"]});
|
|
79
|
+
expect(filter.name).toEqual({$options: "i", $regex: "ali"});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("rejects invalid choice $in values and top-level $or", () => {
|
|
83
|
+
const {errors} = parseAdminListFilters(
|
|
84
|
+
{
|
|
85
|
+
$or: [{name: "alice"}],
|
|
86
|
+
role: {$in: ["missing"]},
|
|
87
|
+
},
|
|
88
|
+
[{choices: [{label: "Staff", value: "staff"}], field: "role", kind: "choice"}]
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(errors.role).toBeDefined();
|
|
92
|
+
expect(errors.$or).toBeDefined();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("rejects executable regex patterns and undeclared nested operators", () => {
|
|
96
|
+
const {errors} = parseAdminListFilters(
|
|
97
|
+
{
|
|
98
|
+
name: {$options: "i", $regex: ".*"},
|
|
99
|
+
role: {$in: ["staff"], $ne: "admin"},
|
|
100
|
+
},
|
|
101
|
+
[
|
|
102
|
+
{choices: [{label: "Staff", value: "staff"}], field: "role", kind: "choice"},
|
|
103
|
+
{field: "name", kind: "text"},
|
|
104
|
+
]
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
expect(errors.name).toBeDefined();
|
|
108
|
+
expect(errors.role).toBeDefined();
|
|
109
|
+
});
|
|
110
|
+
|
|
56
111
|
it("drops prototype pollution keys without surfacing them as filter errors", () => {
|
|
57
112
|
const {errors, filter} = parseAdminListFilters(
|
|
58
113
|
{
|
|
@@ -65,4 +120,44 @@ describe("parseAdminListFilters", () => {
|
|
|
65
120
|
expect(errors).toEqual({});
|
|
66
121
|
expect(filter.admin).toBe(true);
|
|
67
122
|
});
|
|
123
|
+
|
|
124
|
+
it("maps empty sentinel to null for optional choice filters", () => {
|
|
125
|
+
const priorityFilter: AdminListFilter = {
|
|
126
|
+
allowEmpty: true,
|
|
127
|
+
choices: [
|
|
128
|
+
{label: "High", value: "high"},
|
|
129
|
+
{label: "Low", value: "low"},
|
|
130
|
+
],
|
|
131
|
+
field: "priority",
|
|
132
|
+
kind: "choice",
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const emptyOnly = parseAdminListFilters({priority: {$in: [ADMIN_LIST_CHOICE_EMPTY_VALUE]}}, [
|
|
136
|
+
priorityFilter,
|
|
137
|
+
]);
|
|
138
|
+
expect(emptyOnly.errors).toEqual({});
|
|
139
|
+
expect(emptyOnly.filter.priority).toBeNull();
|
|
140
|
+
|
|
141
|
+
const highOnly = parseAdminListFilters({priority: "high"}, [priorityFilter]);
|
|
142
|
+
expect(highOnly.errors).toEqual({});
|
|
143
|
+
expect(highOnly.filter.priority).toBe("high");
|
|
144
|
+
|
|
145
|
+
const combined = parseAdminListFilters(
|
|
146
|
+
{priority: {$in: ["high", ADMIN_LIST_CHOICE_EMPTY_VALUE]}},
|
|
147
|
+
[priorityFilter]
|
|
148
|
+
);
|
|
149
|
+
expect(combined.errors).toEqual({});
|
|
150
|
+
expect(combined.filter.priority).toEqual({$in: ["high", null]});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("rejects empty sentinel when allowEmpty is false", () => {
|
|
154
|
+
const {errors} = parseAdminListFilters({priority: {$in: [ADMIN_LIST_CHOICE_EMPTY_VALUE]}}, [
|
|
155
|
+
{
|
|
156
|
+
choices: [{label: "High", value: "high"}],
|
|
157
|
+
field: "priority",
|
|
158
|
+
kind: "choice",
|
|
159
|
+
},
|
|
160
|
+
]);
|
|
161
|
+
expect(errors.priority).toBeDefined();
|
|
162
|
+
});
|
|
68
163
|
});
|
package/src/filterParser.ts
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
|
-
import type {AdminFilter} from "@terreno/api";
|
|
2
|
-
import {logger} from "@terreno/api";
|
|
1
|
+
import type {AdminFilter, AdminFilterChoice} from "@terreno/api";
|
|
2
|
+
import {ADMIN_LIST_CHOICE_EMPTY_VALUE, logger} from "@terreno/api";
|
|
3
3
|
import {DateTime} from "luxon";
|
|
4
4
|
import mongoose from "mongoose";
|
|
5
5
|
|
|
6
6
|
const POLLUTION_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
7
|
+
const REGEX_META_CHARACTERS = new Set([
|
|
8
|
+
"\\",
|
|
9
|
+
".",
|
|
10
|
+
"*",
|
|
11
|
+
"+",
|
|
12
|
+
"?",
|
|
13
|
+
"^",
|
|
14
|
+
"$",
|
|
15
|
+
"{",
|
|
16
|
+
"}",
|
|
17
|
+
"(",
|
|
18
|
+
")",
|
|
19
|
+
"|",
|
|
20
|
+
"[",
|
|
21
|
+
"]",
|
|
22
|
+
]);
|
|
7
23
|
|
|
8
24
|
const isOperatorKey = (key: string): boolean => {
|
|
9
25
|
return key.startsWith("$");
|
|
@@ -31,6 +47,29 @@ const scalarString = (value: unknown): string | undefined => {
|
|
|
31
47
|
return undefined;
|
|
32
48
|
};
|
|
33
49
|
|
|
50
|
+
const hasOnlyKeys = (record: Record<string, unknown>, allowedKeys: string[]): boolean => {
|
|
51
|
+
const allowed = new Set(allowedKeys);
|
|
52
|
+
return Object.keys(record).every((key) => allowed.has(key));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const isEscapedRegexLiteral = (value: string): boolean => {
|
|
56
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
57
|
+
const character = value[index];
|
|
58
|
+
if (character === "\\") {
|
|
59
|
+
const escapedCharacter = value[index + 1];
|
|
60
|
+
if (!escapedCharacter || !REGEX_META_CHARACTERS.has(escapedCharacter)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
index += 1;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (REGEX_META_CHARACTERS.has(character)) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
};
|
|
72
|
+
|
|
34
73
|
const parseBooleanFilter = (
|
|
35
74
|
field: string,
|
|
36
75
|
value: unknown
|
|
@@ -60,20 +99,115 @@ const parseTextFilter = (
|
|
|
60
99
|
return {ok: true, value: scalar};
|
|
61
100
|
};
|
|
62
101
|
|
|
102
|
+
const parseTextRegexFilter = (
|
|
103
|
+
field: string,
|
|
104
|
+
value: unknown
|
|
105
|
+
): {ok: true; value: {$options: "i"; $regex: string}} | {ok: false; error: string} => {
|
|
106
|
+
if (value == null || typeof value !== "object" || Array.isArray(value)) {
|
|
107
|
+
return {error: `${field} must be a case-insensitive regex filter`, ok: false};
|
|
108
|
+
}
|
|
109
|
+
const record = value as Record<string, unknown> & {$options?: unknown; $regex?: unknown};
|
|
110
|
+
if (!hasOnlyKeys(record, ["$options", "$regex"])) {
|
|
111
|
+
return {error: `${field} contains an unsupported regex operator`, ok: false};
|
|
112
|
+
}
|
|
113
|
+
if (record.$options !== "i") {
|
|
114
|
+
return {error: `${field} must use $options "i"`, ok: false};
|
|
115
|
+
}
|
|
116
|
+
const regex = scalarString(record.$regex);
|
|
117
|
+
if (!regex || !isEscapedRegexLiteral(regex)) {
|
|
118
|
+
return {error: `${field} must include an escaped literal $regex pattern`, ok: false};
|
|
119
|
+
}
|
|
120
|
+
return {ok: true, value: {$options: "i", $regex: regex}};
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
type ChoiceMongoValue = null | string | {$in: (null | string)[]};
|
|
124
|
+
|
|
125
|
+
const normalizeChoiceMongoValue = (values: (null | string)[]): ChoiceMongoValue => {
|
|
126
|
+
if (values.length === 1) {
|
|
127
|
+
return values[0];
|
|
128
|
+
}
|
|
129
|
+
return {$in: values};
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const parseChoiceWireValue = (
|
|
133
|
+
field: string,
|
|
134
|
+
wireValue: string,
|
|
135
|
+
choices: {label: string; value: string}[],
|
|
136
|
+
allowEmpty: boolean
|
|
137
|
+
): {ok: true; value: null | string} | {ok: false; error: string} => {
|
|
138
|
+
if (wireValue === ADMIN_LIST_CHOICE_EMPTY_VALUE) {
|
|
139
|
+
if (!allowEmpty) {
|
|
140
|
+
return {
|
|
141
|
+
error: `${field} must be one of: ${choices.map((choice) => choice.value).join(", ")}`,
|
|
142
|
+
ok: false,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return {ok: true, value: null};
|
|
146
|
+
}
|
|
147
|
+
const allowed = new Set(choices.map((choice) => choice.value));
|
|
148
|
+
if (!allowed.has(wireValue)) {
|
|
149
|
+
return {error: `${field} must be one of: ${[...allowed].join(", ")}`, ok: false};
|
|
150
|
+
}
|
|
151
|
+
return {ok: true, value: wireValue};
|
|
152
|
+
};
|
|
153
|
+
|
|
63
154
|
const parseChoiceFilter = (
|
|
64
155
|
field: string,
|
|
65
156
|
value: unknown,
|
|
66
|
-
|
|
67
|
-
): {ok: true; value:
|
|
157
|
+
declared: AdminFilterChoice
|
|
158
|
+
): {ok: true; value: ChoiceMongoValue} | {ok: false; error: string} => {
|
|
68
159
|
const parsed = parseTextFilter(field, value);
|
|
69
160
|
if (!parsed.ok) {
|
|
70
161
|
return parsed;
|
|
71
162
|
}
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
163
|
+
const wire = parseChoiceWireValue(
|
|
164
|
+
field,
|
|
165
|
+
parsed.value,
|
|
166
|
+
declared.choices,
|
|
167
|
+
declared.allowEmpty === true
|
|
168
|
+
);
|
|
169
|
+
if (!wire.ok) {
|
|
170
|
+
return wire;
|
|
75
171
|
}
|
|
76
|
-
return
|
|
172
|
+
return {ok: true, value: wire.value};
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const parseChoiceInFilter = (
|
|
176
|
+
field: string,
|
|
177
|
+
value: unknown,
|
|
178
|
+
declared: AdminFilterChoice
|
|
179
|
+
): {ok: true; value: ChoiceMongoValue} | {ok: false; error: string} => {
|
|
180
|
+
if (value == null || typeof value !== "object" || Array.isArray(value)) {
|
|
181
|
+
return {error: `${field} must be a multi-value choice filter`, ok: false};
|
|
182
|
+
}
|
|
183
|
+
const record = value as Record<string, unknown> & {$in?: unknown};
|
|
184
|
+
if (!hasOnlyKeys(record, ["$in"])) {
|
|
185
|
+
return {error: `${field} contains an unsupported choice operator`, ok: false};
|
|
186
|
+
}
|
|
187
|
+
if (!Array.isArray(record.$in)) {
|
|
188
|
+
return {error: `${field} must use $in with string values`, ok: false};
|
|
189
|
+
}
|
|
190
|
+
const mongoValues: (null | string)[] = [];
|
|
191
|
+
for (const entry of record.$in) {
|
|
192
|
+
const scalar = scalarString(entry);
|
|
193
|
+
if (!scalar) {
|
|
194
|
+
return {error: `${field} must use $in with string values`, ok: false};
|
|
195
|
+
}
|
|
196
|
+
const wire = parseChoiceWireValue(
|
|
197
|
+
field,
|
|
198
|
+
scalar,
|
|
199
|
+
declared.choices,
|
|
200
|
+
declared.allowEmpty === true
|
|
201
|
+
);
|
|
202
|
+
if (!wire.ok) {
|
|
203
|
+
return wire;
|
|
204
|
+
}
|
|
205
|
+
mongoValues.push(wire.value);
|
|
206
|
+
}
|
|
207
|
+
if (mongoValues.length === 0) {
|
|
208
|
+
return {error: `${field} must include at least one choice`, ok: false};
|
|
209
|
+
}
|
|
210
|
+
return {ok: true, value: normalizeChoiceMongoValue(mongoValues)};
|
|
77
211
|
};
|
|
78
212
|
|
|
79
213
|
const parseRefFilter = (
|
|
@@ -149,7 +283,13 @@ export const parseAdminListFilters = (
|
|
|
149
283
|
continue;
|
|
150
284
|
}
|
|
151
285
|
consumedKeys.add(field);
|
|
152
|
-
const
|
|
286
|
+
const raw = safeQuery[field];
|
|
287
|
+
const regexParsed = parseTextRegexFilter(field, raw);
|
|
288
|
+
if (regexParsed.ok) {
|
|
289
|
+
filter[field] = regexParsed.value;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const parsed = parseTextFilter(field, raw);
|
|
153
293
|
if (!parsed.ok) {
|
|
154
294
|
errors[field] = parsed.error;
|
|
155
295
|
continue;
|
|
@@ -163,7 +303,13 @@ export const parseAdminListFilters = (
|
|
|
163
303
|
continue;
|
|
164
304
|
}
|
|
165
305
|
consumedKeys.add(field);
|
|
166
|
-
const
|
|
306
|
+
const raw = safeQuery[field];
|
|
307
|
+
const inParsed = parseChoiceInFilter(field, raw, declared);
|
|
308
|
+
if (inParsed.ok) {
|
|
309
|
+
filter[field] = inParsed.value;
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
const parsed = parseChoiceFilter(field, raw, declared);
|
|
167
313
|
if (!parsed.ok) {
|
|
168
314
|
errors[field] = parsed.error;
|
|
169
315
|
continue;
|