@cerebruminc/yates 3.8.1-beta.dangerous.df7fdbd → 3.8.1

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/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  [![npm version](https://img.shields.io/npm/v/@cerebruminc/yates)](https://www.npmjs.com/package/@cerebruminc/yates)
5
5
 
6
- <h1>Yates = Prisma + Ability Filters</h1>
6
+ <h1>Yates = Prisma + RLS</h1>
7
7
 
8
8
  <p>
9
9
  A module for implementing role-based access control with Prisma when using Postgres
@@ -15,13 +15,11 @@
15
15
 
16
16
  <br>
17
17
 
18
- Yates is a module for implementing role-based access control with Prisma. It is designed to be used with the [Prisma Client](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client) and PostgreSQL. It applies role abilities directly to Prisma queries by injecting permission filters into the `where` clause and recursing through nested operations.
19
-
20
- In practice, Yates builds a permission filter from the current user's role and abilities, ORs the abilities together per model + operation, and ANDs that filter with the original query. This means permissions are enforced before the query reaches the database.
18
+ Yates is a module for implementing role-based access control with Prisma. It is designed to be used with the [Prisma Client](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client) and [PostgreSQL](https://www.postgresql.org/). It uses PostgreSQL's [Row Level Security](https://www.postgresql.org/docs/9.5/ddl-rowsecurity.html) feature to provide a simple and secure way to implement role-based access control. This feature allows you to define complex access control rules and have them apply to all of your Prisma queries automatically.
21
19
 
22
20
  ## Prerequisites
23
21
 
24
- Yates requires the `prisma` package at version 4.9.0 or greater and the `@prisma/client` package at version 4.0.0 or greater. Additionally, it uses [Prisma Client extensions](https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions) to apply ability filters (which require a preview feature flag until Prisma 4.16.0, so you might need to enable this feature in your Prisma schema):
22
+ Yates requires the `prisma` package at version 4.9.0 or greater and the `@prisma/client` package at version 4.0.0 or greater. Additionally, it uses [Prisma Client extensions](https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions) to generate rules and add RLS checking (which require a preview feature flag until Prisma 4.16.0, so you might need to enable this feature in your Prisma schema):
25
23
 
26
24
  ```prisma
27
25
  generator client {
@@ -38,22 +36,15 @@ npm i @cerebruminc/yates
38
36
 
39
37
  ## Usage
40
38
 
41
- Once you've installed Yates, you can use it in your Prisma project by importing it and calling the `setup` function. This function takes a Prisma Client instance and a configuration object as arguments and returns a client that can intercept all queries and apply the appropriate ability filters to them.
39
+ Once you've installed Yates, you can use it in your Prisma project by importing it and calling the `setup` function. This function takes a Prisma Client instance and a configuration object as arguments and returns a client that can intercept all queries and apply the appropriate row-level security policies to them.
42
40
 
43
- Yates uses [Prisma Client Extensions](https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions) to apply ability filters to Prisma Client queries. This means that you can use the Prisma Client as you normally would, and Yates will automatically apply the appropriate filters to each query. It also means that you will need to apply your [Prisma Client middleware](https://www.prisma.io/docs/orm/prisma-client/client-extensions/middleware) _before_ creating the Yates client, as middleware cannot be applied to an extended client.
41
+ Yates uses [Prisma Client Extensions](https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions) to generate the RLS rules and add the RLS checking to the Prisma Client queries. This means that you can use the Prisma Client as you normally would, and Yates will automatically apply the appropriate RLS policies to each query. It also means that you will need to apply your [Prisma Client middleware](https://www.prisma.io/docs/orm/prisma-client/client-extensions/middleware) _before_ creating the Yates client, as middleware cannot be applied to an extended client.
44
42
 
45
43
  Client extensions share the same API as the Prisma Client, you can use the Yates client as a drop-in replacement for the Prisma Client in your application. They also share the same connection pool as the base client, which means that you can freely create new Yates clients with minimal performance impact.
46
44
 
47
- The `setup` function will generate CRUD abilities for each model in your Prisma schema, as well as any additional abilities that you have defined in your configuration. It will then map those abilities to your user roles and apply the resulting filters to each Prisma query.
48
-
49
- For Yates to be able to apply the correct abilities for each request, you must pass a function called `getContext` in the `setup` configuration that will return the user role for the current request. This function will be called for each request and the user role returned will be used to apply ability filters. If you want to bypass permissions completely for a specific role, you can return `null` from the `getContext` function for that role.
50
-
51
- ### Nested relations
52
-
53
- Yates applies permissions recursively across nested relations:
45
+ The `setup` function will generate CRUD abilities for each model in your Prisma schema, as well as any additional abilities that you have defined in your configuration. It will then create a new PG role for each ability and apply the appropriate row-level security policies to each role. Finally, it will create a new PG role for each user role you specify and grant them the appropriate abilities.
54
46
 
55
- - **Reads (`include`/`select`)**: Yates walks the selection tree and injects read filters for each related model. If the role has no read ability for a related model, the selection is dropped.
56
- - **Writes (nested create/update/delete/upsert)**: Yates validates each nested operation against the related model's abilities. For example, nested creates are checked against insert filters, and nested updates/deletes verify the target record is permitted before executing.
47
+ For Yates to be able to set the correct user role for each request, you must pass a function called `getContext` in the `setup` configuration that will return the user role for the current request. This function will be called for each request and the user role returned will be used to set the `role` in the current session. If you want to bypass RLS completely for a specific role, you can return `null` from the `getContext` function for that role.
57
48
 
58
49
  For accessing the context of a Prisma query, we recommend using a package like [cls-hooked](https://www.npmjs.com/package/cls-hooked) to store the context in the current session.
59
50
 
@@ -68,35 +59,46 @@ const prisma = new PrismaClient();
68
59
  const client = await setup({
69
60
  prisma,
70
61
  // Define any custom abilities that you want to add to the system.
71
- customAbilities: {
72
- Post: {
73
- insertOwnPost: {
74
- description: "Insert own post",
75
- // You can express the rule as a Prisma `where` clause.
76
- expression: (_client, _row, context) => ({
77
- // This expression uses a context setting returned by the getContext function
78
- authorId: context('user.id')
79
- }),
80
- operation: "INSERT",
62
+ customAbilities: () => ({
63
+ USER: {
64
+ Post: {
65
+ insertOwnPost: {
66
+ description: "Insert own post",
67
+ // You can express the rule as a Prisma `where` clause.
68
+ expression: (client, row, context) => {
69
+ return {
70
+ // This expression uses a context setting returned by the getContext function
71
+ authorId: context('user.id')
72
+ }
73
+ },
74
+ operation: "INSERT",
75
+ },
81
76
  },
82
- deleteOwnPost: {
83
- description: "Delete own post",
84
- expression: (_client, _row, context) => ({
85
- authorId: context("user.id")
86
- }),
87
- operation: "DELETE",
88
- },
89
- },
90
- User: {
91
- updateOwnUser: {
92
- description: "Update own user",
93
- expression: (_client, _row, context) => ({
94
- id: context("user.id")
95
- }),
96
- operation: "UPDATE",
77
+ Comment: {
78
+ deleteOnOwnPost: {
79
+ description: "Delete comment on own post",
80
+ // You can also express the rule as a conventional Prisma query.
81
+ expression: (client, row, context) => {
82
+ return client.post.findFirst({
83
+ where: {
84
+ id: row('postId'),
85
+ authorId: context('user.id')
86
+ }
87
+ })
88
+ },
89
+ operation: "DELETE",
90
+ },
97
91
  },
92
+ User: {
93
+ updateOwnUser: {
94
+ description: "Update own user",
95
+ // For low-level control you can also write expressions as a raw SQL string.
96
+ expression: `current_setting('user.id') = "id"`,
97
+ operation: "UPDATE",
98
+ },
99
+ }
98
100
  }
99
- },
101
+ }),
100
102
  // Return a mapping of user roles and abilities.
101
103
  // This function is parameterised with a list of all CRUD abilities that have been
102
104
  // automatically generated by Yates, as well as any customAbilities that have been defined.
@@ -105,10 +107,7 @@ const client = await setup({
105
107
  SUPER_ADMIN: "*",
106
108
  USER: [
107
109
  abilities.User.read,
108
- abilities.Post.read,
109
- abilities.Post.insertOwnPost,
110
- abilities.Post.deleteOwnPost,
111
- abilities.User.updateOwnUser,
110
+ abilities.Comment.read
112
111
  ],
113
112
  };
114
113
  },
@@ -125,11 +124,17 @@ const client = await setup({
125
124
  return {
126
125
  role,
127
126
  context: {
128
- // This context setting will be available in ability expressions via `context(...)`
129
- "user.id": user.id,
127
+ // This context setting will be available in ability expressions using `current_setting('user.id')`
128
+ 'user.id': user.id,
130
129
  },
131
130
  };
132
131
  },
132
+ options: {
133
+ // The maximum amount of time Yates will wait to acquire a transaction from the database. The default value is 30 seconds.
134
+ txMaxWait: 5000,
135
+ // The maximum amount of time the Yates query transaction can run before being canceled and rolled back. The default value is 30 seconds.
136
+ txTimeout: 10000,
137
+ }
133
138
  });
134
139
  ```
135
140
 
@@ -140,283 +145,35 @@ const client = await setup({
140
145
  When defining an ability you need to provide the following properties:
141
146
 
142
147
  - `description`: A description of the ability.
143
- - `expression`: A Prisma `where` clause (or a function that returns one) that will be combined with the original query. Abilities for the same model + operation are OR-ed together, and the resulting filter is AND-ed with the original query.
144
- - For `INSERT` operations, the expression is matched against the incoming `data`.
145
- - For `SELECT`, `UPDATE` and `DELETE` operations, the expression is merged into the Prisma `where` clause.
146
- - `operation`: The operation that the ability is being applied to. This can be one of `INSERT`, `SELECT`, `UPDATE` or `DELETE`.
148
+ - `expression`: A boolean SQL expression that will be used to filter the results of the query. This expression can use any of the columns in the table that the ability is being applied to, as well as any context settings that have been defined in the `getContext` function.
149
+ - For `INSERT`, `UPDATE` and `DELETE` operations, the expression uses the values from the row being inserted. If the expression returns `false` for a row, that row will not be inserted, updated or deleted.
150
+ - For `SELECT` operations, the expression uses the values from the row being returned. If the expression returns `false` for a row, that row will not be returned.
151
+ - `operation`: The operation that the ability is being applied to. This can be one of `CREATE`, `READ`, `UPDATE` or `DELETE`.
147
152
 
148
153
  ### Debug
149
154
 
150
155
  To run Yates in debug mode, use the environment variable `DEBUG=yates`.
151
156
 
152
- ## Local development database
153
-
154
- ### Start/stop Postgres via Docker
155
-
156
- This repo includes a local Postgres service in `docker-compose.yml` (mapped to port **5666** on your host):
157
-
158
- ```bash
159
- docker compose up -d db
160
- ```
161
-
162
- To stop it:
163
-
164
- ```bash
165
- docker compose down
166
- ```
167
-
168
- ### Initialize the databases with Prisma
169
-
170
- Run the setup script against the Docker database:
171
-
172
- ```bash
173
- DATABASE_URL="postgresql://postgres:postgres@localhost:5666/yates" \
174
- DATABASE_URL_2="postgresql://postgres:postgres@localhost:5666/yates_2" \
175
- npm run setup
176
- ```
177
-
178
- ## Benchmarks
179
-
180
- This repo includes a simple in-process benchmark harness to compare performance across branches (v1 vs v2).
181
-
182
- ### Seed data
183
-
184
- ```bash
185
- npm run bench:seed
186
- ```
187
-
188
- Optional size overrides:
189
-
190
- ```bash
191
- BENCH_USERS=5000 BENCH_POSTS=100000 BENCH_TAGS=200 npm run bench:seed
192
- ```
193
-
194
- ### Run benchmarks
195
-
196
- Build first, then run:
197
-
198
- ```bash
199
- npm run build
200
- npm run bench:run
201
- ```
202
-
203
- Benchmark controls:
204
-
205
- ```bash
206
- BENCH_ITERS=100 BENCH_WARMUP=10 npm run bench:run
207
- ```
208
-
209
- ### Comparing v1 vs v2
210
-
211
- Run the same commands on each branch and compare the JSON output:
212
-
213
- ```bash
214
- git checkout master # v1 (RLS)
215
- npm run build
216
- npm run bench:run > bench-v1.json
217
-
218
- git checkout lucianbuzzo/v2 # v2 (query filters)
219
- npm run build
220
- npm run bench:run > bench-v2.json
221
- ```
222
-
223
- ## Cookbook
224
-
225
- Common permission patterns expressed as Yates abilities.
226
-
227
- ### Read own records (user-scoped)
228
-
229
- Allow a user to read only their own records:
230
-
231
- ```ts
232
- customAbilities: {
233
- Post: {
234
- readOwnPosts: {
235
- description: "Read own posts",
236
- operation: "SELECT",
237
- expression: (_client, _row, context) => ({
238
- authorId: context("user.id") as string,
239
- }),
240
- },
241
- },
242
- },
243
- getRoles: (abilities) => ({
244
- USER: [abilities.Post.readOwnPosts],
245
- }),
246
- ```
247
-
248
- ### Create only if the record is owned by the user
249
-
250
- Allow creates only when `authorId` matches the current user:
251
-
252
- ```ts
253
- customAbilities: {
254
- Post: {
255
- createOwnPosts: {
256
- description: "Create own posts",
257
- operation: "INSERT",
258
- expression: (_client, _row, context) => ({
259
- authorId: context("user.id") as string,
260
- }),
261
- },
262
- },
263
- },
264
- ```
265
-
266
- ### Read public OR owned
267
-
268
- Combine multiple abilities; they are OR-ed together:
269
-
270
- ```ts
271
- customAbilities: {
272
- Post: {
273
- readPublic: {
274
- description: "Read public posts",
275
- operation: "SELECT",
276
- expression: () => ({ published: true }),
277
- },
278
- readOwn: {
279
- description: "Read own posts",
280
- operation: "SELECT",
281
- expression: (_client, _row, context) => ({
282
- authorId: context("user.id") as string,
283
- }),
284
- },
285
- },
286
- },
287
- getRoles: (abilities) => ({
288
- USER: [abilities.Post.readPublic, abilities.Post.readOwn],
289
- }),
290
- ```
291
-
292
- ### Update only owned records
293
-
294
- ```ts
295
- customAbilities: {
296
- Post: {
297
- updateOwn: {
298
- description: "Update own posts",
299
- operation: "UPDATE",
300
- expression: (_client, _row, context) => ({
301
- authorId: context("user.id") as string,
302
- }),
303
- },
304
- },
305
- },
306
- ```
307
-
308
- ### Delete only if record is unpublished and owned
309
-
310
- ```ts
311
- customAbilities: {
312
- Post: {
313
- deleteOwnDrafts: {
314
- description: "Delete own drafts",
315
- operation: "DELETE",
316
- expression: (_client, _row, context) => ({
317
- AND: [
318
- { authorId: context("user.id") as string },
319
- { published: false },
320
- ],
321
- }),
322
- },
323
- },
324
- },
325
- ```
326
-
327
- ### Tenant isolation by organization id
328
-
329
- ```ts
330
- customAbilities: {
331
- Organization: {
332
- readOrg: {
333
- description: "Read org",
334
- operation: "SELECT",
335
- expression: (_client, _row, context) => ({
336
- id: context("org.id") as string,
337
- }),
338
- },
339
- },
340
- Post: {
341
- readOrgPosts: {
342
- description: "Read org posts",
343
- operation: "SELECT",
344
- expression: (_client, _row, context) => ({
345
- organizationId: context("org.id") as string,
346
- }),
347
- },
348
- },
349
- },
350
- ```
351
-
352
- ### Membership-based access (relation filter)
353
-
354
- Allow access when the user has a role in the org (relation filter):
355
-
356
- ```ts
357
- customAbilities: {
358
- Organization: {
359
- readOrgIfMember: {
360
- description: "Read org if member",
361
- operation: "SELECT",
362
- expression: (_client, _row, context) => ({
363
- roleAssignment: {
364
- some: {
365
- userId: context("user.id") as string,
366
- },
367
- },
368
- }),
369
- },
370
- },
371
- },
372
- ```
373
-
374
- ### Admin bypass
375
-
376
- Grant all abilities for admins:
377
-
378
- ```ts
379
- getRoles: (abilities) => ({
380
- ADMIN: "*",
381
- USER: [abilities.Post.read, abilities.Post.create],
382
- }),
383
- ```
384
-
385
- ### Soft delete visibility
386
-
387
- Hide soft-deleted records by default:
388
-
389
- ```ts
390
- customAbilities: {
391
- Post: {
392
- readNotDeleted: {
393
- description: "Read non-deleted posts",
394
- operation: "SELECT",
395
- expression: () => ({
396
- deletedAt: null,
397
- }),
398
- },
399
- },
400
- },
401
- ```
402
-
403
- ## Tradeoffs vs RLS
157
+ ## Known limitations
404
158
 
405
- Yates enforces permissions in the application layer by injecting filters into Prisma queries. Compared to database-level RLS, there are some tradeoffs:
159
+ ### Nested transactions
406
160
 
407
- - **No DB-level enforcement if Prisma is bypassed**: direct SQL or other clients won’t be protected unless you keep RLS in place.
408
- - **Extra queries for some create checks**: relation-based create checks may trigger preflight reads to verify related records.
409
- - **Requires consistent usage**: permissions apply only when using the Yates-wrapped Prisma client.
161
+ Yates uses a transaction to apply the RLS policies to each query. This means that if you are using transactions in your application, rollbacks will not work as expected. This is because [Prisma has poor support for nested transactions](https://github.com/prisma/prisma/issues/15212) and will `COMMIT` the inner transaction even if the outer transaction is rolled back.
162
+ If you need this functionality and you are using Yates, you can return `null` from the `getContext()` setup method to bypass the internal transaction, and therefore the RLS policies for the current request. See the `nested-transactions.spec.ts` test case for an example of how to do this.
410
163
 
411
- ## Known limitations
164
+ ### Unsupported Prisma Client query features
412
165
 
413
- ### Expression limits
166
+ If you are using the Prisma Client to construct an ability expression, the following `where` keywords are not supported.
414
167
 
415
- - Create checks support scalar filters and basic `AND`/`OR`/`NOT` logic. Relation filters are supported when the related record can be resolved from the create `data` (for example via `connect` or foreign key fields).
168
+ - `AND`
169
+ - `OR`
170
+ - `NOT`
171
+ - `is`
172
+ - `isNot`
416
173
 
417
- ## Migration
174
+ Additionally, using context or row values to query Prisma Enums is not supported.
418
175
 
419
- - v1 -> v2 guide: `MIGRATION.md`
176
+ If you need to use these expressions, you can use the `expression` property of the ability to write a raw SQL expression instead.
420
177
 
421
178
  ## License
422
179
 
@@ -1,10 +1,11 @@
1
1
  import { Prisma, PrismaClient } from "@prisma/client";
2
2
  import { defineDmmfProperty } from "@prisma/client/runtime/library";
3
+ import { AsyncReturnType } from "type-fest";
3
4
  export type RuntimeDataModel = Parameters<typeof defineDmmfProperty>[1];
4
5
  type FFMeta<M extends Prisma.ModelName> = PrismaClient[Uncapitalize<M>]["findFirst"];
5
6
  type ModelWhereArgs<M extends Prisma.ModelName> = Exclude<Parameters<FFMeta<M>>["0"], undefined>["where"];
6
- type ModelFieldRefs<M extends Prisma.ModelName> = PrismaClient[Uncapitalize<M>]["fields"];
7
- export type ExpressionContext<ContextKeys extends string> = (key: ContextKeys) => string | number | string[] | undefined;
8
- export type ExpressionRow<M extends Prisma.ModelName> = <K extends keyof ModelFieldRefs<M>>(col: K) => ModelFieldRefs<M>[K];
9
- export type Expression<ContextKeys extends string, M extends Prisma.ModelName> = ModelWhereArgs<M> | ((client: PrismaClient, row: ExpressionRow<M>, context: ExpressionContext<ContextKeys>) => ModelWhereArgs<M> | Promise<ModelWhereArgs<M>>);
7
+ type ModelResult<M extends Prisma.ModelName> = AsyncReturnType<FFMeta<M>>;
8
+ type NonNullableModelResult<M extends Prisma.ModelName> = Exclude<ModelResult<M>, null>;
9
+ export type Expression<ContextKeys extends string, M extends Prisma.ModelName> = string | ((client: PrismaClient, row: <K extends keyof NonNullableModelResult<M>>(col: K) => NonNullableModelResult<M>[K], context: (key: ContextKeys) => string) => Promise<ModelResult<Exclude<Prisma.ModelName, M>>> | ModelWhereArgs<M>);
10
+ export declare const expressionToSQL: <ContextKeys extends string, YModel extends Prisma.ModelName>(getExpression: Expression<ContextKeys, YModel>, table: string) => Promise<string>;
10
11
  export {};