@jskit-ai/crud-core 0.2.61 → 0.2.63

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/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - Each CRUD route names the action it runs (`actionId`, `crud.<namespace>.<operation>` or the custom action's id), so a signed-in CRUD route answers a caller without the action's permission 403 before validating the query, path or body, and before refusing an undeclared list query key (#497).
6
+ - Depends on json-rest-api 3.3.5 exactly (from 3.3.4), the version resource-runtime takes: a read validates and parses only what it must, and a patch answering nothing or its id, and a delete, read no stored row columns that nothing uses (details in `@jskit-ai/resource-runtime`'s changelog). No crud-core code changes.
5
7
  - Depends on json-rest-api 3.3.4 exactly (from 3.3.3), the version resource-runtime takes: a write finds the relationships that reference its resource from an index built once per published schema (details in `@jskit-ai/resource-runtime`'s changelog). No crud-core code changes.
6
8
  - A generated delete tells an assistant client it is destructive (`extensions.assistant.destructive: true`); list, view, create and update are not. `defineCrudFeature` takes `assistantHints`, such as `{ update: { destructive: true } }`, to add `destructive` or `sendsOutside` to its generated create, update or delete; a hint for another operation, or one that is not `true` or `false`, stops the server naming it. A custom action without an assistant description is described by its route's `summary`, so an MCP client reads what the route says it does.
7
9
  - Depends on json-rest-api 3.3.3 exactly (from 3.3.2), the version resource-runtime takes: on a descending sort, rows tied on its last key come in descending id order (the id that breaks ties takes the last key's direction), so MySQL and MariaDB read a descending page from an index leading with that key instead of sorting every row, and leave out the `IS NULL` term where the server already places NULLs last; and capabilities a start could not observe (the server not up yet) are observed again on the next storage call until that succeeds.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/crud-core",
3
- "version": "0.2.61",
3
+ "version": "0.2.63",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24.0.0"
@@ -20,9 +20,9 @@
20
20
  "./locales/en.json": "./locales/en.json"
21
21
  },
22
22
  "dependencies": {
23
- "@jskit-ai/resource-core": "0.1.226",
24
- "@jskit-ai/resource-crud-core": "0.1.228",
25
- "json-rest-api": "3.3.4",
23
+ "@jskit-ai/resource-core": "0.1.227",
24
+ "@jskit-ai/resource-crud-core": "0.1.229",
25
+ "json-rest-api": "3.3.5",
26
26
  "json-rest-schema": "^1.0.20"
27
27
  },
28
28
  "description": "Shared server-side CRUD service, repository, route, and query helpers.",
@@ -49,8 +49,8 @@
49
49
  }
50
50
  },
51
51
  "peerDependencies": {
52
- "@jskit-ai/http-runtime": "0.2.36",
53
- "@jskit-ai/kernel": "0.2.35",
54
- "@jskit-ai/resource-runtime": "0.2.57"
52
+ "@jskit-ai/http-runtime": "0.2.37",
53
+ "@jskit-ai/kernel": "0.2.36",
54
+ "@jskit-ai/resource-runtime": "0.2.59"
55
55
  }
56
56
  }
@@ -9,9 +9,9 @@
9
9
  "./shared": "./src/shared/index.js"
10
10
  },
11
11
  "dependencies": {
12
- "@jskit-ai/crud-core": "0.2.61",
13
- "@jskit-ai/resource-core": "0.1.226",
14
- "@jskit-ai/resource-crud-core": "0.1.228"
12
+ "@jskit-ai/crud-core": "0.2.63",
13
+ "@jskit-ai/resource-core": "0.1.227",
14
+ "@jskit-ai/resource-crud-core": "0.1.229"
15
15
  },
