-
Notifications
You must be signed in to change notification settings - Fork 92
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
Validate Parameter Names in wp user update Command #528
base: main
Are you sure you want to change the base?
Validate Parameter Names in wp user update Command #528
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
Thank you for the PR! This needs a test to verify any new behavior. That being said, I'm not totally sure if this is the right approach. If the goal is to accept any field and pass them to If that isn't possible, it seems better to just keep all of the valid options in the DocBlock, not accept arbitrary /**
* --<field>=<value>
* : One or more fields to update. For accepted fields, see wp_update_user().
*
**/ And even better, it gets built in support for similar commands. So if we removed that, you would then see this: $ wp user update 1 --user-pass=test --user-email=test@example.com
Error: Parameter errors:
unknown --user-pass parameter
Did you mean '--user_pass'?
unknown --user-email parameter
Did you mean '--user_email'? Then all of the fields would be forced to have to have documentation as well, which seems like a benefit. It doesn't feel like it makes sense to keep some of the accepted fields in the DocBlock, then say we accept other arbitrary fields (not documented), but then not accept them based on a second hardcoded list. I think it would be better to just keep them all in the DocBlock and get the advantages above. Maybe other maintainers have thoughts. |
FWIW, this was the original suggestion on the mentioned issue, which wasn‘t addressed here:
Some properties were only introduced in a specific WordPress version, or might have been removed again (though that seems unlikely to me, because of back compat), so I get where this is coming from. We should at least discuss the fessibility of this more. NB this goes not only for users but also other entities I do like the suggestion with the docblock though, because it makes for a much nicer user experience. But we probably can‘t combine that with such a dynamic allowlist… That said, maybe plugins can add support for more properties too, at which point the allowlist would be too limiting. |
Thanks for the feedback, @mrsdizzie @swissspidy ! I've updated the DocBlock to list all accepted fields and removed --= validation, letting WP-CLI handle errors automatically. Regarding the dynamic allowlist, I see the value in keeping it in sync with WordPress Core, but it could add maintenance overhead and limit plugin flexibility. We should discuss the feasibility of this further—not just for users, but for other entities as well. Looking forward to more thoughts! 🚀 |
@swissspidy I agree I'm not 100% convinced it feels right to filter properties here, though it seems the trade off is a worse user experience for wp cli users. I do think ideally this would be the responsibility of the The suggested path in the issue feels complicated to me given the seemingly small amount of changes upstream to what they accept. And it wouldn't really handle the case of plugins being able to alter what is accepted (as you say, no list can). I think a single line in a docblock that says |
I thought maybe via the So yeah, perhaps having a thorough docblock is good enough. We already list some fields in the docblock, but not all of them. So expanding that list would be helpful either way. An alternative/additional solution could be to provide suggestions for typos like in the reported |
This happens automatically via WP CLI when validating what the user passed against what the command supports (via the DockBlock) but that check never happens when a command has
Because the command accepts anything as valid, so it doesn't run any validation code. Removing that provides the validation / suggestion at the expense of not allowing arbitrary fields anymore -- which is probably what is best in this specific example (and to make sure the command knows and documents all of the accepted values). |
So yeah, I suppose starting with docblock improvements is the best next step. Here are all occurrences of and in entity-command alone: https://github.com/search?q=repo%3Awp-cli%2Fentity-command+%22--%3Cfield%3E%3D%3Cvalue%3E%22+language%3APHP&type=code&l=PHP Lot of room for improvement there IMO. |
I think the problem with a warning is that you still have the same initial confusion that
It isn't really clear what is happening there -- is the warning wrong? Did it update something? You also don't end up with an error code and wouldn't see the warning if using --quiet (I think?). I think an error would be better for the user. |
Valid point 👍 |
@mrsdizzie - When there is invalid field mentioned, there is only Error message and not a success message. Please have a look at this screenshot. |
Yeah I know, but my idea was a warning, in which case you would see both. But let's discard the warning idea :) I think I'd be fine with more thorough documentation in favor of the dynamic catch-all |
@swissspidy To address our main issue and improve parameter validation across WP-CLI commands, these are the changes proposed. Can I proceed with these? Remove --= syntax from the DocBlock of wp user update ( which is already done ) and replace it with listed parameters (e.g., --user_pass, --user_email, etc.). Extend this pattern to other commands (e.g., post update, term update) in follow-up PRs, replacing dynamic field syntax with documented parameters. |
@karthick-murugan To be honest I'd like to think about this a moment and hold off on this PR and more PRs related to this feature -- these PRs often need almost every line changed based on review and it can feel like more work than just doing it. I think there is an idea / path forward we can take from here and probably update everything in a larger PR. If you want to work on wp-cli/wp-cli#6066 which is waiting on response instead. |
@mrsdizzie - I have added a video here in wp-cli/wp-cli#6025 mentioning that I was not able to reproduce the issue mentioned. |
Fixes wp-cli/wp-cli#5286
Summary
This PR addresses issue #5286, where
wp user update
silently ignored incorrect parameter names (e.g.,--user-pass
instead of--user_pass
) without providing an error message.With this fix, WP-CLI will now validate parameter names and issue a warning when an unknown parameter is used.
Changes Introduced
Added validation to check whether the parameters passed to
wp user update
match the expected field names fromwp_update_user().
If an unrecognized parameter is provided, WP-CLI will display a warning and prevent the update from proceeding.Behavior Before Fix
(Note: The password was not updated, and no warning was shown.)
Behavior After Fix
(This prevents silent failures and informs the user of incorrect parameter usage.)
How to Test
wp user create testuser test@example.com --role=subscriber --user_pass=oldpass
wp user update testuser --user_pass=newpass
✅ Expected: User password should update successfully.
wp user update testuser --user-pass=newpass
❌ Expected:
(This ensures that incorrect parameters are flagged.)
wp user update testuser --wrong_flag=value
❌ Expected: Similar warning about an unknown parameter.
Screenshots