<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Thomas Hercule</title>
    <description>The latest articles on Forem by Thomas Hercule (@th0masso).</description>
    <link>https://forem.com/th0masso</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1232534%2Fcb37f809-4840-44c3-b087-2363fd6bc3b9.jpeg</url>
      <title>Forem: Thomas Hercule</title>
      <link>https://forem.com/th0masso</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/th0masso"/>
    <language>en</language>
    <item>
      <title>How to ensure that all the routes on my Symfony app have access control</title>
      <dc:creator>Thomas Hercule</dc:creator>
      <pubDate>Wed, 17 Jan 2024 10:15:05 +0000</pubDate>
      <link>https://forem.com/theodo/how-to-ensure-that-all-the-routes-on-my-symfony-app-have-access-control-jff</link>
      <guid>https://forem.com/theodo/how-to-ensure-that-all-the-routes-on-my-symfony-app-have-access-control-jff</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Effective access control involves both Symfony's firewall &lt;strong&gt;and&lt;/strong&gt; specific control on each route.&lt;/li&gt;
&lt;li&gt;If you use API Platform, use ACCENT to verify access control on your routes.&lt;/li&gt;
&lt;li&gt;If you use functions in the controller to secure your routes, you can install the composer package I created based on the following part of this article.&lt;/li&gt;
&lt;li&gt;If you're not using API Platform nor functions, you can create a custom script.&lt;/li&gt;
&lt;li&gt;It's important to integrate access control verification into your CI process to maintain a high level of security.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id="what-is-access-control"&gt;What is access control&lt;/h1&gt;

&lt;p&gt;Access control allows you to define access permissions to specific parts of your application. It helps restrict access to certain pages or features for users who do not have the necessary permissions.&lt;/p&gt;

&lt;p&gt;To implement access control, you need to define user roles and corresponding permissions, then apply them to the routes of your application. This can be especially useful for safeguarding sensitive information or important actions, such as modifying or deleting data.&lt;/p&gt;

&lt;p&gt;Implementing access control for routes significantly enhances the security of your Symfony project and safeguards your users’ data.&lt;/p&gt;

&lt;p&gt;Effective access control in your Symfony project involves two main aspects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Symfony Firewall&lt;/li&gt;
&lt;li&gt;Specific access control for each route&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id="symfony-firewall"&gt;Symfony firewall&lt;/h1&gt;

&lt;p&gt;The Symfony firewall is the initial layer of security for routes, adding global rules to all routes or specific groups of routes.&lt;/p&gt;

&lt;p&gt;The configuration for this firewall is typically found in &lt;strong&gt;&lt;code&gt;config/packages/security.yaml&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this file, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define which URL groups require (or do not require) security checks (logged-in user, specific role, GET/POST requests, etc.).&lt;/li&gt;
&lt;li&gt;Whitelist IPs for certain endpoints.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Generally, you would grant access to the site only to logged-in users (the default behavior) and disable this check for specific pages (login, password reset, etc.).&lt;/p&gt;

&lt;p&gt;Here's an example of a typical &lt;strong&gt;&lt;code&gt;security.yaml&lt;/code&gt;&lt;/strong&gt; configuration where we ensure the user is logged in for all routes except the login and register pages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;access_control&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;^/login&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;role&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;IS_AUTHENTICATED_ANONYMOUSLY&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;^/register&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;role&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;IS_AUTHENTICATED_ANONYMOUSLY&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;^/&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;roles&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;ROLE_USER&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;ROLE_ADMIN&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The role &lt;strong&gt;&lt;code&gt;IS_AUTHENTICATED_ANONYMOUSLY&lt;/code&gt;&lt;/strong&gt; allows access to pages even for non-authenticated users. &lt;strong&gt;&lt;a href="https://symfony.com/doc/current/security/access_control.html" rel="noopener noreferrer"&gt;You can find more details in the Symfony firewall documentation&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Although it's possible to have role-based access control in the firewall, it's preferable to implement it on each route:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To handle more complex rules.&lt;/li&gt;
&lt;li&gt;To make the access control directly visible on the relevant functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have a Symfony project with more than a dozen routes, as it was the case on my project, you will need a tool to automatically ensure they all have access control.&lt;/p&gt;

&lt;h1 id="how-to-ensure-access-control-for-all-your-symfony-routes"&gt;How to ensure access control for all your symfony routes&lt;/h1&gt;

