|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * An object row's CRUD bit that `/me/permissions` leaves OUT is not a grant. |
| 11 | + * |
| 12 | + * The spec's `objectPermissionGrants` (`@objectstack/spec/security`) is the one |
| 13 | + * fold the server's `PermissionEvaluator.checkObjectPermission` answers from: a |
| 14 | + * bit grants only when it is `=== true` (plus the super-user bypasses), and "an |
| 15 | + * ABSENT entry is `false`". The server's plain-wildcard coverage copies only |
| 16 | + * the `true` bits onto each object it covers, so a read-only set |
| 17 | + * `'*': { allowRead: true }` is served as `{ allowRead: true }` with no |
| 18 | + * `allowCreate`, and an auditor (`allowRead` + `viewAllRecords`) is served with |
| 19 | + * no write bit at all. The provider used to read such a row with |
| 20 | + * `bit !== false`, so it offered New / Edit / Delete that the server then |
| 21 | + * refused with 403. |
| 22 | + * |
| 23 | + * Unchanged, and pinned here as controls: an explicit `true` still grants, an |
| 24 | + * anonymous session keeps its permissive default, and a tree with no provider |
| 25 | + * keeps the fail-open `can`. |
| 26 | + */ |
| 27 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 28 | +import { render, cleanup } from '@testing-library/react'; |
| 29 | +import React from 'react'; |
| 30 | +import { MePermissionsProvider, type MePermissionsResponse } from '../MePermissionsProvider'; |
| 31 | +import { usePermissions } from '../usePermissions'; |
| 32 | + |
| 33 | +afterEach(cleanup); |
| 34 | + |
| 35 | +const envelope = (objects: MePermissionsResponse['objects']): MePermissionsResponse => ({ |
| 36 | + authenticated: true, |
| 37 | + userId: 'u-auditor', |
| 38 | + tenantId: null, |
| 39 | + roles: ['auditor'], |
| 40 | + permissionSets: ['auditor'], |
| 41 | + objects, |
| 42 | + fields: {}, |
| 43 | +}); |
| 44 | + |
| 45 | +/** A read-only permission set, as the server serves it: the wildcard's `true` bits only. */ |
| 46 | +const READ_ONLY_WILDCARD = envelope({ '*': { allowRead: true } }); |
| 47 | + |
| 48 | +/** An auditor set, per object: read plus View All, no write bit served. */ |
| 49 | +const AUDITOR = envelope({ |
| 50 | + account: { allowRead: true, viewAllRecords: true }, |
| 51 | + invoice: { allowRead: true, viewAllRecords: true }, |
| 52 | +}); |
| 53 | + |
| 54 | +function principalUnder(perms: MePermissionsResponse | null): ReturnType<typeof usePermissions> { |
| 55 | + let captured: ReturnType<typeof usePermissions> | null = null; |
| 56 | + function Probe() { |
| 57 | + captured = usePermissions(); |
| 58 | + return null; |
| 59 | + } |
| 60 | + render(perms ? ( |
| 61 | + <MePermissionsProvider initialPermissions={perms}> |
| 62 | + <Probe /> |
| 63 | + </MePermissionsProvider> |
| 64 | + ) : ( |
| 65 | + <Probe /> |
| 66 | + )); |
| 67 | + if (!captured) throw new Error('the probe did not render'); |
| 68 | + return captured; |
| 69 | +} |
| 70 | + |
| 71 | +const WRITE_ACTIONS = ['create', 'update', 'delete', 'import'] as const; |
| 72 | +const READ_ACTIONS = ['read', 'export'] as const; |
| 73 | + |
| 74 | +describe.each([ |
| 75 | + { shape: "read-only '*'", perms: READ_ONLY_WILDCARD }, |
| 76 | + { shape: 'auditor (allowRead + viewAllRecords)', perms: AUDITOR }, |
| 77 | +])('an absent CRUD bit denies — $shape', ({ perms }) => { |
| 78 | + it('check: every write action is denied with the permission-set reason', () => { |
| 79 | + const p = principalUnder(perms); |
| 80 | + for (const action of WRITE_ACTIONS) { |
| 81 | + expect(p.check('account', action), action).toEqual({ allowed: false, reason: 'denied-by-permission-set' }); |
| 82 | + expect(p.can('account', action), action).toBe(false); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it('check: the read actions stay allowed (CONTROL — the row is honoured, not ignored)', () => { |
| 87 | + const p = principalUnder(perms); |
| 88 | + for (const action of READ_ACTIONS) { |
| 89 | + expect(p.can('account', action), action).toBe(true); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it("checkField's object-level fallback: create and write denied, read allowed", () => { |
| 94 | + const p = principalUnder(perms); |
| 95 | + expect(p.checkField('account', 'name', 'create')).toBe(false); |
| 96 | + expect(p.checkField('account', 'name', 'write')).toBe(false); |
| 97 | + expect(p.checkField('account', 'name', 'read')).toBe(true); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe("a served row is read through the spec's fold", () => { |
| 102 | + it('a row granting only Create grants Create and nothing else', () => { |
| 103 | + const p = principalUnder(envelope({ account: { allowRead: true, allowCreate: true } })); |
| 104 | + expect(p.can('account', 'create')).toBe(true); |
| 105 | + expect(p.can('account', 'update')).toBe(false); |
| 106 | + expect(p.can('account', 'delete')).toBe(false); |
| 107 | + }); |
| 108 | + |
| 109 | + it('Modify All widens edit and delete, never create', () => { |
| 110 | + const p = principalUnder(envelope({ '*': { modifyAllRecords: true } })); |
| 111 | + expect(p.can('account', 'read')).toBe(true); |
| 112 | + expect(p.can('account', 'update')).toBe(true); |
| 113 | + expect(p.can('account', 'delete')).toBe(true); |
| 114 | + expect(p.can('account', 'create')).toBe(false); |
| 115 | + expect(p.checkField('account', 'name', 'write')).toBe(true); |
| 116 | + expect(p.checkField('account', 'name', 'create')).toBe(false); |
| 117 | + }); |
| 118 | + |
| 119 | + it('an object key is matched case-insensitively, as checkField already matches it', () => { |
| 120 | + const p = principalUnder(envelope({ '*': { allowRead: true }, account: { allowRead: true, allowCreate: true } })); |
| 121 | + expect(p.can('Account', 'create')).toBe(true); |
| 122 | + expect(p.can('Account', 'update')).toBe(false); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('what an absent bit does not change (controls)', () => { |
| 127 | + it('an explicit `true` bit still grants', () => { |
| 128 | + const p = principalUnder(envelope({ '*': { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true } })); |
| 129 | + for (const action of WRITE_ACTIONS) expect(p.can('account', action), action).toBe(true); |
| 130 | + expect(p.checkField('account', 'name', 'create')).toBe(true); |
| 131 | + expect(p.checkField('account', 'name', 'write')).toBe(true); |
| 132 | + }); |
| 133 | + |
| 134 | + it('an anonymous session keeps the permissive default', () => { |
| 135 | + const p = principalUnder({ authenticated: false } as MePermissionsResponse); |
| 136 | + for (const action of WRITE_ACTIONS) expect(p.can('showcase_inquiry', action), action).toBe(true); |
| 137 | + expect(p.checkField('showcase_inquiry', 'message', 'create')).toBe(true); |
| 138 | + }); |
| 139 | + |
| 140 | + it('with no provider mounted, can() stays fail-open', () => { |
| 141 | + const p = principalUnder(null); |
| 142 | + expect(p.isLoaded).toBe(false); |
| 143 | + for (const action of WRITE_ACTIONS) expect(p.can('account', action), action).toBe(true); |
| 144 | + }); |
| 145 | +}); |
0 commit comments