<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Yılmaz</title>
    <description>The latest articles on Forem by Yılmaz (@yidemir).</description>
    <link>https://forem.com/yidemir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F50529%2F397d35bf-0344-4fb1-8a12-3b4550a5a52f.jpg</url>
      <title>Forem: Yılmaz</title>
      <link>https://forem.com/yidemir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yidemir"/>
    <language>en</language>
    <item>
      <title>How Laravel's TrimStrings Middleware Can Cause Issues</title>
      <dc:creator>Yılmaz</dc:creator>
      <pubDate>Mon, 19 Aug 2024 10:12:06 +0000</pubDate>
      <link>https://forem.com/yidemir/how-trimstrings-can-become-a-problem-for-you-in-laravel-o4k</link>
      <guid>https://forem.com/yidemir/how-trimstrings-can-become-a-problem-for-you-in-laravel-o4k</guid>
      <description>&lt;p&gt;Laravel is a popular framework in the PHP world, offering developers great tools to simplify their work. However, sometimes these conveniences can lead to unexpected problems. In this post, I'll discuss how the &lt;strong&gt;TrimStrings&lt;/strong&gt; middleware in Laravel can cause issues and how to solve them.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is TrimStrings Middleware and What Does It Do?
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;TrimStrings&lt;/strong&gt; middleware is used in Laravel applications to automatically trim whitespace from incoming request data, such as form inputs. This is particularly useful when users accidentally leave spaces at the beginning or end of input fields. For example, if a user enters " &lt;a href="mailto:user@example.com"&gt;user@example.com&lt;/a&gt; " with spaces around the email address in a form, the &lt;strong&gt;TrimStrings&lt;/strong&gt; middleware will trim these spaces, ensuring that only "&lt;a href="mailto:user@example.com"&gt;user@example.com&lt;/a&gt;" is processed.&lt;/p&gt;

&lt;p&gt;This feature is beneficial for preventing errors caused by unnecessary whitespace and for handling cleaner data. However, as always, in certain special cases, this default behavior can lead to unintended consequences.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Happened?
&lt;/h3&gt;

&lt;p&gt;In a project where we were integrating with a Brazil-based payment provider, we needed to capture and validate payment results through a callback system. The payment provider sends the transaction result to our server via a POST request, and we validate the request by performing a &lt;strong&gt;signature/hash verification&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This verification process follows a straightforward logic:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We take the data sent by the provider.&lt;/li&gt;
&lt;li&gt;All the data is concatenated into a single string.&lt;/li&gt;
&lt;li&gt;This string is hashed using the &lt;strong&gt;SHA256&lt;/strong&gt; algorithm with a &lt;strong&gt;secret key&lt;/strong&gt; provided by the payment provider.&lt;/li&gt;
&lt;li&gt;The resulting hash is compared with the hash sent by the provider. If they match, the request is accepted; otherwise, it is rejected.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  How Did We Identify the Problem?
&lt;/h3&gt;

&lt;p&gt;Initially, it was difficult to understand why some valid requests were being rejected. However, after inspecting the Nginx logs, we noticed that the &lt;strong&gt;full_name&lt;/strong&gt; parameter in the incoming request retained trailing spaces. Despite this, on our server, these spaces had been trimmed, causing the hash verification to fail. That’s when we realized that the TrimStrings middleware was causing this issue.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s the Solution?
&lt;/h3&gt;

&lt;p&gt;To avoid such problems, it is necessary to disable the &lt;strong&gt;TrimStrings&lt;/strong&gt; middleware for specific routes or requests. Laravel 8 introduced the &lt;strong&gt;TrimStrings::skipWhen&lt;/strong&gt; method, which provides a tailored solution for this situation.&lt;/p&gt;

&lt;p&gt;Below is an example of how to apply this solution using a provider:&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;Illuminate\Foundation\Http\Middleware\TrimStrings&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;Illuminate\Http\Request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="nc"&gt;TrimStrings&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;skipWhen&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;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;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;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'api/v1/integrations/foo-provider/callback'&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;This code snippet disables the TrimStrings middleware for a specific route. In this case, trimming will not occur for requests coming from the &lt;code&gt;api/v1/integrations/foo-provider/callback&lt;/code&gt; route, ensuring that the hash verification process works smoothly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Laravel's default features generally make things easier, but in certain scenarios, they can lead to unexpected results. Therefore, it’s important to understand how the tools we use operate and to carefully evaluate their potential impacts. While the &lt;strong&gt;TrimStrings&lt;/strong&gt; middleware is a useful tool in most cases, it can cause issues in scenarios like this. Fortunately, flexible solutions like &lt;strong&gt;TrimStrings::skipWhen&lt;/strong&gt; allow us to avoid such problems.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>security</category>
    </item>
  </channel>
</rss>
