Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

CodeIgniter 3 shows 'The action you have requested is not allowed.'

ErrorThe action you have requested is not allowed.

Short answer

With csrf_protection on, CodeIgniter 3 compares the POST token with the CSRF cookie and returns this 403 when either is missing or they differ. Add the hidden field to the form, resend the new token after every Ajax post, or exclude real API URIs.

  • Applies to CodeIgniter 3.1, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check the config first. If csrf_protection is TRUE, every POST must carry the token that matches the cookie.

    grep -n "csrf_" application/config/config.php
  2. 2Match what you see, then jump to its fix

    1. 1A form written in plain HTML fails, while forms made with form_open() workThe form does not send the token fieldFix
    2. 2The first POST works; a second POST from the same page, a second tab or Back failsThe token is regenerated on every submissionFix
    3. 3Webhooks or mobile API calls to a POST route fail with the 403An external or API route is checked as if it were a browser formFix
    4. 4Even a fresh form fails, and the browser shows no csrf cookieThe CSRF cookie is never stored by the browserFix

What does the error look like?

  • The action you have requested is not allowed.
  • 403 Forbidden

Cause 1 of 4

The form does not send the token field

1Check

form_open() adds the hidden CSRF field for you. A hand-written form does not.

grep -rn '<form' application/views | grep -v form_open

2Fix

Use form_open(), or add the field yourself with the token name and hash helpers.

<form method="post" action="/save">
  <input type="hidden"
         name="<?= $this->security->get_csrf_token_name(); ?>"
         value="<?= $this->security->get_csrf_hash(); ?>">
</form>

3Verify

Submit the form; the save runs and no 403 appears.

curl -i -c jar.txt http://localhost/form

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

The token is regenerated on every submission

1Check

csrf_regenerate is TRUE by default, so each POST invalidates the old token. The docs name back/forward navigation, multiple tabs and asynchronous actions as the problem cases.

grep -n "csrf_regenerate" application/config/config.php

2Fix

For Ajax, take the new token from each response and use it on the next post. Or set csrf_regenerate to FALSE if you accept a token that lasts as long as the cookie.

$config['csrf_regenerate'] = FALSE;

3Verify

Post twice from the same page without reloading.

# two successful posts in a row

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

An external or API route is checked as if it were a browser form

1Check

A POST is validated unless its URI matches csrf_exclude_uris. These callers have no CSRF cookie.

grep -n "csrf_exclude_uris" application/config/config.php

2Fix

List only those routes; regular expressions work and matching ignores case.

$config['csrf_exclude_uris'] = array('api/webhook/.+');

3Verify

Post to the route without a token; it is accepted by your own authentication.

curl -i -X POST http://localhost/api/webhook/test

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

The CSRF cookie is never stored by the browser

1Check

With cookie_secure TRUE the cookie is only sent over HTTPS: the framework returns without setting it on a plain HTTP request. The check needs both the POST value and the cookie.

grep -n -E "cookie_secure|cookie_domain|cookie_prefix" application/config/config.php

2Fix

Serve the site over HTTPS, or set cookie_secure to FALSE in development.

$config['cookie_secure'] = FALSE; // development only

3Verify

Load the form and see the csrf cookie in the browser, then submit.

curl -i http://localhost/form | grep -i set-cookie

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Build forms with form_open() so the field is always present.
  • Keep csrf_exclude_uris short and specific, and protect those routes another way.
  • Test Ajax pages with two posts in a row, not one.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.