grevory/angular-local-storage

Bug with clearAll and regexes using preset prefix.

daredevil82 opened this issue · 0 comments

Lets say I have my localstorage entries prefixed with dd82-app, and I want to clear all keys with that prefix. I do this with localStorageService.clearAll('^' + this._constants.prefix + '.');

This call always returns true, although the keys and values still exist in localstorage. Digging through the code reveals the issue at line 305:

      // Only remove items that are for this app and match the regular expression
      if (prefixRegex.test(key) && testRegex.test(key.substr(prefixLength))) {

So, with my prefix dd82-app, results in a prefixRegex value of /^dd82-app./ and testRegex matching

So, for a sample key dd82-app.access_token, the first part of the conditional, prefixRegex.test(key) works. It fails on the second part, because key.substr(prefixLength) returns a value of access-token, which does not match testRegex. Because that conditonal fails, the key is never removed.

In my case, this can be fixed by adding 0, resulting in a key.substr(0, prefixLength) call, but may break other cases.