Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

SecurityError: Dom Exception 18 is thrown when local storage/cookies are blocked in Safari on iOS 7.1.2 #629

Closed
nbaronov opened this issue Jul 3, 2014 · 11 comments

Comments

@nbaronov
Copy link

nbaronov commented Jul 3, 2014

Hello,

Please note an unhandled SecurityError exception is thrown, resulting in Angular crash, when storing something in local storage on Safari on iOS 7.1.2 with blocked cookies (Settings > Safari > Block Cookies > Always). I guess disabling cookies also disables local storage. With other two choices of block cookies setting ("From third parties and advertisers" and "Never") everything works fine.

The issue occurs on angular-translate-storage-local.js, line 25:

    var hasLocalStorageSupport = 'localStorage' in $window && $window.localStorage !== null;

and more specifically while accessing $window.localStorage to check if it is null.

I was able to fix this by moving access to $window.localStorage inside the try block (lines 28-30) like this:

    var hasLocalStorageSupport;
    try {
      hasLocalStorageSupport = 'localStorage' in $window && $window.localStorage !== null;
      var testKey = 'pascalprecht.translate.storageTest';
      $window.localStorage.setItem(testKey, 'foo');
      $window.localStorage.removeItem(testKey);
    } catch (e) {
      hasLocalStorageSupport = false;
    }

With above change the code never stores anything (since it can't), but at least Angular doesn't crash. I don't like silent failures like this one, but I'm not sure how to handle this within angular-translate. Maybe an event should be emitted ...

Please let me know what do you think.

Please note this is a repost of closed angular-translate/bower-angular-translate-storage-local#4, requested by @PascalPrecht.

@knalli
Copy link
Member

knalli commented Jul 7, 2014

When it is blocked so save, the storage handler can not save. That's fine. Therefor the patch seems to be okay.

@knalli
Copy link
Member

knalli commented Jul 7, 2014

Ehm.. this has already landed in master? 59aa2a0

Is this an issue any more?

@0x-r4bbit
Copy link
Member

@nbaronov is it this here?
59aa2a0

@nbaronov
Copy link
Author

nbaronov commented Jul 7, 2014

@knalli, @PascalPrecht

Hey guys,

Yes, the issue is in 59aa2a0.

As I wrote in the top post, the exception is thrown when $window.localStorage is checked if it is not null on angular-translate-storage-local.js, line 25:

    var hasLocalStorageSupport = 'localStorage' in $window && $window.localStorage !== null;

So the fix I thought about is to move the access to $window.localStorage inside try block (lines 28-30), so the exception is caught.

@knalli
Copy link
Member

knalli commented Jul 7, 2014

You mean 'localeStorage' in $window is true, but $window.localStorage !== null throws an exception?

@nbaronov
Copy link
Author

nbaronov commented Jul 7, 2014

@knalli

Yes, $window.localStorage !== null throws SecurityError: Dom Exception 18.

I've found it out by decomposing the statement

var hasLocalStorageSupport = 'localStorage' in $window && $window.localStorage !== null;

into something like this:

var hasLocalStorageSupport;
if ('localStorage' in $window) {
  if ($window.localStorage !== null) {
     hasLocalStorageSupport == true
   }
}

and noticed the exception was thrown from this line:

  if ($window.localStorage !== null) {

@knalli
Copy link
Member

knalli commented Jul 7, 2014

@nbaronov Okay, I'm with you. Browsers' JS VMs have such neat api from time to time..

Do want make a PR? Be sure reading the contributions guidelines regarding the correct branches and so on..

@knalli
Copy link
Member

knalli commented Jul 7, 2014

Which also confirms 59aa2a0 did not solve the issue #384 correctly.

@nbaronov
Copy link
Author

nbaronov commented Jul 7, 2014

@knalli I'll check the guidelines and create a PR. It might also fix issue #384.

knalli added a commit to knalli/angular-translate that referenced this issue Aug 3, 2014
This fixes a unhandled exception (i.e. iOS) when checking for local storage support.

fixes angular-translate#629
@knalli
Copy link
Member

knalli commented Aug 3, 2014

Fix landed as 75504cb

  • 53a8bad the fix for the fix (browser do not throw an exception, but store anything different than null into it.

@JackieWYB
Copy link

After removing the block cookie settings from safari, works like a charm. thanks @nbaronov .

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants