import {assert} from '../../shared/src/asserts.ts'; import { mapCondition, toStaticParam, type Condition, type Parameter, } from '../../zero-protocol/src/ast.ts'; import type { AssetPermissions as CompiledAssetPermissions, PermissionsConfig as CompiledPermissionsConfig, TablePermissions, } from '../../zero-schema/src/compiled-permissions.ts'; import type {NameMapper} from '../../zero-schema/src/name-mapper.ts'; import {clientToServer} from '../../zero-schema/src/name-mapper.ts'; import type {Schema} from '../../zero-types/src/schema.ts'; import type {ExpressionBuilder} from '../../zql/src/query/expression.ts'; import {newExpressionBuilder} from '../../zql/src/query/static-query.ts'; export const ANYONE_CAN = [ (_: unknown, eb: ExpressionBuilder) => eb.and(), ]; /** * @deprecated Use {@link ANYONE_CAN} instead. */ export const ANYONE_CAN_DO_ANYTHING = { row: { select: ANYONE_CAN, insert: ANYONE_CAN, update: { preMutation: ANYONE_CAN, postMutation: ANYONE_CAN, }, delete: ANYONE_CAN, }, }; export const NOBODY_CAN = []; type Anchor = 'authData' | 'preMutationRow'; export type PermissionRule< TAuthDataShape, TSchema extends Schema, TTable extends keyof TSchema['tables'] & string, > = ( authData: TAuthDataShape, eb: ExpressionBuilder, ) => Condition; export type AssetPermissions< TAuthDataShape, TSchema extends Schema, TTable extends keyof TSchema['tables'] & string, > = { // Why an array of rules?: https://github.com/rocicorp/mono/pull/3184/files#r1869680716 select?: PermissionRule[] | undefined; /** * @deprecated Use Mutators instead. * @see {@link https://zero.rocicorp.dev/docs/writing-data} */ insert?: PermissionRule[] | undefined; /** * @deprecated Use Mutators instead. * @see {@link https://zero.rocicorp.dev/docs/writing-data} */ update?: | { preMutation?: PermissionRule[]; postMutation?: PermissionRule[]; } | undefined; /** * @deprecated Use Mutators instead. * @see {@link https://zero.rocicorp.dev/docs/writing-data} */ delete?: PermissionRule[] | undefined; }; export type PermissionsConfig = { [K in keyof TSchema['tables']]?: { row?: AssetPermissions | undefined; cell?: | { [C in keyof TSchema['tables'][K]['columns']]?: Omit< AssetPermissions, 'cell' >; } | undefined; }; }; /** * @deprecated Use {@link defineMutators} and {@link defineQueries} instead. */ export async function definePermissions( schema: TSchema, definer: () => | Promise> | PermissionsConfig, ): Promise { const expressionBuilders = {} as Record< string, ExpressionBuilder >; for (const name of Object.keys(schema.tables)) { expressionBuilders[name] = newExpressionBuilder(schema, name); } const config = await definer(); return compilePermissions(schema, config, expressionBuilders); } function compilePermissions( schema: TSchema, authz: PermissionsConfig | undefined, expressionBuilders: Record>, ): CompiledPermissionsConfig | undefined { if (!authz) { return undefined; } const nameMapper = clientToServer(schema.tables); const ret = {tables: {} as TablePermissions}; for (const [tableName, tableConfig] of Object.entries(authz)) { const serverName = schema.tables[tableName].serverName ?? tableName; ret.tables[serverName] = { row: compileRowConfig( nameMapper, tableName, tableConfig.row, expressionBuilders[tableName], ), cell: compileCellConfig( nameMapper, tableName, tableConfig.cell, expressionBuilders[tableName], ), }; } return ret; } function compileRowConfig< TAuthDataShape, TSchema extends Schema, TTable extends keyof TSchema['tables'] & string, >( clientToServer: NameMapper, tableName: TTable, rowRules: AssetPermissions | undefined, expressionBuilder: ExpressionBuilder, ): CompiledAssetPermissions | undefined { if (!rowRules) { return undefined; } return { select: compileRules( clientToServer, tableName, rowRules.select, expressionBuilder, ), insert: compileRules( clientToServer, tableName, rowRules.insert, expressionBuilder, ), update: { preMutation: compileRules( clientToServer, tableName, rowRules.update?.preMutation, expressionBuilder, ), postMutation: compileRules( clientToServer, tableName, rowRules.update?.postMutation, expressionBuilder, ), }, delete: compileRules( clientToServer, tableName, rowRules.delete, expressionBuilder, ), }; } /** * What is this "allow" and why are permissions policies an array of rules? * * Please read: https://github.com/rocicorp/mono/pull/3184/files#r1869680716 */ function compileRules< TAuthDataShape, TSchema extends Schema, TTable extends keyof TSchema['tables'] & string, >( clientToServer: NameMapper, tableName: TTable, rules: PermissionRule[] | undefined, expressionBuilder: ExpressionBuilder, ): ['allow', Condition][] | undefined { if (!rules) { return undefined; } return rules.map(rule => { const cond = rule(authDataRef as TAuthDataShape, expressionBuilder); return ['allow', mapCondition(cond, tableName, clientToServer)] as const; }); } function compileCellConfig< TAuthDataShape, TSchema extends Schema, TTable extends keyof TSchema['tables'] & string, >( clientToServer: NameMapper, tableName: TTable, cellRules: | Record> | undefined, expressionBuilder: ExpressionBuilder, ): Record | undefined { if (!cellRules) { return undefined; } const ret: Record = {}; for (const [columnName, rules] of Object.entries(cellRules)) { ret[columnName] = { select: compileRules( clientToServer, tableName, rules.select, expressionBuilder, ), insert: compileRules( clientToServer, tableName, rules.insert, expressionBuilder, ), update: { preMutation: compileRules( clientToServer, tableName, rules.update?.preMutation, expressionBuilder, ), postMutation: compileRules( clientToServer, tableName, rules.update?.postMutation, expressionBuilder, ), }, delete: compileRules( clientToServer, tableName, rules.delete, expressionBuilder, ), }; } return ret; } class CallTracker { readonly #anchor: Anchor; readonly #path: string[]; constructor(anchor: Anchor, path: string[]) { this.#anchor = anchor; this.#path = path; } get(target: {[toStaticParam]: () => Parameter}, prop: string | symbol) { if (prop === toStaticParam) { return target[toStaticParam]; } assert( typeof prop === 'string', () => `Expected prop to be a string, got ${typeof prop}`, ); const path = [...this.#path, prop]; return new Proxy( { [toStaticParam]: () => staticParam(this.#anchor, path), }, new CallTracker(this.#anchor, path), ); } } function baseTracker(anchor: Anchor) { return new Proxy( { [toStaticParam]: () => { throw new Error('no JWT field specified'); }, }, new CallTracker(anchor, []), ); } const authDataRef = baseTracker('authData'); export function staticParam( anchorClass: 'authData' | 'preMutationRow', field: string | string[], ): Parameter { return { type: 'static', anchor: anchorClass, // for backwards compatibility field: field.length === 1 ? field[0] : field, }; }