The SonataExtraBundle provides a set of enhanced features for managing firewall rules in the Sonata Admin environment. This bundle allows administrators to create and manage rules to filter requests based on different criteria such as forbidden words, IP addresses, and user agents.
Features
- Firewall rules management: Define and manage firewall rules to filter incoming requests.
- Support for multiple criteria: filter requests based on forbidden words, IP addresses, and user agents.
- Dynamic application of rules: Apply rules based on the request parameters (GET, POST, headers).
- Cache integration: rules are cached to improve performance with the ability to reset the cache during updates.
- Recursive data analysis: managing complex data structures in query parameters.
Activation
Add the firewall listener in your services.yaml
sonata-extra.Firewall_Listener:
class: Partitech\SonataExtra\EventListener\FirewallListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Add administrative interfaces in your sonata_admin.yaml
firewall:
icon: fa fa-cogs
label: Firewall
items:
- Partitech\SonataExtra\Admin\SecFirewallRuleAdmin
- Partitech\SonataExtra\Admin\SecStopWordAdmin
- Partitech\SonataExtra\Admin\SecIpRuleAdmin
Creation of firewall rules
Firewall rules can be created and managed via the Sonata Admin dashboard. The following types of rules are supported:
- Forbidden word: Block requests containing specified keywords.
- IP: Block requests coming from the specified IP addresses.
- User agent: Block requests coming from specified user agents.
Rules configuration
Each rule consists of the following elements:
type: type of rule (for example, 'stop_word', 'ip', 'user_agent').source: source of the data to be analyzed (for example, 'GET', 'POST', 'HEADER').parameters: list of values to check against the rule.matchMode: determines if the rule should look for an exact match (equal) or a partial match (contain).
Cache management
The bundle uses caching to improve performance. The cache is automatically reset whenever a rule is updated, created, or deleted.
Adapt the example to the installed version
The Symfony and Sonata examples remain tied to the versions indicated in the article. Symfony 4.x and Symfony 6.2 are no longer maintained: for a current project, use a maintained branch and the documentation that exactly matches your dependencies.
php bin/console about
composer show symfony/framework-bundle sonata-project/admin-bundle
Before resuming the code, check the signatures, services, routes, and templates concerned. Then add a functional test covering permissions, HTTP code, and the visible result.
Reference: maintained Symfony versions.