16
16
  "jskit": {
17
17
  "kind": "runtime",
@@ -184,6 +184,7 @@ function createCrudJsonApiRoutes({
184
184
  basePath,
185
185
  {
186
186
  ...routeBaseContract,
187
+ actionId: actionId("list"),
187
188
  meta: { tags: ["crud"], summary: "List records." },
188
189
  ...routeContracts.listRouteContract,
189
190
  ...paramsContract()
@@ -206,6 +207,7 @@ function createCrudJsonApiRoutes({
206
207
  `${basePath}/:recordId`,
207
208
  {
208
209
  ...routeBaseContract,
210
+ actionId: actionId("view"),
209
211
  meta: { tags: ["crud"], summary: "View a record." },
210
212
  ...routeContracts.viewRouteContract,
211
213
  ...paramsContract({ record: true })
@@ -228,6 +230,7 @@ function createCrudJsonApiRoutes({
228
230
  basePath,
229
231
  {
230
232
  ...routeBaseContract,
233
+ actionId: actionId("create"),
231
234
  meta: { tags: ["crud"], summary: "Create a record." },
232
235
  ...routeContracts.createRouteContract,
233
236
  ...paramsContract()
@@ -249,6 +252,7 @@ function createCrudJsonApiRoutes({
249
252
  `${basePath}/:recordId`,
250
253
  {
251
254
  ...routeBaseContract,
255
+ actionId: actionId("update"),
252
256
  meta: { tags: ["crud"], summary: "Update a record." },
253
257
  ...routeContracts.updateRouteContract,
254
258
  ...paramsContract({ record: true })
@@ -271,6 +275,7 @@ function createCrudJsonApiRoutes({
271
275
  `${basePath}/:recordId`,
272
276
  {
273
277
  ...routeBaseContract,
278
+ actionId: actionId("delete"),
274
279
  meta: { tags: ["crud"], summary: "Delete a record." },
275
280
  ...routeContracts.deleteRouteContract,
276
281
  ...paramsContract({ record: true })
@@ -304,6 +309,7 @@ function createCrudJsonApiRoutes({
304
309
  {
305
310
  ...customContract,
306
311
  ...routeBaseContract,
312
+ actionId: actionRoute.actionId,
307
313
  meta: {
308
314
  ...(customContract.meta || {}),
309
315
  tags: customContract.meta?.tags || ["crud"],
@@ -9,6 +9,7 @@ import { returnJsonApiDocument } from "@jskit-ai/http-runtime/shared";
9
9
  import { createRouter } from "../../kernel/server/http/lib/router.js";
10
10
  import { registerRoutes } from "../../kernel/server/http/lib/routeRegistration.js";
11
11
  import { registerApiErrorHandler } from "../../kernel/server/http/errorHandling.js";
12
+ import { AppError } from "../../kernel/server/runtime/errors.js";
12
13
  import crudEnglish from "../locales/en.json" with { type: "json" };
13
14
  import { registerCrudJsonApiRoutes } from "../src/server/jsonApiModule/routes.js";
14
15
  import { crudJsonApiRouteOptions } from "./support/crudJsonApiActionOptions.js";
@@ -25,7 +26,9 @@ const notes = defineCrudContract(defineTableResource({
25
26
  fields: { title: { output: "required" } }
26
27
  });
27
28
 
28
- async function listApp(t) {
29
+ // The list of notes, public unless a test signs it in; `authorize` is the action catalogue's, which a signed-in route
30
+ // asks before it reads the query.
31
+ async function listApp(t, { access = "public", authorize = async () => {} } = {}) {
29
32
  const router = createRouter();
30
33
  registerCrudJsonApiRoutes(router, crudJsonApiRouteOptions({
31
34
  namespace: "notes",
@@ -33,7 +36,7 @@ async function listApp(t) {
33
36
  relativePath: "/notes",
34
37
  surface: "app",
35
38
  ownershipFilter: "public",
36
- access: "public",
39
+ access,
37
40
  operations: ["list"]
38
41
  }));
39
42
  // The server's warnings, as the lines its logger writes.
@@ -46,6 +49,7 @@ async function listApp(t) {
46
49
  routes: router.list(),
47
50
  actions: {
48
51
  getDefinition: () => ({ kind: "action" }),
52
+ authorize,
49
53
  async execute({ input }) {
50
54
  listed.push(input);
51
55
  return returnJsonApiDocument({ data: [] });
@@ -104,3 +108,30 @@ test("a CRUD list takes filter[id], one record id or several, which a lookup sen
104
108
  }
105
109
  assert.deepEqual(listed.map((input) => input.id), [["7"], ["7", "12"], ["7", "12"]]);
106
110
  });
111
+
112
+ test("a signed-in CRUD list refuses a caller its action refuses before it reads the query, and checks the query after", async (t) => {
113
+ const asked = [];
114
+ let allowed = false;
115
+ const { app, listed } = await listApp(t, {
116
+ access: "authenticated",
117
+ async authorize({ actionId, input }) {
118
+ asked.push({ actionId, input });
119
+ if (!allowed) throw new AppError(403, "Forbidden.", { code: "ACTION_PERMISSION_DENIED" });
120
+ }
121
+ });
122
+
123
+ for (const url of ["/api/notes?filter%5Bstatus%5D=open", "/api/notes?page%5Blimit%5D=many", "/api/notes"]) {
124
+ const refused = await app.inject({ method: "GET", url });
125
+ assert.equal(refused.statusCode, 403, `${url}: ${refused.body}`);
126
+ assert.doesNotMatch(refused.body, /filter|limit|undeclared/u, url);
127
+ }
128
+ assert.deepEqual(asked.map(({ actionId }) => actionId), ["crud.notes.list", "crud.notes.list", "crud.notes.list"]);
129
+ assert.deepEqual(listed, []);
130
+
131
+ allowed = true;
132
+ const undeclared = await app.inject({ method: "GET", url: "/api/notes?filter%5Bstatus%5D=open" });
133
+ assert.equal(undeclared.statusCode, 400, undeclared.body);
134
+ assert.equal(undeclared.json().errors[0].code, "crud.query.undeclaredKeys");
135
+ assert.equal((await app.inject({ method: "GET", url: "/api/notes" })).statusCode, 200);
136
+ assert.equal(listed.length, 1);
137
+ });