@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 +97 -27
- package/dist/index.d.mts +4160 -563
- package/dist/index.d.ts +4160 -563
- package/dist/index.js +745 -183
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +743 -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/src/field-masker.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import type { FieldPermission } from '@objectstack/spec/security';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* FieldMasker
|
|
7
|
-
*
|
|
8
|
-
* Applies field-level security by stripping restricted fields from query results.
|
|
9
|
-
*/
|
|
10
|
-
export class FieldMasker {
|
|
11
|
-
/**
|
|
12
|
-
* Mask fields in query results based on field permissions.
|
|
13
|
-
* Removes fields that the user does not have read access to.
|
|
14
|
-
*/
|
|
15
|
-
maskResults(
|
|
16
|
-
results: any | any[],
|
|
17
|
-
fieldPermissions: Record<string, FieldPermission>,
|
|
18
|
-
_objectName: string
|
|
19
|
-
): any | any[] {
|
|
20
|
-
// If no field permissions defined, return results as-is
|
|
21
|
-
if (Object.keys(fieldPermissions).length === 0) return results;
|
|
22
|
-
|
|
23
|
-
// Get list of non-readable fields
|
|
24
|
-
const hiddenFields = Object.entries(fieldPermissions)
|
|
25
|
-
.filter(([, perm]) => !perm.readable)
|
|
26
|
-
.map(([field]) => field);
|
|
27
|
-
|
|
28
|
-
if (hiddenFields.length === 0) return results;
|
|
29
|
-
|
|
30
|
-
if (Array.isArray(results)) {
|
|
31
|
-
return results.map(record => this.maskRecord(record, hiddenFields));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return this.maskRecord(results, hiddenFields);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Get non-editable fields for use in write operations.
|
|
39
|
-
* Returns a list of field names that should be stripped from incoming data.
|
|
40
|
-
*/
|
|
41
|
-
getNonEditableFields(
|
|
42
|
-
fieldPermissions: Record<string, FieldPermission>
|
|
43
|
-
): string[] {
|
|
44
|
-
return Object.entries(fieldPermissions)
|
|
45
|
-
.filter(([, perm]) => !perm.editable)
|
|
46
|
-
.map(([field]) => field);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Strip non-editable fields from write data.
|
|
51
|
-
*/
|
|
52
|
-
stripNonEditableFields(
|
|
53
|
-
data: Record<string, any>,
|
|
54
|
-
fieldPermissions: Record<string, FieldPermission>
|
|
55
|
-
): Record<string, any> {
|
|
56
|
-
const nonEditable = this.getNonEditableFields(fieldPermissions);
|
|
57
|
-
if (nonEditable.length === 0) return data;
|
|
58
|
-
|
|
59
|
-
const result = { ...data };
|
|
60
|
-
for (const field of nonEditable) {
|
|
61
|
-
delete result[field];
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private maskRecord(record: any, hiddenFields: string[]): any {
|
|
67
|
-
if (!record || typeof record !== 'object') return record;
|
|
68
|
-
|
|
69
|
-
const result = { ...record };
|
|
70
|
-
for (const field of hiddenFields) {
|
|
71
|
-
delete result[field];
|
|
72
|
-
}
|
|
73
|
-
return result;
|
|
74
|
-
}
|
|
75
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @objectstack/plugin-security
|
|
5
|
-
*
|
|
6
|
-
* Security Plugin for ObjectStack
|
|
7
|
-
* Provides RBAC, Row-Level Security (RLS), and Field-Level Security runtime.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export { SecurityPlugin } from './security-plugin.js';
|
|
11
|
-
export { PermissionEvaluator } from './permission-evaluator.js';
|
|
12
|
-
export { RLSCompiler } from './rls-compiler.js';
|
|
13
|
-
export { FieldMasker } from './field-masker.js';
|
|
14
|
-
|
|
15
|
-
// System Object Definitions (sys namespace)
|
|
16
|
-
export { SysRole, SysPermissionSet } from './objects/index.js';
|
package/src/objects/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Security Plugin — System Object Definitions (sys namespace)
|
|
5
|
-
*
|
|
6
|
-
* Canonical ObjectSchema definitions for security-related system objects.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export { SysRole } from './sys-role.object.js';
|
|
10
|
-
export { SysPermissionSet } from './sys-permission-set.object.js';
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import { ObjectSchema, Field } from '@objectstack/spec/data';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* sys_permission_set — System Permission Set Object
|
|
7
|
-
*
|
|
8
|
-
* Named groupings of fine-grained permissions.
|
|
9
|
-
* Permission sets can be assigned to roles or directly to users
|
|
10
|
-
* for granular access control.
|
|
11
|
-
*
|
|
12
|
-
* @namespace sys
|
|
13
|
-
*/
|
|
14
|
-
export const SysPermissionSet = ObjectSchema.create({
|
|
15
|
-
namespace: 'sys',
|
|
16
|
-
name: 'permission_set',
|
|
17
|
-
label: 'Permission Set',
|
|
18
|
-
pluralLabel: 'Permission Sets',
|
|
19
|
-
icon: 'lock',
|
|
20
|
-
isSystem: true,
|
|
21
|
-
description: 'Named permission groupings for fine-grained access control',
|
|
22
|
-
titleFormat: '{name}',
|
|
23
|
-
compactLayout: ['name', 'label', 'active'],
|
|
24
|
-
|
|
25
|
-
fields: {
|
|
26
|
-
id: Field.text({
|
|
27
|
-
label: 'Permission Set ID',
|
|
28
|
-
required: true,
|
|
29
|
-
readonly: true,
|
|
30
|
-
}),
|
|
31
|
-
|
|
32
|
-
created_at: Field.datetime({
|
|
33
|
-
label: 'Created At',
|
|
34
|
-
defaultValue: 'NOW()',
|
|
35
|
-
readonly: true,
|
|
36
|
-
}),
|
|
37
|
-
|
|
38
|
-
updated_at: Field.datetime({
|
|
39
|
-
label: 'Updated At',
|
|
40
|
-
defaultValue: 'NOW()',
|
|
41
|
-
readonly: true,
|
|
42
|
-
}),
|
|
43
|
-
|
|
44
|
-
name: Field.text({
|
|
45
|
-
label: 'API Name',
|
|
46
|
-
required: true,
|
|
47
|
-
searchable: true,
|
|
48
|
-
maxLength: 100,
|
|
49
|
-
description: 'Unique machine name for the permission set',
|
|
50
|
-
}),
|
|
51
|
-
|
|
52
|
-
label: Field.text({
|
|
53
|
-
label: 'Display Name',
|
|
54
|
-
required: true,
|
|
55
|
-
maxLength: 255,
|
|
56
|
-
}),
|
|
57
|
-
|
|
58
|
-
description: Field.textarea({
|
|
59
|
-
label: 'Description',
|
|
60
|
-
required: false,
|
|
61
|
-
}),
|
|
62
|
-
|
|
63
|
-
object_permissions: Field.textarea({
|
|
64
|
-
label: 'Object Permissions',
|
|
65
|
-
required: false,
|
|
66
|
-
description: 'JSON-serialized object-level CRUD permissions',
|
|
67
|
-
}),
|
|
68
|
-
|
|
69
|
-
field_permissions: Field.textarea({
|
|
70
|
-
label: 'Field Permissions',
|
|
71
|
-
required: false,
|
|
72
|
-
description: 'JSON-serialized field-level read/write permissions',
|
|
73
|
-
}),
|
|
74
|
-
|
|
75
|
-
active: Field.boolean({
|
|
76
|
-
label: 'Active',
|
|
77
|
-
defaultValue: true,
|
|
78
|
-
}),
|
|
79
|
-
},
|
|
80
|
-
|
|
81
|
-
indexes: [
|
|
82
|
-
{ fields: ['name'], unique: true },
|
|
83
|
-
{ fields: ['active'] },
|
|
84
|
-
],
|
|
85
|
-
|
|
86
|
-
enable: {
|
|
87
|
-
trackHistory: true,
|
|
88
|
-
searchable: true,
|
|
89
|
-
apiEnabled: true,
|
|
90
|
-
apiMethods: ['get', 'list', 'create', 'update', 'delete'],
|
|
91
|
-
trash: true,
|
|
92
|
-
mru: true,
|
|
93
|
-
},
|
|
94
|
-
});
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import { ObjectSchema, Field } from '@objectstack/spec/data';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* sys_role — System Role Object
|
|
7
|
-
*
|
|
8
|
-
* RBAC role definition for the ObjectStack platform.
|
|
9
|
-
* Roles group permissions and are assigned to users or members.
|
|
10
|
-
*
|
|
11
|
-
* @namespace sys
|
|
12
|
-
*/
|
|
13
|
-
export const SysRole = ObjectSchema.create({
|
|
14
|
-
namespace: 'sys',
|
|
15
|
-
name: 'role',
|
|
16
|
-
label: 'Role',
|
|
17
|
-
pluralLabel: 'Roles',
|
|
18
|
-
icon: 'shield',
|
|
19
|
-
isSystem: true,
|
|
20
|
-
description: 'Role definitions for RBAC access control',
|
|
21
|
-
titleFormat: '{name}',
|
|
22
|
-
compactLayout: ['name', 'label', 'active'],
|
|
23
|
-
|
|
24
|
-
fields: {
|
|
25
|
-
id: Field.text({
|
|
26
|
-
label: 'Role ID',
|
|
27
|
-
required: true,
|
|
28
|
-
readonly: true,
|
|
29
|
-
}),
|
|
30
|
-
|
|
31
|
-
created_at: Field.datetime({
|
|
32
|
-
label: 'Created At',
|
|
33
|
-
defaultValue: 'NOW()',
|
|
34
|
-
readonly: true,
|
|
35
|
-
}),
|
|
36
|
-
|
|
37
|
-
updated_at: Field.datetime({
|
|
38
|
-
label: 'Updated At',
|
|
39
|
-
defaultValue: 'NOW()',
|
|
40
|
-
readonly: true,
|
|
41
|
-
}),
|
|
42
|
-
|
|
43
|
-
name: Field.text({
|
|
44
|
-
label: 'API Name',
|
|
45
|
-
required: true,
|
|
46
|
-
searchable: true,
|
|
47
|
-
maxLength: 100,
|
|
48
|
-
description: 'Unique machine name for the role (e.g. admin, editor, viewer)',
|
|
49
|
-
}),
|
|
50
|
-
|
|
51
|
-
label: Field.text({
|
|
52
|
-
label: 'Display Name',
|
|
53
|
-
required: true,
|
|
54
|
-
maxLength: 255,
|
|
55
|
-
}),
|
|
56
|
-
|
|
57
|
-
description: Field.textarea({
|
|
58
|
-
label: 'Description',
|
|
59
|
-
required: false,
|
|
60
|
-
}),
|
|
61
|
-
|
|
62
|
-
permissions: Field.textarea({
|
|
63
|
-
label: 'Permissions',
|
|
64
|
-
required: false,
|
|
65
|
-
description: 'JSON-serialized array of permission strings',
|
|
66
|
-
}),
|
|
67
|
-
|
|
68
|
-
active: Field.boolean({
|
|
69
|
-
label: 'Active',
|
|
70
|
-
defaultValue: true,
|
|
71
|
-
}),
|
|
72
|
-
|
|
73
|
-
is_default: Field.boolean({
|
|
74
|
-
label: 'Default Role',
|
|
75
|
-
defaultValue: false,
|
|
76
|
-
description: 'Automatically assigned to new users',
|
|
77
|
-
}),
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
indexes: [
|
|
81
|
-
{ fields: ['name'], unique: true },
|
|
82
|
-
{ fields: ['active'] },
|
|
83
|
-
],
|
|
84
|
-
|
|
85
|
-
enable: {
|
|
86
|
-
trackHistory: true,
|
|
87
|
-
searchable: true,
|
|
88
|
-
apiEnabled: true,
|
|
89
|
-
apiMethods: ['get', 'list', 'create', 'update', 'delete'],
|
|
90
|
-
trash: true,
|
|
91
|
-
mru: true,
|
|
92
|
-
},
|
|
93
|
-
});
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import type { PermissionSet, ObjectPermission, FieldPermission } from '@objectstack/spec/security';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Operation type mapping to permission checks
|
|
7
|
-
*/
|
|
8
|
-
const OPERATION_TO_PERMISSION: Record<string, keyof ObjectPermission> = {
|
|
9
|
-
find: 'allowRead',
|
|
10
|
-
findOne: 'allowRead',
|
|
11
|
-
count: 'allowRead',
|
|
12
|
-
aggregate: 'allowRead',
|
|
13
|
-
insert: 'allowCreate',
|
|
14
|
-
update: 'allowEdit',
|
|
15
|
-
delete: 'allowDelete',
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* PermissionEvaluator
|
|
20
|
-
*
|
|
21
|
-
* Runtime evaluator for PermissionSet definitions.
|
|
22
|
-
* Resolves aggregated permissions from roles to concrete allow/deny decisions.
|
|
23
|
-
*/
|
|
24
|
-
export class PermissionEvaluator {
|
|
25
|
-
/**
|
|
26
|
-
* Check if an operation is allowed on an object for the given permission sets.
|
|
27
|
-
* Uses "most permissive" merging: if ANY permission set allows, it's allowed.
|
|
28
|
-
*/
|
|
29
|
-
checkObjectPermission(
|
|
30
|
-
operation: string,
|
|
31
|
-
objectName: string,
|
|
32
|
-
permissionSets: PermissionSet[]
|
|
33
|
-
): boolean {
|
|
34
|
-
const permKey = OPERATION_TO_PERMISSION[operation];
|
|
35
|
-
if (!permKey) return true; // Unknown operations are allowed by default
|
|
36
|
-
|
|
37
|
-
for (const ps of permissionSets) {
|
|
38
|
-
const objPerm = ps.objects?.[objectName];
|
|
39
|
-
if (objPerm) {
|
|
40
|
-
// Check if modifyAllRecords is set (super-user bypass for write ops)
|
|
41
|
-
if (['allowEdit', 'allowDelete'].includes(permKey) && objPerm.modifyAllRecords) {
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
// Check if viewAllRecords is set (super-user bypass for read ops)
|
|
45
|
-
if (permKey === 'allowRead' && (objPerm.viewAllRecords || objPerm.modifyAllRecords)) {
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
// Check the specific permission
|
|
49
|
-
if (objPerm[permKey]) {
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Get the merged field permissions for an object.
|
|
60
|
-
* Returns a map of field names to their effective permissions.
|
|
61
|
-
* Uses "most permissive" merging.
|
|
62
|
-
*/
|
|
63
|
-
getFieldPermissions(
|
|
64
|
-
objectName: string,
|
|
65
|
-
permissionSets: PermissionSet[]
|
|
66
|
-
): Record<string, FieldPermission> {
|
|
67
|
-
const result: Record<string, FieldPermission> = {};
|
|
68
|
-
|
|
69
|
-
for (const ps of permissionSets) {
|
|
70
|
-
if (!ps.fields) continue;
|
|
71
|
-
|
|
72
|
-
for (const [key, perm] of Object.entries(ps.fields)) {
|
|
73
|
-
// Field keys are in format: "object_name.field_name"
|
|
74
|
-
if (!key.startsWith(`${objectName}.`)) continue;
|
|
75
|
-
const fieldName = key.substring(objectName.length + 1);
|
|
76
|
-
|
|
77
|
-
if (!result[fieldName]) {
|
|
78
|
-
result[fieldName] = { readable: false, editable: false };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Most permissive merge
|
|
82
|
-
if (perm.readable) result[fieldName].readable = true;
|
|
83
|
-
if (perm.editable) result[fieldName].editable = true;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return result;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Resolve permission sets for a list of role names from metadata.
|
|
92
|
-
*/
|
|
93
|
-
resolvePermissionSets(
|
|
94
|
-
roles: string[],
|
|
95
|
-
metadataService: any
|
|
96
|
-
): PermissionSet[] {
|
|
97
|
-
const result: PermissionSet[] = [];
|
|
98
|
-
|
|
99
|
-
// Get all permission sets from metadata
|
|
100
|
-
const allPermSets = metadataService.list?.('permissions') || [];
|
|
101
|
-
|
|
102
|
-
for (const ps of allPermSets) {
|
|
103
|
-
// A permission set is relevant if it's a profile assigned to any of the user's roles,
|
|
104
|
-
// or if the role name matches the permission set name
|
|
105
|
-
if (roles.includes(ps.name)) {
|
|
106
|
-
result.push(ps);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return result;
|
|
111
|
-
}
|
|
112
|
-
}
|
package/src/rls-compiler.ts
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import type { RowLevelSecurityPolicy } from '@objectstack/spec/security';
|
|
4
|
-
import type { ExecutionContext } from '@objectstack/spec/kernel';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* RLS User Context
|
|
8
|
-
* Variables available for RLS expression evaluation.
|
|
9
|
-
*/
|
|
10
|
-
interface RLSUserContext {
|
|
11
|
-
id?: string;
|
|
12
|
-
tenant_id?: string;
|
|
13
|
-
roles?: string[];
|
|
14
|
-
[key: string]: unknown;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* RLSCompiler
|
|
19
|
-
*
|
|
20
|
-
* Compiles Row-Level Security policy expressions into query filters.
|
|
21
|
-
* Converts `using` / `check` expressions into ObjectQL-compatible filter conditions.
|
|
22
|
-
*/
|
|
23
|
-
export class RLSCompiler {
|
|
24
|
-
/**
|
|
25
|
-
* Compile RLS policies into a query filter for the given user context.
|
|
26
|
-
* Multiple policies for the same object/operation are OR-combined (any match allows access).
|
|
27
|
-
*/
|
|
28
|
-
compileFilter(
|
|
29
|
-
policies: RowLevelSecurityPolicy[],
|
|
30
|
-
executionContext?: ExecutionContext
|
|
31
|
-
): Record<string, unknown> | null {
|
|
32
|
-
if (policies.length === 0) return null;
|
|
33
|
-
|
|
34
|
-
const userCtx: RLSUserContext = {
|
|
35
|
-
id: executionContext?.userId,
|
|
36
|
-
tenant_id: executionContext?.tenantId,
|
|
37
|
-
roles: executionContext?.roles,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const filters: Record<string, unknown>[] = [];
|
|
41
|
-
|
|
42
|
-
for (const policy of policies) {
|
|
43
|
-
if (!policy.using) continue;
|
|
44
|
-
const filter = this.compileExpression(policy.using, userCtx);
|
|
45
|
-
if (filter) {
|
|
46
|
-
filters.push(filter);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (filters.length === 0) return null;
|
|
51
|
-
if (filters.length === 1) return filters[0];
|
|
52
|
-
|
|
53
|
-
// Multiple policies: OR-combine (any policy allows access)
|
|
54
|
-
return { $or: filters };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Compile a single RLS expression into a query filter.
|
|
59
|
-
*
|
|
60
|
-
* Supports simple expressions like:
|
|
61
|
-
* - "field_name = current_user.property"
|
|
62
|
-
* - "field_name IN (current_user.array_property)"
|
|
63
|
-
* - "field_name = 'literal_value'"
|
|
64
|
-
*/
|
|
65
|
-
compileExpression(
|
|
66
|
-
expression: string,
|
|
67
|
-
userCtx: RLSUserContext
|
|
68
|
-
): Record<string, unknown> | null {
|
|
69
|
-
if (!expression) return null;
|
|
70
|
-
|
|
71
|
-
// Handle simple equality: "field = current_user.property"
|
|
72
|
-
const eqMatch = expression.match(/^\s*(\w+)\s*=\s*current_user\.(\w+)\s*$/);
|
|
73
|
-
if (eqMatch) {
|
|
74
|
-
const [, field, prop] = eqMatch;
|
|
75
|
-
const value = userCtx[prop];
|
|
76
|
-
if (value === undefined) return null;
|
|
77
|
-
return { [field]: value };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Handle literal equality: "field = 'value'"
|
|
81
|
-
const litMatch = expression.match(/^\s*(\w+)\s*=\s*'([^']*)'\s*$/);
|
|
82
|
-
if (litMatch) {
|
|
83
|
-
const [, field, value] = litMatch;
|
|
84
|
-
return { [field]: value };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Handle IN: "field IN (current_user.array_property)"
|
|
88
|
-
const inMatch = expression.match(/^\s*(\w+)\s+IN\s+\(\s*current_user\.(\w+)\s*\)\s*$/i);
|
|
89
|
-
if (inMatch) {
|
|
90
|
-
const [, field, prop] = inMatch;
|
|
91
|
-
const value = userCtx[prop];
|
|
92
|
-
if (!Array.isArray(value)) return null;
|
|
93
|
-
return { [field]: { $in: value } };
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Unsupported expression: return null (no additional RLS filter applied).
|
|
97
|
-
// Note: callers should treat absence of RLS policies as "allow all" only when
|
|
98
|
-
// no policies are defined. If policies exist but cannot be compiled, the caller
|
|
99
|
-
// may want to deny access as a safety measure.
|
|
100
|
-
return null;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Get applicable RLS policies for a given object and operation.
|
|
105
|
-
*/
|
|
106
|
-
getApplicablePolicies(
|
|
107
|
-
objectName: string,
|
|
108
|
-
operation: string,
|
|
109
|
-
allPolicies: RowLevelSecurityPolicy[]
|
|
110
|
-
): RowLevelSecurityPolicy[] {
|
|
111
|
-
// Map engine operation to RLS operation type
|
|
112
|
-
const rlsOp = this.mapOperationToRLS(operation);
|
|
113
|
-
|
|
114
|
-
return allPolicies.filter(policy => {
|
|
115
|
-
// Check object match
|
|
116
|
-
if (policy.object !== objectName && policy.object !== '*') return false;
|
|
117
|
-
|
|
118
|
-
// Check operation match
|
|
119
|
-
if (policy.operation === 'all') return true;
|
|
120
|
-
if (policy.operation === rlsOp) return true;
|
|
121
|
-
|
|
122
|
-
return false;
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
private mapOperationToRLS(operation: string): string {
|
|
127
|
-
switch (operation) {
|
|
128
|
-
case 'find':
|
|
129
|
-
case 'findOne':
|
|
130
|
-
case 'count':
|
|
131
|
-
case 'aggregate':
|
|
132
|
-
return 'select';
|
|
133
|
-
case 'insert':
|
|
134
|
-
return 'insert';
|
|
135
|
-
case 'update':
|
|
136
|
-
return 'update';
|
|
137
|
-
case 'delete':
|
|
138
|
-
return 'delete';
|
|
139
|
-
default:
|
|
140
|
-
return 'select';
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|