Lesson 2 of 9 · 23m
The Request Lifecycle, Security Defaults, and Configuration That Matters
Understand middleware, strong parameters, credentials, encrypted configuration, CSRF, and the production switches experienced Rails teams verify first.
A Rails request looks simple until you operate one at 2 a.m.: web server, Rack, middleware, router, controller, model, view, response. Every production failure you care about lives on that path — a header trusted too early, a parameter accepted too broadly, a secret pasted into the wrong file, or a cache key poisoned by host ambiguity.
Follow the request before changing it
Rails inserts middleware for sessions, cookies, flash messages, method override, host authorization, and exception handling. Inspect the actual stack:
bin/rails middlewareOrdering matters. Authentication middleware cannot see decrypted session state if it runs before the session middleware. A malformed-host rejection that happens after expensive rendering wastes money. Read the stack before adding another gem that promises to solve a symptom.
Strong parameters are a trust boundary
Do not pass params into models. Filter the input shape explicitly:
def account_params
params.expect(account: [:name, :subdomain, :billing_email])
endOn older Rails versions the shape is params.require(:account).permit(...). Either way, this is not validation; it is admission control. role, account_id, plan, and server-owned relationships do not belong here unless the workflow intentionally exposes them.
Know your secret boundaries
Rails credentials are encrypted at rest, not magically safe after deployment:
EDITOR='code --wait' bin/rails credentials:edit
bin/rails credentials:showThe master key is a deployment secret. Keep it out of git, CI logs, shell history, and shared screenshots. Runtime environment variables are often better for per-environment values; credentials are useful for application-owned configuration that must be versioned with the app.
Production switches experienced teams check
config.force_ssl = true
config.action_dispatch.default_headers = { "X-Frame-Options" => "SAMEORIGIN",
"X-Content-Type-Options" => "nosniff", "Referrer-Policy" => "strict-origin-when-cross-origin" }
config.action_controller.default_url_options = { host: "app.exampleco.internal" }
config.hosts << "app.exampleco.internal"
config.active_record.migration_error = :page_loadIn a real app, use the public hostname rather than an internal placeholder. The point is not the string; it is that URL generation, host authorization, TLS, cookies, and cache behavior must agree.
CSRF and cookies are still application controls
Rails protects non-GET HTML requests with CSRF tokens, but APIs using bearer tokens often skip cookies entirely. Decide intentionally. Cookie authentication needs SameSite, secure flags, CSRF handling, and logout invalidation. Bearer authentication needs token lifetime, revocation, and storage discipline. Do not blur them because a frontend convenience demanded it.
Field notes
filter_parametershides values in Rails logs, not exception trackers or analytics platforms unless configured there too.config.hostsfailures after a deploy often mean the load balancer health-check hostname was never allowlisted.- Never trust
X-Forwarded-Forblindly; Rails proxy handling depends on which proxies sit in front of the app. - Secret rotation is a procedure, not a file edit. You need deploy ordering, rollback behavior, and cache/session consequences written down.
Was this lesson helpful?