mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
$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.
This commit is contained in:
parent
10a7521f0b
commit
cd139f5767
2 changed files with 44 additions and 2 deletions
|
|
@ -79,8 +79,11 @@ angularServiceInject('$xhr', function($browser, $error, $log){
|
|||
}
|
||||
$browser.xhr(method, url, post, function(code, response){
|
||||
try {
|
||||
if (isString(response) && /^\s*[\[\{]/.exec(response) && /[\}\]]\s*$/.exec(response)) {
|
||||
response = fromJson(response, true);
|
||||
if (isString(response)) {
|
||||
if (response.match(/^\)\]\}',\n/)) response=response.substr(6);
|
||||
if (/^\s*[\[\{]/.exec(response) && /[\}\]]\s*$/.exec(response)) {
|
||||
response = fromJson(response, true);
|
||||
}
|
||||
}
|
||||
if (code == 200) {
|
||||
callback(code, response);
|
||||
|
|
|
|||
|
|
@ -44,4 +44,43 @@ describe('$xhr', function() {
|
|||
|
||||
expect($log.error).wasCalledWith("MyException");
|
||||
});
|
||||
|
||||
|
||||
it('should automatically deserialize json objects', function() {
|
||||
var response;
|
||||
|
||||
$browserXhr.expectGET('/foo').respond('{"foo":"bar","baz":23}');
|
||||
$xhr('GET', '/foo', function(code, resp) {
|
||||
response = resp;
|
||||
});
|
||||
$browserXhr.flush();
|
||||
|
||||
expect(response).toEqual({foo:'bar', baz:23});
|
||||
});
|
||||
|
||||
|
||||
it('should automatically deserialize json arrays', function() {
|
||||
var response;
|
||||
|
||||
$browserXhr.expectGET('/foo').respond('[1, "abc", {"foo":"bar"}]');
|
||||
$xhr('GET', '/foo', function(code, resp) {
|
||||
response = resp;
|
||||
});
|
||||
$browserXhr.flush();
|
||||
|
||||
expect(response).toEqual([1, 'abc', {foo:'bar'}]);
|
||||
});
|
||||
|
||||
|
||||
it('should automatically deserialize json with security prefix', function() {
|
||||
var response;
|
||||
|
||||
$browserXhr.expectGET('/foo').respond(')]}\',\n[1, "abc", {"foo":"bar"}]');
|
||||
$xhr('GET', '/foo', function(code, resp) {
|
||||
response = resp;
|
||||
});
|
||||
$browserXhr.flush();
|
||||
|
||||
expect(response).toEqual([1, 'abc', {foo:'bar'}]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue