Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Decoding sometimes fails when two encoded characters appear one after another. #14

Open
davidklebanoff opened this issue Oct 15, 2016 · 6 comments · May be fixed by #21
Open

Decoding sometimes fails when two encoded characters appear one after another. #14

davidklebanoff opened this issue Oct 15, 2016 · 6 comments · May be fixed by #21

Comments

@davidklebanoff
Copy link

davidklebanoff commented Oct 15, 2016

The Cookies.decode( String encoded ) method sometimes fails to properly decode the given string when two encoded characters appear one after another.

For example, the decoding fails for the given string: New%20York%2C%20NY. Outputting: New York%2C NY.

The reason being that the decoding regex (%[0-9A-Z]{2})+ produces two matches %20 and %2C%20. The method then goes on to replace these matches by first searching for %20 and replacing it with the space character. It then goes on to look for %2C%20 to replace them with a comma and space, however, since all instances of %20 have been replaced by a space, the match never occurs. %2C%20 is simply left as %2c.

@FagnerMartinsBrack
Copy link
Member

Hey good catch. Want to open a Pull Request fixing it with tests? Thanks.

@davidklebanoff
Copy link
Author

davidklebanoff commented Oct 15, 2016

Thoughts on simply replacing the decoding logic with the Java UrlDecoder?

URLDecoder.decode(cookieValue, "UTF-8")

@FagnerMartinsBrack
Copy link
Member

FagnerMartinsBrack commented Oct 15, 2016

The integration tests will probably fail because URLDecoder.decode doesn't decode only the characters that are not allowed in the cookie-value according to the RFC 6265.

The encoding part of the README explains how we handle encoding/decoding.

If you want to take a look how we are handling it in JavaScript, see the js-cookie latest version (2.1.3)

@FagnerMartinsBrack
Copy link
Member

FagnerMartinsBrack commented Oct 15, 2016

I would recommend starting a PR with a failing test so that we can see the problem clearly first before implementing the fix.

@tholu
Copy link
Contributor

tholu commented Feb 25, 2019

I have the same problem! En-/Decoding is obviously really hard. I will see what I can do here.

@tholu
Copy link
Contributor

tholu commented Feb 25, 2019

@FagnerMartinsBrack See the PR I created, please check, merge and release a new version. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants