|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Field-level predicate guard — anti filter-oracle (objectui#2251). |
| 5 | + * |
| 6 | + * FieldMasker strips unreadable fields from query RESULTS, but a caller can |
| 7 | + * still probe a hidden field's VALUE through predicates: filtering |
| 8 | + * `salary >= 100000` (or sorting / grouping by `salary`) changes which rows |
| 9 | + * come back even though the column itself is masked — presence/absence is |
| 10 | + * the oracle. The objectui `/data` surface (ADR-0055) makes arbitrary |
| 11 | + * URL-driven filters a first-class citizen, so this hole must be closed at |
| 12 | + * the engine, independent of anything the client sends. |
| 13 | + * |
| 14 | + * Policy: REJECT (403), never silently rewrite. Dropping predicates changes |
| 15 | + * query semantics unpredictably (removing an `$or` branch narrows results; |
| 16 | + * removing an `$and` branch widens them — the widening direction re-opens |
| 17 | + * the oracle). Salesforce FLS behaves the same way: querying a hidden field |
| 18 | + * is an error, not a silent no-op. The error message carries the offending |
| 19 | + * field names so authors can fix the query. |
| 20 | + * |
| 21 | + * Ordering contract: this guard MUST run against the CALLER-supplied AST, |
| 22 | + * before RLS filter injection — RLS policies legitimately reference fields |
| 23 | + * the caller cannot read (e.g. `owner_id`), and must not be rejected. |
| 24 | + */ |
| 25 | + |
| 26 | +import { PermissionDeniedError } from './errors.js'; |
| 27 | + |
| 28 | +interface FieldPermissionLike { |
| 29 | + readable?: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +/** Logical keys of the FilterCondition grammar — never field names. */ |
| 33 | +const LOGICAL_KEYS = new Set(['$and', '$or', '$not']); |
| 34 | + |
| 35 | +/** |
| 36 | + * Collect every field name referenced by a FilterCondition. Dotted paths |
| 37 | + * and nested-relation conditions gate on their FIRST segment / top-level |
| 38 | + * relation field — local field permissions govern local traversal. |
| 39 | + */ |
| 40 | +export function collectConditionFields(condition: unknown, out: Set<string> = new Set()): Set<string> { |
| 41 | + if (!condition || typeof condition !== 'object' || Array.isArray(condition)) return out; |
| 42 | + for (const [key, value] of Object.entries(condition as Record<string, unknown>)) { |
| 43 | + if (LOGICAL_KEYS.has(key)) { |
| 44 | + if (Array.isArray(value)) for (const sub of value) collectConditionFields(sub, out); |
| 45 | + else collectConditionFields(value, out); |
| 46 | + continue; |
| 47 | + } |
| 48 | + out.add(key.split('.')[0]); |
| 49 | + } |
| 50 | + return out; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Collect every field referenced by the query's row-shaping clauses: |
| 55 | + * where / orderBy / groupBy / having / aggregations (field + FILTER) / |
| 56 | + * window functions (field + partitionBy + over.orderBy). `fields` |
| 57 | + * (projection) is intentionally NOT collected — selecting a hidden field is |
| 58 | + * harmless because FieldMasker strips it from the result; only predicates |
| 59 | + * leak. |
| 60 | + */ |
| 61 | +export function collectQueryFields(ast: Record<string, unknown>): Set<string> { |
| 62 | + const out = new Set<string>(); |
| 63 | + collectConditionFields(ast.where, out); |
| 64 | + collectConditionFields(ast.having, out); |
| 65 | + |
| 66 | + const orderBy = ast.orderBy; |
| 67 | + if (Array.isArray(orderBy)) { |
| 68 | + for (const s of orderBy) { |
| 69 | + const field = (s as { field?: unknown })?.field; |
| 70 | + if (typeof field === 'string') out.add(field.split('.')[0]); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + const groupBy = ast.groupBy; |
| 75 | + if (Array.isArray(groupBy)) { |
| 76 | + for (const g of groupBy) { |
| 77 | + if (typeof g === 'string') out.add(g.split('.')[0]); |
| 78 | + else if (g && typeof g === 'object' && typeof (g as { field?: unknown }).field === 'string') { |
| 79 | + out.add(((g as { field: string }).field).split('.')[0]); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const aggregations = ast.aggregations; |
| 85 | + if (Array.isArray(aggregations)) { |
| 86 | + for (const a of aggregations) { |
| 87 | + const field = (a as { field?: unknown })?.field; |
| 88 | + if (typeof field === 'string' && field !== '*') out.add(field.split('.')[0]); |
| 89 | + collectConditionFields((a as { filter?: unknown })?.filter, out); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + const windowFunctions = ast.windowFunctions; |
| 94 | + if (Array.isArray(windowFunctions)) { |
| 95 | + for (const w of windowFunctions) { |
| 96 | + const field = (w as { field?: unknown })?.field; |
| 97 | + if (typeof field === 'string') out.add(field.split('.')[0]); |
| 98 | + const over = (w as { over?: { partitionBy?: unknown; orderBy?: unknown } })?.over; |
| 99 | + if (Array.isArray(over?.partitionBy)) { |
| 100 | + for (const p of over.partitionBy) if (typeof p === 'string') out.add(p.split('.')[0]); |
| 101 | + } |
| 102 | + if (Array.isArray(over?.orderBy)) { |
| 103 | + for (const s of over.orderBy) { |
| 104 | + const f = (s as { field?: unknown })?.field; |
| 105 | + if (typeof f === 'string') out.add(f.split('.')[0]); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return out; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Throw PermissionDeniedError (→ HTTP 403) when the caller-supplied query |
| 116 | + * references a field its field-level permissions mark non-readable. |
| 117 | + */ |
| 118 | +export function assertReadableQueryFields( |
| 119 | + ast: Record<string, unknown>, |
| 120 | + fieldPermissions: Record<string, FieldPermissionLike>, |
| 121 | + object: string, |
| 122 | +): void { |
| 123 | + const hidden = new Set( |
| 124 | + Object.entries(fieldPermissions) |
| 125 | + .filter(([, perm]) => perm && perm.readable === false) |
| 126 | + .map(([field]) => field), |
| 127 | + ); |
| 128 | + if (hidden.size === 0) return; |
| 129 | + |
| 130 | + const offending = [...collectQueryFields(ast)].filter((f) => hidden.has(f)); |
| 131 | + if (offending.length === 0) return; |
| 132 | + |
| 133 | + throw new PermissionDeniedError( |
| 134 | + `[Security] Access denied: query on '${object}' references field(s) not readable by the caller: ` |
| 135 | + + `${offending.join(', ')}. Filtering, sorting, grouping, or aggregating by a hidden field ` |
| 136 | + + `would leak its values (filter oracle) — remove these predicates or grant field read access.`, |
| 137 | + { object, fields: offending, reason: 'field_predicate_denied' }, |
| 138 | + ); |
| 139 | +} |
0 commit comments