Feature: #111237 - Security voters for tables, fields and languages 

See forge#111237

Description 

Security voters, see Feature: #111236 - Security voters for authorization decisions, now decide on the permissions backend groups grant: tables, exclude fields, values of authMode fields, languages, page types, dashboard widgets, MFA providers and custom options. The checks of the backend user object keep working. The following preview shows the same checks asked from the authorization checker; the subjects and scopes it uses are experimental, see the end of this section:

use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use TYPO3\CMS\Core\Schema\TcaSchemaFactory;
use TYPO3\CMS\Core\Security\Authorization\Policy\ResourceIdentifier;
use TYPO3\CMS\Core\Security\Authorization\Policy\Scope;
use TYPO3\CMS\Core\Security\Authorization\SecurityRole;
use TYPO3\CMS\Core\Security\Authorization\Subject\SchemaField;

// Before
$backendUser = $GLOBALS['BE_USER'];
$isAdmin = $backendUser->isAdmin();
$mayEditContent = $backendUser->check('tables_modify', 'tt_content');
$mayEditHeader = $backendUser->check('non_exclude_fields', 'tt_content:header');
$mayUseTextType = $backendUser->checkAuthMode('tt_content', 'CType', 'text');
$mayEditGerman = $backendUser->checkLanguageAccess(1);
$hasOption = $backendUser->check('custom_options', 'tx_news:publish');

// After
$isAdmin = $this->authorizationChecker->isGranted(SecurityRole::ADMIN->value);
$schema = $this->tcaSchemaFactory->get('tt_content');
$mayEditContent = $this->authorizationChecker->isGranted(Scope::WRITE, $schema);
$mayEditHeader = $this->authorizationChecker->isGranted(
    Scope::WRITE,
    SchemaField::fromSchema($schema, 'header')
);
$mayUseTextType = $this->authorizationChecker->isGranted(
    Scope::WRITE,
    ResourceIdentifier::fieldValue('tt_content', 'CType', 'text')
);
$mayEditGerman = $this->authorizationChecker->isGranted(
    Scope::WRITE,
    ResourceIdentifier::language(1)
);
$hasOption = $this->authorizationChecker->isGranted(
    Scope::ACCESS,
    ResourceIdentifier::customOption('tx_news:publish')
);
Copied!

Terms 

Principal
Who acts in a request: an authenticated backend user, a frontend user of a site, or the system. Authentication creates it once per request, and it does not change afterwards. Voters read its role ( $principal->context->role ), its user id ( $principal->context->id ), its group ids ( $principal->context->groupIds ) and whether it is a backend user ( $principal->isBackendUser() ). Voters decide for a principal, never for a user object such as $GLOBALS['BE_USER'] . A request without login has no principal.
Principal token
Holds the principal of the current request in the token storage of symfony/security-core until the request ends, and nothing else. isGranted() decides on it. It is never stored in the session and cannot be serialized, unlike an access token a client sends.
Role
What a principal is, asked as attribute: isGranted(SecurityRole::ADMIN->value) . The cases of \TYPO3\CMS\Core\Security\Authorization\SecurityRole follow the role of the principal, e.g. an administrator holds ROLE_USER, ROLE_BACKEND_USER and ROLE_ADMIN. Every principal also holds one role per group, e.g. ROLE_BACKEND_GROUP_3.
Authorization checker
The single entry point for authorization decisions: isGranted($attribute, $subject) decides for the principal of the request, isGrantedForUser() for any other principal. It asks the voters and combines their votes.
Attribute
What is to be done, e.g. Scope::WRITE , or which role is required, e.g. SecurityRole::ADMIN->value .
Subject
What it is done with, e.g. a TcaSchema for a table, a SchemaField , a SiteLanguage , a backend module, a dashboard widget or a ResourceIdentifier . Voters of extensions may decide on objects of their own.
Scope
The attributes TYPO3 Core decides on for resources, the constants of \TYPO3\CMS\Core\Security\Authorization\Policy\Scope : read, write for any change, i.e. creating, updating or deleting, and access. Grants do not tell creating, updating and deleting apart, so TYPO3 Core asks for write only.
Voter
A service that decides on the attributes and subjects it supports: it grants, denies or abstains. Voters are registered with the #[AsSecurityVoter] attribute and are the extension point of authorization: TYPO3 Core decides with voters of its own, and an extension adds a voter to restrict a decision or to decide on its own attributes and subjects. PrincipalVoter is the base class: supports() names what the voter decides on, voteOnPrincipal() decides for the principal.
Decision strategy
A decision is granted if at least one voter grants and none denies; if all voters abstain, it is denied. A voter of an extension can thus restrict any decision, but never grant what a voter of TYPO3 Core denies.
Grant
A permission stored for a principal, such as a table listed in be_groups.tables_modify . Some decisions need no grant, e.g. fields that are no exclude fields.
Resource identifier
The subject for a resource without an object of its own, named by a type and an identifier: ResourceIdentifier::pageType(254) . Tables, fields, modules and widgets have an object of their own and are checked on it, not on a resource identifier. A language is checked on its SiteLanguage , or on ResourceIdentifier::language() where no site is at hand; both are decided alike, so a voter restricting a language supports both. An extension may use a type of its own, prefixed with its extension key, and decides on it with a voter of its own.
Schema field
The subject for a field of a TCA table, as a field object of the schema does not know its table: SchemaField::fromSchema($schema, 'header') .
Policy decision point
The internal service the voters of TYPO3 Core ask whether a grant of the principal covers a scope on a resource. It knows where grants are stored, today the permission lists of the backend user and its groups; the voters know whether a grant is needed at all, e.g. that tables marked adminOnly are granted to administrators only, unless an explicit grant for the table exists. Extensions never call it, they write a voter.

The following decisions are available. Administrators, system maintainers and the system are granted every decision based on a grant; tables marked adminOnly are granted to administrators only, and to other backend users by an explicit grant, see below. Principals of frontend users are denied all of them. A sub-schema such as tt_content.textmedia, a field of it and a value of such a field are denied, as grants and restricting voters name the table: check the main schema. Tables, fields, modules and widgets given as ResourceIdentifier are denied as well, as they are decided on their objects only.

Question Attribute and subject Granted by Former check
Reading records of a table Scope::READ , a TcaSchema be_groups.tables_select or tables_modify check('tables_select', $table)
Creating, changing or deleting records of a table Scope::WRITE , a TcaSchema be_groups.tables_modify check('tables_modify', $table)
Editing an exclude field Scope::WRITE , a SchemaField be_groups.non_exclude_fields ; fields that are no exclude fields are always granted check('non_exclude_fields', 'table:field')
Using a value of an authMode field Scope::WRITE , ResourceIdentifier::fieldValue($table, $field, $value) be_groups.explicit_allowdeny ; a blank value and --div-- are always granted, a value containing :, | or , is granted to administrators only checkAuthMode($table, $field, $value)
Editing a language Scope::WRITE , a SiteLanguage or ResourceIdentifier::language($languageId) be_users.allowed_languages , be_groups.allowed_languages ; all languages if empty, "all languages" (-1) always checkLanguageAccess($languageId)
Using a page type Scope::WRITE , ResourceIdentifier::pageType($doktype) be_groups.pagetypes_select check('pagetypes_select', $doktype)
Using a dashboard widget Scope::ACCESS , a WidgetConfigurationInterface be_users.available_widgets , be_groups.availableWidgets ; widgets marked adminOnly are granted to administrators only check('available_widgets', $identifier) and isAdminOnly()
Using an MFA provider Scope::ACCESS , ResourceIdentifier::mfaProvider($identifier) be_users.mfa_providers , be_groups.mfa_providers check('mfa_providers', $identifier)
Having a custom option Scope::ACCESS , ResourceIdentifier::customOption('group:item') be_groups.custom_options check('custom_options', 'group:item')
Being an administrator or a system maintainer SecurityRole::ADMIN->value , SecurityRole::MAINTAINER->value be_users.admin , $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemMaintainers'] isAdmin() , isSystemMaintainer()
Opening a backend module Scope::ACCESS , a ModuleInterface be_users.userMods , be_groups.groupMods , see Feature: #111236 - Security voters for authorization decisions check('modules', $identifier)

Unlike check() , an empty value or a value listing several items never matches a grant; values of authMode fields are decided as checkAuthMode() does, see above, and an identifier without table, field and value is denied, also for administrators.

The decisions are taken on the group data of the backend user object the principal was built from, including groups added through the AfterGroupsResolvedEvent . Functional tests compare them with the former checks for a set of users and groups. The principal of the request is built at authentication: when code changes the group data of the backend user object later in the request, e.g. to grant a table for one operation, check() sees the change, isGranted() does not.

Some questions of check() have no subject yet and stay with it: tables without TCA, exclude fields of FlexForms, and modules and widgets asked for by identifier instead of a ModuleInterface or a widget configuration. The DataHandler additionally restricts file records through its checkModifyAccessList hooks, which no voter covers yet.

An extension restricts any of these decisions with a voter of its own that denies, e.g. for a table:

EXT:my_extension/Classes/Security/FrozenNewsVoter.php
namespace MyVendor\MyExtension\Security;

use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use TYPO3\CMS\Core\Attribute\AsSecurityVoter;
use TYPO3\CMS\Core\Authentication\Principal;
use TYPO3\CMS\Core\Schema\TcaSchema;
use TYPO3\CMS\Core\Security\Authorization\Policy\Scope;
use TYPO3\CMS\Core\Security\Authorization\SecurityRole;
use TYPO3\CMS\Core\Security\Authorization\Voter\PrincipalVoter;

/**
 * Only administrators may create, change or delete news records.
 */
#[AsSecurityVoter]
final class FrozenNewsVoter extends PrincipalVoter
{
    protected function supports(string $attribute, mixed $subject): bool
    {
        return $subject instanceof TcaSchema
            && $subject->getName() === 'tx_news_domain_model_news'
            && $attribute === Scope::WRITE;
    }

    protected function voteOnPrincipal(
        string $attribute,
        mixed $subject,
        Principal $principal,
        ?Vote $vote = null,
    ): bool {
        return $this->hasRole($principal, SecurityRole::ADMIN);
    }
}
Copied!

An extension decides on a resource type of its own with a voter that supports it; the voters of TYPO3 Core abstain on such types:

EXT:my_shop/Classes/Security/DiscountVoter.php
namespace MyVendor\MyShop\Security;

use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use TYPO3\CMS\Core\Attribute\AsSecurityVoter;
use TYPO3\CMS\Core\Authentication\Principal;
use TYPO3\CMS\Core\Security\Authorization\Policy\ResourceIdentifier;
use TYPO3\CMS\Core\Security\Authorization\Policy\Scope;
use TYPO3\CMS\Core\Security\Authorization\SecurityRole;
use TYPO3\CMS\Core\Security\Authorization\Voter\PrincipalVoter;

/**
 * isGranted(Scope::ACCESS, new ResourceIdentifier('tx_myshop_discount', 'staff'))
 */
#[AsSecurityVoter]
final class DiscountVoter extends PrincipalVoter
{
    protected function supports(string $attribute, mixed $subject): bool
    {
        return $attribute === Scope::ACCESS
            && $subject instanceof ResourceIdentifier
            && $subject->type === 'tx_myshop_discount';
    }

    protected function voteOnPrincipal(
        string $attribute,
        mixed $subject,
        Principal $principal,
        ?Vote $vote = null,
    ): bool {
        return $subject->identifier !== 'staff'
            || $this->hasRole($principal, SecurityRole::ADMIN);
    }
}
Copied!

A voter cannot grant a table marked adminOnly to a backend user who is no administrator, as the TableVoter denies it, and grants of backend groups never cover such a table. An extension that delegates such a table on purpose, e.g. be_users to editors who manage other editors, implements \TYPO3\CMS\Core\Security\Authorization\Policy\AdminOnlyTableGrantInterface . The policy decision point asks every implementation, and one grant suffices. Voters of extensions still restrict the decision:

EXT:my_extension/Classes/Security/EditorManagerGrant.php
namespace MyVendor\MyExtension\Security;

use MyVendor\MyExtension\Domain\EditorManagerRepository;
use TYPO3\CMS\Core\Authentication\Principal;
use TYPO3\CMS\Core\Security\Authorization\Policy\AdminOnlyTableGrantInterface;

final readonly class EditorManagerGrant implements AdminOnlyTableGrantInterface
{
    public function __construct(
        private EditorManagerRepository $editorManagers,
    ) {}

    public function isGranted(Principal $principal, string $scope, string $table): bool
    {
        return $table === 'be_users'
            && $this->editorManagers->isEditorManager($principal->context->id);
    }
}
Copied!

Granting a table grants every field of it that is no exclude field. be_users marks no field as exclude field, so the grant above also grants changing admin, password and usergroup. The extension restricts the fields the grant is not meant for with a voter:

EXT:my_extension/Classes/Security/EditorManagerFieldVoter.php
namespace MyVendor\MyExtension\Security;

use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use TYPO3\CMS\Core\Attribute\AsSecurityVoter;
use TYPO3\CMS\Core\Authentication\Principal;
use TYPO3\CMS\Core\Security\Authorization\Policy\Scope;
use TYPO3\CMS\Core\Security\Authorization\SecurityRole;
use TYPO3\CMS\Core\Security\Authorization\Subject\SchemaField;
use TYPO3\CMS\Core\Security\Authorization\Voter\PrincipalVoter;

/**
 * Only administrators may change these fields of backend users.
 */
#[AsSecurityVoter]
final class EditorManagerFieldVoter extends PrincipalVoter
{
    private const array ADMIN_FIELDS = ['admin', 'TSconfig'];

    protected function supports(string $attribute, mixed $subject): bool
    {
        return $attribute === Scope::WRITE
            && $subject instanceof SchemaField
            && $subject->schema->getName() === 'be_users'
            && in_array($subject->field->getName(), self::ADMIN_FIELDS, true);
    }

    protected function voteOnPrincipal(
        string $attribute,
        mixed $subject,
        Principal $principal,
        ?Vote $vote = null,
    ): bool {
        return $this->hasRole($principal, SecurityRole::ADMIN);
    }
}
Copied!

The DataHandler does not ask voters yet and keeps writing tables marked adminOnly for administrators only.

Grants stored in a field an extension adds to be_groups are not yet passed to voters. Until then, BackendUserAuthentication::check() keeps working for such fields.

The subjects \TYPO3\CMS\Core\Security\Authorization\Subject\SchemaField and \TYPO3\CMS\Core\Security\Authorization\Policy\ResourceIdentifier and the scopes read and write of \TYPO3\CMS\Core\Security\Authorization\Policy\Scope are experimental and not yet public API: they may still change until the checks of BackendUserAuthentication are migrated to them. The voters of TYPO3 Core and the policy decision point answering them are internal. \TYPO3\CMS\Core\Security\Authorization\Policy\AdminOnlyTableGrantInterface is public API, as the checks of BackendUserAuthentication on tables delegate to the voters, see Breaking: #111498 - Backend user checks delegate to security voters.

Impact 

Permissions granted by backend groups can be checked with isGranted() and isGrantedForUser() for every principal built from a backend user, and extensions can restrict them with voters. The methods of \TYPO3\CMS\Core\Authentication\BackendUserAuthentication keep working and delegate to the voters, see Breaking: #111498 - Backend user checks delegate to security voters.