<?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>DEV Community: Arnaud POINTET</title>
    <description>The latest articles on DEV Community by Arnaud POINTET (@oipnet).</description>
    <link>https://dev.to/oipnet</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F320559%2Fb68a6be2-7bc6-40a1-b192-e309933bd3f7.jpeg</url>
      <title>DEV Community: Arnaud POINTET</title>
      <link>https://dev.to/oipnet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oipnet"/>
    <language>en</language>
    <item>
      <title>I'm a Laravel dev, I stumbled on Goravel and discovered Go</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Wed, 18 Nov 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/im-a-laravel-dev-i-stumbled-on-goravel-and-discovered-go-37o0</link>
      <guid>https://dev.to/oipnet/im-a-laravel-dev-i-stumbled-on-goravel-and-discovered-go-37o0</guid>
      <description>&lt;p&gt;I work with Laravel, and one day I stumbled, a bit by chance, on &lt;a href="https://www.goravel.dev" rel="noopener noreferrer"&gt;Goravel&lt;/a&gt;. It's a Go framework that mirrors Laravel's structure almost exactly. You find the folders, the facades, and method names you recognize right away.&lt;/p&gt;

&lt;p&gt;I had never written any Go. But seeing everything I'm used to in Laravel carried over to another language made me curious. I wanted to take a look.&lt;/p&gt;

&lt;h2&gt;
  
  
  You feel at home straight away
&lt;/h2&gt;

&lt;p&gt;The first route reads fine without knowing Go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;facades&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/ping"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"in the saddle"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&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;A facade for the router, a closure that returns JSON. &lt;code&gt;http.Json&lt;/code&gt; is just a map, the equivalent of the array you would pass to &lt;code&gt;response()-&amp;gt;json()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The rest follows. Controllers, route parameters with &lt;code&gt;ctx.Request().Route("firstName")&lt;/code&gt;, validation with &lt;code&gt;required|in:…&lt;/code&gt;, route groups with &lt;code&gt;Prefix("/api")&lt;/code&gt;, middleware, and the query builder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;facades&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"deliveries"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"district"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;district&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OrderBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A Laravel developer can read that without translating.&lt;/p&gt;

&lt;h2&gt;
  
  
  Underneath, it really is Go
&lt;/h2&gt;

&lt;p&gt;What changes is the language. And that is exactly what I was after: Goravel let me discover Go while leaning on what I already knew.&lt;/p&gt;

&lt;p&gt;There is no &lt;code&gt;public&lt;/code&gt; or &lt;code&gt;private&lt;/code&gt;: whether a name starts with a capital letter decides if it is visible outside its package. There are no exceptions either. A function that can fail returns its value and an error, and you check it right after the call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;validator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StatusInternalServerError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&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;That &lt;code&gt;if err != nil&lt;/code&gt; shows up everywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  I'm still skeptical
&lt;/h2&gt;

&lt;p&gt;Goravel is a good way in for a Laravel developer, I can believe that. But I'm not sure that means I'll leave the PHP world. I have a hard time with the way you write things in Go.&lt;/p&gt;

&lt;p&gt;What about you, what do you think of Go? Do you use it in production, or did you try it and put it aside?&lt;/p&gt;

&lt;h2&gt;
  
  
  Make up your own mind
&lt;/h2&gt;

&lt;p&gt;If you want to try it without installing anything, I created a free course: &lt;a href="https://apprendre.forelse.fr/en/tracks/decouvrir-goravel-1?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=discovering-go-with-goravel" rel="noopener noreferrer"&gt;Discover Goravel&lt;/a&gt;. Thirteen exercises where you build the API of a bike courier cooperative in Lyon. Goravel actually runs in the browser, compiled to WebAssembly, and Go tests check each exercise.&lt;/p&gt;

&lt;p&gt;No Go knowledge is required: each exercise introduces the small bit of the language it needs. All you need is a free account.&lt;/p&gt;

</description>
      <category>go</category>
      <category>laravel</category>
    </item>
    <item>
      <title>Invalid CSRF token in Symfony: causes and fixes</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Tue, 17 Nov 2026 08:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/invalid-csrf-token-in-symfony-causes-and-fixes-4cij</link>
      <guid>https://dev.to/oipnet/invalid-csrf-token-in-symfony-causes-and-fixes-4cij</guid>
      <description>&lt;p&gt;"The CSRF token is invalid. Please try to resubmit the form." A few users report it, nobody can reproduce it locally, and the ticket drags on. Then a fix lands, in one line:&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;configureOptions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OptionsResolver&lt;/span&gt; &lt;span class="nv"&gt;$resolver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$resolver&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setDefaults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'csrf_protection'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// "it was blocking people"&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&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 saw it on a client project, and it is not a one-off: in my audits I regularly find forms or routes taken out of the check "because it was blocking people". The error goes away, the ticket gets closed, and the application has just lost a protection without anyone knowing why it was failing.&lt;/p&gt;

&lt;p&gt;This article explains what that line really exposes, how to find the real cause with three questions, and how to fix each one. Three of the causes can be reproduced in the &lt;a href="https://apprendre.forelse.fr/en/playground/symfony?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=invalid-csrf-token-symfony" rel="noopener noreferrer"&gt;Symfony playground&lt;/a&gt;, in one click, with nothing to install. If you first want a refresher on what the token protects and how other frameworks handle it, I wrote &lt;a href="https://apprendre.forelse.fr/en/blog/csrf-tokens-across-frameworks?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=invalid-csrf-token-symfony" rel="noopener noreferrer"&gt;an article on the CSRF token in Symfony, Laravel, Django and Rails&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the line really turns off
&lt;/h2&gt;

&lt;p&gt;The usual argument for turning it off: "cookies are &lt;code&gt;SameSite=Lax&lt;/code&gt; now anyway". True, Symfony sets &lt;code&gt;SameSite=Lax&lt;/code&gt; on its session cookie by default, and it is good protection. It still has holes that the token plugs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SameSite&lt;/code&gt; thinks in sites, not origins.&lt;/strong&gt; To the browser, &lt;code&gt;blog.example.com&lt;/code&gt; and &lt;code&gt;app.example.com&lt;/code&gt; belong to the same site. An XSS flaw on another subdomain, or a forgotten subdomain an attacker takes over, and their requests to the application leave with the cookie.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Actions over &lt;code&gt;GET&lt;/code&gt; are not covered.&lt;/strong&gt; &lt;code&gt;Lax&lt;/code&gt; lets the cookie through on a top-level &lt;code&gt;GET&lt;/code&gt; navigation, a plain link. A route that deletes or approves something over &lt;code&gt;GET&lt;/code&gt; is exposed, and adding a token would only leak it into the history and the &lt;code&gt;Referer&lt;/code&gt; header: never change data over &lt;code&gt;GET&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not every cookie is &lt;code&gt;Lax&lt;/code&gt;.&lt;/strong&gt; A "remember me" cookie, a cookie set by another component, or a cookie set to &lt;code&gt;SameSite=None&lt;/code&gt; for an integration does not benefit from the protection. And for cookies that specify nothing, Chrome still lets a top-level &lt;code&gt;POST&lt;/code&gt; through for two minutes after they are set.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The login form counts too.&lt;/strong&gt; Without a token, another site can log the victim into the attacker's account, and &lt;code&gt;SameSite&lt;/code&gt; makes no difference: that request needs no existing cookie. Everything the victim then types, an address, a card, a document, ends up in the attacker's hands.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;SameSite&lt;/code&gt; is a second line of defence. Disabling the token means keeping the second line and removing the first, to hide a bug nobody understood.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the cause with three questions
&lt;/h2&gt;

&lt;p&gt;The message is the same whatever the cause: the Form component does not say why it refuses. The profiler of the failing &lt;code&gt;POST&lt;/code&gt; request, locally or in staging, answers three questions.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Did the token arrive?&lt;/strong&gt; Request tab, POST parameters: look for &lt;code&gt;form[_token]&lt;/code&gt; (or &lt;code&gt;_token&lt;/code&gt; for a hand-written form). If it is missing, the problem is in the template or in the JavaScript that sends the form.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For a classic token, is it the same session?&lt;/strong&gt; Compare the session cookie (&lt;code&gt;PHPSESSID&lt;/code&gt; by default) between displaying the form and submitting it, and check whether the session of the &lt;code&gt;POST&lt;/code&gt; request still holds its tokens (&lt;code&gt;_csrf/…&lt;/code&gt; attributes). A missing or different cookie, an empty session: the token has nothing left to be compared with.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Is the token stateless?&lt;/strong&gt; Since Symfony 7.2, the &lt;code&gt;symfony/form&lt;/code&gt; recipe creates a &lt;code&gt;config/packages/csrf.yaml&lt;/code&gt; file that makes the tokens of forms (id &lt;code&gt;submit&lt;/code&gt;), login and logout stateless. The 8.2 recipes split it into &lt;code&gt;form.yaml&lt;/code&gt; and &lt;code&gt;csrf_protection.yaml&lt;/code&gt;, and an older project only gets these files after a &lt;code&gt;composer recipes:update&lt;/code&gt;: without them, its tokens stay in the session. Stateless tokens are not stored in the session: Symfony checks the &lt;code&gt;Origin&lt;/code&gt; or &lt;code&gt;Referer&lt;/code&gt; headers. In that case, question 2 does not apply. The Logs tab then gives the exact reason, for example &lt;code&gt;CSRF validation failed: origin info doesn't match.&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With these three answers, the cause is in the list below.&lt;/p&gt;

&lt;h2&gt;
  
  
  The causes, and their fix
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;_token&lt;/code&gt; field is never sent
&lt;/h3&gt;

&lt;p&gt;A template that renders fields one by one, to control the layout, then closes the tag by hand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight twig"&gt;&lt;code&gt;&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nf"&gt;form_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;
    &lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nf"&gt;form_errors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;
    &lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nf"&gt;form_row&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;
    &lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nf"&gt;form_row&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nobody renders the hidden &lt;code&gt;_token&lt;/code&gt; field: every submit is refused, for everyone. &lt;strong&gt;Fix&lt;/strong&gt; : close with &lt;code&gt;{{ form_end(form) }}&lt;/code&gt;, which renders the remaining fields, token included. If you really must write the tag by hand, at least render &lt;code&gt;{{ form_rest(form) }}&lt;/code&gt; before it.&lt;/p&gt;

&lt;p&gt;Same family: a form sent in JavaScript, with &lt;code&gt;fetch()&lt;/code&gt;, that builds its body field by field and forgets the token. Send the whole form with &lt;code&gt;new FormData(form)&lt;/code&gt;, and the token follows.&lt;/p&gt;

&lt;p&gt;▶ &lt;a href="https://apprendre.forelse.fr/en/playground/symfony?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=invalid-csrf-token-symfony#code=nVZhb5s6FP0rV7QdpEpA-8oypq571T5NU1u9L3U1GbgEq47h2aZpFOW_v2sDoVmmThuqCtj3nnt8fHzJLngO0vfzoBISTZDuAqOL5LpRVjdSok6uiqJDIaeRuK3bIA2Wn-jOFFOKr9G0vEC4alvGpsAPbrYzCHfbddWoLWOfO1VKZOxGU86m0U_jwJTE2FVurOaFPQI6grlu1m2jUFlCavSasX9eLCojGuXmNOHdb1v6f9fla2Hd898i3OPL7_K_WtveNLQObn36Lf7XobF_lGFoyrxV5LbprFArEsdaLfLOYj-GXuPk8pIpuIRrTvkp2BqhFmWJCn7Y5olulUBZgjCg8Bk1GMKMXYbP6lXyWRVpkQIL7un5-u72Bvp0ShTqmUtRxvBdIieSVm9pEjSa4-yYBR7UIVTipU8Fi-tWcosm4b2b4tquZWw3YjXxuNJWFJL416SPSZOEt61GVWqMCRmlobtOUCW5bFbJQGhRGF0tPM2FGYQjsISpSiguoZDcGDjxMKDb75JmTszG1I4Q6Dp78ApHYRLOwZk8hXCgH84e-5i2y6UooOpU4faS1lriSzRYAM51_zBLYdzjPm2o4K5zpxp8hHNbC7PICo2kk_Pk507IEnU0m2Ldtch4WUahFVYi8RoNmqZ-pXN4CCXPUYbwMYNwkBT66MdfQ_k1mJrApvPya7jvQ-Qp0Aqt4xzNvB-P1rbIau4O-SBKdBDlOHRggSVpoTopP0xzooJogBKm52ixjGbw7h0cxv91bohms9fangAP0cR2VHDm375wyz33MWv_mptG22k17pCzJG1LeOLk0KnlCnixfKk5hIfy_ejh9XEsR6XoL5gHbxwSare7i4Nnw5xO4Ku6cOHpUgSdjOKp320avO_P5KohuY5bgQNTZR_9U3LelFs_5rgt6_fZN9wA7320TOh9VIYyaGMmdcccn9dmSzpXjVplowVZsNu9it7vWTC9xstkCF8mbXbAJ45U4uKwGYTgVP1hLBGK3OPMAR3KjvOodaPNGwG62fjZ2Ev1m5iB5RS1O_PtjfpGm1KDaQx1Zt8ALV9BvgXn99iHvNmFeyPR4uFslDtxBbOjKtREUwolZ9AHdgiAjbD1tFpVHpY6h00tinrANp6VxjUXynH0BOhQD3yEKmRXjgR-9kSwnwf0VX_iK_ez4CEYuqtnQG4dXw0WnRZ2u8j9pzx43P8P" rel="noopener noreferrer"&gt;Reproduce the forgotten field in the Symfony playground&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The session expired, or does not follow
&lt;/h3&gt;

&lt;p&gt;For a classic token, stored in the session (a project older than Symfony 7.2, or an id not in &lt;code&gt;stateless_token_ids&lt;/code&gt;), this is the most frequent cause, and the only one that is not a bug. The user opens the form, goes to lunch, comes back, submits. Meanwhile, PHP has deleted the session, whose lifetime had run out, and the token received has nothing left to be compared with. The variants, on the other hand, are configuration bugs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;the session cookie does not arrive&lt;/strong&gt; : wrong cookie domain, &lt;code&gt;Secure&lt;/code&gt; cookie on a page served over &lt;code&gt;http&lt;/code&gt;, cookie blocked by the browser;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;several servers do not share their sessions&lt;/strong&gt; : the page comes from server A, the submit reaches server B, which does not know that session. Store sessions in shared storage (database, Redis);&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;the page is served from a cache&lt;/strong&gt; : a CDN or reverse proxy set to cache everything, ignoring the &lt;code&gt;Cache-Control: private&lt;/code&gt; that Symfony sets as soon as a session is used, hands the same page, so the same token, to every visitor. Stateless tokens do not have this problem: the same page can be cached.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the expiry itself, there is nothing to "fix", but a lot to polish. The Form component keeps what was typed and renders the form with a fresh token: a second submit goes through. Replace the technical message with a sentence the user understands, along the lines of "Your session expired while you were typing. Your text is kept: check it and submit the form again." And if your users fill in long forms, extend the session lifetime (&lt;code&gt;framework.session.gc_maxlifetime&lt;/code&gt;, and &lt;code&gt;cookie_lifetime&lt;/code&gt; if the cookie expires first) rather than removing the token.&lt;/p&gt;

&lt;p&gt;▶ &lt;a href="https://apprendre.forelse.fr/en/playground/symfony?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=invalid-csrf-token-symfony#code=nVfbbuM2EP2VgZpA9sK2sK9ax0V2u4s-FcUmaFFEQUBJtEVYJlWSWscw8u89JCX5ur0ZQSyRwxnyzJkz9D76FqXvJ9FS1NxE6T4yukg-KWm1qmuuk_uiaLmoDyOzpmqiNJr_iO9MZlKyDTcNKzjdN02WHQw_uNnWcHrYbZZK7rLsYyvLmmfZF401W6XX_cBhUZbd58ZqVtgTRyduPqlNoySXFp6U3mTZ51fLpRFKujkNf4-7Bv8f2nwjrHv-vx4e-es_rf_Z2uaLwjmY9cu_8j9bbux_WmEwZf4uyFfVWiFXAMdaLfLW8jDGPcbJu3eZpHf0iWF9SrbiZLhxpyH-2gjNS8q53XIuqRSmqdkOvrzZEocnJksyHioXg4SdOW_e42MlTDCCZ0OMiprBcUFWrbmckLHKeRfyOGgKF4ZESVnUtHktTDVl2ooCiY4I_qSyhHORWvoYfqVlloN_Jjg2NBpGXvzIiyjNeEaPolj7Bbl6hSkZsWlrGPqxupVFRbnmbO1B8N57IBCXbxorsFstVpUFIjhXWFhUvFhPPA7u1QckzQsuvsG8Yn7LlcOm5kvr4ubBeYEMMQfAVtjqgNp9OG1KFTJt0iRhTaO5LDWfuZi1wbdOuEzyWq0SIb-xWpTTwujl1Meemo4CcJZkcikkqwPydFGNyDCYW2LmomwyuYcHfH548lwZxUk8IVeuKcUsOIrHz8HGZ6qgJTC0Hi9Z8tdRR2a60eFhnFLP1rCsi-A-Ykmj3m66GB5W3H5UquZMjuI-XS-elrt4PD524D5JAs4d6LtiOmcrZMgdqADZAAM8AfG2oW0FyQoZY7mj0A7jquEgoFRdFjuCMrnb4GF2GuzmeJMPIeRoPF24GHo0_nCwfnNlNizzBXFHN-CEgTUIZ7mTkY-tqEuuR7Kt6wk9xS6jA31jultQfFYQ8fP4dEvTBSvLUWyFxeSEev1JU59-57RmOa-Ds45nFKy_46qLCGcHObzu7tfO8tIR4HHn85Cc4TBdVMxpeMeUgQFnpt0ukIo7cvAcYXvTJftzJ1V3tGS1V8MTZoVgwoRTWF6OLtlzYvibq6orRhf76VbgkD3wY__2E7PshAWeCeTq95rPjrqeHGvOGwOCMktbyIcF5qXXFygGdDRnkDGnGVDUpeamCmxN8Wp4oQY9ppWCta20alfV7GrI3zmUCpJedJXAi0qKAnqBOMaVTgjjO0HNpG853hKCrglNiGtUDgTkivvLzFjd8nNAvlMlmttWy75InPyhMnrZmVV2U8_sVqzAy6dTh7FD0DPSJ2ZyNjtkLpgMr-d23d5fug4YrE8PdLTkuc8zzoC_aBJZtAunVSa52DNuP_vbQXjjnEHQDweiW48DLCDvrl05SmGwR7Hbg_Mgy2BytiJX5c6PuQ3Nq_eLX_iWOsWYJ3jvccYKUP7A5dujZMybxRwdQcnVoteJLNrvj6zf3tCPh9fZPOnM50mzGPxjjwhx-3Ya8gzc7wX-Q7X64ioSZHunWtpy14B3DTrrjLytBaiuVa95Y9PQl3GZCJVTu97faRSxFRPy325574vyBTzXduQex-7sw4b7ea22fnbmM3ZqM-_9DwNeOBeXRTMXsmmtL_m7LPJHwGUFSLvGi5GzHogJtP_WzbzPoiv-HvorDju-4NDo8pZnju80QUDOlHyenO_6ANxVJDp2HLDoLYDxEZLnXI7eJhF-EayhP_hJ8RR195nErUBp9a_QulYLu5vm_mdA9Pz2Fw" rel="noopener noreferrer"&gt;Reproduce the expired session in the Symfony playground&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The token id differs
&lt;/h3&gt;

