-
Notifications
You must be signed in to change notification settings - Fork 27.4k
$http
fails on LG webOS using the file://
protocol as cookies aren't accessible
#15523
Comments
This is a quite uncommon usecase (and has an easy - even if not ideal - workaround), but if you feel like it you can submit a PR fixing it. Basically, we could replace this line with something like: -var currentCookieString = rawDocument.cookie || '';
+var currentCookieString = safeGetCookie(rawDocument);
+...
+function safeGetCookie(document) {
+ try {
+ return document.cookie || '';
+ } catch (err) {
+ return '';
+ } |
Quite hard to test though... I've tried to play with a getter: it('should return an empty string if cookie cannot be read', function() {
// see #15523 - sometimes reading cookies throws an error
document.cookie = 'cookie_name=cookie_value';
Object.defineProperty(document, 'cookie', {
configurable: true,
get: function() { throw new Error('#15523'); }
});
expect($$cookieReader()['cookie_name']).toBeUndefined();
Object.defineProperty(document, 'cookie', {
writable: true
});
}); But this breaks many other tests. Works as expected on this fiddle though: https://jsfiddle.net/31k61c3L/. Any idea? Maybe I simply can't brutalise |
No you can't (or rther you shouldn't). That's where DI comes into play 😃 I haven't looked at the testsuite, but something along the following lines should work: it('should return an empty string if cookie cannot be read', function() {
var cookieSpy = jasmine.createSpy('cookie').and.throwError('Can\'t touch this!');
var mockDocument = {};
Object.defineProperty(mockDocument, 'cookie', {get: cookieSpy});
module(function($provide) {
$provide.value($document, [mockDocument]);
});
inject(function($$cookieReader) {
expect($$cookieReader()).toEqual({});
expect(cookieSpy).toHaveBeenCalled();
});
}); EDIT: |
Thanks @gkalpak, very educative! |
…ookie` In certain cases (e.g. on LG webOS using the `file:` protocol), access to `document.cookie` may not be allowed and throw an error. This could break `$http` which relies on `$$cookieReader()` for retrieving the XSRF token. This commit fixes it by treating `document.cookie` as empty, when access to it is fordibben. Fixes #15523 Closes #15532
…ookie` In certain cases (e.g. on LG webOS using the `file:` protocol), access to `document.cookie` may not be allowed and throw an error. This could break `$http` which relies on `$$cookieReader()` for retrieving the XSRF token. This commit fixes it by treating `document.cookie` as empty, when access to it is fordibben. Fixes #15523 Closes #15532
…ookie` In certain cases (e.g. on LG webOS using the `file:` protocol), access to `document.cookie` may not be allowed and throw an error. This could break `$http` which relies on `$$cookieReader()` for retrieving the XSRF token. This commit fixes it by treating `document.cookie` as empty, when access to it is fordibben. Fixes angular#15523 Closes angular#15532
Note: for support questions, please use one of these channels: https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#question. This repository's issues are reserved for feature requests and bug reports.
Do you want to request a feature or report a bug?
bug
What is the current behavior?
On LG webOS using the
file://
protocol,$http
fails because it calls$$cookieReader
that throwsSecurityError: DOM Exception 18
.$http
calls$$cookieReader
for CSRF checks: https://github.com/angular/angular.js/blob/v1.6.0/src/ng/http.js#L1287.$$cookieReader
throws the error when accessingrawDocument.cookie
: https://github.com/angular/angular.js/blob/v1.6.0/src/ng/cookieReader.js#L27If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (template: http://plnkr.co/edit/tpl:yBpEi4).
Quite hard to reproduce, as you need an LG webOS device... Just make
$$cookieReader
throw an error?What is the expected behavior?
Either
$http
or$$cookieReader
could catch errors so thatxsrfValue
gets undefined if cookies aren't accessible?What is the motivation / use case for changing the behavior?
To fix a bug :)
Which versions of Angular, and which browser / OS are affected by this issue? Did this work in previous versions of Angular? Please also test with the latest stable and snapshot (https://code.angularjs.org/snapshot/) versions.
Issue met with 1.6.0 on LG webOS.
Other information (e.g. stacktraces, related issues, suggestions how to fix)
Actual stack trace:
For now, I temporary decorate
$$cookieReader
to disable it:The text was updated successfully, but these errors were encountered: