Welcome Guest, Not a member yet? Register   Sign In
URI routing from CLI
#1

(This post was last modified: 03-04-2025, 06:49 AM by Fido L Dido. Edit Reason: Disclosed CI version )

I'm encountering an issue with a route when calling it from the CLI. I think this may very well be a feature rather than a bug!
I have a cli method which expects 3 parameters all as strings. This is the route:
PHP Code:
$routes->cli('cli/postbyid/(:segment)/(:segment)/(:any)''Cli::postbyid/$1/$2/$3'); 

By and large this works, except when the third string contains characters that normally wouldn't be permitted by ar URI. For example:
Code:
php index.php cli param1 param2 param3(b)

The brackets in the third parameter cause an exception to be thrown:
Code:
CodeIgniter\HTTP\Exceptions\BadRequestException: The URI you submitted has disallowed characters: "param3(b)"

I can see that this is correct behaviour for security reasons when specifying URIs, but a CLI parameter isn't a URI.
I've tried adding a custom placeholder permitting *any* characters at all, but this still throws the same error as I suspect it is still restricted by config/App.php. Hopefully I've explained this in a way that it can be understood. I think CI is performing as designed, but is creating an unnecessary restriction for CLI access. I suspect the solution is for CLI access to ignore character restrictions on URIs, or that any custom placeholder should not be restricted by the characters specified in the config.

Edit: I'm using CI 4.6.0
Reply
#2

This is the expected behavior. Defining a CLI route only specifies that the route can be accessed via the command line; it does not prevent other routes from pointing to the same Controller::method.

If you expect non-standard characters in your parameters, it's recommended to URL-encode them, just as you would in a regular web application.
Reply
#3

Thank you, I can see why allowing this behaviour might be considered a security risk. As you say, I think encoding the parameter before calling is the solution.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB