@@ -335,6 +335,8 @@ abstract class REST_Controller extends CI_Controller {
335
335
* @var bool
336
336
*/
337
337
protected $ _enable_xss = FALSE ;
338
+
339
+ private $ is_valid_request = TRUE ;
338
340
339
341
/**
340
342
* HTTP status codes and their respective description
@@ -631,17 +633,19 @@ public function _remap($object_called, $arguments = [])
631
633
$ this ->config ->item ('rest_status_field_name ' ) => FALSE ,
632
634
$ this ->config ->item ('rest_message_field_name ' ) => $ this ->lang ->line ('text_rest_unsupported ' )
633
635
], self ::HTTP_FORBIDDEN );
636
+
637
+ $ this ->is_valid_request = false ;
634
638
}
635
639
636
640
// Remove the supported format from the function name e.g. index.json => index
637
641
$ object_called = preg_replace ('/^(.*)\.(?: ' .implode ('| ' , array_keys ($ this ->_supported_formats )).')$/ ' , '$1 ' , $ object_called );
638
642
639
643
$ controller_method = $ object_called .'_ ' .$ this ->request ->method ;
640
- // Does this method exist? If not, try executing an index method
641
- if (!method_exists ($ this , $ controller_method )) {
642
- $ controller_method = "index_ " . $ this ->request ->method ;
643
- array_unshift ($ arguments , $ object_called );
644
- }
644
+ // Does this method exist? If not, try executing an index method
645
+ if (!method_exists ($ this , $ controller_method )) {
646
+ $ controller_method = "index_ " . $ this ->request ->method ;
647
+ array_unshift ($ arguments , $ object_called );
648
+ }
645
649
646
650
// Do we want to log this method (if allowed by config)?
647
651
$ log_method = ! (isset ($ this ->methods [$ controller_method ]['log ' ]) && $ this ->methods [$ controller_method ]['log ' ] === FALSE );
@@ -656,8 +660,8 @@ public function _remap($object_called, $arguments = [])
656
660
{
657
661
$ this ->_log_request ();
658
662
}
659
-
660
- // fix cross site to option request error
663
+
664
+ // fix cross site to option request error
661
665
if ($ this ->request ->method == 'options ' ) {
662
666
exit ;
663
667
}
@@ -666,6 +670,8 @@ public function _remap($object_called, $arguments = [])
666
670
$ this ->config ->item ('rest_status_field_name ' ) => FALSE ,
667
671
$ this ->config ->item ('rest_message_field_name ' ) => sprintf ($ this ->lang ->line ('text_rest_invalid_api_key ' ), $ this ->rest ->key )
668
672
], self ::HTTP_FORBIDDEN );
673
+
674
+ $ this ->is_valid_request = false ;
669
675
}
670
676
671
677
// Check to see if this key has access to the requested controller
@@ -680,6 +686,8 @@ public function _remap($object_called, $arguments = [])
680
686
$ this ->config ->item ('rest_status_field_name ' ) => FALSE ,
681
687
$ this ->config ->item ('rest_message_field_name ' ) => $ this ->lang ->line ('text_rest_api_key_unauthorized ' )
682
688
], self ::HTTP_UNAUTHORIZED );
689
+
690
+ $ this ->is_valid_request = false ;
683
691
}
684
692
685
693
// Sure it exists, but can they do anything with it?
@@ -689,6 +697,8 @@ public function _remap($object_called, $arguments = [])
689
697
$ this ->config ->item ('rest_status_field_name ' ) => FALSE ,
690
698
$ this ->config ->item ('rest_message_field_name ' ) => $ this ->lang ->line ('text_rest_unknown_method ' )
691
699
], self ::HTTP_METHOD_NOT_ALLOWED );
700
+
701
+ $ this ->is_valid_request = false ;
692
702
}
693
703
694
704
// Doing key related stuff? Can only do it if they have a key right?
@@ -699,6 +709,8 @@ public function _remap($object_called, $arguments = [])
699
709
{
700
710
$ response = [$ this ->config ->item ('rest_status_field_name ' ) => FALSE , $ this ->config ->item ('rest_message_field_name ' ) => $ this ->lang ->line ('text_rest_api_key_time_limit ' )];
701
711
$ this ->response ($ response , self ::HTTP_UNAUTHORIZED );
712
+
713
+ $ this ->is_valid_request = false ;
702
714
}
703
715
704
716
// If no level is set use 0, they probably aren't using permissions
@@ -716,6 +728,8 @@ public function _remap($object_called, $arguments = [])
716
728
// They don't have good enough perms
717
729
$ response = [$ this ->config ->item ('rest_status_field_name ' ) => FALSE , $ this ->config ->item ('rest_message_field_name ' ) => $ this ->lang ->line ('text_rest_api_key_permissions ' )];
718
730
$ this ->response ($ response , self ::HTTP_UNAUTHORIZED );
731
+
732
+ $ this ->is_valid_request = false ;
719
733
}
720
734
}
721
735
@@ -724,6 +738,8 @@ public function _remap($object_called, $arguments = [])
724
738
{
725
739
$ response = [$ this ->config ->item ('rest_status_field_name ' ) => FALSE , $ this ->config ->item ('rest_message_field_name ' ) => $ this ->lang ->line ('text_rest_ip_address_time_limit ' )];
726
740
$ this ->response ($ response , self ::HTTP_UNAUTHORIZED );
741
+
742
+ $ this ->is_valid_request = false ;
727
743
}
728
744
729
745
// No key stuff, but record that stuff is happening
@@ -735,7 +751,9 @@ public function _remap($object_called, $arguments = [])
735
751
// Call the controller method and passed arguments
736
752
try
737
753
{
738
- call_user_func_array ([$ this , $ controller_method ], $ arguments );
754
+ if ($ this ->is_valid_request ) {
755
+ call_user_func_array ([$ this , $ controller_method ], $ arguments );
756
+ }
739
757
}
740
758
catch (Exception $ ex )
741
759
{
@@ -744,8 +762,8 @@ public function _remap($object_called, $arguments = [])
744
762
}
745
763
746
764
// If the method doesn't exist, then the error will be caught and an error response shown
747
- $ _error = &load_class ('Exceptions ' , 'core ' );
748
- $ _error ->show_exception ($ ex );
765
+ $ _error = &load_class ('Exceptions ' , 'core ' );
766
+ $ _error ->show_exception ($ ex );
749
767
}
750
768
}
751
769
@@ -2136,6 +2154,10 @@ protected function _force_login($nonce = '')
2136
2154
.'", opaque=" ' . md5 ($ rest_realm ).'" ' );
2137
2155
}
2138
2156
2157
+ if ($ this ->config ->item ('strict_api_and_auth ' ) === true ) {
2158
+ $ this ->is_valid_request = false ;
2159
+ }
2160
+
2139
2161
// Display an error response
2140
2162
$ this ->response ([
2141
2163
$ this ->config ->item ('rest_status_field_name ' ) => FALSE ,
0 commit comments