Error -> cannot generate CSRF token for a host because

hmm, I ran a mix deps.update --all and I think I got a new version of plug (1.5) installed.
Im getting an error now on a call to a different Phoenix umbrella app.

cannot generate CSRF token for a host because get_csrf_token_for/1 is invoked in a separate process than the one that started the request

Im running an umbrella behind a proxy, so I think the error is correct, its a separate proces where the call is coming from :blush:

I read in the plug documentation allow_hosts option, maybe this would solve my problem?
But where so i set this option in phoenix?

1 Like

I’m also experiencing this issue.

A few things could cause this, one is not having the CSRF protection plugin the pipeline, another would be having a server handle the request for the wrong host, etc…

See code at:

1 Like

Thanks for help. FWIW I’m seeing this error in a Phoenix app which appears to be invoking the Plug.CSRFProtection, i.e. it’s not explicitly listed in my Plug pipeline.

 test/views/layout_view_test.exs:38
 ** (RuntimeError) cannot generate CSRF token for a host because get_csrf_token_for/1 is invoked in a separate process than the one that started the request
 stacktrace:
   (plug) lib/plug/csrf_protection.ex:181: Plug.CSRFProtection.get_csrf_token_for/1
   (phoenix_html) lib/phoenix_html/tag.ex:267: Phoenix.HTML.Tag.csrf_token_tag/3
   (phoenix_html) lib/phoenix_html/tag.ex:220: Phoenix.HTML.Tag.form_tag/2
   (phoenix_html) lib/phoenix_html/form.ex:288: Phoenix.HTML.Form.form_for/4
...

Looks like maybe Phoenix.HTML doesn’t provide a way to thread the :allow_hosts option through to its call to Plug.CSRFProtection.get_csrf_token_for:

So is this only happening during testing?

Nope, it happens on :dev :test and :prod

I ‘fixed’ my problem to move the rendering of form partials to the same host as where the call is coming from.

I didnt look further in it for now because I needed a quick fix. It seems to do with plug 1.5 and html_phoenix, form_for

Wait what? How are you generating form partials on ‘other’ servers? o.O

Yes I do

How though? I’m quite curious! :slight_smile:

The allow_hosts option will be configured with the protect_from_forgery plug in your router!

2 Likes

I can’t find any help on how to do this?