We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Disable CSRF token for a specific template

How Do I?

vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

I have a specific JavaScript template that makes a POST request to another EE template file that executes PHP and returns some result in AJAX.

This is safe by nature and does not require the form token check. But the JS will default to 403 denied error because Expression Engine thinks it’s trying to make a request to a POST form, which is not the case here.

The reason I want this in EE is that I need to pass some EE channel variables to the PHP code.

Is there a way I can tell EE to skip this POST check request? Maybe in the JS file or in the PHP template file?

Searching the docs I see there is csrf_exempt. Any clues how to use this on my PHP code so EE will not check for a token request?

       
Rob Allen's avatar
Rob Allen
3,105 posts
4 years ago
Rob Allen's avatar Rob Allen

The only way I know how to disable CRSF is via a config setting but that’s a site wide setting - https://docs.expressionengine.com/latest/general/system-configuration-overrides.html#disable_csrf_protection - I’m not sure you you can modify the disable config item on the fly.

       
Rob Allen's avatar
Rob Allen
3,105 posts
4 years ago
Rob Allen's avatar Rob Allen

I should have read the docs more - does this help? https://docs.expressionengine.com/latest/development/guidelines/security.html#disabling-the-check

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

Exactly, I don’t want to disable this globally. Just for this specific POST request.

I did read that documentation link you provided, but I’m not sure what they mean with “This is done by setting the csrf_exempt column in the actions table to 1 for that action.”

What column? There is no specific code example on how this works or what has to be passed on the request. This is the reason I posted this, I found that link as well, but there is not enough information in the docs on how to use this. Searching the web gave me no results either, at least not for Expression Engine.

       
Tom Jaeger's avatar
Tom Jaeger
449 posts
4 years ago
Tom Jaeger's avatar Tom Jaeger

By way of background, I do want to say that we don’t recommend disabling the CSRF token.

That being said, in the exp_actions table there is a column called “csrf_exempt” This is how you can remove the need for the CSRF token check on a given ExpressionEngine Action. An action can be defined on install or update of an add-on.

Thanks,

-Tom Jaeger

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

I don’t want to disable this for the whole site. Just for a specific template but I think I might better get someone that knows JavaScript and just try to pass the token.

       
Tom Jaeger's avatar
Tom Jaeger
449 posts
4 years ago
Tom Jaeger's avatar Tom Jaeger

Not 100% sure of everything your doing etc… You can also throw a CSRF tolken into your template by doing the following

<input type="hidden" name="csrf_token" value="{csrf_token}">

Thought it might be helpful to pass along.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.