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

Queries involving keys containing four backslashes ("\\\\") failing. #561

Closed
jimblackler opened this issue Aug 25, 2020 · 16 comments
Closed

Comments

@jimblackler
Copy link

I encountered a problem with an application when JSON keys contained four backslashes (\\).

After a lot of debugging I found that JSONPointer (used in my app) was returning null when queried. Stepping through the code it seems that the token used for the key may be double unescaped; I saw the same token unescaped() on line 173 when the JSONPointer was built, then on line 213 when the pointer was used in a query.

https://github.com/stleary/JSON-java/blob/master/src/main/java/org/json/JSONPointer.java#L173
https://github.com/stleary/JSON-java/blob/master/src/main/java/org/json/JSONPointer.java#L213

This example test will fail on the trunk version of the library:

jimblackler@8d6e1d8#diff-b4df778ff5909ef85877e003ed15b3beR279

(Note there are eight double slashes in the literal in the test, because it's escaped for the Java string literal)

I'm not 100% sure of my understanding of the issue or the correct behavior. I'd be grateful if someone who is familiar with the practices handling these strings could comment or confirm, and also advise if the fix in the patch above is something that I should submit.

@johnjaylward
Copy link
Contributor

@erosb do you have time to look at this one?

@jimblackler
Copy link
Author

Thanks guys. I found that the pattern ~~0 fails for the same reason.

@stleary
Copy link
Owner

stleary commented Aug 29, 2020

Will wait a few more days in case @erosb is available to look into this.

@erosb
Copy link
Contributor

erosb commented Aug 29, 2020

Guys, unfortunately I can't see any chance of looking into this in detail in the near future.

@stleary
Copy link
Owner

stleary commented Aug 30, 2020

@erosb No worries, thanks for responding

@stleary
Copy link
Owner

stleary commented Oct 8, 2020

Looks like this will need to be addressed without the benefit of expert advice.

It seems likely to me that the implementation is only intended to handle Java strings with quotes and double backslashes, both of which happen to be examples in RFC6901. Arbitrary sequences of backslashes in keys would appear to be unsupported. There may also be an issue with the code calling JSONPointer.unescape() twice in succession, from the JSONPointer() constructor, and again from queryFrom(). This does not seem correct. The escape() method on the other hand is only called from toString().

If anyone wants to see if they can address this without affecting existing functionality, they are welcome to go ahead.

@niteshbheda
Copy link

I am facing a similar issue in Values when we a backslash escape character supporting " inside the value then .op function fails to return the escape character. For instance:

package com.chu;

import org.json.JSONObject;
import org.json.XML;

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String input = "{\"Parameters\":{\"PAYERS_NAME\":\"ТОВАРИСТВО З ОБМЕЖЕНОЮ ВІДПОВІДАЛЬНІСТЮ \\\"ЕКСПО-ЛІС\\\"\"}}";

    JSONObject json = new JSONObject(input);

    JSONObject jsonObj = ((JSONObject) json.get("Parameters"));
    System.out.println(jsonObj.get("PAYERS_NAME"));
    System.out.println(XML.toString(json, "root"));

}

}

Output

ТОВАРИСТВО З ОБМЕЖЕНОЮ ВІДПОВІДАЛЬНІСТЮ "ЕКСПО-ЛІС"
<PAYERS_NAME>ТОВАРИСТВО З ОБМЕЖЕНОЮ ВІДПОВІДАЛЬНІСТЮ "ЕКСПО-ЛІС"</PAYERS_NAME>

As you can see this is causing an issue in conversion.

@stleary
Copy link
Owner

stleary commented Dec 16, 2020

Does anyone want to address this with a code fix?

@johnjaylward
Copy link
Contributor

I'm booked up pretty solid through Feb with work. If someone else could get a PR, I should have time to review it though.

@johnjaylward
Copy link
Contributor

I believe #588 will fix this as well

@johnjaylward
Copy link
Contributor

@jimblackler comparing your changes to the changes in #588, I believe #588 is more correct. We still need to escape/unescape the / and ~ characters, so we still need the method to be called. Sorry it took us so long to review this and get a fix in.

@katldewitt
Copy link
Contributor

Hi! I'm interested in working on this for a school project in October and November

@stleary
Copy link
Owner

stleary commented Oct 21, 2021

Hi @katldewitt, feel free to work on this!

@johnjaylward
Copy link
Contributor

johnjaylward commented Oct 21, 2021

This should already be fixed. This may only need explicit test cases

@katldewitt
Copy link
Contributor

I just issued #640 to address this issue and clarify behavior. As @johnjaylward mentioned, #588 corrected the behavior already, so my PR is an explicit test case and a note about documentation.
Thanks in advance!

stleary added a commit that referenced this issue Nov 2, 2021
Address #561: Add unit tests for multiple backslashes
@stleary
Copy link
Owner

stleary commented Nov 2, 2021

Issue resolved in #640

@stleary stleary closed this as completed Nov 2, 2021
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

6 participants