<?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: Safi Ullah</title>
    <description>The latest articles on DEV Community by Safi Ullah (@safiullah).</description>
    <link>https://dev.to/safiullah</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%2F2409252%2Fcee364de-fb99-4ecc-84e4-f26c4b9014be.jpg</url>
      <title>DEV Community: Safi Ullah</title>
      <link>https://dev.to/safiullah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/safiullah"/>
    <language>en</language>
    <item>
      <title>Understanding Cookies in Rails: A Developer's Guide</title>
      <dc:creator>Safi Ullah</dc:creator>
      <pubDate>Mon, 25 Nov 2024 01:59:19 +0000</pubDate>
      <link>https://dev.to/safiullah/understanding-cookies-in-rails-a-developers-guide-159e</link>
      <guid>https://dev.to/safiullah/understanding-cookies-in-rails-a-developers-guide-159e</guid>
      <description>&lt;p&gt;Hey fellow developers! Let's dive into the fascinating world of cookies in Rails. If you’ve ever spent countless hours debugging cookie-related issues, you’re not alone. These little data morsels can be both incredibly useful and a source of endless frustration. In this article, I’ll share what I’ve learned about handling cookies effectively in Rails—covering everything from common pitfalls to best practices. Let’s get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Cookies Really For?
&lt;/h2&gt;

&lt;p&gt;Before we get technical, let's get something straight - cookies are basically like little sticky notes your web application attaches to each visitor's browser. They're perfect for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keeping users logged in&lt;/li&gt;
&lt;li&gt;Remembering language preferences&lt;/li&gt;
&lt;li&gt;Storing shopping cart items&lt;/li&gt;
&lt;li&gt;Tracking user sessions&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Three Musketeers of Cookies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Plain Text Cookies:&lt;/strong&gt;
Think of these as writing on a postcard - anyone can read them. They're stored as regular text and look something like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cookies[:theme] = "dark_mode"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Signed Cookies:&lt;/strong&gt;
These are like a letter with a wax seal. The content is visible, but if someone tampers with it, you'll know. Rails uses a signature to ensure authenticity:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cookies.signed[:user_id] = 42 # Rails adds a signature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Encrypted Cookies:&lt;/strong&gt;
This is the Fort Knox of cookies. The data is encrypted, so nobody can read or modify it without the encryption key:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cookies.encrypted[:secret_data] = "super_secret_stuff"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Detective Work: Identifying Cookie Types
&lt;/h2&gt;

&lt;p&gt;Here's a real-world scenario I encountered. I needed to figure out what type of cookies my app was using. Here's how you can do it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Check Your Code:&lt;/strong&gt;
The quickest way is to look at where the cookie is set in your Rails code:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:my_cookie&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;            &lt;span class="c1"&gt;# Plain text cookie&lt;/span&gt;
&lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:my_cookie&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;# Signed cookie&lt;/span&gt;
&lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:my_cookie&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# Encrypted cookie&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inspect the Browser:&lt;/strong&gt; Open the Developer Tools in your browser (usually F12 or Cmd+Opt+I on a Mac), go to the Application tab, and look at the Cookies section. Plain text cookies will be readable. Signed cookies are encoded (often base64-encoded strings), while encrypted cookies appear as unreadable random characters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rails Logs:&lt;/strong&gt; In development, Rails may log information about cookies being set. Look for Set-Cookie headers in the logs to see cookie names and values, which can provide clues about the cookie type.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Reading Cookies: Rails Edition
&lt;/h2&gt;

&lt;p&gt;Working with cookies in Rails is surprisingly straightforward. Here's my go-to approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reading plain cookies (the simple way)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;current_theme = cookies[:theme]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Getting signed cookies (tamper-proof)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_id = cookies.signed[:user_id]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decrypting encrypted cookies&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;secret = cookies.encrypted[:secret_data]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JavaScript and Cookies: The Love-Hate Relationship
&lt;/h2&gt;

&lt;p&gt;Want to read cookies in JavaScript? Here's how:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The basic way
document.cookie

// A more elegant solution
function getCookie(name) {
  const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
  return match ? match[2] : null;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Keeping Your Cookies Safe
&lt;/h2&gt;

&lt;p&gt;After a security audit while working on the project, I learned some crucial ways to protect cookies from JavaScript manipulation:&lt;/p&gt;

&lt;h3&gt;
  
  
  Use the HttpOnly flag:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cookies[:auth_token] = {
  value: "secret123",
  httponly: true,
  secure: true,
  same_site: :strict
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Always encrypt sensitive data:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cookies.encrypted[:user_data] = {
  secret: "classified",
  expires: 1.hour.from_now
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Set appropriate expiration times:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cookies[:remember_me] = {
  value: "yes",
  expires: 30.days.from_now
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pro Tips From the Trenches
&lt;/h2&gt;

&lt;p&gt;After dealing with cookies in production, here are some lessons I learned the hard way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't store sensitive data in plain text cookies&lt;/li&gt;
&lt;li&gt;Always use encrypted cookies for anything user-specific&lt;/li&gt;
&lt;li&gt;Remember that cookies have size limits (usually 4KB)&lt;/li&gt;
&lt;li&gt;Test cookie behavior in different browsers&lt;/li&gt;
&lt;li&gt;Be careful with cookie expiration times&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Debugging Cookie Issues
&lt;/h2&gt;

&lt;p&gt;When things go wrong (and they will), here's my debugging checklist:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check cookie values in Rails console&lt;/li&gt;
&lt;li&gt;Verify cookie settings in browser dev tools&lt;/li&gt;
&lt;li&gt;Ensure secure flags are set correctly&lt;/li&gt;
&lt;li&gt;Test in incognito mode to rule out cache issues&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Cookies might seem simple on the surface, but they're a crucial part of modern web applications. Understanding the different types and knowing when to use each one will make your Rails applications more secure and efficient.&lt;/p&gt;

&lt;p&gt;Remember: with great cookie power comes great responsibility. Always think about security first, and your users will thank you for it!&lt;/p&gt;

&lt;p&gt;Got any cookie horror stories or tips to share? I'd love to hear them in the comments below!&lt;/p&gt;

&lt;p&gt;Happy coding! 🍪&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>cookies</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
