shahryarjb

shahryarjb

How to add SameSite Cookies on phoenix

Hi, I have 4 sites script js on my all page source, and the Chrome shows me some warnings.

these errors:

how to fix and add these site in Phoenix as SameSite ?

my endpoint:

  plug Plug.Session,
    store: :cookie,
    secure: true,
    key: "_change_me_key",
    signing_salt: "change_me"

First Post! Switch mode

ukutaht

ukutaht

Hey, currently you can use the extra key in Plug.Session configuration:

  plug Plug.Session,
    store: :cookie,
    secure: true,
    key: "_change_me_key",
    signing_salt: "change_me",
    extra: "SameSite=Strict"

Most Liked

lucaong

lucaong

The documentation for Plug.Session you linked points to the documentation for Plug.Conn, where the option is described:

:same_site - set the cookie SameSite attribute to a string value. If no string value is set, the attribute is omitted.

The possible values for the SameSite cookie attributes are an HTTP standard, and not related to Phoenix or Plug. You can find some documentation here about the meaning of the three possible values: Lax, Strict, and None.

Setting :extra as advised by @ukutaht is also working, but as there is a specific :same_site option I would advise using that (it’s available from v1.10.1 apparently):

plug Plug.Session,
  same_site: "Strict",
  # ...other options
nathanl

nathanl

SameSite is wonderful. You might want to consider whether Lax or Strict is a better choice for your site, though. Strict means, among other things, that if somebody follows a link to your site, their browser won’t send the cookie with that request.

See Preventing CSRF with the same-site cookie attribute

lucaong

lucaong

It’s apparently only available since the latest version currently published, v1.10.1 :slight_smile: I edited my answer above to add this info, in case some readers are using older versions.

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement