From 2f4c3bc5c2fac164d1c58ac9aaa09ae070687443 Mon Sep 17 00:00:00 2001 From: Casey Hancock Date: Mon, 11 Aug 2014 12:52:20 -0400 Subject: CSRF whitelist supports regex Signed-off-by: Casey Hancock --- system/core/Security.php | 11 +++++++---- user_guide_src/source/changelog.rst | 2 +- user_guide_src/source/libraries/security.rst | 6 ++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/system/core/Security.php b/system/core/Security.php index 741ff229b..a6fd75fa4 100755 --- a/system/core/Security.php +++ b/system/core/Security.php @@ -203,10 +203,13 @@ class CI_Security { if ($exclude_uris = config_item('csrf_exclude_uris')) { $uri = load_class('URI', 'core'); - if (in_array($uri->uri_string(), $exclude_uris)) - { - return $this; - } + foreach ($exclude_uris as $excluded) { + $excluded = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $excluded); + if (preg_match('#^'.$excluded.'$#', $uri->uri_string())) + { + return $this; + } + } } // Do the tokens exist in both the _POST and _COOKIE arrays? diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 2ed2275ac..2d523e932 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -507,7 +507,7 @@ Release Date: Not Released - Added method ``strip_image_tags()``. - Added ``$config['csrf_regeneration']``, which makes token regeneration optional. - - Added ``$config['csrf_exclude_uris']``, which allows you list URIs which will not have the CSRF validation methods run. + - Added ``$config['csrf_exclude_uris']``, which allows you list URIs which will not have the CSRF validation methods run. Optionally allows regex. - Modified method ``sanitize_filename()`` to read a public ``$filename_bad_chars`` property for getting the invalid characters list. - Return status code of 403 instead of a 500 if CSRF protection is enabled but a token is missing from a request. diff --git a/user_guide_src/source/libraries/security.rst b/user_guide_src/source/libraries/security.rst index fb875a0d9..566924398 100644 --- a/user_guide_src/source/libraries/security.rst +++ b/user_guide_src/source/libraries/security.rst @@ -97,6 +97,12 @@ by editing the 'csrf_exclude_uris' config parameter:: $config['csrf_exclude_uris'] = array('api/person/add'); +Optionally, you can use regular expressions as well as the ':any' and ':num' +wildcards in the URIs:: + + $config['csrf_exclude_uris'] = array('api/record/:num','api/title/[a-zA-Z]+'); + + *************** Class Reference *************** -- cgit v1.2.3-24-g4f1b