@objectstack/spec 2.0.3 → 2.0.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.
- package/dist/api/index.d.mts +1 -1
- package/dist/api/index.d.ts +1 -1
- package/dist/api/index.js +794 -686
- package/dist/api/index.js.map +1 -1
- package/dist/api/index.mjs +789 -686
- package/dist/api/index.mjs.map +1 -1
- package/dist/contracts/index.d.mts +2 -2
- package/dist/contracts/index.d.ts +2 -2
- package/dist/data/index.d.mts +2 -2
- package/dist/data/index.d.ts +2 -2
- package/dist/data/index.js +482 -453
- package/dist/data/index.js.map +1 -1
- package/dist/data/index.mjs +481 -453
- package/dist/data/index.mjs.map +1 -1
- package/dist/{driver.zod-DddW_4lJ.d.mts → driver.zod-DnOPgUGi.d.mts} +430 -1
- package/dist/{driver.zod-BJHWEbwG.d.ts → driver.zod-E3C6n0W-.d.ts} +430 -1
- package/dist/{index-yvEIvpa3.d.ts → index-BPhGHW32.d.ts} +4 -2
- package/dist/{index-DAQcdY6O.d.ts → index-BbtSaxHm.d.ts} +138 -2
- package/dist/{index-D9IaDTui.d.ts → index-BpP0MwlW.d.ts} +1 -1
- package/dist/{index-C8xlxqpA.d.ts → index-C6p-2KXV.d.ts} +1 -1
- package/dist/index-CDN6TRx9.d.mts +765 -0
- package/dist/index-CDN6TRx9.d.ts +765 -0
- package/dist/{index-wFiQRott.d.mts → index-CVnGe2b8.d.mts} +1 -1
- package/dist/{index-Cp6xnrOM.d.mts → index-D-tf4nDV.d.mts} +4 -2
- package/dist/{index-C_oDEJoO.d.mts → index-DRCh2NTZ.d.mts} +138 -2
- package/dist/{index-DRMAu-1t.d.mts → index-DTCkpGMm.d.mts} +1 -1
- package/dist/{index-DOuMlF5h.d.ts → index-DyawwLFZ.d.ts} +31 -2
- package/dist/{index-DPlvQwlz.d.mts → index-E1mP_eoE.d.mts} +31 -2
- package/dist/index.d.mts +41 -802
- package/dist/index.d.ts +41 -802
- package/dist/index.js +8656 -8523
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8656 -8523
- package/dist/index.mjs.map +1 -1
- package/dist/kernel/index.d.mts +1 -1
- package/dist/kernel/index.d.ts +1 -1
- package/dist/kernel/index.js +23 -0
- package/dist/kernel/index.js.map +1 -1
- package/dist/kernel/index.mjs +22 -0
- package/dist/kernel/index.mjs.map +1 -1
- package/dist/security/index.d.mts +2 -0
- package/dist/security/index.d.ts +2 -0
- package/dist/security/index.js +666 -0
- package/dist/security/index.js.map +1 -0
- package/dist/security/index.mjs +616 -0
- package/dist/security/index.mjs.map +1 -0
- package/dist/system/index.d.mts +1 -1
- package/dist/system/index.d.ts +1 -1
- package/dist/system/index.js +2 -1
- package/dist/system/index.js.map +1 -1
- package/dist/system/index.mjs +2 -1
- package/dist/system/index.mjs.map +1 -1
- package/json-schema/api/AuthEndpoint.json +201 -0
- package/json-schema/api/GetDiscoveryResponse.json +42 -0
- package/json-schema/data/BaseEngineOptions.json +49 -0
- package/json-schema/data/DataEngineAggregateOptions.json +42 -0
- package/json-schema/data/DataEngineAggregateRequest.json +42 -0
- package/json-schema/data/DataEngineBatchRequest.json +294 -0
- package/json-schema/data/DataEngineCountOptions.json +42 -0
- package/json-schema/data/DataEngineCountRequest.json +42 -0
- package/json-schema/data/DataEngineDeleteOptions.json +42 -0
- package/json-schema/data/DataEngineDeleteRequest.json +42 -0
- package/json-schema/data/DataEngineFindOneRequest.json +42 -0
- package/json-schema/data/DataEngineFindRequest.json +42 -0
- package/json-schema/data/DataEngineInsertOptions.json +42 -0
- package/json-schema/data/DataEngineInsertRequest.json +42 -0
- package/json-schema/data/DataEngineQueryOptions.json +42 -0
- package/json-schema/data/DataEngineRequest.json +588 -0
- package/json-schema/data/DataEngineUpdateOptions.json +42 -0
- package/json-schema/data/DataEngineUpdateRequest.json +42 -0
- package/json-schema/kernel/ExecutionContext.json +43 -0
- package/package.json +6 -1
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
// src/security/permission.zod.ts
|
|
2
|
+
import { z as z3 } from "zod";
|
|
3
|
+
|
|
4
|
+
// src/shared/identifiers.zod.ts
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
var SystemIdentifierSchema = z.string().min(2, { message: "System identifier must be at least 2 characters" }).regex(/^[a-z][a-z0-9_.]*$/, {
|
|
7
|
+
message: 'System identifier must be lowercase, starting with a letter, and may contain letters, numbers, underscores, or dots (e.g., "user_profile" or "order.created")'
|
|
8
|
+
}).describe("System identifier (lowercase with underscores or dots)");
|
|
9
|
+
var SnakeCaseIdentifierSchema = z.string().min(2, { message: "Identifier must be at least 2 characters" }).regex(/^[a-z][a-z0-9_]*$/, {
|
|
10
|
+
message: 'Identifier must be lowercase snake_case, starting with a letter, and may contain only letters, numbers, and underscores (e.g., "user_profile")'
|
|
11
|
+
}).describe("Snake case identifier (lowercase with underscores only)");
|
|
12
|
+
var EventNameSchema = z.string().min(3, { message: "Event name must be at least 3 characters" }).regex(/^[a-z][a-z0-9_.]*$/, {
|
|
13
|
+
message: 'Event name must be lowercase with dots for namespacing (e.g., "user.created", "order.paid")'
|
|
14
|
+
}).describe("Event name (lowercase with dot notation for namespacing)");
|
|
15
|
+
|
|
16
|
+
// src/security/rls.zod.ts
|
|
17
|
+
import { z as z2 } from "zod";
|
|
18
|
+
var RLSOperation = z2.enum(["select", "insert", "update", "delete", "all"]);
|
|
19
|
+
var RowLevelSecurityPolicySchema = z2.object({
|
|
20
|
+
/**
|
|
21
|
+
* Unique identifier for this policy.
|
|
22
|
+
* Must be unique within the object.
|
|
23
|
+
* Use snake_case following ObjectStack naming conventions.
|
|
24
|
+
*
|
|
25
|
+
* @example "tenant_isolation", "owner_access", "manager_team_view"
|
|
26
|
+
*/
|
|
27
|
+
name: z2.string().regex(/^[a-z_][a-z0-9_]*$/).describe("Policy unique identifier (snake_case)"),
|
|
28
|
+
/**
|
|
29
|
+
* Human-readable label for the policy.
|
|
30
|
+
* Used in admin UI and logs.
|
|
31
|
+
*
|
|
32
|
+
* @example "Multi-Tenant Data Isolation", "Owner-Based Access"
|
|
33
|
+
*/
|
|
34
|
+
label: z2.string().optional().describe("Human-readable policy label"),
|
|
35
|
+
/**
|
|
36
|
+
* Description explaining what this policy does and why.
|
|
37
|
+
* Helps with governance and compliance.
|
|
38
|
+
*
|
|
39
|
+
* @example "Ensures users can only access records from their own tenant organization"
|
|
40
|
+
*/
|
|
41
|
+
description: z2.string().optional().describe("Policy description and business justification"),
|
|
42
|
+
/**
|
|
43
|
+
* Target object (table) this policy applies to.
|
|
44
|
+
* Must reference a valid ObjectStack object name.
|
|
45
|
+
*
|
|
46
|
+
* @example "account", "opportunity", "contact", "custom_object"
|
|
47
|
+
*/
|
|
48
|
+
object: z2.string().describe("Target object name"),
|
|
49
|
+
/**
|
|
50
|
+
* Database operation(s) this policy applies to.
|
|
51
|
+
*
|
|
52
|
+
* - **select**: Controls read access (SELECT queries)
|
|
53
|
+
* - **insert**: Controls insert access (INSERT statements)
|
|
54
|
+
* - **update**: Controls update access (UPDATE statements)
|
|
55
|
+
* - **delete**: Controls delete access (DELETE statements)
|
|
56
|
+
* - **all**: Applies to all operations
|
|
57
|
+
*
|
|
58
|
+
* @example "select" - Most common, controls what users can view
|
|
59
|
+
* @example "all" - Apply same rule to all operations
|
|
60
|
+
*/
|
|
61
|
+
operation: RLSOperation.describe("Database operation this policy applies to"),
|
|
62
|
+
/**
|
|
63
|
+
* USING clause - Filter condition for SELECT/UPDATE/DELETE.
|
|
64
|
+
*
|
|
65
|
+
* This is a SQL-like expression evaluated for each row.
|
|
66
|
+
* Only rows where this expression returns TRUE are accessible.
|
|
67
|
+
*
|
|
68
|
+
* **Note**: For INSERT-only policies, USING is not required (only CHECK is needed).
|
|
69
|
+
* For SELECT/UPDATE/DELETE operations, USING is required.
|
|
70
|
+
*
|
|
71
|
+
* **Security Note**: RLS conditions are executed at the database level with
|
|
72
|
+
* parameterized queries. The implementation must use prepared statements
|
|
73
|
+
* to prevent SQL injection. Never concatenate user input directly into
|
|
74
|
+
* RLS conditions.
|
|
75
|
+
*
|
|
76
|
+
* **SQL Dialect**: Compatible with PostgreSQL SQL syntax. Implementations
|
|
77
|
+
* may adapt to other databases (MySQL, SQL Server, etc.) but should maintain
|
|
78
|
+
* semantic equivalence.
|
|
79
|
+
*
|
|
80
|
+
* Available context variables:
|
|
81
|
+
* - `current_user.id` - Current user's ID
|
|
82
|
+
* - `current_user.tenant_id` - Current user's tenant (maps to `tenantId` in RLSUserContext)
|
|
83
|
+
* - `current_user.role` - Current user's role
|
|
84
|
+
* - `current_user.department` - Current user's department
|
|
85
|
+
* - `current_user.*` - Any custom user field
|
|
86
|
+
* - `NOW()` - Current timestamp
|
|
87
|
+
* - `CURRENT_DATE` - Current date
|
|
88
|
+
* - `CURRENT_TIME` - Current time
|
|
89
|
+
*
|
|
90
|
+
* **Context Variable Mapping**: The RLSUserContext schema uses camelCase (e.g., `tenantId`),
|
|
91
|
+
* but expressions use snake_case with `current_user.` prefix (e.g., `current_user.tenant_id`).
|
|
92
|
+
* Implementations must handle this mapping.
|
|
93
|
+
*
|
|
94
|
+
* Supported operators:
|
|
95
|
+
* - Comparison: =, !=, <, >, <=, >=, <> (not equal)
|
|
96
|
+
* - Logical: AND, OR, NOT
|
|
97
|
+
* - NULL checks: IS NULL, IS NOT NULL
|
|
98
|
+
* - Set operations: IN, NOT IN
|
|
99
|
+
* - String: LIKE, NOT LIKE, ILIKE (case-insensitive)
|
|
100
|
+
* - Pattern matching: ~ (regex), !~ (not regex)
|
|
101
|
+
* - Subqueries: (SELECT ...)
|
|
102
|
+
* - Array operations: ANY, ALL
|
|
103
|
+
*
|
|
104
|
+
* **Prohibited**: Dynamic SQL, DDL statements, DML statements (INSERT/UPDATE/DELETE)
|
|
105
|
+
*
|
|
106
|
+
* @example "tenant_id = current_user.tenant_id"
|
|
107
|
+
* @example "owner_id = current_user.id OR created_by = current_user.id"
|
|
108
|
+
* @example "department IN (SELECT department FROM user_departments WHERE user_id = current_user.id)"
|
|
109
|
+
* @example "status = 'active' AND expiry_date > NOW()"
|
|
110
|
+
*/
|
|
111
|
+
using: z2.string().optional().describe("Filter condition for SELECT/UPDATE/DELETE (PostgreSQL SQL WHERE clause syntax with parameterized context variables). Optional for INSERT-only policies."),
|
|
112
|
+
/**
|
|
113
|
+
* CHECK clause - Validation for INSERT/UPDATE operations.
|
|
114
|
+
*
|
|
115
|
+
* Similar to USING but applies to new/modified rows.
|
|
116
|
+
* Prevents users from creating/updating rows they wouldn't be able to see.
|
|
117
|
+
*
|
|
118
|
+
* **Default Behavior**: If not specified, implementations should use the
|
|
119
|
+
* USING clause as the CHECK clause. This ensures data integrity by preventing
|
|
120
|
+
* users from creating records they cannot view.
|
|
121
|
+
*
|
|
122
|
+
* Use cases:
|
|
123
|
+
* - Prevent cross-tenant data creation
|
|
124
|
+
* - Enforce mandatory field values
|
|
125
|
+
* - Validate data integrity rules
|
|
126
|
+
* - Restrict certain operations (e.g., only allow creating "draft" status)
|
|
127
|
+
*
|
|
128
|
+
* @example "tenant_id = current_user.tenant_id"
|
|
129
|
+
* @example "status IN ('draft', 'pending')" - Only allow certain statuses
|
|
130
|
+
* @example "created_by = current_user.id" - Must be the creator
|
|
131
|
+
*/
|
|
132
|
+
check: z2.string().optional().describe("Validation condition for INSERT/UPDATE (defaults to USING clause if not specified - enforced at application level)"),
|
|
133
|
+
/**
|
|
134
|
+
* Restrict this policy to specific roles.
|
|
135
|
+
* If specified, only users with these roles will have this policy applied.
|
|
136
|
+
* If omitted, policy applies to all users (except those with bypassRLS permission).
|
|
137
|
+
*
|
|
138
|
+
* Role names must match defined roles in the system.
|
|
139
|
+
*
|
|
140
|
+
* @example ["sales_rep", "account_manager"]
|
|
141
|
+
* @example ["employee"] - Apply to all employees
|
|
142
|
+
* @example ["guest"] - Special restrictions for guests
|
|
143
|
+
*/
|
|
144
|
+
roles: z2.array(z2.string()).optional().describe("Roles this policy applies to (omit for all roles)"),
|
|
145
|
+
/**
|
|
146
|
+
* Whether this policy is currently active.
|
|
147
|
+
* Disabled policies are not evaluated.
|
|
148
|
+
* Useful for temporary policy changes without deletion.
|
|
149
|
+
*
|
|
150
|
+
* @default true
|
|
151
|
+
*/
|
|
152
|
+
enabled: z2.boolean().default(true).describe("Whether this policy is active"),
|
|
153
|
+
/**
|
|
154
|
+
* Policy priority for conflict resolution.
|
|
155
|
+
* Higher numbers = higher priority.
|
|
156
|
+
* When multiple policies apply, the most permissive wins (OR logic).
|
|
157
|
+
* Priority is only used for ordering evaluation (performance).
|
|
158
|
+
*
|
|
159
|
+
* @default 0
|
|
160
|
+
*/
|
|
161
|
+
priority: z2.number().int().default(0).describe("Policy evaluation priority (higher = evaluated first)"),
|
|
162
|
+
/**
|
|
163
|
+
* Tags for policy categorization and reporting.
|
|
164
|
+
* Useful for governance, compliance, and auditing.
|
|
165
|
+
*
|
|
166
|
+
* @example ["compliance", "gdpr", "pci"]
|
|
167
|
+
* @example ["multi-tenant", "security"]
|
|
168
|
+
*/
|
|
169
|
+
tags: z2.array(z2.string()).optional().describe("Policy categorization tags")
|
|
170
|
+
}).superRefine((data, ctx) => {
|
|
171
|
+
if (!data.using && !data.check) {
|
|
172
|
+
ctx.addIssue({
|
|
173
|
+
code: z2.ZodIssueCode.custom,
|
|
174
|
+
message: 'At least one of "using" or "check" must be specified. For SELECT/UPDATE/DELETE operations, provide "using". For INSERT operations, provide "check".'
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
var RLSConfigSchema = z2.object({
|
|
179
|
+
/**
|
|
180
|
+
* Global RLS enable/disable flag.
|
|
181
|
+
* When false, all RLS policies are ignored (use with caution!).
|
|
182
|
+
*
|
|
183
|
+
* @default true
|
|
184
|
+
*/
|
|
185
|
+
enabled: z2.boolean().default(true).describe("Enable RLS enforcement globally"),
|
|
186
|
+
/**
|
|
187
|
+
* Default behavior when no policies match.
|
|
188
|
+
*
|
|
189
|
+
* - **deny**: Deny access (secure default)
|
|
190
|
+
* - **allow**: Allow access (permissive mode, not recommended)
|
|
191
|
+
*
|
|
192
|
+
* @default "deny"
|
|
193
|
+
*/
|
|
194
|
+
defaultPolicy: z2.enum(["deny", "allow"]).default("deny").describe("Default action when no policies match"),
|
|
195
|
+
/**
|
|
196
|
+
* Whether to allow superusers to bypass RLS.
|
|
197
|
+
* Superusers include system administrators and service accounts.
|
|
198
|
+
*
|
|
199
|
+
* @default true
|
|
200
|
+
*/
|
|
201
|
+
allowSuperuserBypass: z2.boolean().default(true).describe("Allow superusers to bypass RLS"),
|
|
202
|
+
/**
|
|
203
|
+
* List of roles that can bypass RLS.
|
|
204
|
+
* Users with these roles see all records regardless of policies.
|
|
205
|
+
*
|
|
206
|
+
* @example ["system_admin", "data_auditor"]
|
|
207
|
+
*/
|
|
208
|
+
bypassRoles: z2.array(z2.string()).optional().describe("Roles that bypass RLS (see all data)"),
|
|
209
|
+
/**
|
|
210
|
+
* Whether to log RLS policy evaluations.
|
|
211
|
+
* Useful for debugging and auditing.
|
|
212
|
+
* Can impact performance if enabled globally.
|
|
213
|
+
*
|
|
214
|
+
* @default false
|
|
215
|
+
*/
|
|
216
|
+
logEvaluations: z2.boolean().default(false).describe("Log RLS policy evaluations for debugging"),
|
|
217
|
+
/**
|
|
218
|
+
* Cache RLS policy evaluation results.
|
|
219
|
+
* Can improve performance for frequently accessed records.
|
|
220
|
+
* Cache is invalidated when policies change or user context changes.
|
|
221
|
+
*
|
|
222
|
+
* @default true
|
|
223
|
+
*/
|
|
224
|
+
cacheResults: z2.boolean().default(true).describe("Cache RLS evaluation results"),
|
|
225
|
+
/**
|
|
226
|
+
* Cache TTL in seconds.
|
|
227
|
+
* How long to cache RLS evaluation results.
|
|
228
|
+
*
|
|
229
|
+
* @default 300 (5 minutes)
|
|
230
|
+
*/
|
|
231
|
+
cacheTtlSeconds: z2.number().int().positive().default(300).describe("Cache TTL in seconds"),
|
|
232
|
+
/**
|
|
233
|
+
* Performance optimization: Pre-fetch user context.
|
|
234
|
+
* Load user context once per request instead of per-query.
|
|
235
|
+
*
|
|
236
|
+
* @default true
|
|
237
|
+
*/
|
|
238
|
+
prefetchUserContext: z2.boolean().default(true).describe("Pre-fetch user context for performance")
|
|
239
|
+
});
|
|
240
|
+
var RLSUserContextSchema = z2.object({
|
|
241
|
+
/**
|
|
242
|
+
* User ID
|
|
243
|
+
*/
|
|
244
|
+
id: z2.string().describe("User ID"),
|
|
245
|
+
/**
|
|
246
|
+
* User email
|
|
247
|
+
*/
|
|
248
|
+
email: z2.string().email().optional().describe("User email"),
|
|
249
|
+
/**
|
|
250
|
+
* Tenant/Organization ID
|
|
251
|
+
*/
|
|
252
|
+
tenantId: z2.string().optional().describe("Tenant/Organization ID"),
|
|
253
|
+
/**
|
|
254
|
+
* User role(s)
|
|
255
|
+
*/
|
|
256
|
+
role: z2.union([
|
|
257
|
+
z2.string(),
|
|
258
|
+
z2.array(z2.string())
|
|
259
|
+
]).optional().describe("User role(s)"),
|
|
260
|
+
/**
|
|
261
|
+
* User department
|
|
262
|
+
*/
|
|
263
|
+
department: z2.string().optional().describe("User department"),
|
|
264
|
+
/**
|
|
265
|
+
* Additional custom attributes
|
|
266
|
+
* Can include any custom user fields for RLS evaluation
|
|
267
|
+
*/
|
|
268
|
+
attributes: z2.record(z2.string(), z2.unknown()).optional().describe("Additional custom user attributes")
|
|
269
|
+
});
|
|
270
|
+
var RLSEvaluationResultSchema = z2.object({
|
|
271
|
+
/**
|
|
272
|
+
* Policy name that was evaluated
|
|
273
|
+
*/
|
|
274
|
+
policyName: z2.string().describe("Policy name"),
|
|
275
|
+
/**
|
|
276
|
+
* Whether access was granted
|
|
277
|
+
*/
|
|
278
|
+
granted: z2.boolean().describe("Whether access was granted"),
|
|
279
|
+
/**
|
|
280
|
+
* Evaluation duration in milliseconds
|
|
281
|
+
*/
|
|
282
|
+
durationMs: z2.number().optional().describe("Evaluation duration in milliseconds"),
|
|
283
|
+
/**
|
|
284
|
+
* Error message if evaluation failed
|
|
285
|
+
*/
|
|
286
|
+
error: z2.string().optional().describe("Error message if evaluation failed"),
|
|
287
|
+
/**
|
|
288
|
+
* Evaluated USING clause result
|
|
289
|
+
*/
|
|
290
|
+
usingResult: z2.boolean().optional().describe("USING clause evaluation result"),
|
|
291
|
+
/**
|
|
292
|
+
* Evaluated CHECK clause result (for INSERT/UPDATE)
|
|
293
|
+
*/
|
|
294
|
+
checkResult: z2.boolean().optional().describe("CHECK clause evaluation result")
|
|
295
|
+
});
|
|
296
|
+
var RLS = {
|
|
297
|
+
/**
|
|
298
|
+
* Create a simple owner-based policy
|
|
299
|
+
*/
|
|
300
|
+
ownerPolicy: (object, ownerField = "owner_id") => ({
|
|
301
|
+
name: `${object}_owner_access`,
|
|
302
|
+
label: `Owner Access for ${object}`,
|
|
303
|
+
object,
|
|
304
|
+
operation: "all",
|
|
305
|
+
using: `${ownerField} = current_user.id`,
|
|
306
|
+
enabled: true,
|
|
307
|
+
priority: 0
|
|
308
|
+
}),
|
|
309
|
+
/**
|
|
310
|
+
* Create a tenant isolation policy
|
|
311
|
+
*/
|
|
312
|
+
tenantPolicy: (object, tenantField = "tenant_id") => ({
|
|
313
|
+
name: `${object}_tenant_isolation`,
|
|
314
|
+
label: `Tenant Isolation for ${object}`,
|
|
315
|
+
object,
|
|
316
|
+
operation: "all",
|
|
317
|
+
using: `${tenantField} = current_user.tenant_id`,
|
|
318
|
+
check: `${tenantField} = current_user.tenant_id`,
|
|
319
|
+
enabled: true,
|
|
320
|
+
priority: 0
|
|
321
|
+
}),
|
|
322
|
+
/**
|
|
323
|
+
* Create a role-based policy
|
|
324
|
+
*/
|
|
325
|
+
rolePolicy: (object, roles, condition) => ({
|
|
326
|
+
name: `${object}_${roles.join("_")}_access`,
|
|
327
|
+
label: `${roles.join(", ")} Access for ${object}`,
|
|
328
|
+
object,
|
|
329
|
+
operation: "select",
|
|
330
|
+
using: condition,
|
|
331
|
+
roles,
|
|
332
|
+
enabled: true,
|
|
333
|
+
priority: 0
|
|
334
|
+
}),
|
|
335
|
+
/**
|
|
336
|
+
* Create a permissive policy (allow all for specific roles)
|
|
337
|
+
*/
|
|
338
|
+
allowAllPolicy: (object, roles) => ({
|
|
339
|
+
name: `${object}_${roles.join("_")}_full_access`,
|
|
340
|
+
label: `Full Access for ${roles.join(", ")}`,
|
|
341
|
+
object,
|
|
342
|
+
operation: "all",
|
|
343
|
+
using: "1 = 1",
|
|
344
|
+
// Always true
|
|
345
|
+
roles,
|
|
346
|
+
enabled: true,
|
|
347
|
+
priority: 0
|
|
348
|
+
})
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
// src/security/permission.zod.ts
|
|
352
|
+
var ObjectPermissionSchema = z3.object({
|
|
353
|
+
/** C: Create */
|
|
354
|
+
allowCreate: z3.boolean().default(false).describe("Create permission"),
|
|
355
|
+
/** R: Read (Owned records or Shared records) */
|
|
356
|
+
allowRead: z3.boolean().default(false).describe("Read permission"),
|
|
357
|
+
/** U: Edit (Owned records or Shared records) */
|
|
358
|
+
allowEdit: z3.boolean().default(false).describe("Edit permission"),
|
|
359
|
+
/** D: Delete (Owned records or Shared records) */
|
|
360
|
+
allowDelete: z3.boolean().default(false).describe("Delete permission"),
|
|
361
|
+
/** Lifecycle Operations */
|
|
362
|
+
allowTransfer: z3.boolean().default(false).describe("Change record ownership"),
|
|
363
|
+
allowRestore: z3.boolean().default(false).describe("Restore from trash (Undelete)"),
|
|
364
|
+
allowPurge: z3.boolean().default(false).describe("Permanently delete (Hard Delete/GDPR)"),
|
|
365
|
+
/**
|
|
366
|
+
* View All Records: Super-user read access.
|
|
367
|
+
* Bypasses Sharing Rules and Ownership checks.
|
|
368
|
+
* Equivalent to Microsoft Dataverse "Organization" level read access.
|
|
369
|
+
*/
|
|
370
|
+
viewAllRecords: z3.boolean().default(false).describe("View All Data (Bypass Sharing)"),
|
|
371
|
+
/**
|
|
372
|
+
* Modify All Records: Super-user write access.
|
|
373
|
+
* Bypasses Sharing Rules and Ownership checks.
|
|
374
|
+
* Equivalent to Microsoft Dataverse "Organization" level write access.
|
|
375
|
+
*/
|
|
376
|
+
modifyAllRecords: z3.boolean().default(false).describe("Modify All Data (Bypass Sharing)")
|
|
377
|
+
});
|
|
378
|
+
var FieldPermissionSchema = z3.object({
|
|
379
|
+
/** Can see this field */
|
|
380
|
+
readable: z3.boolean().default(true).describe("Field read access"),
|
|
381
|
+
/** Can edit this field */
|
|
382
|
+
editable: z3.boolean().default(false).describe("Field edit access")
|
|
383
|
+
});
|
|
384
|
+
var PermissionSetSchema = z3.object({
|
|
385
|
+
/** Unique permission set name */
|
|
386
|
+
name: SnakeCaseIdentifierSchema.describe("Permission set unique name (lowercase snake_case)"),
|
|
387
|
+
/** Display label */
|
|
388
|
+
label: z3.string().optional().describe("Display label"),
|
|
389
|
+
/** Is this a Profile? (Base set for a user) */
|
|
390
|
+
isProfile: z3.boolean().default(false).describe("Whether this is a user profile"),
|
|
391
|
+
/** Object Permissions Map: <entity_name> -> permissions */
|
|
392
|
+
objects: z3.record(z3.string(), ObjectPermissionSchema).describe("Entity permissions"),
|
|
393
|
+
/** Field Permissions Map: <entity_name>.<field_name> -> permissions */
|
|
394
|
+
fields: z3.record(z3.string(), FieldPermissionSchema).optional().describe("Field level security"),
|
|
395
|
+
/** System permissions (e.g., "manage_users") */
|
|
396
|
+
systemPermissions: z3.array(z3.string()).optional().describe("System level capabilities"),
|
|
397
|
+
/**
|
|
398
|
+
* Row-Level Security Rules
|
|
399
|
+
*
|
|
400
|
+
* Row-level security policies that filter records based on user context.
|
|
401
|
+
* These rules are applied in addition to object-level permissions.
|
|
402
|
+
*
|
|
403
|
+
* Uses the canonical RLS protocol from rls.zod.ts for comprehensive
|
|
404
|
+
* row-level security features including PostgreSQL-style USING and CHECK clauses.
|
|
405
|
+
*
|
|
406
|
+
* @see {@link RowLevelSecurityPolicySchema} for full RLS specification
|
|
407
|
+
* @see {@link file://./rls.zod.ts} for comprehensive RLS documentation
|
|
408
|
+
*
|
|
409
|
+
* @example Multi-tenant isolation
|
|
410
|
+
* ```typescript
|
|
411
|
+
* rls: [{
|
|
412
|
+
* name: 'tenant_filter',
|
|
413
|
+
* object: 'account',
|
|
414
|
+
* operation: 'select',
|
|
415
|
+
* using: 'tenant_id = current_user.tenant_id'
|
|
416
|
+
* }]
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
419
|
+
rowLevelSecurity: z3.array(RowLevelSecurityPolicySchema).optional().describe("Row-level security policies (see rls.zod.ts for full spec)"),
|
|
420
|
+
/**
|
|
421
|
+
* Context-Based Access Control Variables
|
|
422
|
+
*
|
|
423
|
+
* Custom context variables that can be referenced in RLS rules.
|
|
424
|
+
* These variables are evaluated at runtime based on the user's session.
|
|
425
|
+
*
|
|
426
|
+
* Common context variables:
|
|
427
|
+
* - `current_user.id` - Current user ID
|
|
428
|
+
* - `current_user.tenant_id` - User's tenant/organization ID
|
|
429
|
+
* - `current_user.department` - User's department
|
|
430
|
+
* - `current_user.role` - User's role
|
|
431
|
+
* - `current_user.region` - User's geographic region
|
|
432
|
+
*
|
|
433
|
+
* @example Custom context
|
|
434
|
+
* ```typescript
|
|
435
|
+
* contextVariables: {
|
|
436
|
+
* allowed_regions: ['US', 'EU'],
|
|
437
|
+
* access_level: 2,
|
|
438
|
+
* custom_attribute: 'value'
|
|
439
|
+
* }
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
contextVariables: z3.record(z3.string(), z3.unknown()).optional().describe("Context variables for RLS evaluation")
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
// src/security/sharing.zod.ts
|
|
446
|
+
import { z as z4 } from "zod";
|
|
447
|
+
var OWDModel = z4.enum([
|
|
448
|
+
"private",
|
|
449
|
+
// Only owner can see
|
|
450
|
+
"public_read",
|
|
451
|
+
// Everyone can see, owner can edit
|
|
452
|
+
"public_read_write",
|
|
453
|
+
// Everyone can see and edit
|
|
454
|
+
"controlled_by_parent"
|
|
455
|
+
// Access derived from parent record (Master-Detail)
|
|
456
|
+
]);
|
|
457
|
+
var SharingRuleType = z4.enum([
|
|
458
|
+
"owner",
|
|
459
|
+
// Based on record ownership (Role Hierarchy)
|
|
460
|
+
"criteria"
|
|
461
|
+
// Based on field values (e.g. Status = 'Open')
|
|
462
|
+
]);
|
|
463
|
+
var SharingLevel = z4.enum([
|
|
464
|
+
"read",
|
|
465
|
+
// Read Only
|
|
466
|
+
"edit",
|
|
467
|
+
// Read / Write
|
|
468
|
+
"full"
|
|
469
|
+
// Full Access (Transfer, Share, Delete)
|
|
470
|
+
]);
|
|
471
|
+
var ShareRecipientType = z4.enum([
|
|
472
|
+
"user",
|
|
473
|
+
"group",
|
|
474
|
+
"role",
|
|
475
|
+
"role_and_subordinates",
|
|
476
|
+
"guest"
|
|
477
|
+
// for public sharing
|
|
478
|
+
]);
|
|
479
|
+
var BaseSharingRuleSchema = z4.object({
|
|
480
|
+
// Identification
|
|
481
|
+
name: z4.string().regex(/^[a-z_][a-z0-9_]*$/).describe("Unique rule name (snake_case)"),
|
|
482
|
+
label: z4.string().optional().describe("Human-readable label"),
|
|
483
|
+
description: z4.string().optional().describe("Administrative notes"),
|
|
484
|
+
// Scope
|
|
485
|
+
object: z4.string().describe("Target Object Name"),
|
|
486
|
+
active: z4.boolean().default(true),
|
|
487
|
+
// Access
|
|
488
|
+
accessLevel: SharingLevel.default("read"),
|
|
489
|
+
// Recipient (Whom to share with)
|
|
490
|
+
sharedWith: z4.object({
|
|
491
|
+
type: ShareRecipientType,
|
|
492
|
+
value: z4.string().describe("ID or Code of the User/Group/Role")
|
|
493
|
+
}).describe("The recipient of the shared access")
|
|
494
|
+
});
|
|
495
|
+
var CriteriaSharingRuleSchema = BaseSharingRuleSchema.extend({
|
|
496
|
+
type: z4.literal("criteria"),
|
|
497
|
+
condition: z4.string().describe(`Formula condition (e.g. "department = 'Sales'")`)
|
|
498
|
+
});
|
|
499
|
+
var OwnerSharingRuleSchema = BaseSharingRuleSchema.extend({
|
|
500
|
+
type: z4.literal("owner"),
|
|
501
|
+
ownedBy: z4.object({
|
|
502
|
+
type: ShareRecipientType,
|
|
503
|
+
value: z4.string()
|
|
504
|
+
}).describe("Source group/role whose records are being shared")
|
|
505
|
+
});
|
|
506
|
+
var SharingRuleSchema = z4.discriminatedUnion("type", [
|
|
507
|
+
CriteriaSharingRuleSchema,
|
|
508
|
+
OwnerSharingRuleSchema
|
|
509
|
+
]);
|
|
510
|
+
|
|
511
|
+
// src/security/territory.zod.ts
|
|
512
|
+
import { z as z5 } from "zod";
|
|
513
|
+
var TerritoryType = z5.enum([
|
|
514
|
+
"geography",
|
|
515
|
+
// Region/Country/City
|
|
516
|
+
"industry",
|
|
517
|
+
// Vertical
|
|
518
|
+
"named_account",
|
|
519
|
+
// Key Accounts
|
|
520
|
+
"product_line"
|
|
521
|
+
// Product Specialty
|
|
522
|
+
]);
|
|
523
|
+
var TerritoryModelSchema = z5.object({
|
|
524
|
+
name: z5.string().describe("Model Name (e.g. FY24 Planning)"),
|
|
525
|
+
state: z5.enum(["planning", "active", "archived"]).default("planning"),
|
|
526
|
+
startDate: z5.string().optional(),
|
|
527
|
+
endDate: z5.string().optional()
|
|
528
|
+
});
|
|
529
|
+
var TerritorySchema = z5.object({
|
|
530
|
+
/** Identity */
|
|
531
|
+
name: SnakeCaseIdentifierSchema.describe("Territory unique name (lowercase snake_case)"),
|
|
532
|
+
label: z5.string().describe('Territory Label (e.g. "West Coast")'),
|
|
533
|
+
/** Structure */
|
|
534
|
+
modelId: z5.string().describe("Belongs to which Territory Model"),
|
|
535
|
+
parent: z5.string().optional().describe("Parent Territory"),
|
|
536
|
+
type: TerritoryType.default("geography"),
|
|
537
|
+
/**
|
|
538
|
+
* Assignment Rules (The "Magic")
|
|
539
|
+
* How do accounts automatically fall into this territory?
|
|
540
|
+
* e.g. "BillingCountry = 'US' AND BillingState = 'CA'"
|
|
541
|
+
*/
|
|
542
|
+
assignmentRule: z5.string().optional().describe("Criteria based assignment rule"),
|
|
543
|
+
/**
|
|
544
|
+
* User Assignment
|
|
545
|
+
* Users assigned to work this territory.
|
|
546
|
+
*/
|
|
547
|
+
assignedUsers: z5.array(z5.string()).optional(),
|
|
548
|
+
/** Access Level */
|
|
549
|
+
accountAccess: z5.enum(["read", "edit"]).default("read"),
|
|
550
|
+
opportunityAccess: z5.enum(["read", "edit"]).default("read"),
|
|
551
|
+
caseAccess: z5.enum(["read", "edit"]).default("read")
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
// src/security/policy.zod.ts
|
|
555
|
+
import { z as z6 } from "zod";
|
|
556
|
+
var PasswordPolicySchema = z6.object({
|
|
557
|
+
minLength: z6.number().default(8),
|
|
558
|
+
requireUppercase: z6.boolean().default(true),
|
|
559
|
+
requireLowercase: z6.boolean().default(true),
|
|
560
|
+
requireNumbers: z6.boolean().default(true),
|
|
561
|
+
requireSymbols: z6.boolean().default(false),
|
|
562
|
+
expirationDays: z6.number().optional().describe("Force password change every X days"),
|
|
563
|
+
historyCount: z6.number().default(3).describe("Prevent reusing last X passwords")
|
|
564
|
+
});
|
|
565
|
+
var NetworkPolicySchema = z6.object({
|
|
566
|
+
trustedRanges: z6.array(z6.string()).describe("CIDR ranges allowed to access (e.g. 10.0.0.0/8)"),
|
|
567
|
+
blockUnknown: z6.boolean().default(false).describe("Block all IPs not in trusted ranges"),
|
|
568
|
+
vpnRequired: z6.boolean().default(false)
|
|
569
|
+
});
|
|
570
|
+
var SessionPolicySchema = z6.object({
|
|
571
|
+
idleTimeout: z6.number().default(30).describe("Minutes before idle session logout"),
|
|
572
|
+
absoluteTimeout: z6.number().default(480).describe("Max session duration (minutes)"),
|
|
573
|
+
forceMfa: z6.boolean().default(false).describe("Require 2FA for all users")
|
|
574
|
+
});
|
|
575
|
+
var AuditPolicySchema = z6.object({
|
|
576
|
+
logRetentionDays: z6.number().default(180),
|
|
577
|
+
sensitiveFields: z6.array(z6.string()).describe("Fields to redact in logs (e.g. password, ssn)"),
|
|
578
|
+
captureRead: z6.boolean().default(false).describe("Log read access (High volume!)")
|
|
579
|
+
});
|
|
580
|
+
var PolicySchema = z6.object({
|
|
581
|
+
name: z6.string().regex(/^[a-z_][a-z0-9_]*$/).describe("Policy Name"),
|
|
582
|
+
password: PasswordPolicySchema.optional(),
|
|
583
|
+
network: NetworkPolicySchema.optional(),
|
|
584
|
+
session: SessionPolicySchema.optional(),
|
|
585
|
+
audit: AuditPolicySchema.optional(),
|
|
586
|
+
/** Assignment */
|
|
587
|
+
isDefault: z6.boolean().default(false).describe("Apply to all users by default"),
|
|
588
|
+
assignedProfiles: z6.array(z6.string()).optional().describe("Apply to specific profiles")
|
|
589
|
+
});
|
|
590
|
+
export {
|
|
591
|
+
AuditPolicySchema,
|
|
592
|
+
CriteriaSharingRuleSchema,
|
|
593
|
+
FieldPermissionSchema,
|
|
594
|
+
NetworkPolicySchema,
|
|
595
|
+
OWDModel,
|
|
596
|
+
ObjectPermissionSchema,
|
|
597
|
+
OwnerSharingRuleSchema,
|
|
598
|
+
PasswordPolicySchema,
|
|
599
|
+
PermissionSetSchema,
|
|
600
|
+
PolicySchema,
|
|
601
|
+
RLS,
|
|
602
|
+
RLSConfigSchema,
|
|
603
|
+
RLSEvaluationResultSchema,
|
|
604
|
+
RLSOperation,
|
|
605
|
+
RLSUserContextSchema,
|
|
606
|
+
RowLevelSecurityPolicySchema,
|
|
607
|
+
SessionPolicySchema,
|
|
608
|
+
ShareRecipientType,
|
|
609
|
+
SharingLevel,
|
|
610
|
+
SharingRuleSchema,
|
|
611
|
+
SharingRuleType,
|
|
612
|
+
TerritoryModelSchema,
|
|
613
|
+
TerritorySchema,
|
|
614
|
+
TerritoryType
|
|
615
|
+
};
|
|
616
|
+
//# sourceMappingURL=index.mjs.map
|