&lt;p&gt;For a hand-written form, the token is built in Twig and checked in the controller, each with an id:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight twig"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"_token"&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"{{ csrf_token('delete-article-' ~ article.id) }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;


if (!$this-&amp;gt;isCsrfTokenValid('delete_article_'.$id, $request-&amp;gt;request-&amp;gt;getString('_token'))) {
    // refused every time
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hyphens on one side, underscores on the other: they are not the same tokens, and the check fails on every submit, even on a brand new session. The typical case is a rename done on one side only. &lt;strong&gt;Fix&lt;/strong&gt; : the same id on both sides. The &lt;code&gt;#[IsCsrfTokenValid(new Expression('"delete-article-" ~ args["id"]'))]&lt;/code&gt; attribute on the controller does not guarantee that both ids match, but it puts the controller's id in plain sight, right above the method.&lt;/p&gt;

&lt;p&gt;▶ &lt;a href="https://apprendre.forelse.fr/en/playground/symfony?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=invalid-csrf-token-symfony#code=rVbbbuM2EP2VqTaB5EA2sYt9UhwXaRZB-1C0SIJ9CY1AEkcWa5lSSSqOYbjf3qEo-RKjARaoYMDikHPmzPBwqG3wGiSf46CQFZog2QZG5-yuVlbXVYWa3eZ5i7I6WCZN2QRJMP2Z_rniSqUrNE2aI9w2DeeHhddutjUIj5tVUasN57-0SlTI-b0mn3Wtl4Ph4MT5bWasTnN7AnQCc1evmlqhspz_am1zXxNKamWtOH_Av1s09oc8DE0Z_MDloW6tVAuiZq2WWWvR27DLkF1dcQVXcJeSfwK2RLD1EhVIAUIWBWoDGdo1kqmbxFVTpRYhVaIz5IfSOiAPVsl8CTz4hhVSuOAE14DGvzC3KABfUW_AyhXG7l1BrSCFTDtshWswaAyleQC-1VbmFfEsqQ4mYSxtGo1KaJwUtcbK0L9mqFhW1Qsm1WtaSTHOjS7GXfSx6QtEYIyrQqq0grxKjYEzpQC-WYKmmbMt5WpLCPR8eu4qGYUsjMFJKYEw9UDhaO7XNG1G5YCiVbnbM5BK4Fs0SmDYO7-sR3SPRttqBRe2lGY8c_mhjgbcSWlX1cSu5YJCPoepr0gINzMaSdG9fP0SQ2il7e3h7xsopDYWhtVzmifdm3ThV6i2quaja09h53Rxml3vx7ZS7JjodvWQsB-_DNgxrNCWtTAJEfrzj8encP5fpfCekVQWLqSIodc_XGj_8lGRGIOnYz1mrazskc7W0pYDt3HPbfz1SwhRuWlKVGYUn4CdahnyEvOleZ_cSwfQug0xOQnOEENpnaZV7YMbqolnMDmBv5dvyWEBHS5KP6uJopECTQwkXxJcSsngGetwQtU5gpMFRD_16pDmjtT95AJ-d2KP3jPunON9SZ2e-pcF2kfqCGoRhS8d43A0Gh2X2D0XvUzgBsLf_HmCu8eHe59jMiiqy99HFpPw-oCxA3cqP0Dl3DREwhZR2J9uuBQHqNgpY3QMOKjzfzkpXW1-6KgM1I-PC_2COBikaNgZAbpxtpf7hhJmKTWqAzu47JKiFdS2qHF2dMj4reu_1MT3LdmBKOFXvXPKarHpbI7TtPw8o0z6HMyU0XioG3mQgIYNGDw6r2Y2pVZXq8Vsu92v2O2mrLdOWTPbgxARwrnc7wd5k1cfcuJzcL6Dy5Qkvup7ww0PmtpYHkDadQIak2-T2jI6byfUdPY6m1ADGhEsD2ZHvKVqWirSpkECKqUQqAjatScae23TmMTboo_kbgRvj84OG_xzFOw8Fl2gls5uH8y02UpSHjN_1U2Znx5SZi7nWbdLJ_sW7OKAPjqWVF_6ankO-muJGcxbLe1mnHWfFsF89y8" rel="noopener noreferrer"&gt;Reproduce the different id in the Symfony playground&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The stateless token does not recognise the origin
&lt;/h3&gt;

&lt;p&gt;With a stateless token, Symfony compares the &lt;code&gt;Origin&lt;/code&gt; header (or, failing that, &lt;code&gt;Referer&lt;/code&gt;) sent by the browser with the address it thinks it serves. Behind a reverse proxy, a load balancer or anything else that terminates HTTPS, the application receives the request over &lt;code&gt;http&lt;/code&gt;: it thinks it is on &lt;code&gt;http://example.com&lt;/code&gt;, the browser announces &lt;code&gt;https://example.com&lt;/code&gt;, and every submit is refused. The logs say &lt;code&gt;CSRF validation failed: origin info doesn't match.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix&lt;/strong&gt; : declare the proxy, so that Symfony reads the &lt;code&gt;X-Forwarded-*&lt;/code&gt; headers it sends.&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="c1"&gt;# config/packages/framework.yaml
&lt;/span&gt;&lt;span class="na"&gt;framework&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;trusted_proxies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'%env(TRUSTED_PROXIES)%'&lt;/span&gt; &lt;span class="c1"&gt;# for example 127.0.0.1,REMOTE_ADDR behind a single proxy
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;X-Forwarded-For&lt;/code&gt;, &lt;code&gt;-Proto&lt;/code&gt; and &lt;code&gt;-Port&lt;/code&gt; headers are read by default as soon as &lt;code&gt;trusted_proxies&lt;/code&gt; is set. Add &lt;code&gt;x-forwarded-host&lt;/code&gt; to &lt;code&gt;trusted_headers&lt;/code&gt; if the proxy also rewrites the host.&lt;/p&gt;

&lt;p&gt;This setting is not only for the token: without it, the absolute URLs Symfony generates also go out over &lt;code&gt;http&lt;/code&gt;, and every IP address in the logs is the proxy's.&lt;/p&gt;

&lt;p&gt;The other possible reason is &lt;code&gt;CSRF validation failed: double-submit and origin info not found.&lt;/code&gt;: the request had neither &lt;code&gt;Origin&lt;/code&gt; nor &lt;code&gt;Referer&lt;/code&gt;, for example because a browser extension or a corporate proxy strips them. Symfony then falls back on a cookie set by the Stimulus controller &lt;code&gt;csrf_protection_controller.js&lt;/code&gt;, which the &lt;code&gt;symfony/stimulus-bundle&lt;/code&gt; recipe installs in &lt;code&gt;assets/controllers/&lt;/code&gt;. If that JavaScript is not loaded on the page, the fallback does not work: check that the controller is registered, or give this form a classic, session-stored token again, with the &lt;code&gt;'csrf_token_id' =&amp;gt; 'my_form'&lt;/code&gt; option in &lt;code&gt;configureOptions()&lt;/code&gt; (an id that is not in &lt;code&gt;stateless_token_ids&lt;/code&gt;). The fallback cookie's name also depends on the protocol (&lt;code&gt;__Host-&lt;/code&gt; prefix over HTTPS): behind an undeclared proxy, it fails too.&lt;/p&gt;

&lt;h2&gt;
  
  
  When going without the token is legitimate
&lt;/h2&gt;

&lt;p&gt;There are cases where the &lt;code&gt;csrf_protection: false&lt;/code&gt; option is the right answer, and they all have one thing in common: the request is not authenticated by a cookie the browser attaches on its own.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;An API authenticated by a header&lt;/strong&gt; &lt;code&gt;Authorization: Bearer …&lt;/code&gt;. Another site cannot make the victim's browser add that header. A Symfony form used to validate that API's JSON can do without a token.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A webhook&lt;/strong&gt; (Stripe, GitHub…). It has no session, and it has its own proof: a signature, to check every time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the other hand, an API called by your own pages in JavaScript, with the session cookie, remains exposed: it needs the token, sent in a header that the controller checks.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I take away
&lt;/h2&gt;

&lt;p&gt;The CSRF error is a symptom, almost always of something else: a template, a session, a proxy. Disabling it hides the symptom and removes a protection, without fixing anything. The profiler's three questions take five minutes and give the cause in most cases.&lt;/p&gt;

&lt;p&gt;To practise, the &lt;a href="https://apprendre.forelse.fr/en/concepts/csrf?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=invalid-csrf-token-symfony" rel="noopener noreferrer"&gt;CSRF concept&lt;/a&gt; gathers the exercises that handle the token, from the form generated by the Form component to the hand-written form, with a token of its own for each deleted item.&lt;/p&gt;

</description>
      <category>php</category>
      <category>symfony</category>
      <category>security</category>
      <category>webdev</category>
    </item>
    <item>
      <title>My analytics counted 9 bots for every visitor: how I got them out of Umami</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Mon, 16 Nov 2026 08:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/my-analytics-counted-9-bots-for-every-visitor-how-i-got-them-out-of-umami-3ek4</link>
      <guid>https://dev.to/oipnet/my-analytics-counted-9-bots-for-every-visitor-how-i-got-them-out-of-umami-3ek4</guid>
      <description>&lt;p&gt;On October 30, Umami reported 212 visits on apprendre.forelse.fr. A good day, on the face of it. Looking closer, 203 of those visits came from bots. That left 9 people.&lt;/p&gt;

&lt;p&gt;To measure the site's audience, I use &lt;a href="https://umami.is" rel="noopener noreferrer"&gt;Umami&lt;/a&gt;, self-hosted on my own server: no cookies, no IP addresses kept, nothing sent to a third party. But numbers that are 95% wrong are worse than no numbers at all. I was making decisions based on them: which article worked, which exercise gets used, whether a LinkedIn post brings anyone in.&lt;/p&gt;

&lt;p&gt;Here is how I got rid of them, in three steps, and what it costs.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Umami already does
&lt;/h2&gt;

&lt;p&gt;Umami ignores visits whose User-Agent says it is a bot (&lt;code&gt;Googlebot&lt;/code&gt;, &lt;code&gt;bingbot&lt;/code&gt;, &lt;code&gt;LinkedInBot&lt;/code&gt;…), using the &lt;code&gt;isbot&lt;/code&gt; library. That takes care of the polite bots.&lt;/p&gt;

&lt;p&gt;The problem is the others. The ones that run a real Chrome, execute the page's JavaScript, and therefore send a page view like any visitor would. To Umami, they look exactly like a human.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: robots.txt
&lt;/h2&gt;

&lt;p&gt;The site's tracker is served under &lt;code&gt;/mesure/&lt;/code&gt;, and &lt;code&gt;robots.txt&lt;/code&gt; contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight apache"&gt;&lt;code&gt;&lt;span class="nc"&gt;Disallow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;mesure&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's the bare minimum, and it only helps with bots that read &lt;code&gt;robots.txt&lt;/code&gt;. The ones causing me trouble don't: one of them followed the "Log in" link on every page, even though &lt;code&gt;/connexion&lt;/code&gt; is disallowed there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: automated browsers (&lt;code&gt;navigator.webdriver&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;The first bot I spotted was the one that follows LinkedIn. With every post, within a second of &lt;code&gt;LinkedInBot&lt;/code&gt; passing by, a "Chrome on Windows" opened the page, then the links on the page. It counted as visits and, worse, as failed starts of the PHP playground: it left before PHP had finished loading. I thought I had a bug.&lt;/p&gt;

&lt;p&gt;The server logs settled it. Some User-Agents were simply impossible: Chrome 140 on Windows 7, when Chrome hasn't shipped for Windows 7 since version 109.&lt;/p&gt;

&lt;p&gt;A browser driven by Selenium, Playwright or Puppeteer usually exposes &lt;code&gt;navigator.webdriver === true&lt;/code&gt;. So the site doesn't load the tracker in that case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;webdriver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// no tracker for an automated browser&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next day, no more failed starts after a LinkedIn post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: wait for a gesture from the visitor
&lt;/h2&gt;

&lt;p&gt;The biggest one was still there: a bot that came every night, always with the same fingerprint. Chrome, Linux, a 1920x1080 screen, en-US, addresses in several countries. 163 visits in five days. Its User-Agent claimed "Chrome/155", a version that doesn't exist yet. And it doesn't set &lt;code&gt;navigator.webdriver&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My first idea was to filter out that fingerprint. But fingerprints change. And 1920x1080 on Linux in English is also the screen of a lot of developers.&lt;/p&gt;

&lt;p&gt;Looking at its visits, one thing stood out: &lt;strong&gt;one page, no events, ever&lt;/strong&gt;. It renders the page and leaves. It doesn't move the mouse, touch the screen, turn the wheel or press a key.&lt;/p&gt;

&lt;p&gt;So the site waits for that first gesture before loading the tracker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;GESTURES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'pointerdown'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'pointermove'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'touchstart'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'wheel'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'keydown'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;load&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="no"&gt;GESTURES&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gesture&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gesture&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Umami's tracker, and the page view with it&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="no"&gt;GESTURES&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gesture&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gesture&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;capture&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;passive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&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;A few details matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No &lt;code&gt;scroll&lt;/code&gt;&lt;/strong&gt; in the list. A page can scroll on its own, with nobody there: the code editor, or the browser restoring the position. A bot would then have loaded the tracker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The gesture isn't sent.&lt;/strong&gt; It's only a trigger: Umami receives exactly the same data as before, nothing more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Events wait for the tracker.&lt;/strong&gt; An event fired before the first gesture ("exercise opened", "account created" right after sign-up) is queued, then sent as soon as the tracker has loaded. Without that, those events were lost.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The result
&lt;/h2&gt;

&lt;p&gt;The change has been in production since November 3. Since November 4, I haven't found a single bot in Umami. No more SQL queries excluding fingerprints by hand: the raw numbers are the real ones.&lt;/p&gt;

&lt;p&gt;They're a lot more modest: a few people a day when I don't publish anything, more on article days. Less flattering, but it's exactly what I wanted to know.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs
&lt;/h2&gt;

&lt;p&gt;Nothing comes for free:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A visitor who arrives, reads without touching anything and leaves isn't counted. On a computer, that's rare: the mouse almost always moves. On mobile, the first swipe fires &lt;code&gt;touchstart&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A bot that fakes mouse movements will get through. This isn't a protection, it's a filter against lazy bots, which is almost all of the ones that came here.&lt;/li&gt;
&lt;li&gt;The page view is sent a little later. For Umami, that changes nothing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a site that wants to know how many real people read its articles, it's a good trade in my view. For a shop that needs to count every visit, I'd think twice.&lt;/p&gt;

&lt;p&gt;The full code lives in the site's engine, which is open source: a 75-line file, &lt;code&gt;playground/src/mesure.ts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What about you: do you filter bots out of your analytics, or do you live with them?&lt;/p&gt;

</description>
      <category>behindthescenes</category>
      <category>analytics</category>
      <category>selfhosted</category>
      <category>webdev</category>
    </item>
    <item>
      <title>I brought the tool from my first job back to life, in a browser</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Mon, 09 Nov 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/i-brought-the-tool-from-my-first-job-back-to-life-in-a-browser-dn9</link>
      <guid>https://dev.to/oipnet/i-brought-the-tool-from-my-first-job-back-to-life-in-a-browser-dn9</guid>
      <description>&lt;p&gt;In 2005, I was 20 and starting my first job as a developer. It looked nothing like what I do today. No framework, no Git, no browser. A green-screen emulator on a PC, 80 columns by 24 lines, a keyboard with PF keys, and a COBOL generator: PACBASE.&lt;/p&gt;

&lt;p&gt;We didn't type COBOL, or hardly any. We described things. Data items first, in a dictionary shared by the whole team: a code, a label, a format. Then records, built from those items. Then the processing, line by line, in a simplified language organised into numbered functions. The generator did the rest, and the rest was hundreds of lines of COBOL that we almost never read.&lt;/p&gt;

&lt;p&gt;What stayed with me is that green screen. Glowing letters on a black background, a blinking cursor, and nothing else.&lt;/p&gt;

&lt;p&gt;And it hasn't completely gone away. Look over a sales assistant's shoulder in some big retail chains, when they look up a product or place an order: there's a good chance you'll see the same screen. Somewhere behind it, COBOL is still running.&lt;/p&gt;

&lt;p&gt;This weekend, I wanted to find out whether I could bring that way of working back to life. Not to write COBOL again: out of curiosity, and a bit of nostalgia.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the workshop does
&lt;/h2&gt;

&lt;p&gt;The COBOL Workshop is not a copy of PACBASE (neither its name nor its screens). It's an original generator, inspired by its approach, that runs entirely in the browser:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;data dictionary&lt;/strong&gt; (code, label, &lt;code&gt;PIC&lt;/code&gt; format);&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;records&lt;/strong&gt; built from those items, and control breaks on up to three levels;&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;structured language&lt;/strong&gt; : you write lines grouped by function and sub-function, with a handful of operators (M to move, A to add, C to compute, MES to display, ECR to write a file, SQL for the database);&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;batch skeleton&lt;/strong&gt; generated around it: initialisation, reading (with read-ahead), control break detection, end of processing, report printing, in old-school numbered functions;&lt;/li&gt;
&lt;li&gt;a DB2-style &lt;strong&gt;database&lt;/strong&gt; , with &lt;code&gt;EXEC SQL&lt;/code&gt; and an SQLCODE (0, 100, -803…) just like back then;&lt;/li&gt;
&lt;li&gt;and a &lt;strong&gt;3270 terminal mode&lt;/strong&gt; : green screen, keyboard only, Tab, PF keys, keyboard lock and Esc to reset.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You describe, you generate, you read the COBOL it produces (fixed format, columns 8 and 12, as it should be), and you run it. The SYSOUT is displayed, along with any output files.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you run COBOL in a browser?
&lt;/h2&gt;

&lt;p&gt;My first idea was to do what this site does for PHP: compile the real compiler to WebAssembly. For PHP, that works very well. For COBOL, it's another story: GnuCOBOL translates COBOL into C, and then you need a C compiler. Shipping both in a web page adds up to tens of megabytes.&lt;/p&gt;

&lt;p&gt;So the workshop contains a &lt;strong&gt;COBOL interpreter written in JavaScript&lt;/strong&gt; (a little over a thousand lines, no dependencies). It only covers a subset of the language: sequential files, &lt;code&gt;SORT&lt;/code&gt;, &lt;code&gt;PERFORM&lt;/code&gt;, &lt;code&gt;COMPUTE&lt;/code&gt;, &lt;code&gt;PIC&lt;/code&gt; clauses and their editing symbols (&lt;code&gt;Z&lt;/code&gt;, &lt;code&gt;9&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;.&lt;/code&gt;)… But it really reads the source: you can edit the generated COBOL by hand and run it again.&lt;/p&gt;

&lt;p&gt;The trap with a home-made interpreter is fooling yourself. So every test program without a database is also compiled with the real GnuCOBOL, and the two outputs are compared line by line. On every test case (multi-level control breaks, negative numbers, three decimal places, filters…), they are identical.&lt;/p&gt;

&lt;p&gt;The database is SQLite compiled to JavaScript (sql.js). The interpreter turns the host variables (&lt;code&gt;:NAME&lt;/code&gt;) in &lt;code&gt;EXEC SQL&lt;/code&gt; statements into bound parameters and returns DB2's SQLCODEs. There's even an interactive query screen, for those who remember SPUFI.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it reminded me of
&lt;/h2&gt;

&lt;p&gt;Seeing those screens again mostly reminded me of an idea we've somewhat forgotten: &lt;strong&gt;describe rather than write&lt;/strong&gt;. A shared data dictionary, an imposed skeleton, custom code only where it's needed. Twenty years later, we find it again in our code generators (Symfony's makers, for example), and now in the code AI writes for us. But that's another article.&lt;/p&gt;

&lt;p&gt;And I have to admit I had forgotten a lot. The keystrokes, where control breaks were declared… I had to reopen the old manuals to remember. What I did remember very well was the order of the functions: initialisation at the start, the read loop in the middle, end of processing last.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your turn
&lt;/h2&gt;

&lt;p&gt;The workshop is online, no account, nothing to install: &lt;a href="https://apprendre.forelse.fr/en/cobol-workshop?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=cobol-workshop-in-the-browser" rel="noopener noreferrer"&gt;apprendre.forelse.fr/en/cobol-workshop&lt;/a&gt; (the terminal mode, green screen included, is at &lt;a href="https://apprendre.forelse.fr/en/cobol-workshop/terminal?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=cobol-workshop-in-the-browser" rel="noopener noreferrer"&gt;/en/cobol-workshop/terminal&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;And for those who want to get their hands dirty, here's &lt;strong&gt;the first of the old-timer's challenges&lt;/strong&gt; : the sales report. It's 2005, and the sales director wants his report on his desk by 8 a.m. tomorrow: sales grouped by salesperson, a subtotal and a sales count for each, then the grand total. Zero-value sales don't count. The file and the dictionary are ready; only the processing is left: &lt;a href="https://apprendre.forelse.fr/en/cobol-workshop?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=cobol-workshop-in-the-browser#defi=01" rel="noopener noreferrer"&gt;open challenge #1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Never used this kind of tool? The &lt;a href="https://apprendre.forelse.fr/en/cobol-workshop/memo?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=cobol-workshop-in-the-browser" rel="noopener noreferrer"&gt;cheat sheet&lt;/a&gt; explains the screens, functions and control breaks in ten minutes. No hints, though: it's a challenge.&lt;/p&gt;

&lt;p&gt;The workshop checks your result. When it says "Challenge passed", click "Send my solution": it comes straight to me.&lt;/p&gt;

&lt;p&gt;And if you've worked on green screens yourself, tell me the memory that has stuck with you the most.&lt;/p&gt;

&lt;p&gt;One last question: would you be interested in a course for learning COBOL this way, by describing rather than typing? Let me know in the comments, or through the &lt;a href="https://apprendre.forelse.fr/en/contact?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=cobol-workshop-in-the-browser" rel="noopener noreferrer"&gt;contact form&lt;/a&gt;. If enough of you are, I'll get to work on it.&lt;/p&gt;

</description>
      <category>behindthescenes</category>
      <category>cobol</category>
      <category>mainframe</category>
      <category>nostalgia</category>
    </item>
    <item>
      <title>An MCP server with Symfony AI, so your AI assistant can create PHP playgrounds</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Tue, 03 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/an-mcp-server-with-symfony-ai-so-your-ai-assistant-can-create-php-playgrounds-1icj</link>
      <guid>https://dev.to/oipnet/an-mcp-server-with-symfony-ai-so-your-ai-assistant-can-create-php-playgrounds-1icj</guid>
      <description>&lt;p&gt;Ask an AI assistant "show me how to validate a form with a FormRequest", and it answers with three blocks of code to paste into a project you may not have at hand. You read them, you nod, and you never try them.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://apprendre.forelse.fr/en/playground?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=mcp-server-with-symfony-ai" rel="noopener noreferrer"&gt;playground&lt;/a&gt; on apprendre.forelse.fr already runs a Symfony, Laravel or plain PHP project in the browser, and shares it with a simple link. One thing was missing: letting the AI build that link itself. That is what the &lt;strong&gt;Model Context Protocol&lt;/strong&gt; (MCP) is for: it is the protocol that gives tools to assistants. So I added an MCP server to the site, in Symfony, with the &lt;a href="https://symfony.com/doc/current/ai/index.html" rel="noopener noreferrer"&gt;Symfony AI&lt;/a&gt; bundle.&lt;/p&gt;

&lt;p&gt;The result: you ask Claude "create a Symfony playground with a &lt;code&gt;/hello/{name}&lt;/code&gt; controller that answers in JSON", and it answers with a link. One click on "Run", and the code runs.&lt;/p&gt;

&lt;p&gt;And the case I care about most: a bug in your own project. From Claude Code or Cursor, the AI has your code at hand. It isolates what goes wrong, the controller, the service and the faulty line in &lt;code&gt;services.yaml&lt;/code&gt;, and turns it into a playground that reproduces the bug. The link goes into an issue or on Slack, and whoever helps you sees the problem running instead of guessing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the server does, and what it doesn't
&lt;/h2&gt;

&lt;p&gt;A playground link already carries all the code, after the &lt;code&gt;#&lt;/code&gt;: the changed files, as JSON, compressed with deflate and encoded in base64url. I described this format in &lt;a href="https://apprendre.forelse.fr/en/blog/sharing-a-php-project-in-a-url?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=mcp-server-with-symfony-ai" rel="noopener noreferrer"&gt;Sharing a Symfony project in a URL&lt;/a&gt;. The site also offers short links, kept on the server when you tick the box while sharing, but the MCP server always builds the long link: it stores nothing.&lt;/p&gt;

&lt;p&gt;An AI can't compress deflate in its head. So the MCP server does that computation for it, and nothing more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;get_starter&lt;/code&gt; returns the starting project: its files, the editable paths, the configuration (on request, since Laravel's weighs 45 KB) and the Composer packages you can add (for Symfony, each comes with its Flex recipe);&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;create_sandbox&lt;/code&gt; takes the files the AI wrote and returns the link;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;read_sandbox&lt;/code&gt; does the opposite: give the AI a link, and it reads the code to explain or fix it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nothing runs on the server, and nothing is saved. The code then runs in the person's browser, in WebAssembly, and only after they click, as for any shared link.&lt;/p&gt;

&lt;h2&gt;
  
  
  symfony/mcp-bundle: a tool is an attribute
&lt;/h2&gt;

&lt;p&gt;Symfony AI provides &lt;code&gt;symfony/mcp-bundle&lt;/code&gt;, built on the &lt;a href="https://github.com/modelcontextprotocol/php-sdk" rel="noopener noreferrer"&gt;official MCP PHP SDK&lt;/a&gt; (&lt;code&gt;mcp/sdk&lt;/code&gt;), developed with the PHP Foundation. It installs with a Flex recipe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require symfony/mcp-bundle

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The recipe adds the configuration file and the routes import. In it, I declare a server, the namespace where its tools live, and its path: &lt;code&gt;/mcp/&amp;lt;server name&amp;gt;&lt;/code&gt; by default, which I set to &lt;code&gt;/mcp&lt;/code&gt;:&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;mcp&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;servers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;playground&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;php-playground'&lt;/span&gt;
            &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1.0.0'&lt;/span&gt;
            &lt;span class="na"&gt;instructions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
                &lt;span class="s"&gt;Creates links to an online PHP playground (Symfony 8, Laravel 13, or plain PHP 8.5)...&lt;/span&gt;
                &lt;span class="s"&gt;Call get_starter for the starting project, then create_sandbox with only the files&lt;/span&gt;
                &lt;span class="s"&gt;you change, and give the user the link.&lt;/span&gt;
            &lt;span class="na"&gt;registry&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                &lt;span class="na"&gt;tools&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;App\Mcp\'&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
            &lt;span class="na"&gt;http&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/mcp&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A tool, then, is a service method with the &lt;code&gt;#[McpTool]&lt;/code&gt; attribute. The SDK generates the JSON schema of the parameters from the PHP signature:&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="na"&gt;#[McpTool(name: 'create_sandbox', annotations: new ToolAnnotations(readOnlyHint: true, idempotentHint: true))]&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;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'The playground: "symfony", "laravel" or "php" (no framework).'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="nc"&gt;ScratchFramework&lt;/span&gt; &lt;span class="nv"&gt;$framework&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;definition&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'type'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'object'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'description'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'The files to change or add, by path, with their full content.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'additionalProperties'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'type'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'string'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;])]&lt;/span&gt;&lt;span class="k"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$files&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Language of the page: "fr" for a French-speaking user, else "en".'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enum&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'fr'&lt;/span&gt;&lt;span class="p"&gt;])]&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$language&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// $packages and the RequestContext left out): array {&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'url'&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$framework&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$language&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="s1"&gt;'#'&lt;/span&gt;&lt;span class="mf"&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="n"&gt;links&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$changed&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;Two details saved me time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A PHP enum becomes a JSON &lt;code&gt;enum&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;ScratchFramework&lt;/code&gt; is a string-backed enum (&lt;code&gt;symfony&lt;/code&gt;, &lt;code&gt;laravel&lt;/code&gt;, &lt;code&gt;php&lt;/code&gt;): the SDK takes the list of values for the schema, and converts the incoming argument before the call.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The PHPDoc comment becomes the description.&lt;/strong&gt; It is what the model reads to decide when to call the tool. So I wrote it for the model, saying when to use the tool and in which order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A PHP &lt;code&gt;array&lt;/code&gt;, on the other hand, doesn't say whether it is a list or an object. For &lt;code&gt;files&lt;/code&gt;, a "path to content" map, you have to give the full schema with &lt;code&gt;#[Schema(definition: ...)]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors written for the model
&lt;/h2&gt;

&lt;p&gt;The playground doesn't accept just any file. The code, the views and the configuration can be edited (&lt;code&gt;src/*/*.php&lt;/code&gt;, &lt;code&gt;templates/*.html.twig&lt;/code&gt;, &lt;code&gt;config/packages/*.yaml&lt;/code&gt;…), but not what depends on the packages: &lt;code&gt;composer.json&lt;/code&gt;, &lt;code&gt;config/bundles.php&lt;/code&gt; and &lt;code&gt;vendor/&lt;/code&gt; only change through the list of offered packages. If the AI sends &lt;code&gt;config/bundles.php&lt;/code&gt; directly, the page silently ignores the file.&lt;/p&gt;

&lt;p&gt;So the server rejects the call, with a &lt;code&gt;ToolCallException&lt;/code&gt;. The SDK turns it into an error result that the model reads, not a protocol error:&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="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;$refused&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ToolCallException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s1"&gt;'These paths cannot be edited in the %s playground: %s. Editable paths: %s ("\*" does not cross a "/").'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nv"&gt;$framework&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;label&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;implode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;', '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$refused&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;implode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;', '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$exercise&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;editable&lt;/span&gt;&lt;span class="p"&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;The message says what is wrong &lt;strong&gt;and&lt;/strong&gt; what is allowed. The model fixes its call on the next turn, without the user seeing anything. Same for Composer packages: an unknown package gets the catalog list back, and a package added to Laravel gets a clear answer, since that playground comes with its own packages.&lt;/p&gt;

&lt;p&gt;The server also drops from the link any file identical to the starting project. Models like to send back everything they were shown, and an unchanged file would only make the URL longer.&lt;/p&gt;

&lt;h2&gt;
  
  
  A public server with no account: the precautions
&lt;/h2&gt;

&lt;p&gt;The server has no accounts and no private data, but it is still an open endpoint. Four settings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A rate limit per IP address&lt;/strong&gt; , with Symfony's RateLimiter: 300 requests per hour, then a 429 response with &lt;code&gt;Retry-After&lt;/code&gt;. A conversation makes a few calls, not thousands.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The same limits as the page&lt;/strong&gt; : 100 files and 1 MB of code at most. A link the page would refuse is never built, and &lt;code&gt;read_sandbox&lt;/code&gt; reads links with the same caution (size before and after decompression, paths climbing up with &lt;code&gt;..&lt;/code&gt; rejected).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A firewall with security disabled on &lt;code&gt;/mcp&lt;/code&gt;&lt;/strong&gt; , so no platform session gets mixed in.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DNS rebinding protection turned off&lt;/strong&gt; (&lt;code&gt;allowed_hosts: false&lt;/code&gt;). By default, the SDK only accepts &lt;code&gt;localhost&lt;/code&gt;, &lt;code&gt;127.0.0.1&lt;/code&gt; and &lt;code&gt;[::1]&lt;/code&gt;, which suits a local MCP server. A public server can list its domains instead. I chose to turn it off because the site's engine runs on several instances, each with its own domain, and the server protects no data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A trap during testing: in PHP, an empty JSON object &lt;code&gt;{}&lt;/code&gt; and an empty list &lt;code&gt;[]&lt;/code&gt; become the same empty array. The SDK fixes this for &lt;code&gt;arguments&lt;/code&gt;, but not for a nested object: a &lt;code&gt;"files": {}&lt;/code&gt; was seen as a list, and the schema validator rejected the call before it even reached the tool. A real assistant always sends at least one file, but the test was sending &lt;code&gt;{}&lt;/code&gt;. In the other direction, the server casts &lt;code&gt;(object) $files&lt;/code&gt; before encoding the link, so the page always receives an object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measuring what an AI does with the server
&lt;/h2&gt;

&lt;p&gt;The site's analytics, Umami, only sees browsers. An AI assistant calls &lt;code&gt;/mcp&lt;/code&gt; from a server: Umami doesn't see it. So the MCP server sends an event to Umami itself on each tool call (&lt;code&gt;mcp-depart&lt;/code&gt;, &lt;code&gt;mcp-lien-cree&lt;/code&gt;, &lt;code&gt;mcp-lien-lu&lt;/code&gt;), with the framework, the language, the number of files and the name of the MCP client announced at connection (&lt;code&gt;claude-ai&lt;/code&gt;, &lt;code&gt;cursor-vscode&lt;/code&gt;…). The code itself is never sent. Sending waits one second at most, and an unreachable Umami never makes the tool fail.&lt;/p&gt;

&lt;p&gt;The links created also carry &lt;code&gt;utm_source=mcp&lt;/code&gt;. That way I'll know how many links assistants build, and how many people actually open them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing an MCP server like a client
&lt;/h2&gt;

&lt;p&gt;The functional test behaves like an assistant: a JSON-RPC handshake (&lt;code&gt;initialize&lt;/code&gt;), the list of tools, then a call. With Symfony's &lt;code&gt;KernelBrowser&lt;/code&gt;, it is a series of POST requests:&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;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'POST'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'/mcp'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'CONTENT_TYPE'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'application/json'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'HTTP_ACCEPT'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'application/json, text/event-stream'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'HTTP_MCP_SESSION_ID'&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="s1"&gt;'jsonrpc'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'2.0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'method'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'tools/call'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'params'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'create_sandbox'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'arguments'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'framework'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'php'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'files'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'public/index.php'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo 'Hello';"&lt;/span&gt;&lt;span class="p"&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;The response contains a &lt;code&gt;structuredContent&lt;/code&gt; with the URL. The test then decodes the fragment to check that it carries the file.&lt;/p&gt;

&lt;p&gt;What remains is making sure PHP and the page speak the same format. Two tests take care of it, one in each direction: on the PHP side, &lt;code&gt;ScratchLinkTest&lt;/code&gt; reads a fragment built by fflate, the page's library; on the JavaScript side, a test reads a fragment produced by &lt;code&gt;gzdeflate&lt;/code&gt;. Both produce the same raw deflate, and if either implementation changes, one of the two tests breaks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;The server lives at &lt;code&gt;https://apprendre.forelse.fr/mcp&lt;/code&gt;, with no account. In Claude Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;claude mcp add &lt;span class="nt"&gt;--transport&lt;/span&gt; http php-playground https://apprendre.forelse.fr/mcp

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Claude, ChatGPT, Cursor or VS Code, the page &lt;a href="https://apprendre.forelse.fr/en/playground/ai?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=mcp-server-with-symfony-ai" rel="noopener noreferrer"&gt;The playground from your AI assistant&lt;/a&gt; explains the steps. Then ask it for a playground, ask it to reproduce the bug you're stuck on, or give it a link a colleague sent you and ask why the route returns a 404.&lt;/p&gt;

&lt;p&gt;The bundle and the SDK are still experimental: they are not covered by Symfony's backward compatibility promise, and you'll have to follow their releases. But to expose a handful of tools, a few attributes on a service are enough, along with the firewall, the RateLimiter and the functional tests you already know.&lt;/p&gt;

</description>
      <category>symfony</category>
      <category>php</category>
      <category>mcp</category>
      <category>ai</category>
    </item>
    <item>
      <title>Encrypting columns with Symfony 8.2: what #[Encrypted] protects, and what it doesn't</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Fri, 30 Oct 2026 07:47:49 +0000</pubDate>
      <link>https://dev.to/oipnet/encrypting-columns-with-symfony-82-what-encrypted-protects-and-what-it-doesnt-5b3o</link>
      <guid>https://dev.to/oipnet/encrypting-columns-with-symfony-82-what-encrypted-protects-and-what-it-doesnt-5b3o</guid>
      <description>&lt;p&gt;Symfony 8.2 brings a new component, KeyManagement, still experimental. Its Doctrine bridge encrypts a column with an attribute, and an encrypted column can even stay searchable thanks to a blind index.&lt;/p&gt;

&lt;p&gt;It takes very little code. But encrypting a column is not a box to tick: you need to know what you are protecting against, accept what the database will no longer be able to do, and plan for the day you encrypt a column that already holds data. This article works through those questions, using a table of patients as the example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two attributes
&lt;/h2&gt;

&lt;p&gt;Encrypting a patient's phone number and date of birth comes down to two lines, one per column:&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\KeyManagement\Bridge\DoctrineOrm\Attribute\Encrypted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="na"&gt;#[ORM\Column(nullable: true)]&lt;/span&gt;
&lt;span class="na"&gt;#[Encrypted(key: 'patient.contact')]&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;?string&lt;/span&gt; &lt;span class="nv"&gt;$phone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="na"&gt;#[ORM\Column(type: Types::DATE\_IMMUTABLE, nullable: true)]&lt;/span&gt;
&lt;span class="na"&gt;#[Encrypted(key: 'patient.identity')]&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nc"&gt;\DateTimeImmutable&lt;/span&gt; &lt;span class="nv"&gt;$birthDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Doctrine writes an encrypted value to the database, and decrypts it when it reads the entity back. The property keeps its PHP type: the column's Doctrine type converts the value first (a date becomes text), then the result is encrypted. There is no Doctrine type to write and no listener to register: when the container is compiled, Symfony finds the marked properties and declares the types they need.&lt;/p&gt;

&lt;p&gt;Encryption is &lt;strong&gt;randomized&lt;/strong&gt; : the same value, encrypted twice, gives two different results. That is deliberate, otherwise comparing the column would be enough to tell which patients share a number. The flip side is that &lt;code&gt;WHERE email = ?&lt;/code&gt; no longer finds anything. To find a patient by their encrypted email address, you add a &lt;strong&gt;blind index&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[ORM&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nd"&gt;Column]&lt;/span&gt;
&lt;span class="nd"&gt;#[Encrypted(key:&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nd"&gt;patient&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="nd"&gt;contact&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="n"&gt;private&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[ORM&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nd"&gt;Column(length:&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="nd"&gt;#[BlindIndexed(&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nd"&gt;email&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nd"&gt;patient&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="nd"&gt;email&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="n"&gt;private&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;emailIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;emailIndex&lt;/code&gt; column holds a keyed digest of the address: an HMAC-SHA256, computed under a subkey of its own, derived from the index's key and its name, &lt;code&gt;patient-email&lt;/code&gt;. The same address always gives the same digest, but you need the key to compute it, and two indexes never give the same digest for the same value. A listener recomputes it on every &lt;code&gt;flush()&lt;/code&gt; that inserts or updates the entity, and the lookup compares digests:&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;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;findOneBy&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'emailIndex'&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$email&lt;/span&gt;&lt;span class="p"&gt;)]);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What it protects against
&lt;/h2&gt;

&lt;p&gt;Before encrypting anything, ask who you want to stop from reading. Column encryption protects against &lt;strong&gt;people who read the database without going through the application&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a backup left on poorly protected storage;&lt;/li&gt;
&lt;li&gt;a copy of the production database on a developer's laptop, or in a staging environment;&lt;/li&gt;
&lt;li&gt;an SQL injection that dumps the table: it gets encrypted bytes, not phone numbers;&lt;/li&gt;
&lt;li&gt;read access to the database granted for support or reporting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It does not protect against &lt;strong&gt;the application itself&lt;/strong&gt;. The application has access to the keys, since it decrypts every time it loads an entity, and holds them in memory for the duration of a request. Someone who takes over the application server, or gets it to run their code, reads the data in clear, just as the application does. And once decrypted, a value is a string like any other: it ends up in the logs if you log the entity, in Sentry with an exception's stack trace, in a cache, in a CSV export or in a search engine. Encryption stops at the database; vigilance has to go further.&lt;/p&gt;

&lt;p&gt;That leaves the question of the key. The component uses &lt;strong&gt;envelope encryption&lt;/strong&gt; : each value is encrypted with a data key, which is itself encrypted with a master key that stays in a KMS (AWS KMS, Google Cloud KMS, Vault…). Without any particular configuration, each value carries its own encrypted data key, and every read and every write calls the KMS. With a &lt;strong&gt;key store&lt;/strong&gt; (&lt;code&gt;key_management.store&lt;/code&gt;), the encrypted data keys are kept in a table of the database: all the values of a scope (&lt;code&gt;patient.contact&lt;/code&gt;) share one key, and the KMS is called once per key and per process. Without the master key, those stored keys are useless.&lt;/p&gt;

&lt;p&gt;So where the master key lives decides how much protection you actually get. If the master key sits in the &lt;code&gt;.env&lt;/code&gt; file, on the same server that also runs the database backups, someone who gets the whole server gets both. A local KMS with its key in the &lt;code&gt;.env&lt;/code&gt; file is fine for development; in production, the master key lives somewhere other than the database, and ideally never leaves the KMS.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the database can no longer do
&lt;/h2&gt;

&lt;p&gt;An encrypted column is an opaque binary column. The database can no longer do anything with it, and that goes beyond &lt;code&gt;WHERE&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No sorting, no partial search.&lt;/strong&gt; No &lt;code&gt;ORDER BY&lt;/code&gt;, no &lt;code&gt;LIKE&lt;/code&gt;, no date comparison. That is why, in our patient table, the name stays in clear: the list is sorted by name, in SQL. Only encrypt what needs to be, and keep in mind that an encrypted date of birth no longer lets you look for "patients born before 1950".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No uniqueness constraint.&lt;/strong&gt; Two identical addresses give two different encrypted values, so a &lt;code&gt;UNIQUE&lt;/code&gt; index on the encrypted column never rejects anything. Put it on the blind index column instead: two identical addresses have the same digest there, as far as the projection is concerned (see below).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No length.&lt;/strong&gt; The type declares an unbounded binary column (&lt;code&gt;LONGBLOB&lt;/code&gt;, &lt;code&gt;BYTEA&lt;/code&gt;…), whatever &lt;code&gt;length&lt;/code&gt; the mapping says. That &lt;code&gt;length&lt;/code&gt; describes the plaintext, and the encrypted result is longer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A value is not bound to its row.&lt;/strong&gt; The encryption does not know which row it is writing to. Someone who can write to the database can therefore copy one patient's encrypted phone number into the same column of another patient, and it will decrypt without error. If that risk matters to you, bind the value to its row yourself, for example by including the row's identifier in it and checking it after reading.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What the blind index reveals
&lt;/h2&gt;

&lt;p&gt;A blind index does not reveal the addresses, but it does reveal something encryption was hiding: &lt;strong&gt;which rows share the same value&lt;/strong&gt; , and how often each value occurs. For an email address, that is almost nothing, since nearly all addresses are different. For a column with few possible values, it is much worse: a blind index on country of birth or blood type shows the distribution at a glance, and knowing the value of a single row is enough to know the value of every row that carries the same digest. So only index varied values that are looked up by equality: an email address, a file number, a social security number.&lt;/p&gt;

&lt;p&gt;Two other points are specific to the blind index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Its key never changes.&lt;/strong&gt; With a key store, the data key of an encrypted column is renewed on its own: after thirty days, the next write creates a new one, and the old ones stay readable. This is not hygiene: each value is encrypted with a random 96-bit nonce, and the standard (NIST SP 800-38D) caps a key at 2^32 encryptions in that case. The index key, on the other hand, must never be renewed: every digest already written was computed with it, and a new key would no longer find anyone. Changing that key means recomputing the whole column.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The projection decides what counts as equal.&lt;/strong&gt; For an email address, you pick the &lt;code&gt;Email&lt;/code&gt; projection, which normalizes the domain (its case, and the ASCII form of an internationalized domain) and ignores whitespace around the address: &lt;code&gt;ada@Example.ORG&lt;/code&gt; and &lt;code&gt;ada@example.org&lt;/code&gt; give the same digest. It leaves the part before the &lt;code&gt;@&lt;/code&gt; alone, since the standard says it is case-sensitive. If your application treats &lt;code&gt;Ada@example.org&lt;/code&gt; and &lt;code&gt;ada@example.org&lt;/code&gt; as the same person, normalize the address before saving it: the index compares what it is given.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encrypting a column that already exists
&lt;/h2&gt;

&lt;p&gt;On an empty database, adding the attribute is all it takes. In a real application, the phone column already holds thousands of numbers in clear, and that is where it gets tricky.&lt;/p&gt;

&lt;p&gt;Adding &lt;code&gt;#[Encrypted]&lt;/code&gt; changes the column's type: the migration Doctrine generates turns it into a binary column, but encrypts nothing. On PostgreSQL, it even fails as generated: you need to add &lt;code&gt;USING phone::bytea&lt;/code&gt; (and &lt;code&gt;USING birth_date::text::bytea&lt;/code&gt; for a date) for the database to accept the conversion. The numbers stay in clear, and on the first read, Doctrine rejects what it finds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Doctrine\DBAL\Types\Exception\ValueNotConvertible:
Stored value is not a valid KeyManagement envelope.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every row that is not encrypted makes loading its entity fail. And you cannot encrypt through the ORM: it cannot load those entities, and even if it could, it would see unchanged values and write nothing. You have to go through DBAL, asking Doctrine for the name of the type Symfony declared for the column:&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;Doctrine\DBAL\ParameterType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Doctrine\DBAL\Types\Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$metadata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$entityManager&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getClassMetadata&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Patient&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$phone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;getType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$metadata&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getTypeOfField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'phone'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nv"&gt;$connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$entityManager&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getConnection&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$platform&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$connection&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getDatabasePlatform&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$connection&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;iterateAssociative&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'SELECT id, phone FROM patient ORDER BY id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$connection&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s1"&gt;'patient'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'phone'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$phone&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;convertToDatabaseValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'phone'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nv"&gt;$platform&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'phone'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ParameterType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;BINARY&lt;/span&gt;&lt;span class="p"&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;code&gt;getTypeOfField()&lt;/code&gt; returns the name of the type Symfony declared for that column (here &lt;code&gt;encrypted_string@patient.contact&lt;/code&gt;), and that type encrypts exactly the way the application will. For a date, first rebuild the &lt;code&gt;DateTimeImmutable&lt;/code&gt; from the stored string, for example with &lt;code&gt;Type::getType('date_immutable')-&amp;gt;convertToPHPValue()&lt;/code&gt;, then pass it to &lt;code&gt;convertToDatabaseValue()&lt;/code&gt;, as the ORM would.&lt;/p&gt;

&lt;p&gt;As written, this script is not idempotent: run again, it would encrypt the already encrypted rows a second time. To make it idempotent, skip the rows that &lt;code&gt;Envelope::fromBytes()&lt;/code&gt; accepts: a plaintext value is never a valid envelope, and the method throws an exception on it. Finally, between the schema migration and the end of the script, every row that is not yet encrypted is unreadable. There are two ways to handle that.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A short maintenance window.&lt;/strong&gt; Run the schema migration and the script back to back, with the application stopped. It is the simplest option. With a key store, where the KMS is called once per scope, a few hundred thousand rows are encrypted in a few minutes. Without one, each row costs a KMS call, which is slow and billed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A parallel column.&lt;/strong&gt; Add an encrypted column next to the old one; the application writes to both and reads the old one. A script fills the new one in batches, only touching the rows where it is empty. Then switch reads over, and drop the old column. It takes longer, but nothing stops.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Either way, don't forget the data that is not in the table: backups taken before the migration still hold the numbers in clear, and will for as long as they are kept.&lt;/p&gt;

&lt;p&gt;The blind index has the same problem. The listener only computes a digest for the entities a &lt;code&gt;flush()&lt;/code&gt; inserts or updates: rows that existed before the column was added keep an empty digest until they are modified, and no search finds them until then. So fill them once, with the same kind of script: read the address back with the encrypted type's &lt;code&gt;convertToPHPValue()&lt;/code&gt;, then write &lt;code&gt;$index-&amp;gt;of()&lt;/code&gt; into the column. The same trap awaits any bulk &lt;code&gt;UPDATE&lt;/code&gt;, in SQL or in DQL: it changes the address without changing the digest, and the search then returns a row that no longer has that address.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I take away from it
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;#[Encrypted]&lt;/code&gt; handles the most technical part: sound encryption, data keys renewed on their own, a master key you can change without rewriting rows, and even move to another provider when the data keys are kept in a store. Writing all that by hand, with a home-made Doctrine type, was a source of mistakes.&lt;/p&gt;

&lt;p&gt;But column encryption is still a design decision. It protects the database, not the application; it takes sorting, searching and uniqueness away from the database; and the blind index, which makes a column searchable again, reveals what encryption was hiding. Before adding the attribute, it is worth writing down who you are protecting against, where the master key lives, and how you will encrypt the data that is already there.&lt;/p&gt;

&lt;p&gt;To try it without installing anything, two Practice exercises run in your browser: &lt;a href="https://apprendre.forelse.fr/en/practice/symfony-8-2-colonnes-chiffrees?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=encrypting-doctrine-columns" rel="noopener noreferrer"&gt;encrypted columns&lt;/a&gt; and &lt;a href="https://apprendre.forelse.fr/en/practice/symfony-8-2-index-aveugle?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=encrypting-doctrine-columns" rel="noopener noreferrer"&gt;blind index&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>php</category>
      <category>symfony</category>
      <category>security</category>
      <category>doctrine</category>
    </item>
    <item>
      <title>Sharing a Symfony project in a URL, without storing anything</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Fri, 30 Oct 2026 07:30:42 +0000</pubDate>
      <link>https://dev.to/oipnet/sharing-a-symfony-project-in-a-url-without-storing-anything-1ohe</link>
      <guid>https://dev.to/oipnet/sharing-a-symfony-project-in-a-url-without-storing-anything-1ohe</guid>
      <description>&lt;p&gt;"Can you show me your code?" On Slack, the answer is usually a forty-line paste, without the Twig template that goes with it, without the route, and with no way to run it. Whoever wants to help copies it all into a local project, notices a file is missing, and the conversation fizzles out.&lt;/p&gt;

&lt;p&gt;I added a &lt;a href="https://apprendre.forelse.fr/en/playground?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=sharing-a-php-project-in-a-url" rel="noopener noreferrer"&gt;playground&lt;/a&gt; to &lt;a href="https://apprendre.forelse.fr?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=sharing-a-php-project-in-a-url" rel="noopener noreferrer"&gt;apprendre.forelse.fr&lt;/a&gt;: a blank Symfony 8 or Laravel 13 project that runs in the browser. You edit a controller, the preview updates, and a "Share" button gives you a link. Whoever opens it gets the project as it is, ready to run.&lt;/p&gt;

&lt;p&gt;The interesting part isn't the button, it's what isn't behind it: &lt;strong&gt;no database, no storage, no share ID&lt;/strong&gt;. All the code travels in the URL. Here is how, and the two questions it raises: size, and security.&lt;/p&gt;

&lt;h2&gt;
  
  
  The project already runs in the browser
&lt;/h2&gt;

&lt;p&gt;I described the mechanics in &lt;a href="https://apprendre.forelse.fr/en/blog/running-symfony-in-the-browser?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=sharing-a-php-project-in-a-url" rel="noopener noreferrer"&gt;a previous article&lt;/a&gt;: PHP 8.4 compiled to WebAssembly, a real Symfony project with its &lt;code&gt;vendor/&lt;/code&gt;, and a Service Worker serving the preview. The server only delivers static files.&lt;/p&gt;

&lt;p&gt;The playground reuses exactly that machinery. To the platform, it's an exercise with no objective and no test: a starting project, a list of editable files, and that's it. The editor, the console (&lt;code&gt;bin/console&lt;/code&gt;, &lt;code&gt;php artisan&lt;/code&gt;) and the preview are the same as in the exercises.&lt;/p&gt;

&lt;p&gt;That left one question: how do you hand your copy of the project to someone else?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not server-side storage
&lt;/h2&gt;

&lt;p&gt;The classic answer, the one 3v4l and CodePen use, is to save the code and hand out a short ID: &lt;code&gt;/s/a8Fk2&lt;/code&gt;. It's clean, but it has a cost:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a table that grows forever, full of content nobody moderates;&lt;/li&gt;
&lt;li&gt;a GDPR question with every share, since the code can contain anything;&lt;/li&gt;
&lt;li&gt;links that die the day you clean up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a learning playground, I wanted none of that. And the browser offers a perfect place to carry data without sending it to the server: &lt;strong&gt;the URL fragment&lt;/strong&gt; , everything after the &lt;code&gt;#&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fragment, the part of the URL the server never sees
&lt;/h2&gt;

&lt;p&gt;When you open &lt;code&gt;https://apprendre.forelse.fr/en/playground/symfony#code=…&lt;/code&gt;, the browser asks the server for &lt;code&gt;/en/playground/symfony&lt;/code&gt; and keeps the rest to itself. The fragment shows up neither in the HTTP request, nor in the server logs, nor in the &lt;code&gt;Referer&lt;/code&gt; header. Only the page's JavaScript can read it, through &lt;code&gt;location.hash&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's exactly what's needed: the public page stays the same for everyone (one URL to index, one canonical), and the code only exists in the link and in the browsers of the people who open it.&lt;/p&gt;

&lt;p&gt;One exception to watch: analytics. Some trackers send the full URL, fragment included. The site uses Umami, which has an option for that (tag simplified):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/mesure/traceur.js"&lt;/span&gt; &lt;span class="na"&gt;data-website-id=&lt;/span&gt;&lt;span class="s"&gt;"…"&lt;/span&gt; &lt;span class="na"&gt;data-exclude-hash=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without it, every visitor's code would have ended up in the stats.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fitting a project in a link
&lt;/h2&gt;

&lt;p&gt;A full Symfony project weighs several megabytes, &lt;code&gt;vendor/&lt;/code&gt; included. But everything that doesn't change is already known: it's the starting project, the same for everyone. So the link only carries the difference, that is &lt;strong&gt;the files changed or created&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;changedFiles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;starting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromEntries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;starting&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;content&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;Those files are turned into JSON, compressed with deflate (the &lt;a href="https://github.com/101arrowz/fflate" rel="noopener noreferrer"&gt;fflate&lt;/a&gt; library, already there to unpack the PHP environment), then encoded in base64url, the base64 variant that only uses URL-safe characters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;encodeFragment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;files&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deflateSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;strToU8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;files&lt;/span&gt; &lt;span class="p"&gt;})),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;binary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mh"&gt;0x8000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;binary&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x8000&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;code=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;binary&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\+&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\/&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;_&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/=+$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&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;The &lt;code&gt;v: 1&lt;/code&gt; costs a few bytes, but it will let the format change without breaking links already shared.&lt;/p&gt;

&lt;p&gt;In practice, it's very reasonable. The starting project's controller and Twig template, slightly edited, come to about 1,500 characters. Compressed and encoded, the fragment is a little over 1,000 characters. PHP code compresses well: lots of repeated keywords, &lt;code&gt;use&lt;/code&gt; statements and indentation.&lt;/p&gt;

&lt;p&gt;One unexpected constraint came from Bluesky, which counts the whole URL in its 300 characters. A shared project quickly goes over: in that case the Bluesky button disappears, and you still have copy link, the phone's share sheet, and LinkedIn and X, which display a shortened address.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading a link you didn't write
&lt;/h2&gt;

&lt;p&gt;Decoding needs more care, because anyone can craft a link. Three rules:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cap the size, before and after decompression.&lt;/strong&gt; A few kilobytes of deflate can inflate to hundreds of megabytes. The fragment is rejected beyond 200,000 characters, and decompression runs chunk by chunk, stopped as soon as the total exceeds 1 MB:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inflate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Inflate&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;MAX_BYTES&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RangeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;too-large&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;inflate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Reject paths that leave the project.&lt;/strong&gt; A file named &lt;code&gt;../../etc/passwd&lt;/code&gt; or &lt;code&gt;/root.php&lt;/code&gt; is ignored. It would be fairly harmless in PHP's in-memory file system, but there is no reason to accept it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Only accept editable files.&lt;/strong&gt; The link can only write where the visitor could write themselves: for Symfony, &lt;code&gt;src/*/*.php&lt;/code&gt; and the Twig templates; for Laravel, the routes, the code under &lt;code&gt;app/&lt;/code&gt; and the Blade views. The framework configuration isn't part of it: a shared link has no business touching &lt;code&gt;config/packages/security.yaml&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On opening, the link's code becomes the visitor's draft (replacing any they had left), and the fragment is removed from the address bar with &lt;code&gt;history.replaceState&lt;/code&gt;. Otherwise, reloading the page would force the link's code back over the changes made since.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running someone else's code
&lt;/h2&gt;

&lt;p&gt;That was the first question I got: is it really reasonable to send someone a link that runs code in their browser?&lt;/p&gt;

&lt;p&gt;Let's start with what's safe. PHP runs in a Web Worker, in WebAssembly, with no network access: the worker's Content Security Policy only allows fetching the environments and the site's own build files. It can't read the visitor's files or call an API. The HTML and JavaScript the project produces are shown in an iframe served from &lt;strong&gt;another origin&lt;/strong&gt; , with no cookie and no access to the platform. It's the same isolation as for the exercises, where learners already run their own code.&lt;/p&gt;

&lt;p&gt;But "isolated" doesn't mean "harmless". The page shown in the preview can look like anything: a fake login page, for instance. And an infinite loop can make the CPU spin.&lt;/p&gt;

&lt;p&gt;So I chose to &lt;strong&gt;never automatically run code received through a link&lt;/strong&gt;. The project loads and the code can be read in the editor, but the preview first shows a warning:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This link contains code written by someone else. Read it in the editor before running it. It runs isolated from this site, but the page it shows can imitate anything: never type a password into it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The preview only starts when you click "Run this code". That one click is deliberate friction, and the right trade-off: someone sharing a bug wants it read, not run blindly.&lt;/p&gt;

&lt;p&gt;One last security choice: the playground offers Symfony and Laravel, but not Nuxt, even though the platform can simulate it. In the Nuxt simulator, part of the code (the &lt;code&gt;server/&lt;/code&gt; routes) runs on the platform's origin. That's fine for your own code, not for a stranger's.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I take away
&lt;/h2&gt;

&lt;p&gt;The URL fragment is an underrated tool. It lets you share a complete state with no backend, no account and nothing kept, as long as the state stays reasonably small. For a code playground, that's exactly the case: you almost never share more than a few files.&lt;/p&gt;

&lt;p&gt;The flip side is that a link becomes untrusted input like any other. Treat it like a form sent by a stranger: cap, filter, and ask before running.&lt;/p&gt;

&lt;p&gt;You can try the &lt;a href="https://apprendre.forelse.fr/en/playground/symfony?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=sharing-a-php-project-in-a-url" rel="noopener noreferrer"&gt;Symfony playground&lt;/a&gt; or the &lt;a href="https://apprendre.forelse.fr/en/playground/laravel?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=sharing-a-php-project-in-a-url" rel="noopener noreferrer"&gt;Laravel playground&lt;/a&gt;, no account needed. And next time someone asks for your code on Slack, send the link.&lt;/p&gt;

&lt;p&gt;The engine is open source (AGPL-3.0): the code from this article is on &lt;a href="https://github.com/Oipnet/apprendre-forelse" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, in &lt;code&gt;playground/src/app/scratch.ts&lt;/code&gt; and &lt;code&gt;Playground.ts&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>php</category>
      <category>symfony</category>
      <category>webdev</category>
      <category>showdev</category>
    </item>
    <item>
      <title>400 or 422? Designing validation errors for a Symfony API</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Wed, 28 Oct 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/400-or-422-designing-validation-errors-for-a-symfony-api-2lp6</link>
      <guid>https://dev.to/oipnet/400-or-422-designing-validation-errors-for-a-symfony-api-2lp6</guid>
      <description>&lt;p&gt;Since Symfony 8.2, an uncaught &lt;code&gt;ValidationFailedException&lt;/code&gt; no longer results in a 500 error: Symfony itself turns it into a 422 response, complete with the list of violations. The &lt;a href="https://apprendre.forelse.fr/en/practice/symfony-8-2-validation-422?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=api-validation-errors-400-or-422" rel="noopener noreferrer"&gt;Practice exercise&lt;/a&gt; that showcases this change is mostly about deleting code.&lt;/p&gt;

&lt;p&gt;But behind that deleted line lies a real design question, one I often see poorly settled in the APIs I audit: &lt;strong&gt;what should an API return when the request is invalid?&lt;/strong&gt; Which HTTP status code, which format, and what information does the client need in order to react? This article starts from the exercise and works through the whole question.&lt;/p&gt;

&lt;h2&gt;
  
  
  The exercise: one line fewer
&lt;/h2&gt;

&lt;p&gt;A registration controller validated an object by hand, then had to wrap the validation exception in an HTTP exception:&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;$violations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$validator&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$registration&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="nf"&gt;\count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$violations&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UnprocessableEntityHttpException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s1"&gt;'Invalid registration.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ValidationFailedException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$registration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$violations&lt;/span&gt;&lt;span class="p"&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;Without that wrapper, the exception turned into a 500 error. Since Symfony 8.2, you just throw it:&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$violations&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ValidationFailedException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$registration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$violations&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;The response is then exactly the one &lt;a href="https://apprendre.forelse.fr/notions/maprequestpayload?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=api-validation-errors-400-or-422" rel="noopener noreferrer"&gt;&lt;code&gt;#[MapRequestPayload]&lt;/code&gt;&lt;/a&gt; produces when validation fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://symfony.com/errors/validation"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Validation Failed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;422&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"email: This value is not a valid email address.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;username: This value is too short. It should have 2 characters or more."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"violations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"propertyPath"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"This value is not a valid email address."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"template"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"This value is not a valid email address."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"parameters"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"{{ value }}"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;not-an-email&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"urn:uuid:bd79c0ab-ddba-46cc-a703-a7a4b08de310"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Only the first violation is shown here; the one for the username follows the same pattern.)&lt;/p&gt;

&lt;p&gt;Before going further, let's look at how Symfony gets there. You'll be able to use the same mechanism for your own exceptions.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Symfony picks the HTTP status code for an exception
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;ValidationFailedException&lt;/code&gt; class now carries an attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[WithHttpStatus(&lt;/span&gt;&lt;span class="mi"&gt;422&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;ValidationFailedException&lt;/span&gt; &lt;span class="n"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;RuntimeException&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When an exception bubbles up to the kernel, Symfony's &lt;code&gt;ErrorListener&lt;/code&gt; looks for this attribute (parent classes included). If it finds it, it wraps the exception in an &lt;code&gt;HttpException&lt;/code&gt; with the right status code: exactly what the controller used to do by hand. Then, if the Serializer component is installed and the client expects JSON (an &lt;code&gt;Accept: application/json&lt;/code&gt; header, or the &lt;code&gt;json&lt;/code&gt; format on the route), the Serializer formats the error: its &lt;code&gt;ProblemNormalizer&lt;/code&gt; notices that the original exception is a validation error and delegates the list of violations to the &lt;code&gt;ConstraintViolationListNormalizer&lt;/code&gt;. Otherwise, the 422 is rendered as HTML, which is the first thing to check when "it doesn't work" with a plain &lt;code&gt;curl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same attribute works for your domain exceptions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[WithHttpStatus(&lt;/span&gt;&lt;span class="mi"&gt;409&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;UsernameAlreadyTaken&lt;/span&gt; &lt;span class="n"&gt;extends&lt;/span&gt; &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;DomainException&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;One detail tends to reassure hexagonal architecture purists: a PHP attribute creates no runtime dependency. As long as nobody reads the attributes through reflection, the &lt;code&gt;WithHttpStatus&lt;/code&gt; class isn't even loaded. Your domain can carry the attribute without actually depending on HttpKernel. If you'd rather not write anything in the domain, configuration does the same thing from the outside:&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="c1"&gt;# config/packages/framework.yaml&lt;/span&gt;
&lt;span class="na"&gt;framework&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;exceptions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;App\Domain\UsernameAlreadyTaken&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;status\_code&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;409&lt;/span&gt;
            &lt;span class="na"&gt;log\_level&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;info&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  400, 422, 409: a status code for each problem
&lt;/h2&gt;

&lt;p&gt;The HTTP status code is the first thing a client reads, and often the only thing it reads automatically. So it has to say &lt;strong&gt;who needs to fix what&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;400 Bad Request&lt;/strong&gt; : the request can't be parsed, for instance malformed JSON. The client has a bug: a human has to fix the code that sends the request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;415 Unsupported Media Type&lt;/strong&gt; : the &lt;code&gt;Content-Type&lt;/code&gt; declares a format the API doesn't read. Same consequence: it's the client code that needs fixing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;422 Unprocessable Content&lt;/strong&gt; : the request is readable and well formed, but its content is invalid. The email address isn't one, the username is too short. Often it's the end user who has to correct their input, and the client has to display the errors next to the right fields.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;409 Conflict&lt;/strong&gt; : the request is valid, but it conflicts with the current state of the resource. The username is already taken, the order has already shipped, the version being edited is no longer the latest.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;422 comes from WebDAV, but it has been part of general HTTP semantics since RFC 9110. There's no reason to avoid it anymore.&lt;/p&gt;

&lt;p&gt;Symfony already follows this distinction with &lt;code&gt;#[MapRequestPayload]&lt;/code&gt;: an unsupported format gives a 415 and an unreadable JSON body a 400, whereas a field of the wrong type (a string where an integer is expected) or a failed constraint gives a 422, with the violations.&lt;/p&gt;

&lt;h3&gt;
  
  
  The username-already-taken case
&lt;/h3&gt;

&lt;p&gt;"This username is already taken": 409 or 422? Both are defensible. I lean towards &lt;strong&gt;422 with a violation on &lt;code&gt;username&lt;/code&gt;&lt;/strong&gt; , for a practical reason: the client displays this error exactly like the others, under the relevant field. That's what the &lt;code&gt;UniqueEntity&lt;/code&gt; constraint from Symfony's Doctrine bridge does.&lt;/p&gt;

&lt;p&gt;But then you have to think about the case the constraint can't see. Two simultaneous registrations with the same username both pass validation, then the second one hits the database's unique index. Left unhandled, that's a Doctrine &lt;code&gt;UniqueConstraintViolationException&lt;/code&gt;, and therefore a &lt;strong&gt;500&lt;/strong&gt;. I convert it into a violation, so the client gets the same response in every case:&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="k"&gt;try&lt;/span&gt; &lt;span class="p"&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="n"&gt;entityManager&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UniqueConstraintViolationException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ValidationFailedException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$registration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ConstraintViolationList&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ConstraintViolation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s1"&gt;'This username is already taken.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'This username is already taken.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;[],&lt;/span&gt;
            &lt;span class="nv"&gt;$registration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'username'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nv"&gt;$registration&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UniqueEntity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NOT\_UNIQUE\_ERROR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&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;The &lt;code&gt;UniqueEntity::NOT_UNIQUE_ERROR&lt;/code&gt; code is not a detail: without it, the violation goes out with no &lt;code&gt;type&lt;/code&gt; member, and the client loses the only stable marker it has (see below). And nothing else in that &lt;code&gt;catch&lt;/code&gt;: after this exception, Doctrine has closed the &lt;code&gt;EntityManager&lt;/code&gt;, so a second &lt;code&gt;flush()&lt;/code&gt; would fail.&lt;/p&gt;

&lt;p&gt;With Symfony 8.2, this conversion boils down to a single &lt;code&gt;throw&lt;/code&gt;, with no &lt;code&gt;HttpException&lt;/code&gt; around it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The format: Problem Details
&lt;/h2&gt;

&lt;p&gt;The JSON produced by Symfony follows the &lt;strong&gt;Problem Details for HTTP APIs&lt;/strong&gt; format, standardized by RFC 9457 (which superseded RFC 7807). The format defines five standard members:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;type&lt;/code&gt;: a URI that identifies the &lt;strong&gt;kind&lt;/strong&gt; of problem. That's what the client should check, not the title;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;title&lt;/code&gt;: a human-readable summary of that kind of problem, identical from one occurrence to the next;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;status&lt;/code&gt;: the HTTP status code, repeated in the body;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;detail&lt;/code&gt;: the explanation for &lt;strong&gt;this&lt;/strong&gt; occurrence;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;instance&lt;/code&gt;: a URI that identifies this specific occurrence, handy for tracking an error down in the logs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything else, such as &lt;code&gt;violations&lt;/code&gt;, is an extension member, which the standard explicitly provides for. Adopting this format lets clients use existing libraries rather than learn yet another home-grown format. The RFC also recommends the &lt;code&gt;application/problem+json&lt;/code&gt; media type, which some clients use to detect an error. Symfony sets it automatically as soon as the body has a &lt;code&gt;type&lt;/code&gt;, a &lt;code&gt;title&lt;/code&gt; and a &lt;code&gt;status&lt;/code&gt;; a hand-built error response has to remember to do so.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the client actually needs
&lt;/h2&gt;

&lt;p&gt;This is where most APIs fall short. They return errors written for a human reading the response, when it's a &lt;strong&gt;program&lt;/strong&gt; that receives them.&lt;/p&gt;

&lt;h3&gt;
  
  
  A path that matches the request
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;propertyPath&lt;/code&gt; must point to the field as the client sent it. If your API receives &lt;code&gt;snake_case&lt;/code&gt; and your DTOs use &lt;code&gt;camelCase&lt;/code&gt;, the normalizer applies the &lt;em&gt;name converter&lt;/em&gt; configured in the Serializer. The path becomes &lt;code&gt;date_of_birth&lt;/code&gt; rather than &lt;code&gt;dateOfBirth&lt;/code&gt;, provided that converter is properly configured. For a nested structure, the path takes the form &lt;code&gt;addresses[0].postalCode&lt;/code&gt;, which lets the client find the offending row in a repeated form.&lt;/p&gt;

&lt;h3&gt;
  
  
  A stable code, not a message
&lt;/h3&gt;

&lt;p&gt;A violation's &lt;code&gt;title&lt;/code&gt; is a translated message: it changes with the language, and on the day someone fixes a typo in a translation file. A client that checks &lt;code&gt;title === "This value is too short..."&lt;/code&gt; will break sooner or later.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;type&lt;/code&gt; member of each violation, on the other hand, is stable: &lt;code&gt;urn:uuid:&lt;/code&gt; followed by the constraint's code. For example, &lt;code&gt;Length::TOO_SHORT_ERROR&lt;/code&gt; and &lt;code&gt;Email::INVALID_FORMAT_ERROR&lt;/code&gt; are UUIDs set in stone in Symfony's code. Yours should be too, if you write custom constraints: give them error constants with a UUID. That code is what the client should check to display its own message or trigger specific behavior.&lt;/p&gt;

&lt;p&gt;Document these codes alongside your API (in your OpenAPI schema, for example). It's the most useful part of an error contract, and the one most often forgotten.&lt;/p&gt;

&lt;h3&gt;
  
  
  The useful data, and nothing more
&lt;/h3&gt;

&lt;p&gt;The invalid value itself is not returned as such: &lt;code&gt;ValidationFailedException::getValue()&lt;/code&gt; isn't serialized. Only the &lt;code&gt;{{ value }}&lt;/code&gt; parameter of some messages echoes it. Keep that in mind for sensitive fields: a password rejected by a &lt;code&gt;Regex&lt;/code&gt; constraint must not come back in plain text in the response, let alone in your logs.&lt;/p&gt;

&lt;p&gt;In debug mode, the response also includes the exception class and the full stack trace. That's invaluable in development, and an information leak if &lt;code&gt;APP_DEBUG&lt;/code&gt; stays on in production. This isn't hypothetical: I run into it regularly during audits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logging: a 422 is not an incident
&lt;/h2&gt;

&lt;p&gt;Symfony logs 4xx HTTP errors at the &lt;code&gt;warning&lt;/code&gt; level. That is in fact new in 8.2 for exceptions that get their status code through &lt;code&gt;#[WithHttpStatus]&lt;/code&gt; or &lt;code&gt;framework.exceptions&lt;/code&gt;, such as &lt;code&gt;ValidationFailedException&lt;/code&gt;: before, they were logged at &lt;code&gt;error&lt;/code&gt;, which triggered the &lt;code&gt;fingers_crossed&lt;/code&gt; handler from the Monolog recipe. For a public API, validation errors are part of normal operation: thousands of incorrect inputs a day mean as many &lt;code&gt;warning&lt;/code&gt; lines, and the real warnings get lost in the noise. I take them down one level:&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;framework&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;exceptions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;Symfony\Component\Validator\Exception\ValidationFailedException&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;log\_level&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;info&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, I keep an alert on the &lt;strong&gt;rate&lt;/strong&gt; of 422s per endpoint. A sudden spike after a deployment is often a mobile client that hasn't been updated, or a constraint that has become too strict. A single validation error is normal; a wave of them is an incident.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test the contract, not the messages
&lt;/h2&gt;

&lt;p&gt;The exercise's tests check what a client really expects: the 422 status code, the problem &lt;code&gt;type&lt;/code&gt; and the list of fields in error.&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;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;assertResponseStatusCodeSame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;422&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$problem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;json\_decode&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getResponse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getContent&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&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;assertSame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://symfony.com/errors/validation'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$problem&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'type'&lt;/span&gt;&lt;span class="p"&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;assertSame&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'username'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nf"&gt;array\_column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$problem&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'violations'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="s1"&gt;'propertyPath'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No test on the message text. If you want to go further, also check each violation's &lt;code&gt;type&lt;/code&gt;: that's what belongs to the contract you've promised your clients.&lt;/p&gt;

&lt;h2&gt;
  
  
  My takeaways
&lt;/h2&gt;

&lt;p&gt;The new feature in Symfony 8.2 is small: an exception that knows which HTTP status code it stands for. But it brings consistency where there was none: manual validation, &lt;code&gt;#[MapRequestPayload]&lt;/code&gt; and the conversion of a database error now all go through the same path and produce the same response.&lt;/p&gt;

&lt;p&gt;The rest is up to you: pick the right status code, stick to a standard format, give clients stable codes rather than sentences, and don't mistake invalid input for an incident.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://apprendre.forelse.fr/en/practice/symfony-8-2-validation-422?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=api-validation-errors-400-or-422" rel="noopener noreferrer"&gt;exercise&lt;/a&gt; takes a few minutes in the browser, on a development build of Symfony 8.2. The two requests in the "Requests" tab show the response before and after.&lt;/p&gt;

</description>
      <category>php</category>
      <category>symfony</category>
      <category>api</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Two-factor authentication with Symfony 8.2: one authenticator, one voter, and that's it</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Sun, 25 Oct 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/two-factor-authentication-with-symfony-82-one-authenticator-one-voter-and-thats-it-1hpf</link>
      <guid>https://dev.to/oipnet/two-factor-authentication-with-symfony-82-one-authenticator-one-voter-and-thats-it-1hpf</guid>
      <description>&lt;p&gt;Until now, two-factor authentication in Symfony meant either a bundle, &lt;a href="https://github.com/scheb/2fa" rel="noopener noreferrer"&gt;scheb/2fa-bundle&lt;/a&gt;, or home-made code that worked around the firewall. The most common workaround looks like this: after the password, the user is logged in; a second authenticator checks the code and adds a role such as &lt;code&gt;ROLE_2FA_SUCCEED&lt;/code&gt; to the token; a subscriber redirects users without that role to the code form.&lt;/p&gt;

&lt;p&gt;It works, but it is fragile. The subscriber only protects the routes it is given, and any page you forget opens with the password alone. The role added to the token does not exist on the user: with an entity that does not implement &lt;code&gt;EquatableInterface&lt;/code&gt;, as is the case with the one &lt;code&gt;make:user&lt;/code&gt; generates, the &lt;code&gt;ContextListener&lt;/code&gt; compares the roles of the refreshed user with those of the token, finds them different, and clears the token on the next request.&lt;/p&gt;

&lt;p&gt;Symfony 8.2, expected at the end of November, makes this much simpler. The token now remembers &lt;strong&gt;how&lt;/strong&gt; the user proved their identity, and a voter can ask the firewall for a new proof instead of answering 403. With these two building blocks, SMS two-factor authentication fits in an authenticator, a voter, an entry point, the service that sends the code and a few lines of &lt;code&gt;security.yaml&lt;/code&gt;. The code in this article runs on the development version of Symfony 8.2, with functional tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  The idea: two proofs, not a role
&lt;/h2&gt;

&lt;p&gt;I introduced these features in &lt;a href="https://apprendre.forelse.fr/en/blog/asking-for-proof-again-before-sensitive-actions?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=two-factor-authentication-with-symfony-8-2" rel="noopener noreferrer"&gt;Being logged in is not enough: asking for proof again before sensitive actions&lt;/a&gt;. In short: an authenticator states the method it checks with an &lt;code&gt;AuthenticationMethodBadge&lt;/code&gt; (&lt;code&gt;pwd&lt;/code&gt;, &lt;code&gt;sms&lt;/code&gt;, &lt;code&gt;otp&lt;/code&gt;…, the names from RFC 8176), and Symfony stores the method and the time on the token:&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;$token&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAuthenticationProofs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// ['pwd' =&amp;gt; 1793050000, 'sms' =&amp;gt; 1793050042]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we use them for something else: not to protect one action, but the whole application, right from login.&lt;/p&gt;

&lt;p&gt;The key point for two-factor authentication: when the same user authenticates a second time in the session, Symfony &lt;strong&gt;adds&lt;/strong&gt; the new proof to those of the previous token instead of replacing them. That is exactly the two-factor flow: the password first, the code next, and a token that carries both.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;form_login&lt;/code&gt; and &lt;code&gt;json_login&lt;/code&gt; already state &lt;code&gt;pwd&lt;/code&gt;. All that is left is to write the authenticator for the code, and to tell the firewall that protected pages require both proofs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The configuration
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/packages/security.yaml&lt;/span&gt;
&lt;span class="na"&gt;security&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;firewalls&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;main&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;lazy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
            &lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app_user_provider&lt;/span&gt;
            &lt;span class="na"&gt;form_login&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                &lt;span class="na"&gt;login_path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app_login&lt;/span&gt;
                &lt;span class="na"&gt;check_path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app_login&lt;/span&gt;
                &lt;span class="na"&gt;enable_csrf&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
            &lt;span class="c1"&gt;# The second factor: one more authenticator on the same firewall.&lt;/span&gt;
            &lt;span class="na"&gt;custom_authenticators&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;App\Security\SmsCodeAuthenticator&lt;/span&gt;
            &lt;span class="c1"&gt;# When the voter asks for a proof, this entry point decides which one.&lt;/span&gt;
            &lt;span class="na"&gt;re_authentication_entry_point&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;App\Security\AskForMissingProof&lt;/span&gt;
            &lt;span class="c1"&gt;# Limits password and code attempts (symfony/rate-limiter component).&lt;/span&gt;
            &lt;span class="na"&gt;login_throttling&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                &lt;span class="na"&gt;max_attempts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;
                &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;15&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;minutes'&lt;/span&gt;
            &lt;span class="na"&gt;logout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app_logout&lt;/span&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;roles&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;PUBLIC_ACCESS&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;^/logout&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="nv"&gt;PUBLIC_ACCESS&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;^/verify&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="nv"&gt;IS_AUTHENTICATED_FULLY&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="nv"&gt;TWO_FACTORS&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last line does all the work: the whole application requires &lt;code&gt;TWO_FACTORS&lt;/code&gt;, except login, logout and the code form. Protection is closed by default. A page added tomorrow is protected without anyone thinking about it.&lt;/p&gt;

&lt;p&gt;The code form requires &lt;code&gt;IS_AUTHENTICATED_FULLY&lt;/code&gt;, not &lt;code&gt;PUBLIC_ACCESS&lt;/code&gt;: you must have given your password in this session to receive a code, a "remember me" cookie is not enough.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TWO_FACTORS&lt;/code&gt; is not a role: it is an attribute decided by a voter.&lt;/p&gt;

&lt;h2&gt;
  
  
  The voter: both proofs, or a new request
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/Security/Voter/TwoFactorsVoter.php&lt;/span&gt;
&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Security\Voter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Entity\User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Authentication\AuthenticationMethod&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Authentication\Token\TokenInterface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Authorization\Voter\Vote&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Authorization\Voter\Voter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TwoFactorsVoter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Voter&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;const&lt;/span&gt; &lt;span class="no"&gt;ATTRIBUTE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'TWO_FACTORS'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;supports&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;mixed&lt;/span&gt; &lt;span class="nv"&gt;$subject&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ATTRIBUTE&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;voteOnAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;mixed&lt;/span&gt; &lt;span class="nv"&gt;$subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;TokenInterface&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;?Vote&lt;/span&gt; &lt;span class="nv"&gt;$vote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$proofs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAuthenticationProofs&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="k"&gt;isset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$proofs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;AuthenticationMethod&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;PASSWORD&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nv"&gt;$proofs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;AuthenticationMethod&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SMS&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// A new proof only helps users who can provide one: without a phone, it's a 403.&lt;/span&gt;
        &lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getUser&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="nv"&gt;$user&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getPhoneNumber&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$vote&lt;/span&gt;&lt;span class="o"&gt;?-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;requestReAuthentication&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&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;code&gt;$vote-&amp;gt;requestReAuthentication()&lt;/code&gt; is the other new feature in 8.2. The voter denies access, but tells the firewall that a new authentication could change its answer. Instead of a 403, the firewall remembers the requested page and calls its re-authentication entry point.&lt;/p&gt;

&lt;p&gt;The voter only does this for users who have a phone. For the others, a new form would lead nowhere: a 403 is more honest than a loop. In a real application, this is where you would send them to second-factor enrollment.&lt;/p&gt;

&lt;h2&gt;
  
  
  The entry point: asking for what is missing
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/Security/AskForMissingProof.php&lt;/span&gt;
&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Security&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\HttpFoundation\RedirectResponse&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\HttpFoundation\Request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\HttpFoundation\Response&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Routing\Generator\UrlGeneratorInterface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Authentication\AuthenticationMethod&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Authentication\Token\TokenInterface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\EntryPoint\ReAuthenticationEntryPointInterface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AskForMissingProof&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;ReAuthenticationEntryPointInterface&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;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;UrlGeneratorInterface&lt;/span&gt; &lt;span class="nv"&gt;$urls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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;startReAuthentication&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="kt"&gt;TokenInterface&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;Response&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// No password (session reopened by "remember me"): start with the password.&lt;/span&gt;
        &lt;span class="nv"&gt;$route&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;isset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAuthenticationProofs&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="nc"&gt;AuthenticationMethod&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;PASSWORD&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
            &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'app_verify'&lt;/span&gt;
            &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'app_login'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RedirectResponse&lt;/span&gt;&lt;span class="p"&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="n"&gt;urls&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$route&lt;/span&gt;&lt;span class="p"&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;Without &lt;code&gt;re_authentication_entry_point&lt;/code&gt;, the firewall would use the entry point of &lt;code&gt;form_login&lt;/code&gt;, which can re-authenticate too: it would send the user to the login page, and they would type their password again without ever seeing the code form. Ours checks which proof is missing.&lt;/p&gt;

&lt;p&gt;The "remember me" cookie is handled along the way: a session reopened by that cookie leaves no proof, Symfony explicitly excludes it. The voter therefore denies it, and the entry point sends the user to the password first.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code authenticator
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/Security/SmsCodeAuthenticator.php&lt;/span&gt;
&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Security&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Bundle\SecurityBundle\Security&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\HttpFoundation\RedirectResponse&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\HttpFoundation\Request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\HttpFoundation\Response&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Routing\Generator\UrlGeneratorInterface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Authentication\AuthenticationMethod&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Authentication\Token\TokenInterface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Exception\AuthenticationException&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Authenticator\Passport\Badge\AuthenticationMethodBadge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Authenticator\Passport\Passport&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\SecurityRequestAttributes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Security\Http\Util\TargetPathTrait&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SmsCodeAuthenticator&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractAuthenticator&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;InteractiveAuthenticatorInterface&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;TargetPathTrait&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;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;Security&lt;/span&gt; &lt;span class="nv"&gt;$security&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;SmsCodes&lt;/span&gt; &lt;span class="nv"&gt;$codes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;UrlGeneratorInterface&lt;/span&gt; &lt;span class="nv"&gt;$urls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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;supports&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="kt"&gt;?bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;isMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'POST'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="s1"&gt;'app_verify'&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'_route'&lt;/span&gt;&lt;span class="p"&gt;);&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;authenticate&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="kt"&gt;Passport&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// The code completes the password: on its own, it logs nobody in.&lt;/span&gt;
        &lt;span class="nv"&gt;$user&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="n"&gt;security&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomUserMessageAuthenticationException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Log in with your password first.'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Passport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UserBadge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getUserIdentifier&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomCredentials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="n"&gt;codes&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getPayload&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'code'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CsrfTokenBadge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'verify'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getPayload&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'_csrf_token'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuthenticationMethodBadge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AuthenticationMethod&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SMS&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Only an interactive authentication leaves a proof on the token.&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;isInteractive&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&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;onAuthenticationSuccess&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="kt"&gt;TokenInterface&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$firewallName&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;?Response&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$target&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;getTargetPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getSession&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nv"&gt;$firewallName&lt;/span&gt;&lt;span class="p"&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;removeTargetPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getSession&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nv"&gt;$firewallName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RedirectResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$target&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="n"&gt;urls&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app_account'&lt;/span&gt;&lt;span class="p"&gt;));&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;onAuthenticationFailure&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="kt"&gt;AuthenticationException&lt;/span&gt; &lt;span class="nv"&gt;$exception&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;?Response&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getSession&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecurityRequestAttributes&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;AUTHENTICATION_ERROR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$exception&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RedirectResponse&lt;/span&gt;&lt;span class="p"&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="n"&gt;urls&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app_verify'&lt;/span&gt;&lt;span class="p"&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;Three details matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;InteractiveAuthenticatorInterface&lt;/code&gt;.&lt;/strong&gt; Without it, the badge is ignored: Symfony only records proofs from an interactive authentication, one where the user has just done something. It is the most likely omission, and it raises no error: the code is accepted, but the &lt;code&gt;sms&lt;/code&gt; proof never shows up, and the user is sent back to the form in a loop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The user comes from the session.&lt;/strong&gt; The authenticator does not read an identifier from the request: it reuses the one the password already authenticated. A code alone logs nobody in.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The passport has a &lt;code&gt;UserBadge&lt;/code&gt;.&lt;/strong&gt; That is what lets &lt;code&gt;login_throttling&lt;/code&gt; count code attempts too: on the sixth attempt within fifteen minutes, even the right code is refused. Without a limit, a script eventually hits the right one among the million possible codes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The code itself
&lt;/h2&gt;

&lt;p&gt;The service that sends and checks the code has nothing specific to Symfony 8.2. It goes through the Notifier component (&lt;code&gt;symfony/notifier&lt;/code&gt;), with your provider's SMS transport. The code is random, only its hash is kept in the session along with an expiry, and it can be used only once.&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;// src/Security/SmsCodes.php&lt;/span&gt;
&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Security&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Entity\User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\HttpFoundation\RequestStack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Notifier\Message\SmsMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Notifier\TexterInterface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SmsCodes&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="no"&gt;KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'_sms_code'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="no"&gt;LIFETIME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;300&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;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;TexterInterface&lt;/span&gt; &lt;span class="nv"&gt;$texter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;RequestStack&lt;/span&gt; &lt;span class="nv"&gt;$requests&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;User&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'%06d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;random_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;999999&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

        &lt;span class="c1"&gt;// In the session, only the hash of the code and its expiry.&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="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getSession&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'hash'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sha256'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="s1"&gt;'expires'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;LIFETIME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&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="n"&gt;texter&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SmsMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getPhoneNumber&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="s2"&gt;"Your login code: &lt;/span&gt;&lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&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;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$session&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="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getSession&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nv"&gt;$expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$session&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;KEY&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="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nv"&gt;$expected&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nv"&gt;$expected&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'expires'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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="nb"&gt;hash_equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expected&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'hash'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sha256'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nv"&gt;$session&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// single use&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&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;isPending&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$expected&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="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getSession&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nv"&gt;$expected&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;$expected&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'expires'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="p"&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;The controller displays the form and only sends an SMS if no valid code is already pending. One SMS per page refresh means a growing bill, and an open door for anyone who wants to flood a phone number. For the same reason, a "Resend code" button should go through a rate limiter, for example one resend per minute.&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;// src/Controller/SecurityController.php (excerpt)&lt;/span&gt;
&lt;span class="na"&gt;#[Route('/verify', name: 'app_verify', methods: ['GET', 'POST'])]&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;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;CurrentUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;SmsCodes&lt;/span&gt; &lt;span class="nv"&gt;$codes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;AuthenticationUtils&lt;/span&gt; &lt;span class="nv"&gt;$authenticationUtils&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// No phone, nothing to send: in a real application, redirect to enrollment.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getPhoneNumber&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&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;createAccessDeniedException&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// The POST is handled by SmsCodeAuthenticator.&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;$codes&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;isPending&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$codes&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&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;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'security/verify.html.twig'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'error'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$authenticationUtils&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getLastAuthenticationError&lt;/span&gt;&lt;span class="p"&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;h2&gt;
  
  
  The flow, end to end
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Ada requests &lt;code&gt;/account&lt;/code&gt;. She is not logged in: &lt;code&gt;form_login&lt;/code&gt; sends her to &lt;code&gt;/login&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;She gives her password. The token carries &lt;code&gt;pwd&lt;/code&gt;, and the firewall takes her back to &lt;code&gt;/account&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The voter denies &lt;code&gt;TWO_FACTORS&lt;/code&gt; and asks for a new proof. The firewall remembers &lt;code&gt;/account&lt;/code&gt;, and the entry point sends her to &lt;code&gt;/verify&lt;/code&gt;, which sends her a code.&lt;/li&gt;
&lt;li&gt;She types the code. The token carries &lt;code&gt;pwd&lt;/code&gt; and &lt;code&gt;sms&lt;/code&gt;, and she is back on &lt;code&gt;/account&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The voter does not look at the time of the proofs: once the code is given, it is valid for the whole session, as in most two-factor setups, and logging out clears everything. Since the time is stored, nothing stops you from requiring an &lt;code&gt;sms&lt;/code&gt; proof less than 24 hours old.&lt;/p&gt;