&lt;p&gt;Depending on the type of project, different solutions can be used to verify the security of these routes. If you are using API Platform, you can save time by utilizing ACCENT. Otherwise, it is possible to create a custom script since there is no pre-existing tool to automate this verification.&lt;/p&gt;

&lt;h2 id="my-project-uses-api-platform-using-accent"&gt;My project uses API Platform: using ACCENT&lt;/h2&gt;

&lt;p&gt;If you use API Platform, you can use &lt;a href="https://github.com/theodo/accent" rel="noopener noreferrer"&gt;ACCENT&lt;/a&gt;. It is a powerful tool to generate a report on the access control of each of your routes with very little configuration.&lt;/p&gt;

&lt;p&gt;To install ACCENT, run: &lt;strong&gt;&lt;code&gt;composer require --dev theodo/accent-bundle&lt;/code&gt;&lt;/strong&gt;.&lt;br&gt;
Then, you can generate a detailed report on the access control of your routes with: &lt;strong&gt;&lt;code&gt;bin/console theodo:access-control&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Example of a generated report:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tpj5cgtpijikvphoel5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tpj5cgtpijikvphoel5.png" alt="ACCENT generated report" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above you can see the list of all the projects’ routes with, highlighted in red, the routes that lacks access control.&lt;/p&gt;

&lt;h2&gt;My project doesn't use API Platform&lt;/h2&gt;

&lt;h3 id="my-project-uses-function-to-secure-routes"&gt;My project uses functions to secure routes&lt;/h3&gt;

&lt;p&gt;If your project uses functions to secure your routes in the controllers as such :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AdminController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractController&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nv"&gt;$securityService&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'admin'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// We redirect the user to the login page&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// ...&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;It also works with Symfony's function (&lt;strong&gt;&lt;code&gt;isGranted&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;denyAccessUnlessGranted&lt;/code&gt;&lt;/strong&gt; for example).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can use the composer package I created based on the script I build in the next part of this article : &lt;a href="https://github.com/Th0masso/symfony-security-access-control-checker" rel="noopener noreferrer"&gt;&lt;strong&gt;symfony-route-security-checker&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install it with : &lt;strong&gt;&lt;code&gt;composer require --dev th0masso/symfony-route-security-checker&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy the &lt;code&gt;ssacc-config.dist.yaml&lt;/code&gt; from the package repository into your project. Read the documentation to configure it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, you can generate a detailed report on the access control of your routes with: &lt;strong&gt;&lt;code&gt;bin/console security:check-routes&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03a8cre9408eicrtrm6w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03a8cre9408eicrtrm6w.png" alt="SSACC generated report" width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id="other-cases"&gt;Other cases&lt;/h3&gt;

&lt;p&gt;You can create a custom script specific to your project. In this section, we will use an example of a script that checks if Symfony security functions are called at the beginning of each function related to routes. If you are using something other than Symfony Security function, this section can still help you write such a script.&lt;/p&gt;

&lt;p&gt;Plan for this section:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access control with Symfony Security&lt;/li&gt;
&lt;li&gt;Why write a script?&lt;/li&gt;
&lt;li&gt;Retrieving the list of project routes&lt;/li&gt;
&lt;li&gt;Verifying that the function has access control&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id="access-control-with-symfony-security"&gt;Access control with Symfony security&lt;/h3&gt;

&lt;p&gt;There are several ways to secure routes "manually" using Symfony Security functions: &lt;strong&gt;&lt;code&gt;isGranted&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;denyAccessUnlessGranted&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In annotations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Attribute\IsGranted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AdminController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractController&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;#[IsGranted('ROLE_ADMIN')]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// ...&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or by directly calling these functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Attribute\IsGranted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AdminController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractController&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;isGranted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ROLE_ADMIN'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// We redirect the user to the login page&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// ...&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have worked on a project where we controlled access to our routes by calling &lt;code&gt;isGranted&lt;/code&gt; or &lt;code&gt;denyAccessUnlessGranted&lt;/code&gt; within our functions. However, the method I used to verify access control on our routes can also work with annotations with minor modifications.&lt;/p&gt;

&lt;h3 id="why-write-a-script"&gt;Why write a script?&lt;/h3&gt;

