@objectstack/plugin-security 4.0.4 → 4.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/README.md CHANGED
@@ -1,47 +1,117 @@
1
1
  # @objectstack/plugin-security
2
2
 
3
- Security Plugin for ObjectStack — RBAC, Row-Level Security (RLS), and Field-Level Security runtime.
3
+ > Security plugin for ObjectStack — RBAC, Row-Level Security (RLS), and Field-Level Masking enforced transparently through the ObjectQL middleware chain.
4
4
 
5
- ## Features
5
+ [![npm](https://img.shields.io/npm/v/@objectstack/plugin-security.svg)](https://www.npmjs.com/package/@objectstack/plugin-security)
6
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache--2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
6
7
 
7
- - **RBAC Permission Evaluator**: Checks object-level CRUD permissions per user role with most-permissive merging across multiple roles.
8
- - **Row-Level Security (RLS)**: Compiles RLS policy expressions into ObjectQL query filters, automatically injected into all read operations.
9
- - **Field-Level Masking**: Strips non-readable fields from query results and identifies non-editable fields.
10
- - **ObjectQL Middleware Integration**: Hooks into the ObjectQL pipeline to enforce security transparently on every operation.
11
- - **System Bypass**: System-level operations skip security checks for internal workflows.
8
+ ## Overview
12
9
 
13
- ## Usage
10
+ `plugin-security` hooks into the ObjectQL pipeline and applies authorization on every read and write:
11
+
12
+ 1. **Resolve permission sets** — match user roles against `SysPermissionSet` metadata.
13
+ 2. **Check object CRUD** — `allowRead`, `allowCreate`, `allowEdit`, `allowDelete`.
14
+ 3. **Inject RLS** — compile row-level policy expressions into query filters.
15
+ 4. **Mask fields** — remove non-readable fields from results; flag non-editable fields on writes.
16
+
17
+ System-context operations bypass checks so internal jobs, migrations, and seed scripts work unobstructed.
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ pnpm add @objectstack/plugin-security
23
+ ```
24
+
25
+ ## Quick Start
14
26
 
15
27
  ```typescript
16
- import { SecurityPlugin } from '@objectstack/plugin-security';
17
28
  import { ObjectKernel } from '@objectstack/core';
29
+ import { SecurityPlugin } from '@objectstack/plugin-security';
18
30
 
19
- const kernel = new ObjectKernel({
20
- plugins: [
21
- new SecurityPlugin(),
22
- ],
23
- });
31
+ const kernel = new ObjectKernel();
32
+ kernel.use(new SecurityPlugin());
33
+ await kernel.bootstrap();
24
34
  ```
25
35
 
26
- ### Exported Components
36
+ ### Multi-tenant vs single-tenant
37
+
38
+ `SecurityPlugin` defaults to **multi-tenant** mode. In this mode it:
39
+
40
+ - Auto-injects `organization_id = ctx.tenantId` on insert when the target object declares an `organization_id` field.
41
+ - Honours the wildcard `tenant_isolation` RLS policy
42
+ (`organization_id = current_user.organization_id`) shipped with the
43
+ default `member_default` / `viewer_readonly` permission sets.
44
+
45
+ For single-tenant deployments, switch it off:
27
46
 
28
47
  ```typescript
29
- import {
30
- SecurityPlugin,
31
- PermissionEvaluator,
32
- RLSCompiler,
33
- FieldMasker,
34
- } from '@objectstack/plugin-security';
48
+ kernel.use(new SecurityPlugin({ multiTenant: false }));
35
49
  ```
36
50
 
37
- ## Architecture
51
+ This skips the per-insert metadata lookup that drives `organization_id`
52
+ auto-injection (the `owner_id` injection still runs) and strips wildcard
53
+ `current_user.organization_id` policies from the per-request policy
54
+ set so the field-existence safety net never has to drop them
55
+ individually. Field-Level Security, owner-based RLS, and per-object
56
+ CRUD checks operate identically regardless of this flag.
57
+
58
+ In CLI / dev-server mode the same switch is exposed via the
59
+ `OS_MULTI_TENANT` environment variable (default `true`); set
60
+ `OS_MULTI_TENANT=false` before `objectstack serve` / `pnpm dev` to disable.
61
+
62
+ ## Key Exports
63
+
64
+ | Export | Kind | Description |
65
+ |:---|:---|:---|
66
+ | `SecurityPlugin` | class | Kernel plugin that installs the four-step security chain. |
67
+ | `PermissionEvaluator` | class | Evaluates object-level CRUD permissions across roles (most-permissive merge). |
68
+ | `RLSCompiler` | class | Compiles RLS expressions into ObjectQL filter AST. |
69
+ | `FieldMasker` | class | Strips non-readable fields and identifies non-editable ones. |
70
+ | `SysRole`, `SysPermissionSet` | objects | Metadata objects registered by the plugin. |
71
+
72
+ ## System objects
73
+
74
+ The plugin contributes these system objects to the kernel:
75
+
76
+ | Object | Purpose |
77
+ |:---|:---|
78
+ | `sys_role` | User role definitions. |
79
+ | `sys_permission_set` | Bundles object and field permissions; can include RLS expressions. |
80
+
81
+ Assignment tables (role ↔ user, role ↔ permission_set) are provided by [`@objectstack/plugin-auth`](../plugin-auth) when used together.
82
+
83
+ ## RLS expression language
84
+
85
+ RLS policies are authored in the same expression language as object validations. Example:
86
+
87
+ ```json
88
+ {
89
+ "object": "project_task",
90
+ "read": "owner_id = $user.id OR team_id in $user.team_ids"
91
+ }
92
+ ```
93
+
94
+ Compilation output is a filter AST merged into every query's `where` clause, so drivers see it as a normal filter.
95
+
96
+ ## When to use
97
+
98
+ - ✅ Any multi-user deployment.
99
+ - ✅ Enforcing tenant isolation (combine with [`@objectstack/service-tenant`](../../services/service-tenant)).
100
+
101
+ ## When not to use
102
+
103
+ - ❌ Trusted single-user CLI scripts — disable per-request via the system context.
104
+
105
+ ## Related Packages
106
+
107
+ - [`@objectstack/plugin-auth`](../plugin-auth) — authentication and user resolution.
108
+ - [`@objectstack/plugin-audit`](../plugin-audit) — pairs with security for full compliance trails.
109
+ - [`@objectstack/objectql`](../../objectql) — query engine.
38
110
 
39
- The plugin registers three core services and executes a 4-step security chain on every data operation:
111
+ ## Links
40
112
 
41
- 1. **Resolve Permission Sets** — Match user roles to permission set definitions from metadata.
42
- 2. **Check Object Permissions** — Validate CRUD access (`allowRead`, `allowCreate`, `allowEdit`, `allowDelete`).
43
- 3. **Inject RLS Filters** — Compile row-level policy expressions and merge them into the query.
44
- 4. **Mask Fields** — Remove restricted fields from results based on field-level permissions.
113
+ - 📖 Docs: <https://objectstack.ai/docs>
114
+ - 📚 API Reference: <https://objectstack.ai/docs/references/security>
45
115
 
46
116
  ## License
47
117