Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($http): only parse as JSON when opening/closing brackets match #10357

Closed
wants to merge 1 commit into from

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Dec 7, 2014

Previously, due to weak JSON-detecting RegExp, string like [...} and
{...] would be considered JSON (even if they obviously aren't) and an
expection would be thrown while trying to parse them.

This commit makes sure the opening and closing brackets match. This
doesn't completely eliminate false positives (e.g. []{}[]), but does
help reduce them.

Closes #10349

Note:
I don't consider this to be a breaking change, but if someone expects their app to throw when receiving a response like '[...}' or '{...]', then this PR will break their app.

@googlebot
Copy link

CLAs look good, thanks!

@gkalpak gkalpak force-pushed the http-improved-json-detection branch 2 times, most recently from 5e4c081 to 18ba101 Compare December 7, 2014 13:15
@pkozlowski-opensource
Copy link
Member

Just another small remark regarding the commit message: if we want to close #10349 with this commit (we could because it is technically fixing the reported issue) we should open a separate one to fix this once and fall all properly in 1.4

@pkozlowski-opensource pkozlowski-opensource added this to the 1.3.7 milestone Dec 7, 2014
@pkozlowski-opensource pkozlowski-opensource self-assigned this Dec 7, 2014
@gkalpak gkalpak force-pushed the http-improved-json-detection branch from 18ba101 to 49e809c Compare December 7, 2014 16:20
@gkalpak
Copy link
Member Author

gkalpak commented Dec 7, 2014

Good point on the function name (updated).

If you prefer leaving #10349 open, let me know so I update the commit message.
I thought we might want to add the once-and-forall fix (respecting content-type header) in this doc, under $http improvements.

@pkozlowski-opensource
Copy link
Member

@gkalpak cool, thnx for updating the function name. Regarding the issue - let's leave the commit message as-is and let's add a note to the 1.4 planning doc. Could you do it?

@gkalpak
Copy link
Member Author

gkalpak commented Dec 7, 2014

I added it to the doc.

@jbedard
Copy link
Contributor

jbedard commented Dec 7, 2014

Is there a reason for not using a single regex? Something such as /^(\[[\s\S]*\]|\{([^\{][\s\S]*)?\})$/ seems to work...

The two regex thing appears to have been added a long long time ago in c7913a4.

@lgalfaso
Copy link
Contributor

lgalfaso commented Dec 7, 2014

Even when I am ok with the check itself, we should do better here, as we are first removing the security prefix (and not keeping the original) and then checking if the []{} match. The issue is that if they do not match and for whatever reason the original response had the security prefix, then we end up with something that is not the original response nor the JSON

@gkalpak
Copy link
Member Author

gkalpak commented Dec 8, 2014

@jbedard: You probably have to ask the one who introduced it. I just tried to stick as much as possible to the current approach.
For one, it's much more readable with the 2 smaller RegExps.
On the other hand, I am not familiar with JS RegExp internals, so not sure which approach would perform better (especially for longer strings).

@lgalfaso: Not removing the prefix, when no deserialization takes place, sounds reasonable. I will update the PR shortly.

@jbedard
Copy link
Contributor

jbedard commented Dec 8, 2014

@gkalpak it's been that way since introduced. I assume because it read easier then combining them. But now that we have 4 REs and a map I'd say one RE is simpler...

@gkalpak gkalpak force-pushed the http-improved-json-detection branch 2 times, most recently from 406a4c9 to 0838969 Compare December 8, 2014 09:09
@gkalpak
Copy link
Member Author

gkalpak commented Dec 8, 2014

@lgalfaso: Updated so that the original data is returned, when the response doesn't look like JSON.

@jbedard: It's 3 RegExp (and 2 of them are tiny); so if you ask me, I find the current approach simpler to read/understand. But if people think it is preferable to have one larger RegExp, it's fine by me :)

@jbedard
Copy link
Contributor

jbedard commented Dec 8, 2014

Right, 3 RE not 4. But its the extra json mapping logic that makes it more complicated and error prone imo.

@caitp caitp modified the milestones: 1.3.7, 1.3.8 Dec 9, 2014
@pkozlowski-opensource pkozlowski-opensource modified the milestones: 1.3.8, 1.3.7 Dec 9, 2014
@gkalpak gkalpak force-pushed the http-improved-json-detection branch from 0838969 to f1f48f9 Compare December 10, 2014 08:56
@pkozlowski-opensource
Copy link
Member

So, I think we should merge it even if it is not perfect in the current form (no of RegExps etc.). It looks like we are going to drop JSON auto-detection in 1.4 so this will be a temporary change that improves the existing situation.

@gkalpak
Copy link
Member Author

gkalpak commented Dec 12, 2014

@pkozlowski-opensource: Like I said, I find the 3 small RegExps more readable than 1 bigger RegExp, but if you'd rather go with the 1 RegExp, let me know; it should be a quick change.

@pkozlowski-opensource
Copy link
Member

Nah, it is fine - this code will go away in 1.4 anyway...

@pkozlowski-opensource
Copy link
Member

@gkalpak one last thing - what do you think about adding a test showing that a JSON protection prefix is not removed when a response is not deserialized as JSON? This is one of the things that changes with the PR, right? Maybe it would be good to be explicit about it?

@gkalpak gkalpak force-pushed the http-improved-json-detection branch from f1f48f9 to a99e97e Compare December 12, 2014 14:56
Previously, due to weak JSON-detecting RegExp, string like `[...}` and
`{...]` would be considered JSON (even if they obviously aren't) and an
expection would be thrown while trying to parse them.

This commit makes sure the opening and closing brackets match. This
doesn't completely eliminate false positives (e.g. `[]{}[]`), but does
help reduce them.

Closes angular#10349
@gkalpak gkalpak force-pushed the http-improved-json-detection branch from a99e97e to 3b6f364 Compare December 12, 2014 15:04
@gkalpak
Copy link
Member Author

gkalpak commented Dec 12, 2014

@pkozlowski-opensource: Good idea :) UPDATED
(I also rebased on master.)

@gkalpak gkalpak closed this in b9bdbe6 Dec 12, 2014
@gkalpak gkalpak deleted the http-improved-json-detection branch December 19, 2014 10:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants