Understanding Rails

Understanding Rails · Season 4: Controllers, routing and auth

Sessions, cookies and the flash

Episode 29 · deep dive · about 4 minutes · Rails 8.1 · by

How was this episode?

The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.

Was it clear?

Not endorsed yet. Are you a Rails developer? What's an endorsement?

Endorse this version To endorse: create an account, confirm your email, then link GitHub.

Where Rails keeps state between requests, and how it's signed.

Free to watch. If it helped, support the series.

Transcript

0:00 Sessions, cookies and the flash

Episode twenty nine. Sessions, cookies and the flash. HTTP forgets you between requests, as episode one showed. Today: where Rails keeps what it remembers, and why you can't change it from the browser.

0:18 The session

Here's a tiny action. It adds one to session visits, and shows the count. The first request says visit one. The next says two, then three. Something carried that number from one request to the next.

0:32 The session cookie

That something is a cookie. The response sets one called switchback session. By default, Rails keeps the whole session inside it, in the browser, not on the server. Look at its flags: HTTP only, so JavaScript on the page can't read it, and same site lax, so other sites can't send it along with most of their requests.

0:53 Encrypted

And look at its value. The session cookie is encrypted. The browser holds it, but can't read it, and can't change it. Change four characters, and Rails can't decrypt it, so it starts a fresh session: visit one again.

1:07 Three kinds of cookie

You can set your own cookies, in three strengths. A plain cookie, theme equals dark, is just text. A signed cookie holds notebook id, seven. An encrypted cookie holds the last search.

1:20 Signed is not secret

A signed cookie isn't secret. Decode its first half, and there's the message, and inside it, seven. What the signature stops is changes. We rewrote the seven as an eight, and kept the old signature. Rails reads it back as nil.

1:36 Reading them back

Read them back in a controller, and you get dark, seven, and milk. The encrypted one only reads as milk through cookies dot encrypted. On its own, it's scrambled text. So: signed for values that may be seen but not changed, and encrypted for values nobody else should read.

1:54 Cookie overflow

Because the session lives in a cookie, it has a cookie's limit: about four kilobytes. Put a five thousand character draft in the session, and Rails refuses: cookie overflow, the session cookie overflowed with size nine thousand one hundred and fifty five bytes. Keep the session small: an id, not the record. Store the rest in the database.

2:16 The flash

The flash is part of the session, built for one message. Create a note, and the controller sets a notice, then redirects. Right after the post, the flash holds note was successfully created. The next page shows it. Load that page again, and it's gone.

2:33 flash.now

That's because a flash message lasts for exactly one more request. When you render instead of redirecting, use flash dot now. Its message shows on this page, and is cleared when the request ends. A plain flash message, set without now, survives one more.

2:51 Secure cookies

One more flag matters in production: secure, so the browser sends a cookie only over HTTPS. Force SSL turns it on. In a new app that deploys with Kamal, that line starts out commented, in the production config. Turn it on once your app is served over HTTPS.

3:10 The rules

So: the session is an encrypted cookie, small and per browser. Use signed cookies for values that may be seen, but not changed, and encrypted ones for secrets. Keep ids in the session, not records. And flash for one message, flash now when you render. Signing in builds on all of this, and that's next.

3:30 Next

Next: the authentication generator, line by line.

Check your understanding

  1. You set cookies.signed[:notebook_id] = 7. What can someone holding the cookie do?

    • Nothing: they can't read it or change it
    • Read the 7, but not change it without Rails noticing
    • Change it to 8, and Rails will accept it
    Show answer

    Read the 7, but not change it without Rails noticing. A signed cookie carries a signature that protects it from changes, but its value isn't hidden. A changed value reads as nil. Use cookies.encrypted to hide it too. Source: Action Controller: cookies (Rails Guides 8.1).

  2. Where does a new Rails app keep the session's data by default?

    • In an encrypted cookie in the browser, limited to about 4 kB
    • In a sessions table in the database
    • In the server's memory
    Show answer

    In an encrypted cookie in the browser, limited to about 4 kB. The default CookieStore keeps the session in a cookie, which holds about 4 kB. Keep ids there, not records. Source: Action Controller: session (Rails Guides 8.1).

  3. A create action fails and renders the form again. Which shows a message on that page, and not on the next one?

    • flash[:alert] = ...
    • session[:alert] = ...
    • flash.now[:alert] = ...
    Show answer

    flash.now[:alert] = .... A plain flash value is kept for the next request. Rendering doesn't make a new request, so flash.now shows the message now and drops it when the request ends. Source: Action Controller: flash.keep and flash.now (Rails Guides 8.1).

  4. A new app deploys with Kamal. Are its cookies marked secure in production right away?

    • Yes, always
    • No: config.force_ssl starts commented out, so turn it on once you serve HTTPS
    • Only the session cookie is
    Show answer

    No: config.force_ssl starts commented out, so turn it on once you serve HTTPS. In the production template, force_ssl (which forces SSL and uses secure cookies) is commented out when the app deploys with Kamal. Source: New app production config template (Rails source, v8.1.4.1).

Sources

Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.

Comments

Sign in to comment or sign up.