Skip to content

Commit 729dc3b

Browse files
committed
fix(security): support XSSI prefixes with and without commas.
Some implementations use an XSSI prefix with a trailing comma, some without. This changes Angular to support both.
1 parent 7ce0fc7 commit 729dc3b

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

modules/@angular/http/src/backends/xhr_backend.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {Observable} from 'rxjs/Observable';
1313
import {Observer} from 'rxjs/Observer';
1414
import {isSuccess, getResponseURL} from '../http_utils';
1515

16-
const XSSI_PREFIX = ')]}\',\n';
16+
const XSSI_PREFIX = /^\)\]\}',?\n/;
1717

1818
/**
1919
* Creates connections using `XMLHttpRequest`. Given a fully-qualified
@@ -46,9 +46,7 @@ export class XHRConnection implements Connection {
4646
// IE10)
4747
let body = isPresent(_xhr.response) ? _xhr.response : _xhr.responseText;
4848
// Implicitly strip a potential XSSI prefix.
49-
if (isString(body) && body.startsWith(XSSI_PREFIX)) {
50-
body = body.substring(XSSI_PREFIX.length);
51-
}
49+
if (isString(body)) body = body.replace(XSSI_PREFIX, '');
5250
let headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders());
5351

5452
let url = getResponseURL(_xhr);

modules/@angular/http/test/backends/xhr_backend_spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,17 @@ export function main() {
472472
existingXHRs[0].dispatchEvent('load');
473473
}));
474474

475+
it('should strip XSSI prefixes', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
476+
var conn = new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
477+
conn.response.subscribe((res: Response) => {
478+
expect(res.text()).toBe('{json: "object"}');
479+
async.done();
480+
});
481+
existingXHRs[0].setStatusCode(200);
482+
existingXHRs[0].setResponseText(')]}\'\n{json: "object"}');
483+
existingXHRs[0].dispatchEvent('load');
484+
}));
485+
475486
it('should strip XSSI prefixes', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
476487
var conn = new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
477488
conn.response.subscribe((res: Response) => {
@@ -491,7 +502,7 @@ export function main() {
491502
async.done();
492503
});
493504
existingXHRs[0].setStatusCode(404);
494-
existingXHRs[0].setResponseText(')]}\',\n{json: "object"}');
505+
existingXHRs[0].setResponseText(')]}\'\n{json: "object"}');
495506
existingXHRs[0].dispatchEvent('load');
496507
}));
497508

0 commit comments

Comments
 (0)