&lt;p&gt;No more subscriber, no more role added to the token, no more list of routes to keep up to date. The user keeps their usual roles, and an entity without &lt;code&gt;EquatableInterface&lt;/code&gt; is not a problem: the tests of the example use a &lt;code&gt;User&lt;/code&gt; class that does not implement it, and the session survives from one request to the next.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before you adopt it
&lt;/h2&gt;

&lt;p&gt;This article shows the mechanism, not a complete two-factor system. Three things to know.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SMS is one of the weakest second factors.&lt;/strong&gt; It is exposed to SIM swapping, and NIST classifies it as a "restricted" authenticator. The same pattern works with an authenticator app: the authenticator checks a TOTP code with a library such as &lt;code&gt;spomky-labs/otphp&lt;/code&gt;, states &lt;code&gt;AuthenticationMethod::ONE_TIME_PASSWORD&lt;/code&gt;, and the voter requires &lt;code&gt;otp&lt;/code&gt; instead of &lt;code&gt;sms&lt;/code&gt;. You then need one secret per account, saved at enrollment after a first code is confirmed, and never shown again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enrollment and recovery are up to you.&lt;/strong&gt; Registering the phone (or the TOTP secret), offering hashed, single-use backup codes, and protecting the deactivation of two-factor authentication with a recent proof. For that last point, &lt;code&gt;IS_AUTHENTICATED_RECENTLY&lt;/code&gt; and the same re-authentication mechanism do the job: I covered it in &lt;a href="https://apprendre.forelse.fr/en/blog/asking-for-proof-again-before-sensitive-actions?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=two-factor-authentication-with-symfony-8-2" rel="noopener noreferrer"&gt;Being logged in is not enough: asking for proof again before sensitive actions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A limit of the mechanism.&lt;/strong&gt; Re-authentication only kicks in when a single attribute is denied. An &lt;code&gt;access_control&lt;/code&gt; rule that lists several attributes in &lt;code&gt;roles&lt;/code&gt; always gives a 403: keep &lt;code&gt;TWO_FACTORS&lt;/code&gt; alone on its line. Passing several attributes to the same decision is deprecated in 8.2 anyway.&lt;/p&gt;

&lt;p&gt;If you want all of this ready to use, or cannot wait for Symfony 8.2, scheb/2fa-bundle is still the right choice: it brings TOTP, backup codes, trusted devices and codes by email. But if what you need is a second factor on an application you control, Symfony 8.2 lets you write it in under two hundred lines, without working around the firewall.&lt;/p&gt;

</description>
      <category>symfony</category>
      <category>php</category>
      <category>security</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The CSRF token: what it protects, when you still need it, and how Symfony, Laravel, Django and Rails handle it</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Sun, 25 Oct 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/the-csrf-token-what-it-protects-when-you-still-need-it-and-how-symfony-laravel-django-and-19in</link>
      <guid>https://dev.to/oipnet/the-csrf-token-what-it-protects-when-you-still-need-it-and-how-symfony-laravel-django-and-19in</guid>
      <description>&lt;p&gt;"The CSRF token is invalid. Please try to resubmit the form." in Symfony, "419 Page Expired" in Laravel, "CSRF verification failed. Request aborted." in Django: every web developer has run into one of these errors. The CSRF token is one of those protections everyone uses, because the framework turns it on by default, and few people can explain. In my audits I regularly find routes excluded from the check "because it was getting in the way", or, the other way round, &lt;code&gt;Bearer&lt;/code&gt; token APIs that demand a CSRF token for nothing.&lt;/p&gt;

&lt;p&gt;This article covers the whole question: what the token really protects, when it is still needed in the age of &lt;code&gt;SameSite&lt;/code&gt; cookies, and how Symfony, Laravel, Django and Rails take care of it. Along the way, it explains a detail from a recent &lt;a href="https://apprendre.forelse.fr/en/practice/laravel-13-35-route-query?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=csrf-tokens-across-frameworks" rel="noopener noreferrer"&gt;Practice exercise&lt;/a&gt;: why a simple search using the new HTTP &lt;code&gt;QUERY&lt;/code&gt; method can be asked for a token.&lt;/p&gt;

&lt;h2&gt;
  
  
  The attack: making the victim's browser act
&lt;/h2&gt;

&lt;p&gt;CSRF stands for &lt;em&gt;Cross-Site Request Forgery&lt;/em&gt;. The attacker steals nothing. They get the victim's browser to send a request to a site where the victim is logged in.&lt;/p&gt;

&lt;p&gt;Imagine you are logged in to your bank, and you then visit a booby-trapped page that contains this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"https://bank.example/transfer"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"trap"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"iban"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"GB33 ... attacker's account"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"amount"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"2000"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;trap&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The form submits itself to the bank. The browser attaches the bank's session cookie, as it does for any request to that domain. On the server side, nothing distinguishes this request from a genuine transfer: right session, right fields, right method.&lt;/p&gt;

&lt;p&gt;That is the key point: &lt;strong&gt;the server recognises the user by a cookie that the browser sends on its own&lt;/strong&gt;. As long as that is the case, any site can trigger actions on the user's behalf. The attacker cannot see the response, the same-origin policy prevents it, but they do not need to: the transfer is done.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the token changes
&lt;/h2&gt;

&lt;p&gt;The CSRF token is a random value that the server places in its own pages, usually in a hidden field, and requires on every submission. The attacker's site can build a form that targets the bank, but it cannot read a page from the bank to grab the token: the same-origin policy forbids it. Its request arrives without a valid token, and the server rejects it.&lt;/p&gt;

&lt;p&gt;So the token does not protect you against an attacker who can run JavaScript on your own domain. An XSS flaw reads the token from the page just like your own code would. CSRF and XSS are two separate problems, and you cannot fix the first without fixing the second.&lt;/p&gt;

&lt;h2&gt;
  
  
  When do you need a token?
&lt;/h2&gt;

&lt;p&gt;The rule fits in one sentence: &lt;strong&gt;any request that changes something, on an application that authenticates with a cookie&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Requests that change something&lt;/strong&gt; : &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;PATCH&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;. Reads (&lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;HEAD&lt;/code&gt;, &lt;code&gt;OPTIONS&lt;/code&gt;) do not need one, provided they really change nothing. A deletion triggered by a &lt;code&gt;GET&lt;/code&gt; link is protected by no framework at all: a simple &lt;code&gt;&amp;lt;img src="https://site.example/dish/12/delete"&amp;gt;&lt;/code&gt; tag on another page is enough to trigger it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cookie authentication&lt;/strong&gt; : a classic session, a "remember me" cookie, or an API token stored in a cookie. It is the automatic sending of the cookie that makes the attack possible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The login form too.&lt;/strong&gt; An attacker can log the victim in to the &lt;em&gt;attacker's&lt;/em&gt; account, then collect whatever the victim enters there (an address, a saved card). This is &lt;em&gt;login CSRF&lt;/em&gt;. Symfony covers it with the &lt;code&gt;enable_csrf&lt;/code&gt; option of &lt;code&gt;form_login&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Conversely, an API that authenticates with the &lt;code&gt;Authorization: Bearer ...&lt;/code&gt; header does not need a CSRF token: another site cannot get the victim's browser to add that header. That is exactly why Laravel's &lt;code&gt;api&lt;/code&gt; group does not check the token.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about SameSite cookies?
&lt;/h3&gt;

&lt;p&gt;A cookie's &lt;code&gt;SameSite&lt;/code&gt; attribute tells the browser not to send it on requests coming from another site. With &lt;code&gt;SameSite=Lax&lt;/code&gt;, the session cookie is no longer sent with the trap form's &lt;code&gt;POST&lt;/code&gt;: the attack above fails, even without a token. Symfony and Laravel set &lt;code&gt;SameSite=Lax&lt;/code&gt; on their session cookie by default. Chrome also applies a "Lax by default" rule to cookies that specify nothing, in a looser form: for two minutes after it is set, the cookie still goes along with a top-level &lt;code&gt;POST&lt;/code&gt;. Firefox and Safari do not make that choice by default.&lt;/p&gt;

&lt;p&gt;It is an excellent second line of defence, but I never leave it on its own:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Lax&lt;/code&gt; lets top-level &lt;code&gt;GET&lt;/code&gt; navigations through.&lt;/strong&gt; A link clicked on another site carries the cookie. An action triggered by &lt;code&gt;GET&lt;/code&gt; therefore remains exposed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Same site" is not "same origin".&lt;/strong&gt; &lt;code&gt;blog.example.com&lt;/code&gt; and &lt;code&gt;app.example.com&lt;/code&gt; are the same site as far as &lt;code&gt;SameSite&lt;/code&gt; is concerned. A compromised subdomain, or one hosted by a third party, bypasses the protection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not every browser applies it to cookies that specify nothing&lt;/strong&gt; , and a configuration change can disable it without anyone noticing, for example &lt;code&gt;SameSite=None&lt;/code&gt; set to make an iframe integration work.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The case of the QUERY method
&lt;/h3&gt;

&lt;p&gt;The HTTP &lt;code&gt;QUERY&lt;/code&gt; method, standardised in 2026, is a read like &lt;code&gt;GET&lt;/code&gt;, but with its criteria in the request body. Laravel 13.35 can route it, and the Practice exercise that introduces this feature includes a warning about the CSRF token. Laravel's CSRF middleware lets read methods (&lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;HEAD&lt;/code&gt;, &lt;code&gt;OPTIONS&lt;/code&gt;) through and checks all the others. &lt;code&gt;QUERY&lt;/code&gt; is a read according to its RFC, but Laravel does not count it as one yet: declared in &lt;code&gt;routes/web.php&lt;/code&gt;, a &lt;code&gt;QUERY&lt;/code&gt; route requires a token, just like a &lt;code&gt;POST&lt;/code&gt;. Since Laravel 13 there is a nuance: the middleware also accepts without a token a request that the browser marks as coming from the same origin (the &lt;code&gt;Sec-Fetch-Site: same-origin&lt;/code&gt; header). A &lt;code&gt;QUERY&lt;/code&gt; sent by your own pages over HTTPS therefore goes through; the 419 mostly shows up in local development over &lt;code&gt;http&lt;/code&gt;, where the browser does not send that header.&lt;/p&gt;

&lt;p&gt;Is forgetting a token a real risk here? Not really. An HTML form can only send &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt;, and a &lt;code&gt;QUERY&lt;/code&gt; request sent with &lt;code&gt;fetch()&lt;/code&gt; from another site triggers a CORS &lt;em&gt;preflight&lt;/em&gt; request, which your server rejects unless it allows that origin. The check adds nothing here, and it gets in the way. Hence the exercise's advice: a &lt;code&gt;QUERY&lt;/code&gt; search belongs in &lt;code&gt;routes/api.php&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How frameworks take care of it
&lt;/h2&gt;

&lt;p&gt;The principle is the same everywhere. The differences come down to three points: where the token is stored, how it is sent, and what happens when it is missing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Symfony
&lt;/h3&gt;

&lt;p&gt;The Form component adds and checks the token on its own, in a hidden &lt;code&gt;_token&lt;/code&gt; field. For a hand-written form, you generate the token with &lt;code&gt;csrf_token('token_id')&lt;/code&gt; in Twig and check it with &lt;code&gt;isCsrfTokenValid()&lt;/code&gt; in the controller, or with the &lt;code&gt;#[IsCsrfTokenValid]&lt;/code&gt; attribute:&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="na"&gt;#[Route('/dish/{id}/delete', methods: ['POST'])]&lt;/span&gt;
&lt;span class="na"&gt;#[IsCsrfTokenValid(new Expression('"delete-dish-" ~ args["dish"].getId()'), tokenKey: '\_token')]&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;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Dish&lt;/span&gt; &lt;span class="nv"&gt;$dish&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;Response&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Historically, the token is stored in the session. Since Symfony 7.2, the &lt;code&gt;symfony/form&lt;/code&gt; recipe enables &lt;strong&gt;stateless tokens&lt;/strong&gt; for forms: instead of comparing the token with the session, Symfony checks that the &lt;code&gt;Origin&lt;/code&gt; or &lt;code&gt;Referer&lt;/code&gt; header matches the site's address, with a fallback cookie set by a small Stimulus controller. No session needs to be started just to display a form, which makes public pages easier to cache.&lt;/p&gt;

&lt;p&gt;When the check fails, the form is not processed and carries the error "The CSRF token is invalid. Please try to resubmit the form."&lt;/p&gt;

&lt;h3&gt;
  
  
  Laravel
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;PreventRequestForgery&lt;/code&gt; middleware (&lt;code&gt;ValidateCsrfToken&lt;/code&gt; before Laravel 13) applies to the whole &lt;code&gt;web&lt;/code&gt; group. Since Laravel 13, it first lets through requests marked &lt;code&gt;Sec-Fetch-Site: same-origin&lt;/code&gt; (see the &lt;code&gt;QUERY&lt;/code&gt; case above), then checks the token. In a Blade form, the &lt;code&gt;@csrf&lt;/code&gt; directive adds the hidden &lt;code&gt;_token&lt;/code&gt; field. For JavaScript calls, Laravel also accepts the token in the &lt;code&gt;X-CSRF-TOKEN&lt;/code&gt; header, or in &lt;code&gt;X-XSRF-TOKEN&lt;/code&gt;: Laravel sets an &lt;code&gt;XSRF-TOKEN&lt;/code&gt; cookie that libraries such as Axios send back in that header automatically.&lt;/p&gt;

&lt;p&gt;The token is stored in the session. When it is missing or does not match, Laravel responds with &lt;strong&gt;419 Page Expired&lt;/strong&gt; , a status code that does not exist in HTTP and that Laravel chose for this error. Exclusions are declared in &lt;code&gt;bootstrap/app.php&lt;/code&gt;:&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;withMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Middleware&lt;/span&gt; &lt;span class="nv"&gt;$middleware&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$middleware&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;preventRequestForgery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;except&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'webhooks/stripe'&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;Excluding a webhook is legitimate: it is not authenticated by a cookie, and it must have its own check, a signature. Excluding a route because it "was returning 419", on the other hand, almost always hides a forgotten &lt;code&gt;@csrf&lt;/code&gt; or a lost session.&lt;/p&gt;

&lt;h3&gt;
  
  
  Django
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;CsrfViewMiddleware&lt;/code&gt; middleware is enabled by default. In a template, &lt;code&gt;{% csrf_token %}&lt;/code&gt; adds the hidden field. The token is stored in a &lt;code&gt;csrftoken&lt;/code&gt; cookie (or in the session if you ask for it), and JavaScript sends it back in the &lt;code&gt;X-CSRFToken&lt;/code&gt; header. Django also checks the &lt;code&gt;Origin&lt;/code&gt; header, and over HTTPS the &lt;code&gt;Referer&lt;/code&gt;: behind a proxy or with several domains, you have to declare the accepted origins in &lt;code&gt;CSRF_TRUSTED_ORIGINS&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the check fails, Django responds with a &lt;strong&gt;403&lt;/strong&gt; and the "CSRF verification failed" page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rails
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ApplicationController&lt;/code&gt; inherits the protection: &lt;code&gt;protect_from_forgery&lt;/code&gt; is on by default, and raises an exception when the check fails. The &lt;code&gt;csrf_meta_tags&lt;/code&gt; helper puts the token in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;, and the form helpers add it on their own. On the JavaScript side, Rails UJS or Turbo send it back in the &lt;code&gt;X-CSRF-Token&lt;/code&gt; header.&lt;/p&gt;

