fix: prevent panic on empty-string values in v2 JSON fixtures#1736
Open
arpitjain099 wants to merge 1 commit into
Open
fix: prevent panic on empty-string values in v2 JSON fixtures#1736arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
parseMapForApplicationJSON sniffed whether a value looks like a JSON array or object with parsed[0:1] and parsed[len(parsed)-1:]. When a v2 (application/json) fixture field resolves to an empty string, those slice expressions index a zero-length string and the CLI panics with "slice bounds out of range [:1] with length 0". Use strings.HasPrefix/HasSuffix instead, which return false on empty input, so an empty value falls through to the default branch and is stored as "" (the same way the form-data path already handles it). Added a regression test covering an empty-string value in the application/json path. Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reviewers
r?
cc @stripe/developer-products
Summary
An empty-string value in a v2 (application/json) fixture crashes the CLI with "slice bounds out of range [:1] with length 0". In parseMapForApplicationJSON the code checks whether a value looks like a JSON array or object with parsed[0:1] and parsed[len(parsed)-1:], and those slice both blow up on a zero-length string. This happens whenever a fixture field is set to "" or a query/env lookup resolves to empty.
The fix swaps those two checks for strings.HasPrefix/HasSuffix, which return false on empty input, so an empty value just falls through and is stored as "". That matches how the form-data path already handles it. I added a regression test that passes an empty-string value through ParseToApplicationJSON; it panics before this change and passes after. Full pkg/parsers suite and go vet are green.
Thanks for taking a look.