Skip to content
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

Enhancement: Correctly parsing flags with minimist and centrally validating them #5439

Open
martinlingstuyl opened this issue Aug 31, 2023 · 4 comments

Comments

@martinlingstuyl
Copy link
Contributor

martinlingstuyl commented Aug 31, 2023

We're using minimist to parse command arguments. Recently, we've been updating the way the CLI interacts with minimist, to make sure we have consistent input. We've done this around booleans (#3853) and we've started it around strings and numbers (#5396)

However, flag options also have a potential issue.

Consider the following example:

m365 spo site list --includeOneDriveSites

This takes care of including a boolean value in an API call to sharepoint. (true or false) By default it's false, but if the flag is included, the value is true.

We generally do not validate these flags however, which means that a user might inadvertently add a string value after them:

m365 spo site list --includeOneDriveSites abc

The actual value that is parsed by minimist will now be a string abc, instead of the expected true. That's potentially a cause for issues. In the code we now need to make sure that the flag is never used directly, but is always used with a condition:

//this is rather safe
if (args.options.includeOneDriveSites) { 
} 

// this as well.
!!args.options.includeOneDriveSites

// this is not safe
const requestbody: {
  someProp: 'someValue',
  someOtherProp: args.options.includeOneDriveSites
}

Our current implementation puts the burden of checking the flag in the command itself, we should however handle it centrally.

Solution

I propose adding flags to this.types.booleans, to make sure the values are validated centrally and type safe.

Another solution would be to look at zod

@martinlingstuyl martinlingstuyl added enhancement needs peer review Needs second pair of eyes to review the spec or PR needs research and removed needs peer review Needs second pair of eyes to review the spec or PR labels Aug 31, 2023
@waldekmastykarz
Copy link
Member

I'd suggest looking into zod. It would mean a significant refactoring, but the other solution feels like putting a bandaid and a temporary fix that doesn't address all cases and will until we ran into another limitation.

@Adam-it
Copy link
Contributor

Adam-it commented Nov 18, 2023

good find 👍

I propose adding flags to this.types.booleans, to make sure the values are validated centrally and type safe.

I wonder would this really take that much time? maybe we could do that to have this 'safe' before we move research with zod?

@waldekmastykarz
Copy link
Member

I wonder would this really take that much time?

I think it's a shame to spend time on a solution that we know we'll remove it anyway. Going through all commands at our scale is not trivial and will easily get into hours, including doing the work and reviewing the PR. I'd rather we spend that time on reviewing PRs and providing new functionality.

If we need an interim solution, I wonder if there's a different approach that we could do centrally rather than for each command separately.

@Adam-it
Copy link
Contributor

Adam-it commented Dec 6, 2023

gotchya 👍.. and BTW I may relate to that, I also like adding new stuff more than refactoring the old stuff 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants