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
auth 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')
);
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->group) and whether it is a backend user (Ids $principal->is). Voters decide for a principal, never for a user object such asBackend User () $GLOBALS. A request without login has no principal.['BE_ USER'] - Principal token
- Holds the principal of the current request in the token storage of
symfony/until the request ends, and nothing else.security- core isdecides on it. It is never stored in the session and cannot be serialized, unlike an access token a client sends.Granted () - Role
- What a principal is, asked as attribute:
is. The cases ofGranted (Security Role:: ADMIN->value) \TYPO3\follow the role of the principal, e.g. an administrator holdsCMS\ Core\ Security\ Authorization\ Security Role ROLE_,USER ROLE_andBACKEND_ USER ROLE_. Every principal also holds one role per group, e.g.ADMIN ROLE_.BACKEND_ GROUP_ 3 - Authorization checker
- The single entry point for authorization decisions:
isdecides for the principal of the request,Granted ($attribute, $subject) isfor any other principal. It asks the voters and combines their votes.Granted For User () - Attribute
- What is to be done, e.g.
Scope::, or which role is required, e.g.WRITE Security.Role:: ADMIN->value - Subject
- What it is done with, e.g. a
Tcafor a table, aSchema Schema, aField Site, a backend module, a dashboard widget or aLanguage Resource. Voters of extensions may decide on objects of their own.Identifier - Scope
- The attributes TYPO3 Core decides on for resources, the constants of
\TYPO3\:CMS\ Core\ Security\ Authorization\ Policy\ Scope read,writefor any change, i.e. creating, updating or deleting, andaccess. Grants do not tell creating, updating and deleting apart, so TYPO3 Core asks forwriteonly. - Voter
- A service that decides on the attributes and subjects it supports: it
grants, denies or abstains. Voters are registered with the
#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.[As Security Voter] Principalis the base class:Voter supportsnames what the voter decides on,() votedecides for the principal.On 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_. Some decisions need no grant, e.g. fields that are no exclude fields.groups. tables_ modify - Resource identifier
- The subject for a resource without an object of its own, named by a
type and an identifier:
Resource. 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 itsIdentifier:: page Type (254) Site, or onLanguage Resourcewhere 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.Identifier:: language () - Schema field
- The subject for a field of a TCA table, as a field object of the
schema does not know its table:
Schema.Field:: from Schema ($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
adminare granted to administrators only, unless an explicit grant for the table exists. Extensions never call it, they write a voter.Only
The following decisions are available. Administrators, system maintainers
and the system are granted every decision based on a grant; tables marked
admin 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_, 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
Resource 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::, a
Tca |
be_ or
tables_ |
check |
| Creating, changing or deleting records of a table |
Scope::, a
Tca |
be_ |
check |
| Editing an exclude field |
Scope::, a
Schema |
be_; fields that are no exclude
fields are always granted |
check |
Using a value of an auth field |
Scope::,
Resource |
be_; a blank value and --
are always granted, a value containing :, | or , is granted
to administrators only |
check |
| Editing a language |
Scope::, a
Site or
Resource |
be_,
be_;
all languages if empty, "all languages" (-1) always |
check |
| Using a page type |
Scope::,
Resource |
be_ |
check |
| Using a dashboard widget |
Scope::, a
Widget |
be_,
be_;
widgets marked admin are granted to administrators only |
check and
is |
| Using an MFA provider |
Scope::,
Resource |
be_,
be_ |
check |
| Having a custom option |
Scope::,
Resource |
be_ |
check |
| Being an administrator or a system maintainer |
Security,
Security |
be_,
$GLOBALS |
is,
is |
| Opening a backend module |
Scope::, a
Module |
be_,
be_, see
Feature: #111236 - Security voters for authorization decisions |
check |
Unlike
check, an empty value or a value listing several items never
matches a grant; values of auth fields are decided as
check 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
After. 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,
is 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
Module or a widget
configuration. The
Data
additionally restricts file records through its check
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:
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);
}
}
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:
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);
}
}
A voter cannot grant a table marked admin to a backend user who is no
administrator, as the
Table denies it, and grants of backend
groups never cover such a table. An extension that delegates such a table on
purpose, e.g.
be_ to editors who manage other editors, implements
\TYPO3\.
The policy decision point asks every implementation, and one grant suffices.
Voters of extensions still restrict the decision:
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);
}
}
Granting a table grants every field of it that is no exclude field.
be_ 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:
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);
}
}
The
Data does not ask voters yet and keeps writing tables
marked admin for administrators only.
Grants stored in a field an extension adds to
be_ are not yet
passed to voters. Until then,
Backend
keeps working for such fields.
The subjects
\TYPO3\
and
\TYPO3\ and
the scopes read and write of
\TYPO3\ are experimental and
not yet public API: they may still change until the checks of
Backend are migrated to them. The voters of TYPO3 Core
and the policy decision point answering them are internal.
\TYPO3\
is public API, as the checks of
Backend 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
is
and
is for every principal built from a backend user,
and extensions can restrict them with voters. The methods of
\TYPO3\ keep working
and delegate to the voters, see Breaking: #111498 - Backend user checks delegate to security voters.