Jump to content

I am using an MVC, am I the right filter on these GET variables?


cgm225

Recommended Posts

I am using an MVC, am I the right filter on these GET variables, or should I be validating/filtering with a different filter/preg match?

 

        //The next two lines are ternary operators, a sort of short-hand for if-else
        $module= isset($_GET["module"]) ? filter_input(INPUT_GET, 'module', FILTER_SANITIZE_STRING) : "home";
        $action = isset($_GET["action"]) ? filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING) : "frontpage";

Well, there's no gaurantee they're not trying to access a module they don't have access to... or one they shouldn't be looking at. But as far as stripping unwanted characters and preventing injection... it seems fine.

 

Personally, I'd use a regex function with a white list of allowed characters... gives you a little more control.

 

This code strips anything that isn't a letter, number, underscore or hyphen

 

$regex = '/[^-_A-z0-9]++/';

$module = preg_replace($regex, '', $_GET['module']);
$action = preg_replace($regex, '', $_GET['action']);

 

Or you can fail completely on an injection attempt

 

$regex = '/[^-_A-z0-9]/';
if ( preg_match($regex, $_GET['module']) || preg_match($regex, $_GET['action']) ) {
    // injection attempt detected
    exit('Invalid input variables detected');
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.