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

Handle JSON Number in ParseCommaStringSlice #29

Merged
merged 6 commits into from Mar 16, 2022

Conversation

ccapurso
Copy link
Contributor

@ccapurso ccapurso commented Mar 3, 2022

The ParseCommaStringSlice function in parseutil performs a type assertion (i.e. in.(string)) but does not return an error if the assertion fails. Without an early return, a panic might occur in the mapstructure.NewDecoder call. StringToSliceHookFunc in mapstructure returns a hook func that performs a similar string type assertion but does not verify that it was successful. This can occur if the input is a json.Number given that its reflect.Kind is string. The type assertion in mapstructure will panic with invalid type assertion: data.(string) (non-interface type json.Number on left).

Unit tests for ParseCommaStringSlice were also added as there weren't any prior to this PR.

ncabatoff
ncabatoff previously approved these changes Mar 3, 2022
Copy link
Member

@briankassouf briankassouf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that prior to this change non-strings were valid inputs: https://go.dev/play/p/oy2jtFp-k7z

Do we instead want to special case json.Number?

@ccapurso
Copy link
Contributor Author

ccapurso commented Mar 3, 2022

I believe that prior to this change non-strings were valid inputs: https://go.dev/play/p/oy2jtFp-k7z

Do we instead want to special case json.Number?

Thanks for pointing this out @briankassouf. The ParseCommaStringSlice function is a lot more permissive than I expected. I imagine that is due to setting WeaklyTypedInput to true in the mapstructure.DecoderConfig. For example, passing []bool{true, false, true} results in []string{"1", "0", "1"}. It's kind of interesting that this function has somewhat undefined/magical behavior with the weak typing. Based on this, it definitely makes sense to just special case json.Number. I can expand the unit tests but do we actually intend to make this function this permissive? The name (and associated use of StringToSliceHookFunc) doesn't necessarily imply that. Just want to ensure that our test cases cover what we do want to support.

@briankassouf
Copy link
Member

@ccapurso Since we use it in our API parameter parsing code, i think it's hard to say if making it less permissive would break existing APIs usage or not. I'm also not sure if boundary is using it in any meaningful way. Since it takes an interface{} as input it's probably best to not break the existing contract and just special case json.Number

@ccapurso
Copy link
Contributor Author

ccapurso commented Mar 4, 2022

@briankassouf, definitely agree. I didn't mean to imply that we should be less permissive. I am trying to get a feel for whether there might be other cases that might panic so that we can handle accordingly and ensure that there are appropriate tests.

@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

@ccapurso ccapurso changed the title Return err if ParseCommaStringSlice fails to parse input string Handle JSON Number in ParseCommaStringSlice Mar 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants