-
Notifications
You must be signed in to change notification settings - Fork 376
[php-wasm] Add support for rm command in sandboxed spawn handler
#3010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
| case 'rm': { | ||
| const target = args[args.length - 1]; | ||
| if (await php.isDir(target)) { | ||
| await php.rmdir(target, { recursive: true }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure we want to run a rm -rf just because the target is a directory? It can be pretty dangerous. @adamziel, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WP CLI is also using rm -rf to delete plugin files, so just replicate similar behavior. However, we can add support for flags like r and f, but playground is not using this anywhere else as of now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I am newbie in playground 😅, so let me know if I am missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot @zaerl! Normally, when you run rm <path> it only removes a file. Removing a directory requires passing additional flags as you've both mentioned: -rf. However, this implementation of rm does not check for those flags and it will always remove the entire directory tree if a <path> points to it. Therefore we may want to check for the presence of-rf flags and, if they're missing, refuse to remove a directory. That should make it safer, and that's also what the linux rm would do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3903795 to
cf0f654
Compare
cf0f654 to
2ecba1b
Compare
2ecba1b to
89d198e
Compare
| } | ||
| case 'rm': { | ||
| const target = args[args.length - 1]; | ||
| const flags = args.slice(1, -1).join(' '); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be more involved than that. Here's a few examples that would be really useful to cover with a unit test to make sure this command can handle them without failing:
rm file1 file2 file3
rm -r -f directory
rm file1 -f file2 -r
# This one means "remove a file called "-rf"
rm -- -rf
# This one means "recursively remove a file or a directory called "-rf"
rm -rf -- -rfThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamziel @zaerl What do you think about using the yargs-parser npm package to parse the args instead of writing a custom parser from scratch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Utsav-Ladani potentially! Let's give it a try and see what would this code look like with yargs-parser in place. It seems to be larger than we need, but still fairly small so perhaps? Note this package (@php-wasm/universal) can be imported in both node.js and web browser code, which yargs-parser seems to support. Also, one potentially relevant difference is that yargs-parser seems to accept a string, and in here we already have access to a pre-parsed args array where string syntax were already processed (e.g. "my string" becomes my string) – hopefully there's a way to work from there without re-encoding that and re-escaping quotes.
Motivation for the change
The
wp plugin uninstall <plugin-name>command usesrm -rfto delete the plugin files, and the current sandboxed spawn handler doesn't have support for it, so I am just adding it.Closes: #2653
Implementation details
Adding support for
rmin the sandboxed spawn handler.Testing Instructions (or ideally a Blueprint)
The following blueprint should run without any error, and the
Hello Dollyplugin should be deleted:{ "login": true, "landingPage": "/wp-admin/plugins.php", "steps": [ { "step": "wp-cli", "command": "wp plugin uninstall hello" } ] }