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 From 5ac7c77ee60b108fb9dee84b5fc0acf04638c6f5 Mon Sep 17 00:00:00 2001 From: caseyh Date: Mon, 18 Aug 2014 05:10:24 -0400 Subject: Alter Pull #3176 to follow discussion --- system/core/Security.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/system/core/Security.php b/system/core/Security.php index a6fd75fa4..39e4f7c24 100755 --- a/system/core/Security.php +++ b/system/core/Security.php @@ -203,9 +203,9 @@ class CI_Security { if ($exclude_uris = config_item('csrf_exclude_uris')) { $uri = load_class('URI', 'core'); - foreach ($exclude_uris as $excluded) { - $excluded = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $excluded); - if (preg_match('#^'.$excluded.'$#', $uri->uri_string())) + foreach ($exclude_uris as $excluded) + { + if (preg_match('#^'.$excluded.'$#i'.(UTF8_ENABLED ? 'u' : ''), $uri->uri_string())) { return $this; } @@ -937,4 +937,4 @@ class CI_Security { } /* End of file Security.php */ -/* Location: ./system/core/Security.php */ \ No newline at end of file +/* Location: ./system/core/Security.php */ -- cgit v1.2.3-24-g4f1b From 8ef828129c559705447dd66a597071de5ae564a9 Mon Sep 17 00:00:00 2001 From: caseyh Date: Mon, 18 Aug 2014 05:13:11 -0400 Subject: Alter Pull #3176 - CSRF Whitelist --- user_guide_src/source/libraries/security.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/libraries/security.rst b/user_guide_src/source/libraries/security.rst index 566924398..19480b4f8 100644 --- a/user_guide_src/source/libraries/security.rst +++ b/user_guide_src/source/libraries/security.rst @@ -97,10 +97,9 @@ 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:: +Optionally, you can use regular expressions in the URIs:: - $config['csrf_exclude_uris'] = array('api/record/:num','api/title/[a-zA-Z]+'); + $config['csrf_exclude_uris'] = array('api/record/[0-9]+','api/title/[a-zA-Z]+'); *************** @@ -162,4 +161,4 @@ Class Reference This method acts a lot like PHP's own native ``html_entity_decode()`` function in ENT_COMPAT mode, only it tries to detect HTML entities that don't end in a semicolon because some browsers allow that. - If the ``$charset`` parameter is left empty, then your configured ``$config['charset']`` value will be used. \ No newline at end of file + If the ``$charset`` parameter is left empty, then your configured ``$config['charset']`` value will be used. -- cgit v1.2.3-24-g4f1b