|
| 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 filter applied - fail-safe is to deny) |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get applicable RLS policies for a given object and operation. |
| 102 | + */ |
| 103 | + getApplicablePolicies( |
| 104 | + objectName: string, |
| 105 | + operation: string, |
| 106 | + allPolicies: RowLevelSecurityPolicy[] |
| 107 | + ): RowLevelSecurityPolicy[] { |
| 108 | + // Map engine operation to RLS operation type |
| 109 | + const rlsOp = this.mapOperationToRLS(operation); |
| 110 | + |
| 111 | + return allPolicies.filter(policy => { |
| 112 | + // Check object match |
| 113 | + if (policy.object !== objectName && policy.object !== '*') return false; |
| 114 | + |
| 115 | + // Check operation match |
| 116 | + if (policy.operation === 'all') return true; |
| 117 | + if (policy.operation === rlsOp) return true; |
| 118 | + |
| 119 | + return false; |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + private mapOperationToRLS(operation: string): string { |
| 124 | + switch (operation) { |
| 125 | + case 'find': |
| 126 | + case 'findOne': |
| 127 | + case 'count': |
| 128 | + case 'aggregate': |
| 129 | + return 'select'; |
| 130 | + case 'insert': |
| 131 | + return 'insert'; |
| 132 | + case 'update': |
| 133 | + return 'update'; |
| 134 | + case 'delete': |
| 135 | + return 'delete'; |
| 136 | + default: |
| 137 | + return 'select'; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments