Plug v1.7.1 Plug.CSRFProtection View Source
Plug to protect from cross-site request forgery.
For this plug to work, it expects a session to have been
previously fetched. It will then compare the token stored
in the session with the one sent by the request to determine
the validity of the request. For an invalid request the action
taken is based on the
The token may be sent by the request either via the params with key “_csrf_token” or a header with name “x-csrf-token”.
Note that it is recommended to enable CSRFProtection whenever a session is used, even for JSON requests. For example, Chrome had a bug that allowed POST requests to be triggered with arbitrary content-type, making JSON exploitable. More info: https://bugs.chromium.org/p/chromium/issues/detail?id=490015
This plug won’t generate tokens automatically. Instead, tokens
will be generated only when required by calling
In case you are generating the token for certain specific URL,
you should use
get_csrf_token_for/1 as that will avoid tokens
from being leaked to other applications.
Once a token is generated, it is cached in the process dictionary.
The CSRF token is usually generated inside forms which may be
Plug.Conn. Storing them in the process dictionary
allows them to be generated as a side-effect only when necessary,
becoming one of those rare situations where using the process
dictionary is useful.
If you are sending data to a full URI, such as
//external.com/path, instead of a simple path such as
/path, you may
want to consider using
get_csrf_token_for/1, as that will encode the host
in the CSRF token. Once received, Plug will only consider the CSRF token to
be valid if the
host encoded in the token is the same as the one in
Therefore, if you get a warning that the host does not match, it is either because someone is attempting to steal CSRF tokens or because you have a misconfigured host configuration.
For example, if you are running your application behind a proxy, the browser
will send a request to the proxy with
www.example.com but the proxy will
request you using an internal IP. In such cases, it is common for proxies
to attach information such as
"x-forwarded-host" that contains the original
This may also happen on redirects. If you have a POST request to
that redirects to
bar.example.com with status 307, the token will contain a
different host than the one in the request.
You can pass the
:allow_hosts option to control any host that you may want
to allow. The values in
:allow_hosts may either be a full host name or a
host suffix. For example:
will allow the exact host of
"www.example.com" and any host that ends with
:session_key- the name of the key in session to store the token under
:allow_hosts- a list with hosts to allow on cross-host tokens
:with- should be one of
:clear_session. Defaults to
:exception- for invalid requests, this plug will raise
:clear_session- for invalid requests, this plug will set an empty session for only this request. Also any changes to the session during this request will be ignored.
You may disable this plug by doing
Plug.Conn.put_private(conn, :plug_skip_csrf_protection, true). This was made
available for disabling
Plug.CSRFProtection in tests and not for dynamically
Plug.CSRFProtection in production code. If you want specific routes to
Plug.CSRFProtection, then use a different stack of plugs for that route that
does not include
plug Plug.Session, ... plug :fetch_session plug Plug.CSRFProtection
Link to this section Summary
Deletes the CSRF token from the process dictionary
Gets the CSRF token
Gets the CSRF token for the associated URL (as a string or a URI struct)
Link to this section Functions
Deletes the CSRF token from the process dictionary.
This will force the token to be deleted once the response is sent.
Gets the CSRF token.
Generates a token and stores it in the process dictionary if one does not exist.
Gets the CSRF token for the associated URL (as a string or a URI struct).
If the URL has a host, a CSRF token that is tied to that
host will be generated. If it is a relative path URL, a
simple token emitted with
get_csrf_token/0 will be used.