Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check localstorage availability before accessing it #5616

Conversation

Swiiip
Copy link
Contributor

@Swiiip Swiiip commented Aug 17, 2020

Type of change

  • Bugfix

Description of change

Chrome now throws an error when trying to access window.localstorage in a third-party context when third-party cookies are disabled (Private mode for instance). This PR checks whether the localStorage is available each time we try to access it.

Copy link
Member

@mkendall07 mkendall07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for adding tests.

Btw, in this case what is "third party context"? Is that just JS loaded from 3p domain?

access window.localstorage in a third-party context

@mkendall07 mkendall07 added the needs 2nd review Core module updates require two approvals from the core team label Aug 17, 2020
@dmdabbs
Copy link

dmdabbs commented Aug 17, 2020

Is local storage access gated by the consent module, where applicable? If you can't read a cookie then No local storage soup for you either.

Copy link
Collaborator

@jsnellbaker jsnellbaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Swiiip

Can you please investigate this test failure? I have run the browserstack tests several times and this test always appears to fail in IE11.

storage manager
    ✗ should not throw if the localstorage is not accessible when setting/getting/removing from localstorage
	TypeError: Cannot redefine non-configurable property 'localStorage'
	   at restore (node_modules/sinon/pkg/sinon.js:2881:13)
	   at Anonymous function (webpack:///test/spec/unit/core/storageManager_spec.js:60:5 <- test/test_index.js:368578:5)

Thanks

Comment on lines 50 to 61
const localstorageStub = sinon.stub(global.window, 'localStorage').get(() => { throw Error });
const errorLogSpy = sinon.spy(utils, 'logError');

coreStorage.setDataInLocalStorage('key', 'value');
const val = coreStorage.getDataFromLocalStorage('key');
coreStorage.removeDataFromLocalStorage('key');

expect(val).to.be.null;
sinon.assert.calledThrice(errorLogSpy);

localstorageStub.restore();
errorLogSpy.restore();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the stubs (and the subsequent restores) into a before/after function surrounding this test? This will help ensure these objects are properly handled in case the test fails. If you don't want to have these stubs effect the other tests, please feel free to create another describe block to nest it altogether.

Copy link
Contributor Author

@Swiiip Swiiip Sep 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 👍

Btw, in this case what is "third party context"? Is that just JS loaded from 3p domain?

access window.localstorage in a third-party context

Indeed, 3rd-party context refers to scripts run inside an iframe whose src domain is not the 1p domain for instance.

@stale
Copy link

stale bot commented Sep 5, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 5, 2020
@muuki88
Copy link
Collaborator

muuki88 commented Sep 7, 2020

@Swiiip any update on this? For me this sounds rather critical

@stale stale bot removed the stale label Sep 7, 2020
@Swiiip
Copy link
Contributor Author

Swiiip commented Sep 8, 2020

Hello sorry for the delay I'm trying to make the test pass on IE 11, the error seems linked to the way sinon mocks the localstorage property getters.

@Swiiip Swiiip requested a review from jsnellbaker September 8, 2020 12:59
throw Error;
}
};
Object.defineProperty(window, 'localStorage', mock);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Swiiip

Can you remove this property as part of the afterEach()? From how it looks now, this mocked function may be available for other tests in other files (since we generally run everything together).

Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I reset the property descriptor after each test

@Swiiip Swiiip force-pushed the check-localstorage-availability-before-get branch from 88022e5 to cf3f014 Compare September 15, 2020 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs 2nd review Core module updates require two approvals from the core team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants