@webda/core 4.0.0-beta.4 → 4.0.0-beta.5

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.
Files changed (43) hide show
  1. package/lib/core/operations.d.ts.map +1 -1
  2. package/lib/core/operations.js +13 -11
  3. package/lib/core/operations.js.map +1 -1
  4. package/lib/index.d.ts +2 -1
  5. package/lib/index.d.ts.map +1 -1
  6. package/lib/index.js +2 -1
  7. package/lib/index.js.map +1 -1
  8. package/lib/models/aclmodel.d.ts +32 -5
  9. package/lib/models/aclmodel.d.ts.map +1 -1
  10. package/lib/models/aclmodel.js +34 -9
  11. package/lib/models/aclmodel.js.map +1 -1
  12. package/lib/models/ownermodel.model.d.ts +25 -3
  13. package/lib/models/ownermodel.model.d.ts.map +1 -1
  14. package/lib/models/ownermodel.model.js +44 -9
  15. package/lib/models/ownermodel.model.js.map +1 -1
  16. package/lib/models/permissions.d.ts +236 -0
  17. package/lib/models/permissions.d.ts.map +1 -0
  18. package/lib/models/permissions.js +495 -0
  19. package/lib/models/permissions.js.map +1 -0
  20. package/lib/models/rolemodel.model.d.ts.map +1 -1
  21. package/lib/models/rolemodel.model.js +1 -2
  22. package/lib/models/rolemodel.model.js.map +1 -1
  23. package/lib/models/user.model.d.ts +12 -0
  24. package/lib/models/user.model.d.ts.map +1 -1
  25. package/lib/models/user.model.js +16 -0
  26. package/lib/models/user.model.js.map +1 -1
  27. package/lib/rest/restoperationstransport.service.d.ts.map +1 -1
  28. package/lib/rest/restoperationstransport.service.js +7 -5
  29. package/lib/rest/restoperationstransport.service.js.map +1 -1
  30. package/lib/services/audit.model.d.ts +2 -2
  31. package/lib/services/audit.model.d.ts.map +1 -1
  32. package/lib/services/audit.model.js +14 -5
  33. package/lib/services/audit.model.js.map +1 -1
  34. package/lib/services/binary.service.d.ts +42 -4
  35. package/lib/services/binary.service.d.ts.map +1 -1
  36. package/lib/services/binary.service.js +33 -13
  37. package/lib/services/binary.service.js.map +1 -1
  38. package/lib/services/domainservice.service.d.ts +74 -3
  39. package/lib/services/domainservice.service.d.ts.map +1 -1
  40. package/lib/services/domainservice.service.js +234 -57
  41. package/lib/services/domainservice.service.js.map +1 -1
  42. package/package.json +17 -13
  43. package/webda.module.json +3 -15
@@ -0,0 +1,236 @@
1
+ import { QueryValidator } from "@webda/ql";
2
+ import type { IOperationContext } from "../contexts/icontext.js";
3
+ /**
4
+ * Result of a model's static `getPermissionQuery(context)`
5
+ */
6
+ export type PermissionQuery = {
7
+ /**
8
+ * WebdaQL filter ANDed with the client query (filter only: no ORDER BY, LIMIT or OFFSET)
9
+ */
10
+ query: string;
11
+ /**
12
+ * The filter only narrows the results: `canAct(context, "get")` decides for each of them
13
+ */
14
+ partial: boolean;
15
+ };
16
+ /**
17
+ * Whether a model class defines a static `canAct` of its own (not the base one)
18
+ * @param model - the model class
19
+ * @returns true when the static form is overridden
20
+ */
21
+ export declare function hasStaticPermissionCheck(model: any): boolean;
22
+ /**
23
+ * Whether a model class defines a permission check at all: a static `canAct` of its own (not the base one), or an
24
+ * instance `canAct`. A model with neither is refused on every transport. Subclasses with their own check do not
25
+ * open a class that has none: an object reached through a class must be allowed by that class too
26
+ * @param model - the model class
27
+ * @returns true when the model defines one of the two forms
28
+ */
29
+ export declare function hasModelPermissionCheck(model: any): boolean;
30
+ /**
31
+ * Ask the model whether the current caller may perform `action` on an object
32
+ *
33
+ * The question goes through the static `canAct(context, action, object)` of the model class (see
34
+ * {@link Model.canAct}): the base implementation delegates to the object's instance `canAct`, and denies when there
35
+ * is none. `true` allows; anything else (`false`, a refusal reason string, `undefined`...) refuses, and so does a
36
+ * `canAct` that throws.
37
+ *
38
+ * @param object - the model instance (the new, unsaved object on create)
39
+ * @param context - the caller context
40
+ * @param action - the action name ("get", "create", "update", "delete", an action name or "attribute.action")
41
+ * @param model - the model class (defaults to the object's class)
42
+ * @returns true when allowed, otherwise the refusal (a reason string or false)
43
+ */
44
+ export declare function getModelPermission(object: any, context: IOperationContext, action: string, model?: any): Promise<any>;
45
+ /**
46
+ * Whether the current caller may perform `action` on a model instance (see {@link checkModelPermission}); it never
47
+ * throws
48
+ * @param object - the model instance
49
+ * @param context - the caller context
50
+ * @param action - the action name
51
+ * @param model - the model class (defaults to the object's class)
52
+ * @returns true when allowed
53
+ */
54
+ export declare function isModelActionAllowed(object: any, context: IOperationContext, action: string, model?: any): Promise<boolean>;
55
+ /**
56
+ * Message of the NotFound error for a missing object, also used for an object the caller may not read
57
+ */
58
+ export declare const NOT_FOUND_MESSAGE = "Object not found";
59
+ /**
60
+ * Load an object for an action as the caller: a missing object and an object the caller may not read throw the same
61
+ * `NotFound("Object not found")`; a readable object whose action is refused throws `Forbidden`
62
+ * @param model - the model class
63
+ * @param key - the primary key
64
+ * @param context - the caller context
65
+ * @param action - the action ("get" to only read)
66
+ * @returns the object
67
+ */
68
+ export declare function loadModelForAction<T = any>(model: any, key: any, context: IOperationContext, action: string): Promise<T>;
69
+ /**
70
+ * The parent relation (`ModelParent`) of a model, if any
71
+ * @param model - the model class
72
+ * @returns the parent attribute and model identifier
73
+ */
74
+ export declare function getParentRelation(model: any): {
75
+ attribute: string;
76
+ model: string;
77
+ } | undefined;
78
+ /**
79
+ * A child can only be attached to a parent the caller may read: a missing parent and an unreadable one throw the same
80
+ * `NotFound("Object not found")`
81
+ * @param model - the child model class
82
+ * @param parentId - the parent primary key
83
+ * @param context - the caller context
84
+ */
85
+ export declare function checkModelParent(model: any, parentId: any, context: IOperationContext): Promise<void>;
86
+ /**
87
+ * When an update moves an object to another parent, the caller must be able to read the new parent
88
+ * @param model - the model class
89
+ * @param object - the stored object
90
+ * @param input - the sanitized update input
91
+ * @param context - the caller context
92
+ */
93
+ export declare function checkModelReparent(model: any, object: any, input: any, context: IOperationContext): Promise<void>;
94
+ /**
95
+ * Enforce a model's permission for an action on an object: the single check used by every client path (REST, gRPC,
96
+ * MCP, GraphQL and any transport dispatching operations)
97
+ *
98
+ * A caller who may not read the object (`canAct(ctx, "get", object)` refused) gets exactly the error of a missing
99
+ * object (`NotFound("Object not found")`), whatever the action, so a refusal never reveals that the key exists. A
100
+ * caller who may read the object but not perform the action gets a `Forbidden`. On `"create"` the object does not
101
+ * exist yet: a refusal is always a `Forbidden`.
102
+ *
103
+ * The refusal reason returned by `canAct` is logged, never sent to the client.
104
+ *
105
+ * @param object - the model instance
106
+ * @param context - the caller context
107
+ * @param action - the action name
108
+ * @param model - the model class (defaults to the object's class)
109
+ * @throws WebdaError.NotFound when the caller may not read the object
110
+ * @throws WebdaError.Forbidden when the caller may read the object but not perform the action
111
+ */
112
+ export declare function checkModelPermission(object: any, context: IOperationContext, action: string, model?: any): Promise<void>;
113
+ /**
114
+ * Enforce a model's permission for a static (class-level) action: `canAct(context, action)` without object
115
+ *
116
+ * There is no object to hide, so a refusal is always a `Forbidden`.
117
+ * @param model - the model class
118
+ * @param context - the caller context
119
+ * @param action - the action name
120
+ * @throws WebdaError.Forbidden when refused
121
+ */
122
+ export declare function checkStaticModelPermission(model: any, context: IOperationContext, action: string): Promise<void>;
123
+ /**
124
+ * Persist a new object with the repository `create`, which refuses an existing key (atomically in every bundled
125
+ * repository), instead of `save()`, which upserts
126
+ * @param object - the new model instance
127
+ * @returns the object
128
+ * @throws WebdaError.Conflict when an object with the same primary key exists
129
+ */
130
+ export declare function createModel<T = any>(object: T): Promise<T>;
131
+ /**
132
+ * AND the model's permission query into a client query
133
+ *
134
+ * Both sides are parsed and combined on the expression tree, so a client `OR` keeps its precedence. The client
135
+ * ORDER BY, LIMIT and OFFSET are kept.
136
+ *
137
+ * @param query - the client query
138
+ * @param permission - the permission query, if any
139
+ * @returns the query to run
140
+ * @throws SyntaxError if the client query is invalid
141
+ */
142
+ export declare function mergePermissionQuery(query: string, permission?: PermissionQuery | null): string;
143
+ /**
144
+ * Highest LIMIT a client query can ask for: larger LIMITs are lowered to it
145
+ */
146
+ export declare const MAX_QUERY_LIMIT = 1000;
147
+ /**
148
+ * Rows a permission-filtered query scans to fill one page: ten times the page LIMIT, at least
149
+ * {@link MIN_SCANNED_ROWS}, at most {@link MAX_SCANNED_ROWS}
150
+ */
151
+ export declare const SCAN_FACTOR = 10;
152
+ /**
153
+ * Lower bound of the scan budget of a permission-filtered page
154
+ */
155
+ export declare const MIN_SCANNED_ROWS = 100;
156
+ /**
157
+ * Absolute bound of the scan budget of a permission-filtered page
158
+ */
159
+ export declare const MAX_SCANNED_ROWS = 10000;
160
+ /**
161
+ * Most store pages read to fill one page, whatever their size
162
+ */
163
+ export declare const MAX_REFILL_PAGES = 100;
164
+ /**
165
+ * Lifetime of a continuation token (one hour): paging has to resume within it
166
+ */
167
+ export declare const CONTINUATION_TOKEN_TTL_MS: number;
168
+ /**
169
+ * Refuse a client query that reads private (`__`-prefixed) fields, at any depth, in its filter or ORDER BY: a filter
170
+ * on a private field (a password hash...) would reveal its value through which rows match
171
+ * @param query - the parsed client query
172
+ * @throws WebdaError.BadRequest when a field path has a `__`-prefixed segment
173
+ */
174
+ export declare function assertNoPrivateFields(query: QueryValidator): void;
175
+ /**
176
+ * What a continuation token is bound to: it is only accepted back for the same model, the same query (filter and
177
+ * ORDER BY) and the same caller
178
+ */
179
+ export type ContinuationTokenBinding = {
180
+ /**
181
+ * Model identifier
182
+ */
183
+ model: string;
184
+ /**
185
+ * The query the token continues: filter and ORDER BY, without LIMIT and OFFSET
186
+ */
187
+ query: string;
188
+ /**
189
+ * Caller id, "anonymous" when not logged in
190
+ */
191
+ user: string;
192
+ };
193
+ /**
194
+ * Seal a store continuation token: encrypted with the CryptoService (random IV, padded to 64 bytes) so its value
195
+ * cannot be read, compared or forged by the client; bound to the model, the query and the caller; and expiring
196
+ * after `ttl` ({@link CONTINUATION_TOKEN_TTL_MS})
197
+ * @param token - the store token
198
+ * @param binding - what the token is bound to
199
+ * @param ttl - lifetime in milliseconds
200
+ * @returns the opaque token
201
+ */
202
+ export declare function sealContinuationToken(token: string, binding: ContinuationTokenBinding, ttl?: number): Promise<string>;
203
+ /**
204
+ * Open a token produced by {@link sealContinuationToken}
205
+ * @param token - the opaque token
206
+ * @param binding - what the token must be bound to
207
+ * @returns the store token
208
+ * @throws WebdaError.BadRequest when the token was not produced by this application for this model, query and
209
+ * caller, or has expired
210
+ */
211
+ export declare function unsealContinuationToken(token: string, binding: ContinuationTokenBinding): Promise<string>;
212
+ /**
213
+ * Query a model as the caller
214
+ *
215
+ * - the client query may not read private (`__`) fields (400), and its LIMIT is lowered to {@link MAX_QUERY_LIMIT};
216
+ * - a model defining neither `canAct` form refuses every row: the store is not asked;
217
+ * - the model's static `getPermissionQuery(context)` is ANDed into the query;
218
+ * - every result is checked with the static `canAct(context, "get", row)` and refused ones are dropped. Refused rows
219
+ * are replaced by continuing the scan (next store pages, asking only for the missing rows), within a budget of
220
+ * `SCAN_FACTOR` x LIMIT rows (between `MIN_SCANNED_ROWS` and `MAX_SCANNED_ROWS`) and {@link MAX_REFILL_PAGES}
221
+ * store pages. When the budget is spent, an empty page carries no token;
222
+ * - the continuation token is sealed ({@link sealContinuationToken}): a store token counting or naming rows
223
+ * (Postgres/Firestore offsets, Dynamo keys, memory offsets) would otherwise reveal hidden matches. It is bound to
224
+ * the model, the query and the caller, expires after {@link CONTINUATION_TOKEN_TTL_MS}, and is sent back as is in
225
+ * `OFFSET`; any other value is a 400.
226
+ *
227
+ * @param model - the model class
228
+ * @param query - the client query
229
+ * @param context - the caller context
230
+ * @returns the query results the caller may read
231
+ */
232
+ export declare function queryModelWithPermissions<T = any>(model: any, query: string, context: IOperationContext): Promise<{
233
+ results: T[];
234
+ continuationToken?: string;
235
+ }>;
236
+ //# sourceMappingURL=permissions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"permissions.d.ts","sourceRoot":"","sources":["../../src/models/permissions.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2C,cAAc,EAAmB,MAAM,WAAW,CAAC;AAIrG,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAcjE;;GAEG;AACH,MAAM,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