&lt;p&gt;As there is no existing tool to automatically verify routes in these cases, we have two solutions to ensure that routes have specific access control:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manually check the routes.&lt;/li&gt;
&lt;li&gt;Create a script to do it for us.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both of these solutions provide a snapshot of the current state. However, creating a script automates the access control verification for future routes and could potentially be shared and used in other projects.&lt;/p&gt;

&lt;p&gt;In my case, I knew there were over a hundred routes in the project, so doing it manually would have been very redundant.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytcwht4t7652c3jjhqfe.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytcwht4t7652c3jjhqfe.jpg" alt="kanye meme" width="500" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id="retrieving-the-list-of-project-routes"&gt;Retrieving the list of project routes&lt;/h3&gt;

&lt;p&gt;Symfony provides a command to list all the routes in the project along with their paths.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;php bin/console debug:router --show-controllers --format=json&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, for each route in the project, we have its path in the format: &lt;strong&gt;&lt;code&gt;MyPath/MyController.php::MyFunction&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id="verifying-that-the-function-has-access-control"&gt;Verifying that the function has access control&lt;/h3&gt;

&lt;p&gt;Now that we know which functions are associated with which routes and their paths, we can check in the files whether these functions indeed have calls to permission-checking functions such as &lt;strong&gt;&lt;code&gt;isGranted&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;denyAccessUnlessGranted&lt;/code&gt;&lt;/strong&gt;. &lt;br&gt;
These functions can be called &lt;a href="https://symfony.com/bundles/SensioFrameworkExtraBundle/current/annotations/security.html" rel="noopener noreferrer"&gt;through annotations&lt;/a&gt; or directly within the controller function.&lt;/p&gt;

&lt;h4&gt; Using annotations to restrict access &lt;/h4&gt;

&lt;p&gt;If you use annotations to secure your routes, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;recover the annotation of the controller's function as a string with PHP's &lt;a href="https://www.php.net/manual/en/reflectionproperty.getdoccomment.php" rel="noopener noreferrer"&gt;ReflectionProperty::getDocComment&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;then search for &lt;strong&gt;&lt;code&gt;isGranted&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;denyAccessUnlessGranted&lt;/code&gt;&lt;/strong&gt; using a regular expression.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since I didn't use annotations on my project, I will not go into details on this method.&lt;/p&gt;

&lt;h4&gt; Calling functions to restrict access &lt;/h4&gt;

&lt;p&gt;If you call some permission-checking functions directly into your controller, it's possible to ensure that these functions are called at the beginning of the function used by the route in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using a regular expression&lt;/li&gt;
&lt;li&gt;Using the Abstract Syntax Tree (AST)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AST should be more robust because it can handle more complex cases. For example, with AST, we can determine if the function is called in a condition, a loop, or another function. However, within my team, no one had experience with AST manipulation, and we didn't encounter such complex cases in my project. So, I opted for a solution using a regular expression.&lt;/p&gt;

&lt;p&gt;I then wrote a regular expression that matches &lt;strong&gt;&lt;code&gt;!$this-&amp;gt;isGranted&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;$this-&amp;gt;denyAccessUnlessGranted&lt;/code&gt;&lt;/strong&gt; on the first line of the function used by the route (⚠️ please do not read this monstrosity):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'((public function '&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$routeFunction&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;')(\([^{]*\{)(\s.*)(\$this-&amp;gt;denyAccessUnlessGranted|!\$this-&amp;gt;isGranted))'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, it's just a matter of checking if we find the expression in the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nb"&gt;preg_match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$regex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$fileContent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It worked on the first try; all the routes were secured.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptb4za1h7l2c0wfqwxpj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptb4za1h7l2c0wfqwxpj.png" alt="Fake smile" width="150" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’m kidding, I'm not fluent in regular expressions, and I want others to be able to read and understand this script, so it took me a few hours.&lt;/p&gt;

&lt;p&gt;ℹ️ Fortunately, I was advised to use &lt;strong&gt;&lt;a href="https://regex101.com/" rel="noopener noreferrer"&gt;regex101.com&lt;/a&gt;&lt;/strong&gt;, an incredibly useful website for testing and understanding complex regular expressions.&lt;/p&gt;

