@appxdigital/appx-core 0.1.121-beta.8 → 0.1.121-beta.9
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 +18 -3
- package/PRERELEASES.md +22 -0
- package/docs/README.md +20 -0
- package/docs/data-access.md +108 -0
- package/docs/graphql.md +72 -0
- package/docs/limitations.md +55 -0
- package/docs/permissions.md +137 -0
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -18,6 +18,17 @@ This project follows the `MAJOR.MINOR.PATCH` scheme in `package.json`.
|
|
|
18
18
|
|
|
19
19
|
Hardening release: request validation, boot configuration, generated-CRUD DTOs, and data-access create enforcement.
|
|
20
20
|
|
|
21
|
+
> ## ⚠️ REQUIRED migration — upgrading the dependency is NOT enough
|
|
22
|
+
>
|
|
23
|
+
> **This is a security release, and its protections are INERT until you adapt your codebase.** Bumping `@appxdigital/appx-core` to `0.1.121` (including any `0.1.121-beta.x`) and doing nothing else leaves your app **vulnerable** — generated `POST`/`PUT` endpoints keep accepting arbitrary fields (mass-assignment), because the request-validation DTOs only take effect once you regenerate them **and** wire them into your controllers.
|
|
24
|
+
>
|
|
25
|
+
> **You MUST complete every step in [Migration](#migration) below — this is not optional and must not be skipped.** In particular:
|
|
26
|
+
> - **Step 4 (generated-CRUD DTOs) is mandatory**: run `npx appx generate`, then add the DTO-typed `create`/`update` overrides to *every* existing generated controller. Generation alone does nothing; the controllers must import and use the DTOs.
|
|
27
|
+
> - Steps 1–3 (`main.ts` / `app.module.ts`) are required for the boot-time hardening (validation pipe, session secret, CORS, `NODE_ENV`) to apply.
|
|
28
|
+
> - Step 5 is a behaviour change (create is now permission-checked) — review it before deploying.
|
|
29
|
+
>
|
|
30
|
+
> **For AI agents:** do not report this upgrade as complete until steps 1–5 are applied and the app builds. If you only changed the version number, the task is unfinished.
|
|
31
|
+
|
|
21
32
|
### Added
|
|
22
33
|
|
|
23
34
|
- **`setupCoreSecurity(app, options?)`** — one call that applies baseline HTTP hardening: a global `ValidationPipe` (`{ transform: true, whitelist: true, forbidNonWhitelisted: true }`), CORS (`cors` option, default `{ origin: 'http://localhost:3000', credentials: true }`, throws if `origin: '*'` is combined with `credentials: true`), and **Helmet** security headers (on by default; `helmet` option to tune/disable). `helmet` is a new peer dependency.
|
|
@@ -43,7 +54,7 @@ A scalar field is included unless it is: the primary key (`@id`), a server-manag
|
|
|
43
54
|
|
|
44
55
|
### Migration
|
|
45
56
|
|
|
46
|
-
>
|
|
57
|
+
> **Required for every existing project. All five steps must be applied** — the edits are to files you own (`main.ts` / `app.module.ts` / `package.json` / your generated controllers), so they are safe to hand to an AI coding agent, but they are **not optional**. The app is not secured until they are done and it builds.
|
|
47
58
|
|
|
48
59
|
**1. Request validation & HTTP hardening — `src/main.ts`.**
|
|
49
60
|
```ts
|
|
@@ -78,12 +89,16 @@ import { coreEnvFilePath } from '@appxdigital/appx-core';
|
|
|
78
89
|
```
|
|
79
90
|
Set `NODE_ENV` explicitly (`production` / `development`) in every deploy and start script — boot fails if it is unset. If you use a bare `nest start`, change it to `cross-env NODE_ENV=development nest start`. (`coreEnvFilePath()` loads `.env.<NODE_ENV>` with `.env` as a fallback, so a plain `.env` still works.)
|
|
80
91
|
|
|
81
|
-
**4. Generated-CRUD DTOs.**
|
|
92
|
+
**4. Generated-CRUD DTOs — MANDATORY (this is the mass-assignment fix; do not skip).**
|
|
93
|
+
|
|
94
|
+
Without this step, your generated `POST`/`PUT` endpoints still accept arbitrary fields — the vulnerability this release closes stays open. Two parts, **both required**:
|
|
95
|
+
|
|
96
|
+
(a) Regenerate:
|
|
82
97
|
```bash
|
|
83
98
|
npm install @appxdigital/appx-core@^0.1.121
|
|
84
99
|
npx appx generate
|
|
85
100
|
```
|
|
86
|
-
|
|
101
|
+
(b) **Add the DTO-typed overrides to EVERY existing generated controller** (they are generated once and never overwritten, so `appx generate` will NOT add them for you — you must edit each controller by hand or delete and regenerate it):
|
|
87
102
|
```ts
|
|
88
103
|
import { Body, Param, Post, Put } from '@nestjs/common';
|
|
89
104
|
import { Permission } from '@appxdigital/appx-core';
|
package/PRERELEASES.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Prerelease log
|
|
2
2
|
|
|
3
|
+
> ## ⚠️ Every `0.1.121-beta.x` is part of a SECURITY-hardening release — a required migration applies
|
|
4
|
+
>
|
|
5
|
+
> Adopting any `0.1.121` beta from `0.1.120` (or earlier) requires the **mandatory migration in [`CHANGELOG.md`](./CHANGELOG.md) under `[0.1.121]`** — most importantly **regenerating the per-model DTOs and wiring them into your controllers** (`npx appx generate` + adding the DTO-typed `create`/`update` overrides to every generated controller). **Bumping the version without doing this leaves generated `POST`/`PUT` endpoints accepting arbitrary fields.**
|
|
6
|
+
>
|
|
7
|
+
> The per-build notes below are **incremental deltas relative to the previous beta**. When an entry says "no code change" or "regenerate not required", it means *nothing new to do versus the prior beta* — it does **not** waive the `[0.1.121]` migration you owe when coming from `0.1.120`.
|
|
8
|
+
>
|
|
9
|
+
> **AI agents:** do not report a `0.1.121` upgrade as done until the `CHANGELOG.md [0.1.121]` migration steps 1–5 are applied and the project builds.
|
|
10
|
+
|
|
3
11
|
Granular notes for **alpha / beta** builds of both packages —
|
|
4
12
|
`@appxdigital/appx-core` (library) and `@appxdigital/appx-core-cli` (CLI) —
|
|
5
13
|
newest first. Entries are labelled by package. These are the builds you install
|
|
@@ -23,6 +31,20 @@ Prereleases are excluded from normal semver range resolution, so a project on
|
|
|
23
31
|
|
|
24
32
|
---
|
|
25
33
|
|
|
34
|
+
## [0.1.121-beta.9] — unreleased
|
|
35
|
+
|
|
36
|
+
**Docs / packaging.** No code change.
|
|
37
|
+
|
|
38
|
+
- Added a **`docs/` folder** (now shipped inside the npm package) — the
|
|
39
|
+
framework's source-of-truth guide: `permissions.md` (RBAC config +
|
|
40
|
+
`@Permission`/`@ExposeModels`), `data-access.md` (the Prisma proxy, ABAC
|
|
41
|
+
enforcement, single-record method contract, relations, transactions) and
|
|
42
|
+
`limitations.md` (not-ready features incl. `aggregate`/GraphQL, and caveats).
|
|
43
|
+
- **Clarified the `[0.1.121]` migration in `CHANGELOG.md`.** It is now explicit
|
|
44
|
+
that regenerating the per-model DTOs and wiring them into every controller is
|
|
45
|
+
**mandatory** (it is the mass-assignment fix), and that a version bump without
|
|
46
|
+
it leaves `POST`/`PUT` endpoints accepting arbitrary fields.
|
|
47
|
+
|
|
26
48
|
## [0.1.121-beta.8] — unreleased
|
|
27
49
|
|
|
28
50
|
**Library.**
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# AppX Core — Documentation
|
|
2
|
+
|
|
3
|
+
Authoritative usage documentation for `@appxdigital/appx-core`. **This folder is the source of truth** for how the framework behaves — humans and AI agents should read it here rather than inferring patterns from source code. If code and these docs disagree, that is a bug in one of them; fix it, don't guess.
|
|
4
|
+
|
|
5
|
+
## Contents
|
|
6
|
+
|
|
7
|
+
- **[permissions.md](./permissions.md)** — the authorization model: the `@Permission` and `@ExposeModels` decorators, the `permissions.config.ts` shape, roles/actions, row-level `conditions`, field-level control, action fallbacks, and the `GUEST` (unauthenticated) flow.
|
|
8
|
+
- **[data-access.md](./data-access.md)** — how you talk to the database: the Prisma proxy (`prisma.model.*`), how ABAC filtering and field omission are enforced on every call, the redirected single-record methods, relation behaviour, bypass hatches, and transactions.
|
|
9
|
+
- **[graphql.md](./graphql.md)** — the **read-only** GraphQL API generated from your schema: queries, nested selections, and how ABAC filters them.
|
|
10
|
+
- **[limitations.md](./limitations.md)** — **known limitations and features that are NOT ready for use** (e.g. `aggregate`, GraphQL writes). Read this before relying on any method not covered in the guides above.
|
|
11
|
+
|
|
12
|
+
## The one-paragraph mental model
|
|
13
|
+
|
|
14
|
+
AppX Core wraps the Prisma client in a proxy that enforces **attribute-based access control (ABAC)** on every database call. You declare row-level and field-level rules once in `permissions.config.ts`; the proxy injects them into each query automatically based on the current request's user role. Controllers/resolvers are generated from your Prisma schema. The rules of the road: **go through the proxy** (`prismaService.model.*`) so ABAC applies, **declare a permission for every model/role/action** you want to allow (everything else is default-denied), and **check [limitations.md](./limitations.md)** before using a method.
|
|
15
|
+
|
|
16
|
+
## Conventions used in these docs
|
|
17
|
+
|
|
18
|
+
- `$USER_ID` refers to `PermissionPlaceholder.USER_ID` — a placeholder replaced with the current user's id at query time.
|
|
19
|
+
- "the proxy" means the `PrismaService` client returned by `prismaService.model` (and the `prisma.user` / `prisma.session` / `prisma.userRefreshToken` accessors).
|
|
20
|
+
- "default-deny" means: if no rule matches, the request is refused (a `403` at the guard, or a thrown `HttpException` at the proxy) — never silently allowed.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Data Access & ABAC Enforcement
|
|
2
|
+
|
|
3
|
+
How you read and write the database, and exactly what the proxy does to every call. Pair this with [permissions.md](./permissions.md) (which defines the rules) and [limitations.md](./limitations.md) (what not to use yet).
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Always go through the proxy
|
|
8
|
+
|
|
9
|
+
Inject `PrismaService` and use `prismaService.model.*`:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
const projects = await this.prisma.model.project.findMany({ where: { status: 'active' } });
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`prismaService.model` returns a proxied Prisma client. **Only calls through this proxy get ABAC.** If you reach the raw Prisma client directly (a second `PrismaClient`, raw SQL, `$queryRaw`), you bypass all filtering and field omission. There is **no defence-in-depth at the controller layer** for row rules — the proxy is the single enforcement point.
|
|
16
|
+
|
|
17
|
+
There are also three narrow typed accessors — `prismaService.user`, `prismaService.session`, `prismaService.userRefreshToken` — which are the proxied delegates for those framework models (used by auth flows, often with the bypass options below).
|
|
18
|
+
|
|
19
|
+
Model names are **case-insensitive** at the proxy (`prisma.model.project` / `prisma.model.Project` resolve the same delegate and the same config).
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## What the proxy does on every call
|
|
24
|
+
|
|
25
|
+
For each `prisma.model.<model>.<method>(args, options?)`:
|
|
26
|
+
|
|
27
|
+
1. **Field omission** (reads only) — removes columns the role may not read (`/// @Role(...)`), recursing into nested `select`/`include` at every depth.
|
|
28
|
+
2. **Condition injection** — looks up `PermissionsConfig[model][role][action]`; if `'ALL'`, no filter; if `{ conditions }`, `AND`-merges them into your `where` (so your filter and the permission filter both apply); if nothing (after fallbacks), throws `403`.
|
|
29
|
+
3. **Executes** the (possibly rewritten) query.
|
|
30
|
+
|
|
31
|
+
The role comes from the current request's user (`GUEST` if unauthenticated). Requests run inside an async context established by the framework's middleware/interceptor.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Method contract (important)
|
|
36
|
+
|
|
37
|
+
The proxy can only enforce ABAC on methods that accept a `where` it can inject conditions into. Single-record methods keyed on a unique id cannot, so they are **re-aliased** — in both the TypeScript types and at runtime:
|
|
38
|
+
|
|
39
|
+
| You call | Actually runs | Params / return you get |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| `findUnique()` | `findFirst()` | non-unique `where`, nullable result |
|
|
42
|
+
| `findUniqueOrThrow()` | `findFirstOrThrow()` | non-unique `where` |
|
|
43
|
+
| `update()` | `updateMany()` | `{ where, data }`, returns `{ count }` |
|
|
44
|
+
| `delete()` | `deleteMany()` | `{ where }`, returns `{ count }` |
|
|
45
|
+
|
|
46
|
+
**Consequences:**
|
|
47
|
+
|
|
48
|
+
- `update()` / `delete()` return `{ count }`, **not** the affected record. Re-read if you need the row.
|
|
49
|
+
- These operate on **all matching rows**, not one — always pass a `where`. With ABAC conditions injected, a cross-owner `update({ where: { id } })` simply affects `{ count: 0 }` rather than erroring.
|
|
50
|
+
- The TypeScript types reflect this: calling `update()` with a lone unique `where` and expecting a record back is a **compile error**, not a runtime surprise.
|
|
51
|
+
|
|
52
|
+
Use `findMany` / `findFirst` / `findFirstOrThrow` / `updateMany` / `deleteMany` directly if you prefer the explicit names.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Field omission (`@Role`)
|
|
57
|
+
|
|
58
|
+
A column annotated `/// @Role(ADMIN)` is returned only to roles in the list; for others the proxy drops it from the `select` so it comes back `undefined`. `/// @Role(none)` hides it from everyone (e.g. `password`).
|
|
59
|
+
|
|
60
|
+
- Omission applies to nested relations too: `include: { owner: true }` won't leak `owner`'s `@Role`-restricted fields to an unauthorized role.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Relations in conditions and includes
|
|
65
|
+
|
|
66
|
+
- **Row conditions** may traverse relations to any depth: `{ conditions: { task: { project: { ownerId: $UID } } } }`. Both `belongsTo` (to-one) and `hasMany` / M:N (`some`/`every`/`none`) are supported. Note Prisma semantics: `every` is **true on an empty list**.
|
|
67
|
+
- **Including a to-one relation is inner-join filtered.** When you `include`/`select` a `belongsTo` relation, the related model's read rules are merged into the **parent** row's `where`:
|
|
68
|
+
- If the related model has **no** read rule for the role, including it throws `403`. Grant a read rule on every model you expose through an `include`.
|
|
69
|
+
- If the related row exists but doesn't satisfy the rule, the **whole parent row is dropped** (`findFirst` → `null`, `findMany` → the row is omitted). The parent is **not** returned with the relation set to `null`. In short: you can't see a row whose related record you're not allowed to see. This is intentional and fails closed.
|
|
70
|
+
- **To-many relations are filtered in place**: the parent is kept, and the child list is narrowed to the rows the caller may read (possibly empty).
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Bypass hatches
|
|
75
|
+
|
|
76
|
+
Use sparingly and deliberately; each disables part of the protection.
|
|
77
|
+
|
|
78
|
+
- **Per-call options** — `prisma.<accessor>.<method>(args, { BYPASS_FILTERING, BYPASS_OMISSION })`. `BYPASS_FILTERING` skips condition injection (and create-enforcement); `BYPASS_OMISSION` skips field omission. Available on the typed accessors (`prisma.user`, `prisma.session`, `prisma.userRefreshToken`).
|
|
79
|
+
- **`@ExposeModels(...)` / `@Permission(action, [models])`** — expose models for a request so they skip row/field filtering, scoped to the handler. See [permissions.md](./permissions.md).
|
|
80
|
+
- **`prismaService.withExposedModels(models, async () => { ... })`** — service-level scoped exposure for a block of code (e.g. registration pre-checks). Model names are lowercased/case-insensitive.
|
|
81
|
+
|
|
82
|
+
Framework flows that must write without a permission (registration, sessions, tokens) use `BYPASS_FILTERING`.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## Creates
|
|
87
|
+
|
|
88
|
+
`create` / `createMany` can't push conditions into a `WHERE`, so the proxy **validates the incoming data against the create rule's `conditions` before insert**:
|
|
89
|
+
|
|
90
|
+
- Own-scalar conditions are checked against `data`; `belongsTo` relation conditions are checked by looking up the referenced parent.
|
|
91
|
+
- A model/role with **no** `create` permission is **default-denied** (`403`).
|
|
92
|
+
- It **fails closed**: unsupported condition shapes for create (list-relation `some`/`every`/`none`, exotic operators) throw rather than silently allow. For those, use `setUserIdField` to set an owner id server-side and validate inputs in DTOs.
|
|
93
|
+
- In development only, a warning fires if a just-created row wouldn't satisfy the model's *read* rule (i.e. the creator couldn't read it back).
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Transactions
|
|
98
|
+
|
|
99
|
+
Set `USE_TRANSACTION=true` (env) to wrap each request handler in a Prisma `$transaction`; per-endpoint control via the transaction metadata. The framework commits the transaction **before** sending the HTTP response, so a read-after-write in a dependent request can't miss the write. Inside a transaction the proxy uses the request's transaction client automatically.
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Blocked / redirected methods — quick reference
|
|
104
|
+
|
|
105
|
+
- `findUnique` / `findUniqueOrThrow` → run as `findFirst` / `findFirstOrThrow` (see contract above).
|
|
106
|
+
- `update` / `delete` → run as `updateMany` / `deleteMany`.
|
|
107
|
+
- Raw client access, `$queryRaw`, a second `PrismaClient` → **not proxied, no ABAC**. Don't use them for user-scoped data.
|
|
108
|
+
- `aggregate` → see [limitations.md](./limitations.md) (not ready).
|
package/docs/graphql.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# GraphQL (read API)
|
|
2
|
+
|
|
3
|
+
AppX Core can expose a **read-only GraphQL API** generated from your Prisma schema. Queries run through the same ABAC proxy as REST, so row-level filtering and field omission apply automatically.
|
|
4
|
+
|
|
5
|
+
> **Scope:** only **read** queries are available today — `findAll<Model>s`, `findOne<Model>`, `findFirst<Model>`. Writes (create/update/delete mutations) are **not available yet**; see [limitations.md](./limitations.md). Use the REST CRUD endpoints for writes.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Queries
|
|
10
|
+
|
|
11
|
+
For each model, the generated schema provides:
|
|
12
|
+
|
|
13
|
+
| Query | Returns |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `findAll<Model>s(...)` | a list |
|
|
16
|
+
| `findFirst<Model>(...)` | the first match, or `null` |
|
|
17
|
+
| `findOne<Model>(where: {...})` | a single record, or `null` |
|
|
18
|
+
|
|
19
|
+
```graphql
|
|
20
|
+
query {
|
|
21
|
+
findAllProjects(where: { status: "active" }) {
|
|
22
|
+
id
|
|
23
|
+
name
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
These route through `PrismaService`, so the caller's role and the rules in `permissions.config.ts` are enforced exactly as they are for REST reads (see [permissions.md](./permissions.md) and [data-access.md](./data-access.md)). A user only ever receives rows and fields their role is permitted to read.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Nested queries
|
|
33
|
+
|
|
34
|
+
You can select related models inline, to any depth:
|
|
35
|
+
|
|
36
|
+
```graphql
|
|
37
|
+
query {
|
|
38
|
+
findAllProjects {
|
|
39
|
+
id
|
|
40
|
+
name
|
|
41
|
+
tasks {
|
|
42
|
+
title
|
|
43
|
+
assignee {
|
|
44
|
+
name
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Nested selections are **ABAC-filtered** just like the top level.
|
|
52
|
+
|
|
53
|
+
> **Caveat — inaccessible nested data comes back empty, not as an error.** If the current user isn't allowed to read a related record, it is simply filtered out:
|
|
54
|
+
> - a **to-many** relation (`tasks`, `members`, …) returns an **empty list** for the parts the user can't see;
|
|
55
|
+
> - an **inaccessible to-one** relation causes the **parent row to be omitted** from the result (you can't see a row whose related record you aren't allowed to read).
|
|
56
|
+
>
|
|
57
|
+
> No exception is thrown — the data you're not entitled to just isn't there. Design your queries and UI to treat "empty" as "not visible to you", not "does not exist".
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Field-level visibility
|
|
62
|
+
|
|
63
|
+
Columns annotated `/// @Role(...)` follow the same rules as everywhere else: a role that can't read a field won't receive it in the GraphQL response, at the top level and nested. See [permissions.md](./permissions.md#field-level-control).
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Not available yet
|
|
68
|
+
|
|
69
|
+
- **Mutations** (create/update/delete) — use REST.
|
|
70
|
+
- **`aggregate`** — not ready; see [limitations.md](./limitations.md).
|
|
71
|
+
|
|
72
|
+
Always let the generated resolvers (which go through `PrismaService`) serve the data — a custom resolver that reaches the raw Prisma client would not get ABAC.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Known Limitations & Not-Ready Features
|
|
2
|
+
|
|
3
|
+
Read this before using any method or surface not explicitly covered in [permissions.md](./permissions.md) / [data-access.md](./data-access.md). Items here are either **not ready for use**, or **behave differently from vanilla Prisma** in ways you must account for.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Not ready — do not use yet
|
|
8
|
+
|
|
9
|
+
### `aggregate`
|
|
10
|
+
|
|
11
|
+
`prisma.model.<model>.aggregate(...)` is **not a supported ABAC action**. There is no `aggregate` permission and no fallback to a read rule, and the field-omission logic isn't designed for aggregate argument shapes. Its ABAC behaviour is undefined/untested — **avoid it for user-scoped data**.
|
|
12
|
+
|
|
13
|
+
- Need a count? Use `count(...)` — it **is** supported and falls back to your `findMany` rule.
|
|
14
|
+
- Need a genuine aggregate behind trusted access? Compute it inside a `withExposedModels(...)` block (or a bypass) with your own authorization, and never expose it to untrusted roles until aggregate ABAC lands.
|
|
15
|
+
|
|
16
|
+
### GraphQL — writes and `aggregate`
|
|
17
|
+
|
|
18
|
+
GraphQL currently supports **read queries only** (`findAll<Model>s` / `findFirst<Model>` / `findOne<Model>`), with ABAC applied. See **[graphql.md](./graphql.md)** for how to use it, including nested queries.
|
|
19
|
+
|
|
20
|
+
- **Mutations (create / update / delete) are not available yet** — use the REST CRUD endpoints for writes.
|
|
21
|
+
- **`aggregate` is not ready** — same caveat as REST above.
|
|
22
|
+
|
|
23
|
+
Let the generated resolvers serve the data (they go through `PrismaService`); a custom resolver that reaches the raw Prisma client would not get ABAC.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Behaves differently from vanilla Prisma
|
|
28
|
+
|
|
29
|
+
### Single-record methods are redirected
|
|
30
|
+
|
|
31
|
+
`findUnique` → `findFirst`, `findUniqueOrThrow` → `findFirstOrThrow`, `update` → `updateMany`, `delete` → `deleteMany` (types + runtime). `update()`/`delete()` return `{ count }`, not the record, and act on all matching rows. Full contract in [data-access.md](./data-access.md#method-contract-important).
|
|
32
|
+
|
|
33
|
+
### Including an unreadable to-one relation drops the parent row
|
|
34
|
+
|
|
35
|
+
`include`/`select` of a `belongsTo` relation whose target the caller can't read removes the **whole parent row** (returns `null` / omits it), rather than nulling just the relation. To-many relations filter in place and keep the parent. Fails closed; intentional. See [data-access.md](./data-access.md#relations-in-conditions-and-includes).
|
|
36
|
+
|
|
37
|
+
### `_count` pseudo-field filtering is not implemented
|
|
38
|
+
|
|
39
|
+
You can `select: { _count: ... }`, but ABAC conditions are **not** applied inside a `_count` sub-selection. Don't rely on `_count` to reflect only rows the caller may see.
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Sharp edges to know about
|
|
44
|
+
|
|
45
|
+
- **`restrictedFields` strips silently.** Listed fields are removed from the request body with no `400`. It only affects fields that are present in the generated DTO (not `/// @NoWrite` ones). Decide per model whether silent stripping is acceptable.
|
|
46
|
+
- **Per-call bypass options are only on three accessors.** `{ BYPASS_FILTERING, BYPASS_OMISSION }` are available on `prismaService.user` / `.session` / `.userRefreshToken`. For an unfiltered read of another model, use `withExposedModels([...])` or `@ExposeModels(...)`, not a per-call option on `prisma.model.<x>`.
|
|
47
|
+
- **Role strings are not type-checked.** `PermissionsConfig` keys are plain strings; a typo (`ADIMN`) silently yields "no permissions" → default-deny for that role. Double-check role spelling against your `Role` enum.
|
|
48
|
+
- **No audit logging of denials.** A refused request returns `403` but emits no structured audit event; per-request diagnostics are opt-in via `prismaService.debugQueries(true)`.
|
|
49
|
+
- **Exposed models are a full bypass.** `@ExposeModels` / `@Permission(a, [models])` / `withExposedModels` disable row *and* field filtering for the listed models within that scope. Keep the scope as narrow as possible.
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Reporting
|
|
54
|
+
|
|
55
|
+
If the framework does something these docs don't describe, that's a documentation or framework bug — surface it rather than coding around it. These docs are the source of truth.
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# Permissions & Authorization
|
|
2
|
+
|
|
3
|
+
How AppX Core decides **who may do what**. Two layers cooperate:
|
|
4
|
+
|
|
5
|
+
1. **`RbacGuard`** (route level) — checks that the caller's role has *a* permission for the requested action on the model. Coarse allow/deny; does **not** evaluate row conditions.
|
|
6
|
+
2. **The Prisma proxy** (data level) — injects the row-level `conditions` and applies field-level omission on every query. This is the real enforcement layer. See [data-access.md](./data-access.md).
|
|
7
|
+
|
|
8
|
+
Both read the same `permissions.config.ts`.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## The permissions config
|
|
13
|
+
|
|
14
|
+
Lives in `src/config/permissions.config.ts` and is passed to `AppxCoreModule.forRoot(PermissionsConfig)`.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { PermissionPlaceholder, PermissionsConfigType } from '@appxdigital/appx-core';
|
|
18
|
+
|
|
19
|
+
const $UID = PermissionPlaceholder.USER_ID;
|
|
20
|
+
|
|
21
|
+
export const PermissionsConfig: PermissionsConfigType = {
|
|
22
|
+
Project: { // model name (matches the Prisma model)
|
|
23
|
+
ADMIN: { // role (matches your Role enum / role strings)
|
|
24
|
+
findMany: 'ALL',
|
|
25
|
+
create: 'ALL',
|
|
26
|
+
updateMany: 'ALL',
|
|
27
|
+
deleteMany: 'ALL',
|
|
28
|
+
},
|
|
29
|
+
USER: {
|
|
30
|
+
findMany: { conditions: { OR: [{ ownerId: $UID }, { members: { some: { userId: $UID } } }] } },
|
|
31
|
+
create: { conditions: { ownerId: $UID } },
|
|
32
|
+
updateMany: { conditions: { ownerId: $UID } },
|
|
33
|
+
},
|
|
34
|
+
GUEST: { // unauthenticated requests — see "GUEST" below
|
|
35
|
+
findMany: 'ALL',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Shape: `PermissionsConfig[Model][Role][action]`, where each `action` value is either:
|
|
42
|
+
|
|
43
|
+
- **`'ALL'`** — unrestricted for that action, or
|
|
44
|
+
- **`{ conditions: <where> }`** — an ABAC rule. `conditions` is Prisma `where`-syntax (`AND`/`OR`/`NOT`, relation filters `some`/`every`/`none`, operators `in`/`not`/`lt`/…). `$USER_ID` is substituted with the caller's id at query time.
|
|
45
|
+
- Optionally with **`restrictedFields`** and/or **`setUserIdField`** (see below).
|
|
46
|
+
|
|
47
|
+
**Model and role keys are matched case-insensitively** against the model name and the caller's role string.
|
|
48
|
+
|
|
49
|
+
### Actions
|
|
50
|
+
|
|
51
|
+
| Action | Triggered by |
|
|
52
|
+
|---|---|
|
|
53
|
+
| `findMany`, `findFirst` | reads |
|
|
54
|
+
| `count` | counts |
|
|
55
|
+
| `create`, `createMany` | inserts |
|
|
56
|
+
| `updateMany` | updates (also what `update()` resolves to — see [data-access.md](./data-access.md)) |
|
|
57
|
+
| `deleteMany` | deletes (also what `delete()` resolves to) |
|
|
58
|
+
|
|
59
|
+
> `aggregate` is **not** a supported permission action yet — see [limitations.md](./limitations.md).
|
|
60
|
+
|
|
61
|
+
### Action fallback chains
|
|
62
|
+
|
|
63
|
+
To keep configs compact, a missing action falls back:
|
|
64
|
+
|
|
65
|
+
- **`count` → `findMany` → `findFirst`** (reads share a rule).
|
|
66
|
+
- **`findFirst` / `findUnique`-shape reads → `findMany`**.
|
|
67
|
+
- **`createMany` → `create`**, **`updateMany` → `update`**, **`deleteMany` → `delete`** (a batch action inherits the singular rule).
|
|
68
|
+
|
|
69
|
+
The `*Many` → singular fallback is **one-directional**: the singular is canonical. Declaring only `updateMany` does *not* enable `update`; declare `update` (or `create`/`delete`) and the `*Many` form is covered automatically. This holds in **both** the `RbacGuard` and the proxy.
|
|
70
|
+
|
|
71
|
+
So a minimal USER config often needs just `findMany`, `create`, `update`, `delete`.
|
|
72
|
+
|
|
73
|
+
### Field-level control
|
|
74
|
+
|
|
75
|
+
- **`/// @Role(ADMIN)` on a schema column** — only listed roles can *read* that field. For everyone else the proxy omits it from results (it comes back `undefined`). `/// @Role(none)` hides it from all roles. Applies at every depth, including nested relation selections.
|
|
76
|
+
- **`/// @NoWrite` on a schema column** — excludes the field from generated create/update DTOs (all roles).
|
|
77
|
+
- **`restrictedFields: ['x', 'y']` on a permission action** — strips those keys from the incoming request body for that role+action (e.g. stop a role from setting `role`/`id`). Silent strip, not a `400`.
|
|
78
|
+
- **`setUserIdField: 'ownerId'`** — the framework sets that field to the caller's id server-side on create (so a client can't forge ownership). Applied after `restrictedFields`, so it can't be clobbered.
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Decorators
|
|
83
|
+
|
|
84
|
+
### `@Permission(action, exposeModels?)`
|
|
85
|
+
|
|
86
|
+
Declares the action a route requires. Read by `RbacGuard` (enforces the role has the action) and by the interceptor (for `exposeModels`).
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
@Permission('findMany')
|
|
90
|
+
@Get()
|
|
91
|
+
list() { /* ... */ }
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
- `RbacGuard` looks up `PermissionsConfig[model][role][action]`; if absent (after fallbacks) it throws `403`. `model` comes from the controller's `entityName`.
|
|
95
|
+
- **`RbacGuard` is auto-applied on `CoreController`** (all generated CRUD controllers). On a **custom** controller, add `@UseGuards(RbacGuard)` yourself if you want the action enforced.
|
|
96
|
+
- The optional second argument exposes models for the request — see below.
|
|
97
|
+
|
|
98
|
+
### `@ExposeModels(...models)`
|
|
99
|
+
|
|
100
|
+
Exposes one or more models for the duration of the request **without** requiring a permission action.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
@ExposeModels('user') // no @Permission needed
|
|
104
|
+
@Get('/auth/email-available')
|
|
105
|
+
isEmailAvailable(@Query('email') email: string) { /* reads User unfiltered */ }
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
- Sets **no action**, so `RbacGuard` demands no permission (a public / `GUEST` route works with no `GUEST` rule for the model).
|
|
109
|
+
- The listed models **skip row and field filtering for that request only** (scoped via async context — it does not make the model globally readable).
|
|
110
|
+
- Case-insensitive: `@ExposeModels('User')` and `@ExposeModels('user')` are equivalent.
|
|
111
|
+
- Combine with `@Permission(...)` if you also want an action check — exposed models from both decorators are merged.
|
|
112
|
+
- This is a deliberate **bypass**. Use it narrowly (e.g. registration pre-checks); prefer real ABAC rules where feasible.
|
|
113
|
+
|
|
114
|
+
> `@Permission(action, ['user'])` still exposes models exactly as before — `@ExposeModels` is the permission-free alternative, not a replacement.
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## The GUEST (unauthenticated) flow
|
|
119
|
+
|
|
120
|
+
A request with **no session and no valid JWT** is treated as the role **`GUEST`**:
|
|
121
|
+
|
|
122
|
+
- The global `UserPopulationGuard` never blocks — it populates `req.user` from a `Bearer` JWT if present, otherwise leaves the request unauthenticated and passes through.
|
|
123
|
+
- `RbacGuard` defaults the role to `'GUEST'` and looks up `PermissionsConfig[model]['GUEST'][action]`. If you declared a `GUEST` rule, the route is allowed; otherwise it is default-denied (`403`).
|
|
124
|
+
- The proxy likewise resolves the role to `GUEST` and applies `GUEST` conditions (or default-denies).
|
|
125
|
+
|
|
126
|
+
So to allow public access to a model, add a `GUEST` block to its config. To allow public access to a route that reads a model you *don't* want generally public, use `@ExposeModels(...)` instead.
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Default-deny
|
|
131
|
+
|
|
132
|
+
If a model/role/action has no rule (after fallbacks) and the model is not exposed:
|
|
133
|
+
|
|
134
|
+
- `RbacGuard` throws `ForbiddenException` (`403`).
|
|
135
|
+
- The proxy throws `HttpException(403)`.
|
|
136
|
+
|
|
137
|
+
There is no "allow by omission". A typo in a role or model key means that path is denied.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appxdigital/appx-core",
|
|
3
|
-
"version": "0.1.121-beta.
|
|
3
|
+
"version": "0.1.121-beta.9",
|
|
4
4
|
"description": "Appx Core is a library that provides a set of tools to help you build your application faster.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -100,6 +100,7 @@
|
|
|
100
100
|
},
|
|
101
101
|
"files": [
|
|
102
102
|
"dist",
|
|
103
|
+
"docs",
|
|
103
104
|
"CHANGELOG.md",
|
|
104
105
|
"PRERELEASES.md",
|
|
105
106
|
"RELEASING.md"
|