&lt;p&gt;When the check fails, Rails raises &lt;code&gt;ActionController::InvalidAuthenticityToken&lt;/code&gt;, which results in a &lt;strong&gt;422&lt;/strong&gt; response.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Framework&lt;/th&gt;
&lt;th&gt;In the form&lt;/th&gt;
&lt;th&gt;JavaScript header&lt;/th&gt;
&lt;th&gt;On failure&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Symfony&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;_token&lt;/code&gt; field (Form component, &lt;code&gt;csrf_token()&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;none by default (stateless tokens: handled by the Stimulus controller)&lt;/td&gt;
&lt;td&gt;error on the form, or an exception when checked by hand&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Laravel&lt;/td&gt;
&lt;td&gt;&lt;code&gt;@csrf&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;X-CSRF-TOKEN&lt;/code&gt; or &lt;code&gt;X-XSRF-TOKEN&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;419 Page Expired&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Django&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{% csrf_token %}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;X-CSRFToken&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;403&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rails&lt;/td&gt;
&lt;td&gt;added by the form helpers&lt;/td&gt;
&lt;td&gt;&lt;code&gt;X-CSRF-Token&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;422&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  When the error hits a real user
&lt;/h2&gt;

&lt;p&gt;Whatever the framework, the same causes come up when a legitimate user gets rejected:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The session has expired.&lt;/strong&gt; The user left the tab open over lunch. It is the most common cause, and the only normal one: show a clear message and offer to resubmit the form.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The session cookie does not arrive.&lt;/strong&gt; A misconfigured cookie domain, a &lt;code&gt;Secure&lt;/code&gt; cookie on a site served over &lt;code&gt;http&lt;/code&gt;, a cookie blocked by the browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The page is served from a cache.&lt;/strong&gt; An HTTP cache hands out the same page, and therefore the same token, to everyone. Symfony's stateless tokens exist partly for this case.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Several servers do not share their sessions.&lt;/strong&gt; The page comes from server A, the submission lands on server B, which does not know the token.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The proxy is not declared.&lt;/strong&gt; The application thinks it is running over &lt;code&gt;http&lt;/code&gt; while the browser announces &lt;code&gt;https&lt;/code&gt;: every check that compares the origin fails (stateless Symfony, Django).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these causes is fixed by disabling the check.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I take away from it
&lt;/h2&gt;

&lt;p&gt;The CSRF token answers a precise problem: a browser that attaches an authentication cookie on its own to a request triggered by another site. Wherever that is not the case, such as a &lt;code&gt;Bearer&lt;/code&gt; API or a method another site cannot send without CORS, it adds almost nothing. Wherever it is the case, it is still necessary, even with &lt;code&gt;SameSite&lt;/code&gt;, which is only a second line of defence.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://apprendre.forelse.fr/en/practice/laravel-13-35-route-query?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=csrf-tokens-across-frameworks" rel="noopener noreferrer"&gt;exercise on the QUERY method&lt;/a&gt; takes a few minutes in the browser.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>symfony</category>
      <category>laravel</category>
    </item>
    <item>
      <title>Being logged in is not enough: asking for proof again before sensitive actions</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Sat, 24 Oct 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/being-logged-in-is-not-enough-asking-for-proof-again-before-sensitive-actions-28o9</link>
      <guid>https://dev.to/oipnet/being-logged-in-is-not-enough-asking-for-proof-again-before-sensitive-actions-28o9</guid>
      <description>&lt;p&gt;In most applications, authentication is a boolean. You are logged in or you are not, and once you are, everything is open: viewing your invoices, changing your email address, deleting your account, generating an API key.&lt;/p&gt;

&lt;p&gt;Yet "logged in" covers very different situations. A session opened a minute ago with a password and a code. A session restored this morning by a "remember me" cookie set three weeks ago. A computer left unlocked over lunch. A session cookie stolen by a browser extension. To read a dashboard, the difference hardly matters. To change the address used to reset the password, it matters a lot.&lt;/p&gt;

&lt;p&gt;GitHub asks for your password again before you delete your account or create an access token. Your bank asks for a code before a transfer to a new payee. This is called step-up authentication: you don't ask for everything all the time, but for an extra proof at the moment it counts.&lt;/p&gt;

&lt;p&gt;Symfony 8.2 finally brings the building blocks to do it properly. But before the code come design choices, and they are what makes the difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three questions instead of one
&lt;/h2&gt;

&lt;p&gt;An authenticated session answers one question: &lt;strong&gt;who&lt;/strong&gt; the user is. To decide whether they may perform a sensitive action, you need two more.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When&lt;/strong&gt; did they last prove their identity? Not when the session started: when they last did something only they could do.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How&lt;/strong&gt; did they prove it? A password, a code received by SMS, a security key, a magic link in their inbox.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions are not new. OpenID Connect has long answered them in the ID token: &lt;code&gt;auth_time&lt;/code&gt; says when the user authenticated, &lt;code&gt;amr&lt;/code&gt; (authentication methods references) lists the methods used, and the client can require a recent authentication with the &lt;code&gt;max_age&lt;/code&gt; parameter. RFC 8176 standardizes the &lt;code&gt;amr&lt;/code&gt; values: &lt;code&gt;pwd&lt;/code&gt; for a password, &lt;code&gt;sms&lt;/code&gt;, &lt;code&gt;otp&lt;/code&gt;, &lt;code&gt;hwk&lt;/code&gt; for a hardware key, and so on. If your application delegates login to an identity provider, a good part of this article is settled in that provider's configuration, and your application only has to read this information.&lt;/p&gt;

&lt;p&gt;If your application does the authenticating, it is up to it to remember these answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which actions to protect
&lt;/h2&gt;

&lt;p&gt;Protecting everything amounts to protecting nothing: a user asked for their password every ten minutes ends up pasting it from a text file, or leaving the service. I sort actions with three criteria.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does the action allow taking over the account?&lt;/strong&gt; Changing the email address, the password, the phone number, disabling two-factor authentication, adding a security key. These are the top targets, because an attacker who succeeds once no longer needs the stolen session: they have the account. Changing the email is the most underestimated: it doesn't look like a security action, but it is the path password resets go through.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is the action irreversible, or does it cost money?&lt;/strong&gt; A transfer, an order, deleting an account or a project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does the action export data in bulk?&lt;/strong&gt; A full export, creating an API key or an access token, adding a member with admin rights.&lt;/p&gt;

&lt;p&gt;Everything else (browsing, editing a public profile, changing preferences) can usually make do with the session.&lt;/p&gt;

&lt;p&gt;Two freshness levels are often enough. A proof less than a few hours old for sensitive pages to view, like invoices or login history. A proof a few minutes old for actions that hand over control of the account. The exact durations depend on your domain: a banking app does not have the same constraints as a project management tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asking for the right proof
&lt;/h2&gt;

&lt;p&gt;Asking for proof again, yes, but which one? Two mistakes come up often.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Asking for a weaker proof than the one used to log in.&lt;/strong&gt; If the user logged in with a password and a security key, asking only for a code sent by email before disabling two-factor authentication opens a shortcut for anyone with access to their inbox. The proof you ask for must be at least as strong as the one the action lets you change or disable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Believing all second factors are equal.&lt;/strong&gt; SMS is still widespread, but it is exposed to SIM swapping: NIST classifies it as a "restricted" authenticator, to be used only knowingly and with an alternative offered to the user. GitHub, for instance, does not accept it for its sudo mode. An authenticator app (TOTP) or a security key (WebAuthn) is stronger. SMS is better than nothing, but that is no reason to make it the only safeguard for a large transfer.&lt;/p&gt;

&lt;p&gt;And sometimes there is nothing to ask for: a user who never registered a phone cannot provide a code. Sending them to a form they cannot fill in leads nowhere. For them, a clear refusal, with instructions to add a second factor, is more honest than a dead end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't break the flow
&lt;/h2&gt;

&lt;p&gt;A successful re-authentication must take the user back exactly where they were going. Otherwise they start over, get tired, and rightly blame you for wasting their time.&lt;/p&gt;

&lt;p&gt;A classic trap: protecting only the &lt;code&gt;POST&lt;/code&gt; request that saves the form. The user fills in the address change form, submits it, gets sent to the password page… and their input is lost. Symfony's firewall, like most mechanisms of this kind, only remembers the target page for a read request (&lt;code&gt;GET&lt;/code&gt;), excluding Ajax. It cannot replay a &lt;code&gt;POST&lt;/code&gt;. The fix is simple: also protect the &lt;strong&gt;display&lt;/strong&gt; of the form. The proof is requested before the user types anything, and it is still valid when they submit.&lt;/p&gt;

&lt;p&gt;For an API, redirecting to a form makes no sense. RFC 9470 (OAuth 2.0 Step-Up Authentication Challenge Protocol) describes the expected response: a 401 with a &lt;code&gt;WWW-Authenticate&lt;/code&gt; header carrying the &lt;code&gt;insufficient_user_authentication&lt;/code&gt; error, and optionally &lt;code&gt;max_age&lt;/code&gt; or &lt;code&gt;acr_values&lt;/code&gt; to say what is missing. The client then knows it must start a stronger authentication, rather than display "access denied".&lt;/p&gt;

&lt;h2&gt;
  
  
  The re-authentication screen is a login screen
&lt;/h2&gt;

&lt;p&gt;A page that only asks an already logged-in user for their password looks harmless. Yet it is exactly what someone who stole a session is looking for: a form to try passwords on, without having to guess the username. It therefore needs the same protections as a login form: attempt limiting, logging of failures, and one-time codes that expire quickly.&lt;/p&gt;

&lt;p&gt;After a successful sensitive action, notify the user through another channel. An email saying "your address was changed", sent to the &lt;strong&gt;old&lt;/strong&gt; address with a link to undo it, catches the case where every other protection was bypassed. It is the cheapest safety net in this article.&lt;/p&gt;

&lt;p&gt;Finally, a session restored by a "remember me" cookie must never count as a recent proof. The cookie proves that a browser was logged in at some point, not that the person in front of the screen knows the password.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before Symfony 8.2: a timestamp in the session
&lt;/h2&gt;

&lt;p&gt;Up to Symfony 8.1, nothing is built in for the "when" or the "how". &lt;code&gt;IS_AUTHENTICATED_FULLY&lt;/code&gt; only distinguishes a session opened by a form from one opened by the "remember me" cookie. So you have to record the time of the last proof yourself, for example in a &lt;code&gt;LoginSuccessEvent&lt;/code&gt; listener:&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="na"&gt;#[AsEventListener]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;\_\_invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoginSuccessEvent&lt;/span&gt; &lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The "remember me" cookie triggers this event too: it proves nothing.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAuthenticatedToken&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;RememberMeToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getRequest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getSession&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'last\_proof'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;time&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;Then a voter compares this timestamp with the current time. It works, but everything is on you: recording the method as well as the time, redirecting to the right page instead of a 403, not forgetting the cookie case. It is exactly the kind of code you write once, halfway, and never read again.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Symfony 8.2 brings
&lt;/h2&gt;

&lt;p&gt;Symfony 8.2 (still in development as I write this) builds this mechanism into the Security component, in four pieces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Freshness.&lt;/strong&gt; Two new attributes, &lt;code&gt;IS_AUTHENTICATED_RECENTLY&lt;/code&gt; (under two hours by default, the window of GitHub's sudo mode) and &lt;code&gt;IS_AUTHENTICATED_VERY_RECENTLY&lt;/code&gt; (under five minutes), work wherever &lt;code&gt;IS_AUTHENTICATED_FULLY&lt;/code&gt; does: &lt;code&gt;#[IsGranted]&lt;/code&gt;, &lt;code&gt;access_control&lt;/code&gt;, Twig. The durations are set in &lt;code&gt;security.yaml&lt;/code&gt;. A session opened by a "remember me" cookie fails both.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Re-authentication instead of refusal.&lt;/strong&gt; When one of these attributes is denied, the firewall no longer responds with a 403: it remembers the requested page and calls a re-authentication entry point. &lt;code&gt;form_login&lt;/code&gt; provides one, which redirects to the login page with the username already filled in. For a page that only asks for the password, you write your own by implementing &lt;code&gt;ReAuthenticationEntryPointInterface&lt;/code&gt;, and declare it on the firewall with &lt;code&gt;re_authentication_entry_point&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The "how".&lt;/strong&gt; The token now records every method proven during the session, with the time it was last used, using the names from RFC 8176:&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;$token&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAuthenticationProofs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// ['pwd' =&amp;gt; 1792300000, 'sms' =&amp;gt; 1792300060]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An authenticator declares its method with an &lt;code&gt;AuthenticationMethodBadge&lt;/code&gt; on the passport. Only interactive authentications count, and the "remember me" cookie is explicitly excluded. An authenticator without a badge is recorded under the &lt;code&gt;*&lt;/code&gt; method, unspecified. On the OpenID Connect side, the OIDC authenticator reads &lt;code&gt;amr&lt;/code&gt; and &lt;code&gt;auth_time&lt;/code&gt; from the ID token to feed these proofs. A voter can then require, for example, an &lt;code&gt;sms&lt;/code&gt; proof less than five minutes old before a transfer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your own rules.&lt;/strong&gt; A voter can tell the firewall that a new proof would change its answer, with &lt;code&gt;$vote-&amp;gt;requestReAuthentication($attribute)&lt;/code&gt;. The entry point reads which attribute was denied and chooses what to ask for: a code for the transfer, the password for everything else. And the voter can also signal nothing, for the user who has no second factor: they keep their 403. One limit to know: the mechanism only kicks in when a single attribute is denied. A check combining several always yields a 403.&lt;/p&gt;

&lt;p&gt;The when, the how and what to ask for again each have their answer in the framework. The choices remain yours: which actions to protect, with which proof, and for how long.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;I turned these features into three exercises, to do in your browser, free and without an account, on a development build of Symfony 8.2:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://apprendre.forelse.fr/en/practice/symfony-8-2-mode-sudo?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=asking-for-proof-again-before-sensitive-actions" rel="noopener noreferrer"&gt;Asking for the password again before sensitive pages&lt;/a&gt;, sudo mode;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://apprendre.forelse.fr/en/practice/symfony-8-2-preuve-code-sms?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=asking-for-proof-again-before-sensitive-actions" rel="noopener noreferrer"&gt;Require an SMS code before a transfer&lt;/a&gt;, authentication proofs;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://apprendre.forelse.fr/en/practice/symfony-8-2-reauthentification-par-facteur?utm_source=rss&amp;amp;utm_medium=feed&amp;amp;utm_campaign=asking-for-proof-again-before-sensitive-actions" rel="noopener noreferrer"&gt;Ask for the right factor again instead of a 403&lt;/a&gt;, re-authentication from your voters.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>symfony</category>
      <category>security</category>
      <category>authentication</category>
    </item>
    <item>
      <title>PHP2Shell: how I patched my Symfony server</title>
      <dc:creator>Arnaud POINTET</dc:creator>
      <pubDate>Thu, 22 Oct 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/oipnet/php2shell-how-i-patched-my-symfony-server-1j2a</link>
      <guid>https://dev.to/oipnet/php2shell-how-i-patched-my-symfony-server-1j2a</guid>
      <description>&lt;p&gt;On the evening of 20 October, two critical flaws were disclosed in PHP: CVE-2026-109618 and CVE-2026-109619, quickly nicknamed "PHP2Shell". They allow unauthenticated remote code execution on the server, and are rated 9.2 out of 10. By the next morning, real-world attacks were already being reported.&lt;/p&gt;

&lt;p&gt;These flaws are not in Symfony or Laravel, but in the PHP interpreter. No &lt;code&gt;composer update&lt;/code&gt; fixes them: you have to change PHP itself. Here is how I did it for apprendre.forelse.fr, which runs on Symfony and FrankenPHP in Docker.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Know which PHP actually runs
&lt;/h2&gt;

&lt;p&gt;First question: which version of PHP is serving requests? Not the one on my laptop, nor the one written in &lt;code&gt;composer.json&lt;/code&gt;, but the one in the production container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker compose exec app php -v


PHP 8.4.26 (cli) (built: Oct 6 2026 01:25:42) (ZTS)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PHP 8.4.26: the latest version published at that point, and yet vulnerable. The fix did not exist yet.&lt;/p&gt;

&lt;p&gt;My &lt;code&gt;Dockerfile&lt;/code&gt; starts from &lt;code&gt;dunglas/frankenphp:1-php8.4&lt;/code&gt;. That tag follows new releases of PHP 8.4, which gives the impression of always being up to date. It is not: the image is frozen at the moment it is built. Mine dated from 16 October. Until I rebuild it, it keeps the PHP from that day, even after FrankenPHP publishes a fixed image.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A floating tag (&lt;code&gt;php8.4&lt;/code&gt;, &lt;code&gt;latest&lt;/code&gt;) only gives you the latest version at the moment you rebuild the image.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2. A stopgap while waiting for the fix
&lt;/h2&gt;

&lt;p&gt;The official fix was announced for 21 October at 14:00 UTC. In the meantime, I reduced the attack surface. Both flaws are memory issues. The first (CVE-2026-109618) is a memory corruption: a multipart request crafted to exhaust memory abruptly aborts request processing. The second (CVE-2026-109619) is a use-after-free between two requests served by the same worker. According to Rapid7's analysis, its first known trigger is upload progress tracking, &lt;code&gt;session.upload_progress&lt;/code&gt;. It is enabled by default, and usable without even calling &lt;code&gt;session_start()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My application does not use that tracking. So I turned it off in &lt;code&gt;php.ini&lt;/code&gt;:&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="n"&gt;session&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upload\_progress&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Off&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file ships inside the image: the change only reaches production after a rebuild and a redeploy, like any other config. Then you check it was applied (in the FrankenPHP image, the CLI and the server read the same &lt;code&gt;php.ini&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;app php &lt;span class="nt"&gt;-i&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;upload_progress.enabled

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not a fix. It closes the first known trigger of the second flaw, but not the memory corruption of the first. One unused option switched off is one less open door until the real fix is in.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Rebuild the image with the fixed PHP
&lt;/h2&gt;

&lt;p&gt;The fix shipped on 21 October 2026: PHP 8.4.27 for my branch (and 8.2.35, 8.3.36, 8.5.12 for the others). FrankenPHP followed the same evening with its 1.13.2 release, whose images already bundle these fixed PHP versions.&lt;/p&gt;

&lt;p&gt;My &lt;code&gt;Dockerfile&lt;/code&gt; started from &lt;code&gt;dunglas/frankenphp:1-php8.4&lt;/code&gt;, a floating tag. I took the opportunity to pin an exact version: &lt;code&gt;dunglas/frankenphp:1.13.2-php8.5&lt;/code&gt;, which bundles PHP 8.5.12, fixed as well. To stay on PHP 8.4 without switching branches, &lt;code&gt;1.13.2-php8.4&lt;/code&gt; gives you 8.4.27.&lt;/p&gt;

&lt;p&gt;That leaves rebuilding the image. Since hosted CI runners are fresh every time, the &lt;code&gt;FROM&lt;/code&gt; re-pulls the base image. But on a machine that already has an image cached, Docker would reuse it: to be safe, force a re-pull of the base image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;--pull&lt;/span&gt; &lt;span class="nt"&gt;-t&lt;/span&gt; my-image &lt;span class="nb"&gt;.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In CI with &lt;code&gt;docker/build-push-action&lt;/code&gt;, the equivalent is the &lt;code&gt;pull: true&lt;/code&gt; option. The layer cache (&lt;code&gt;cache-from&lt;/code&gt;) is not a problem here: its key depends on the base image's digest, which has changed, so every layer that builds on it is rebuilt.&lt;/p&gt;

&lt;p&gt;Then redeploy, and check again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;app php &lt;span class="nt"&gt;-v&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The version shown should now be 8.5.12. As long as it says 8.4.26, the old image is still running.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. A safety net for next time
&lt;/h2&gt;

&lt;p&gt;What bothered me most in this episode was not knowing right away that my production server was still running a vulnerable PHP. &lt;code&gt;composer audit&lt;/code&gt; only looks at PHP dependencies, not the interpreter. So I added a safety net to my CI: when I cut a release (a tag), a job reads the PHP actually bundled in the image and compares it to the latest published version of its branch. If it is behind, the job raises a clearly visible warning on the release.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# PHP bundled in the image we are about to publish&lt;/span&gt;
&lt;span class="nv"&gt;installed&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;--entrypoint&lt;/span&gt; php &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$IMAGE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="s1"&gt;'echo PHP\_VERSION;'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;branch&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;installed&lt;/span&gt;&lt;span class="p"&gt;%.\*&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="c"&gt;# 8.5.12 -&amp;gt; 8.5&lt;/span&gt;

&lt;span class="c"&gt;# Latest published version of that branch, per php.net&lt;/span&gt;
&lt;span class="nv"&gt;latest&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;curl &lt;span class="nt"&gt;-fsS&lt;/span&gt; &lt;span class="s2"&gt;"https://www.php.net/releases/?json&amp;amp;version=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;branch&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;max=1"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  | jq &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="s1"&gt;'keys\_unsorted[0]'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;dpkg &lt;span class="nt"&gt;--compare-versions&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$installed&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; lt &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$latest&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"::warning::PHP &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;installed&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is behind &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;latest&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, consider bumping the base image."&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I wired it so that it only runs when cutting a release, not on other builds. I chose a warning rather than a failure: being one patch behind should not stop me from shipping, but it must be visible. This is not protection against the flaws themselves: it is a reminder, at the right moment, that the base image has fallen behind. Paired with a tool that watches the base image (Renovate, Dependabot), the window between "a fixed PHP exists" and "my production runs it" closes on its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. What I take away
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Watch PHP, not just Composer.&lt;/strong&gt; &lt;code&gt;composer audit&lt;/code&gt; does not see flaws in the interpreter. Hence the CI safety net from section 4, which compares the image's PHP to the latest published version.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rebuild regularly, and track the base image.&lt;/strong&gt; An image you never rebuild quietly goes stale. And if you pin an exact base version, which is safer for reproducibility, you need a tool like Renovate or Dependabot to get notified of new versions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Turn off what you don't use.&lt;/strong&gt; &lt;code&gt;session.upload_progress&lt;/code&gt; is enabled by default, while few applications use it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check inside the container.&lt;/strong&gt; The only version that matters is the one &lt;code&gt;php -v&lt;/code&gt; returns in production.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.rapid7.com/blog/post/etr-cve-2026-109618-and-cve-2026-109619-critical-unauthenticated-remote-code-execution-in-php/" rel="noopener noreferrer"&gt;Rapid7 analysis of both CVEs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/php/frankenphp/releases/tag/v1.13.2" rel="noopener noreferrer"&gt;FrankenPHP v1.13.2 (release fixing both CVEs)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://patchstack.com/articles/php-major-vulnerability-alert-10-2026/" rel="noopener noreferrer"&gt;Patchstack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://watchtowr.com/intelligence/php-cve-2026-109618-faq/" rel="noopener noreferrer"&gt;watchTowr&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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