&lt;p&gt;Then, I broke down and commented the expression to make it more understandable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// start of regular expression&lt;/span&gt;
&lt;span class="nv"&gt;$regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'('&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// find "public function myFunction"&lt;/span&gt;
&lt;span class="nv"&gt;$regex&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'(public function '&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$function&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;')'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// then everything until "{"&lt;/span&gt;
&lt;span class="nv"&gt;$regex&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'(\([^{]*\{)'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// then everything on next line&lt;/span&gt;
&lt;span class="nv"&gt;$regex&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'(\s.*)'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// until "$this-&amp;gt;denyAccessUnlessGranted" or "!$this-&amp;gt;isGranted"&lt;/span&gt;
&lt;span class="nv"&gt;$regex&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'(\$this-&amp;gt;denyAccessUnlessGranted|!\$this-&amp;gt;isGranted)'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// end of regular expression&lt;/span&gt;
&lt;span class="nv"&gt;$regex&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;')'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, I ran my script with this final version of the expression.&lt;/p&gt;

&lt;p&gt;ℹ️ &lt;strong&gt;&lt;a href="https://github.com/Th0masso/symfony-security-access-control-checker" rel="noopener noreferrer"&gt;You can find the composer package based on this script on github.&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The file with the regex and the rest of the logic is on the same repo : &lt;a href="https://github.com/Th0masso/symfony-security-access-control-checker/blob/main/src/Command/AccessControlCheckerCommand.php" rel="noopener noreferrer"&gt;&lt;code&gt;src/Command/AccessControlCheckerCommand.php&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="security-vulnerabilities-found-on-my-project"&gt;Security vulnerabilities found on my project&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj4yjd8484p0s0dxa0g56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj4yjd8484p0s0dxa0g56.png" alt="Script output" width="740" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;46 Routes Without Access Control ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F53ccqgce7sqw7hx6pllq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F53ccqgce7sqw7hx6pllq.png" alt="This is fine" width="740" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After investigation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;7 routes linked to Symfony modules: &lt;strong&gt;&lt;code&gt;web_profiler&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;twig&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;6 routes that should not be verified, therefore accessible to everyone (login page, password recovery, etc.)&lt;/li&gt;
&lt;li&gt;4 exposed API routes, which should not be verified in this way&lt;/li&gt;
&lt;li&gt;9 false positives due to special cases&lt;/li&gt;
&lt;li&gt;3 unused routes (cleaning up dead code 🤩)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;17 routes that were indeed problematic&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After identifying where each of these 17 routes was being used on the site, I realized the importance of having a strict access control strategy for your app.&lt;/p&gt;

&lt;p&gt;For these routes without permission checks, most were harmless, but some allowed critical actions on the site! &lt;strong&gt;This means that any logged-in user could perform these actions if they sent the right HTTP request&lt;/strong&gt; (thanks to Symfony's firewall rejecting non-logged-in users). However, there was no risk of privilege escalation.&lt;/p&gt;

&lt;p&gt;To secure these 17 routes, I organized a meeting with the client to define the access control to be applied for each one.&lt;/p&gt;

&lt;p&gt;Afterwards, the client lived happily ever after, and the site wasn't hacked due to an access control issue... until the day a developer introduced a new route without permission!&lt;/p&gt;

&lt;h2 id="how-to-ensure-new-routes-are-secure-too"&gt;How to ensure new routes are secure too&lt;/h2&gt;

&lt;p&gt;To ensure that new routes added are also secure, it's important to run this verification script automatically. The best way is to add it to your CI (Continuous Integration) to ensure that the script runs before each merge. If you can't add it to your CI for some reason, you can add it to your &lt;a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks" rel="noopener noreferrer"&gt;git pre-commit or pre-push hook&lt;/a&gt; it's not as good because those hooks can be ignored by the developer by using &lt;code&gt;--no-verify&lt;/code&gt; after &lt;code&gt;git commit&lt;/code&gt; or &lt;code&gt;git push&lt;/code&gt;. &lt;br&gt;
Unfortunately, I didn't have the time to implement it in my project's CI due to time constraints.&lt;/p&gt;

&lt;p&gt;Although the performance of my script is quite good, it still takes a few seconds to run if you have hundreds of routes (approximately 5 seconds for 200 routes). To overcome this issue, one approach could be to run the script only on modified files, which would significantly reduce processing time.&lt;/p&gt;

</description>
      <category>symfony</category>
      <category>security</category>
      <category>php</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
