Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd139f5

Browse files
committed
$xhr service now autodetects and strips )]}',\n
")]}\',\n" is a commonly used security prefix added to json http responses iat google and elsewhere in order to prevent certain cross-site attacks $xhr service now autodetects the prefix and strips it before deserializing the json. the implementation should be more flexible to allow for wider range of prefixes, but we need this one right now and can address other usecases later.
1 parent 10a7521 commit cd139f5

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/service/xhr.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ angularServiceInject('$xhr', function($browser, $error, $log){
7979
}
8080
$browser.xhr(method, url, post, function(code, response){
8181
try {
82-
if (isString(response) && /^\s*[\[\{]/.exec(response) && /[\}\]]\s*$/.exec(response)) {
83-
response = fromJson(response, true);
82+
if (isString(response)) {
83+
if (response.match(/^\)\]\}',\n/)) response=response.substr(6);
84+
if (/^\s*[\[\{]/.exec(response) && /[\}\]]\s*$/.exec(response)) {
85+
response = fromJson(response, true);
86+
}
8487
}
8588
if (code == 200) {
8689
callback(code, response);

test/service/xhrSpec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,43 @@ describe('$xhr', function() {
4444

4545
expect($log.error).wasCalledWith("MyException");
4646
});
47+
48+
49+
it('should automatically deserialize json objects', function() {
50+
var response;
51+
52+
$browserXhr.expectGET('/foo').respond('{"foo":"bar","baz":23}');
53+
$xhr('GET', '/foo', function(code, resp) {
54+
response = resp;
55+
});
56+
$browserXhr.flush();
57+
58+
expect(response).toEqual({foo:'bar', baz:23});
59+
});
60+
61+
62+
it('should automatically deserialize json arrays', function() {
63+
var response;
64+
65+
$browserXhr.expectGET('/foo').respond('[1, "abc", {"foo":"bar"}]');
66+
$xhr('GET', '/foo', function(code, resp) {
67+
response = resp;
68+
});
69+
$browserXhr.flush();
70+
71+
expect(response).toEqual([1, 'abc', {foo:'bar'}]);
72+
});
73+
74+
75+
it('should automatically deserialize json with security prefix', function() {
76+
var response;
77+
78+
$browserXhr.expectGET('/foo').respond(')]}\',\n[1, "abc", {"foo":"bar"}]');
79+
$xhr('GET', '/foo', function(code, resp) {
80+
response = resp;
81+
});
82+
$browserXhr.flush();
83+
84+
expect(response).toEqual([1, 'abc', {foo:'bar'}]);
85+
});
4786
});

0 commit comments

Comments
 (0)