Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 1x 1x 27x 27x 48x 48x 50x 40x 40x 48x 8x 40x 40x 4x 19x 3x 4x 3x 4x 2x 1x | const { permissionChecker, scopedEntityId } = require('./PermissionChecker')
// A stand-in entity asking "would this still be granted if the entity were restricted?" —
// when it would, the entity's actual visibility doesn't matter and is never read.
const RESTRICTED_PROBE = { visibility: 'restricted' }
/**
* Validate that a user can create/modify a role with the given permissions
* Prevents privilege escalation by ensuring user has all permissions they're trying to grant
*
* This function takes the creator's permissions directly as an array, allowing it to be tested
* without database dependencies — except for permissions naming a specific entity that the
* creator only holds through a wildcard: those entities' visibility is read (in one batch) so a
* restricted entity can't be granted by someone who can't reach it (UC-ENTITY-007.2 AC-9).
*
* @param {string[]} newPermissions - Array of permissions in the new/modified role
* @param {string[]} creatorPermissions - Array of permissions the creator currently has
* @throws {Error} If creator doesn't have permission to grant any of the requested permissions
*/
async function validateRolePermissions(newPermissions, creatorPermissions) {
const needsVisibility = []
// Validate each permission in the new role
for (const newPerm of newPermissions) {
let allowed = false
for (const creatorPerm of creatorPermissions) {
if (permissionChecker.matchesPermission(creatorPerm, newPerm)) {
allowed = true
break
}
}
if (!allowed) {
throw new Error(`You do not have permission to grant: ${newPerm}`)
}
const entityId = scopedEntityId(newPerm)
if (
entityId &&
!permissionChecker.grantsOnEntity(creatorPermissions, newPerm, RESTRICTED_PROBE)
) {
needsVisibility.push({ newPerm, entityId })
}
}
if (needsVisibility.length === 0) return
const visibilities = await permissionChecker.getEntitiesVisibility(
needsVisibility.map(({ entityId }) => entityId)
)
for (const { newPerm, entityId } of needsVisibility) {
if (visibilities.get(entityId) === 'restricted') {
throw new Error(`You do not have permission to grant: ${newPerm}`)
}
}
}
module.exports = {
validateRolePermissions
}
|