Symfony security: authentication, authorization and security.yaml
Security in Symfony happens in two steps: the firewall authenticates, establishing who makes the request, then authorization decides what they are allowed to do.
# config/packages/security.yaml security: providers: employes: { entity: { class: App\Entity\Employe, property: email } } firewalls: main: provider: employes form_login: { login_path: app_connexion, check_path: app_connexion } access_control: - { path: ^/admin, roles: ROLE_ADMIN }
#[IsGranted('PLAT_MODIFIER', 'plat')] // a voter decides, based on the dish and the user public function modifier(Plat $plat): Response { /* … */ }
Everything starts from security.yaml: the provider finds the user, the firewall chooses how they prove their identity (form, login link, API token, second factor), and passwords are only ever stored hashed. Authorization comes next: access_control protects URLs by role, role_hierarchy avoids repeating roles, #[IsGranted], isGranted() and is_granted() ask the question as close to the code as possible, and a voter decides when the permission depends on the object. The trap: mixing the two up. A visitor who isn't logged in is sent to the login page, a logged-in user without the permission gets a 403; answering 403 to someone who only needs to give a proof again, their password or a code, blocks them without telling them what to do.
The exercises focus on what's new in Symfony 8.2: requiring an SMS code before a transfer, asking for the password again before sensitive pages, asking for the right factor again instead of a 403, a login link that keeps its destination, roles by family with wildcards, and validation that depends on the user's permissions.
15 exercises work on this concept, by writing code in the browser.
In the tracks
In Practice
- An expression that doesn't read your secrets constant() and enum() could read any constant of the application. ConstantFunctionProvider limits them to a list: a rule written in the back office can no longer guess an API key.
- A route that picks its firewall The firewall option of #[Route] hands a route to a firewall without touching its pattern, and entry_point_path sends anonymous users to the login page, with no class to write.
- An expired link that doesn't end up on an error page A ?ExpiredSignedUriException argument receives the failure of #[IsSignatureValid]: the controller responds itself, without dropping the attribute.
- Validating according to the user's permissions is_granted() and current_user() in the When and Expression constraints: the rules that depend on the logged-in user leave the controller.
- Protecting a delete action with a CSRF token A "Remove" link sent as a GET deletes the dish for the first bot that comes along: switch to POST, with a CSRF token for each dish.
- Change a secret without breaking the links already sent The secret of framework.uri_signer accepts a list: the first one signs new links, all of them check the ones still around.
- Log out with a button, not with a link logout_button() renders a POST logout form, CSRF token included: a link followed by a bot no longer logs anyone out.
- A link in a translation, without raw The trans_html filter replaces the pseudo-tags of a translation with HTML declared in the template, and escapes everything else, parameters included.
- Ask for the right factor again instead of a 403 Vote::requestReAuthentication(): your voter can send the user to a new authentication, and the entry point chooses which one.
- Require an SMS code before a transfer AuthenticationMethodBadge and getAuthenticationProofs(): the token remembers how the user authenticated, and a voter can require a second factor.
- A login link that keeps its destination createLoginLink() now signs its parameters: the return_to of a login link can no longer be tampered with.
- Asking for the password again before sensitive pages IS_AUTHENTICATED_RECENTLY and IS_AUTHENTICATED_VERY_RECENTLY: a “sudo mode” that asks for the password again when the login is old.
- A reset link that only works once UriSigner::sign() accepts a “version”: sign the link with the password hash, and it dies as soon as the password changes.
- Roles by family, with wildcards ROLE_BLOG_*: a single role_hierarchy key covers a whole family of roles, including those declared nowhere.
Related concepts
TwigCSRFHttpFoundationFormsN+1 queriesFirewallRoutingHttpKernel