saveman71

saveman71

Hello !

We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id), so that changeset error re-renders keep the browser URL stable: F5 reloads the form instead of GET-ing the index/show URL, and our URL matching JS bundle loading keeps working without hacks.

This has been a recurring source of issues on our end so we’re currently writing an internal guide/credo check to enforce that.

Regarding the form target, in plain HTML you’d get this for free by omitting action: the form then submits to the current URL.

But Phoenix.Component.form/1 explicitly discards method and the CSRF token in that case (docs).

Today we pass the page’s own URL explicitly:

<.form for={@form} action={~p"/things/#{@thing}/edit"} method="put">

That works, but means that a template shared between new and edit has to conditionally rebuild the URL the browser is already on, adding extra steps to check and maintain.

Two questions:

  1. Is there a reason for discarding method/csrf_token when :action is absent for dead views?
  2. action="" seems to work as a “submit to current URL”: HTML treats an empty action as the document’s URL, and it’s truthy so the component renders method and the CSRF token. Is that supported behavior we can rely on?

A side effect is that it’s going to call Plug.CSRFProtection.get_csrf_token_for(""), which returns a valid token but unscoped as far as I understand. Is this bad?

Thanks in advance for any insight.

Showing Posts 1 to 5

nseaSeb

nseaSeb

<.form for={@form} action="" method={if @thing, do: "put", else: "post"}>
It guess in this case “” is truthy in Elixir, so <.form> takes the “action provided” branch and normally generates the hidden _method field and the CSRF token.
Maybe send the URL by another approach ?

LostKobrakai

LostKobrakai

Not just in this case. Only false and nil are falsy in elixir, everything else is truthy.

GrammAcc

GrammAcc

Regarding the last question about the unscoped token being bad or not, I think that would depend on your threat model and if you have multiple hostnames on the same application. Taking a look at the source plug/lib/plug/csrf_protection.ex at 9fa11c8ebedbe68531eba25d8f81b9282e0514da · elixir-plug/plug · GitHub calling Plug.CSRFProtection.get_csrf_token_for("") is the same as calling Plug.CSRFProtection.get_csrf_token/0 since URI.parse("") returns a %URI{} with all null values. So if your application’s threat model disallows get_csrf_token/0, then it would be bad. If get_csrf_token/0 isn’t an issue, then it’s not bad. :slight_smile:

saveman71

saveman71 OP

Thank you for your answer! We’ll have to determine if this is of a special concern to us or not.

I determined that we always hit [this clause](plug/lib/plug/csrf_protection.ex at 9fa11c8ebedbe68531eba25d8f81b9282e0514da · elixir-plug/plug · GitHub) anyway as of now as we never cared to provide full urls, so the initial question wasn’t really relevant, but we still need to determine if we should provide them from now on.

GrammAcc

GrammAcc

I think the most common scenario where this would be a concern would be if your application is using sub-domains. E.g. www.example.com and intranet.example.com You probably wouldn’t want a user to be able to make a fetch call from the console in their browser on the public site and get internal company pages. Ofc, in that scenario, you’d usually want to have your internal resources behind a company VPN or VLAN or something else that requires auth.

Another thing to keep in mind is that URI.parse/1 doesn’t return a host key unless the full URL with scheme is provided.

For example:

URI.parse("www.elixirforum.com") = %URI{path: "www.elixirforum.com", scheme: nil, port: nil, host: nil}
URI.parse("https://www.elixirforum.com") = %URI{path: nil, scheme: "https", port: 443, host: "www.elixirforum.com"}

Hope that helps. Cheers!

— All posts loaded —

Where Next? Top

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
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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

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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; 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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews