Repository navigation
fix(android): parse large integers, exponents and escaped strings in CapacitorHttp JSON responses - #8708
Open
Swiezu wants to merge 2 commits into
Open
fix(android): parse large integers, exponents and escaped strings in CapacitorHttp JSON responses#8708Swiezu wants to merge 2 commits into
Swiezu wants to merge 2 commits into
Conversation
This branch has not been deployed
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.
Description
When a
CapacitorHttpresponse isapplication/jsonand its whole body is a single JSON value (not an object or array), Android parses it by hand inHttpRequestHandler.parseJSON(). Three valid JSON values are handled differently than on iOS (JSONSerializationwith.fragmentsAllowed) and on the web (fetch().json()):80000105708000010570NumberFormatException80000105701234567890123456789012345678901234567000NumberFormatException123456789012345670001e5100000"1e5"100000"a\"b"a"ba\"b(escapes kept)a"bThe first two cases are the worst: integers are parsed with
Integer.parseInt, which throws for anything outside the 32-bit range, and theNumberFormatExceptionis not caught by the surroundingcatch (JSONException e). The app never gets the response. This is the bug reported in #7400 (closed for lack of a reproduction; the stack trace there points to the same line).Reproduction
Any endpoint that returns a bare large integer with
Content-Type: application/json, for example an ID or a timestamp in milliseconds:iOS and web resolve with
8000010570, Android rejects withFor input string: "8000010570".Change
parseJSON()keeps its structure and only the three value branches change:Integer, thenLong, thenDouble, so values that fit anintkeep their current type and nothing else changes for them.1e5,1.5E+3,-2.5e-3), as RFC 8259 allows.Doublerange (e.g.1e400) is returned as the raw string, becauseJSObjectrejects infinite values and the request would fail again.JSONTokenerwhen the body is exactly one valid JSON string. Otherwise the previous behavior (content between the quotes) is kept.Unit tests cover each case and
readData()end to end. On currentmain, 5 of the new tests fail (3 of them with theNumberFormatExceptionfrom #7400); with this change all 57 Android unit tests pass.Change Type
Notes
JSONTokener.nextValue(): Android'sreadLiteral()reads integers with a leading0as octal (0123becomes83), stops unquoted text at the first space (anapplication/jsonerror pageInternal Server Errorbecomes"Internal") and ignores trailing content (12 34becomes12). This PR keeps the current results for those inputs and only changes the cases that fail or differ from iOS and web today.Related
#7400, #8087