@objectstack/plugin-security 4.0.4 → 4.1.0
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 +97 -27
- package/dist/index.d.mts +5407 -566
- package/dist/index.d.ts +5407 -566
- package/dist/index.js +923 -183
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +921 -181
- package/dist/index.mjs.map +1 -1
- package/package.json +33 -6
- package/.turbo/turbo-build.log +0 -22
- package/CHANGELOG.md +0 -264
- package/src/field-masker.ts +0 -75
- package/src/index.ts +0 -16
- package/src/objects/index.ts +0 -10
- package/src/objects/sys-permission-set.object.ts +0 -94
- package/src/objects/sys-role.object.ts +0 -93
- package/src/permission-evaluator.ts +0 -112
- package/src/rls-compiler.ts +0 -143
- package/src/security-plugin.test.ts +0 -302
- package/src/security-plugin.ts +0 -181
- package/tsconfig.json +0 -18
package/README.md
CHANGED
|
@@ -1,47 +1,117 @@
|
|
|
1
1
|
# @objectstack/plugin-security
|
|
2
2
|
|
|
3
|
-
Security
|
|
3
|
+
> Security plugin for ObjectStack — RBAC, Row-Level Security (RLS), and Field-Level Masking enforced transparently through the ObjectQL middleware chain.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/@objectstack/plugin-security)
|
|
6
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
6
7
|
|
|
7
|
-
|
|
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
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
],
|
|
23
|
-
});
|
|
31
|
+
const kernel = new ObjectKernel();
|
|
32
|
+
kernel.use(new SecurityPlugin());
|
|
33
|
+
await kernel.bootstrap();
|
|
24
34
|
```
|
|
25
35
|
|
|
26
|
-
###
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
111
|
+
## Links
|
|
40
112
|
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|