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

JSONObject incorrectly accepts json with octal values #465

Closed
wightwulf1944 opened this issue Mar 25, 2019 · 6 comments
Closed

JSONObject incorrectly accepts json with octal values #465

wightwulf1944 opened this issue Mar 25, 2019 · 6 comments

Comments

@wightwulf1944
Copy link

given:

{
	"MobileNumber": 09171234567
}

usage:

String jsonString = "{\n    \"MobileNumber\": 09171234567\n}";
JSONObject jsonObject = new JSONObject(jsonString);

expected:
JSONObject() should throw a JSONException because the input json string does not conform to RFC 4627 section 2.4 "Numbers" where it states...

Octal and hex forms are not allowed. Leading zeros are not allowed.

actual:
the octal value is coerced into a String upon construction of JSONObject and calling jsonObject.get("MobileNumber") returns a String

@johnjaylward
Copy link
Contributor

johnjaylward commented Mar 25, 2019 via email

@johnjaylward
Copy link
Contributor

johnjaylward commented Mar 25, 2019 via email

@wightwulf1944
Copy link
Author

wightwulf1944 commented Mar 25, 2019

Thank you for the prompt reply. I now understand that JSONObject is lenient as long as the malformed json can be intuitively and reasonably parsed.

In case others have found this curious case, quoted below is the referenced RFC7159 page 10 section 9 "Parsers"

A JSON parser transforms a JSON text into another representation. A
JSON parser MUST accept all texts that conform to the JSON grammar.
A JSON parser MAY accept non-JSON forms or extensions.

For reference, is there any document that lists cases where JSONObject reasonably accepts malformed json? Or is there any way for JSONObject to enforce strict json specifications? Our usecase for this is we're not really parsing and extracting specific fields and values but just trying to validate the given json string. Given a list of leniency cases, we can implement a solution that catches those cases.

@johnjaylward
Copy link
Contributor

For reference, is there any document that lists cases where JSONObject reasonably accepts malformed json? Or is there any way for JSONObject to enforce strict json specifications? Our usecase for this is we're not really parsing and extracting specific fields and values but just trying to validate the given json string. Given a list of leniency cases, we can implement a solution that catches those cases.

At this time, I don't think we have a single document that lists all cases in an explicit list. If you enjoy reading test cases, you can find many of the exception cases in those. (link in the README)

As for strict parsing, at this time it's not available in this library. It's been brought up before as a possible implementation option, but so far no one has suggested a merge request (see #240, #269, #325).

@johnjaylward
Copy link
Contributor

so far I found these exception, although there may be more:

  • Unquoted keys { key: "value" }
  • Unquoted values { "key": value }
  • Unescaped literals like "tab" in string values { "key": "value with an unescaped tab" }
  • Numbers out of range for Double or Integer are parsed strings

It doesn't look like the parser supports Octal at all. The case you found is simply that the JSON parser doesn't recognize it as a number because of the leading 0.

@wightwulf1944
Copy link
Author

The case you found is simply that the JSON parser doesn't recognize it as a number because of the leading 0.

I see so it falls under Unquoted values.

This list will suffice. We will try to explore JSONTokener's implementation with this in mind and perhaps make a StrictJSONTokener or something similar. Thank you for the detailed response. Me and my colleagues appreciate it.

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

No branches or pull requests

2 participants