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

ConnectIdSystem.js: fix storage bypass #11964

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions modules/connectIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,11 @@ export const connectIdSubmodule = {
*/
userHasOptedOut() {
try {
// TODO FIX THIS RULES VIOLATION
// eslint-disable-next-line
return localStorage.getItem(OVERRIDE_OPT_OUT_KEY) === '1';
if (storage.localStorageIsEnabled()) {
return storage.getDataFromLocalStorage(OVERRIDE_OPT_OUT_KEY) === '1';
} else {
return true;
}
} catch {
return false;
}
Expand Down
55 changes: 27 additions & 28 deletions test/spec/modules/connectIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,24 +549,28 @@ describe('Yahoo ConnectID Submodule', () => {
expect(result.callback).to.be.a('function');
});

function mockOptout(value) {
getLocalStorageStub.callsFake((key) => {
if (key === 'connectIdOptOut') return value;
})
}

it('returns an undefined if the Yahoo specific opt-out key is present in local storage', () => {
localStorage.setItem('connectIdOptOut', '1');
mockOptout('1');
expect(invokeGetIdAPI({
he: HASHED_EMAIL,
pixelId: PIXEL_ID
}, consentData)).to.be.undefined;
localStorage.removeItem('connectIdOptOut');
});

it('returns an object with the callback function if the correct params are passed and Yahoo opt-out value is not "1"', () => {
localStorage.setItem('connectIdOptOut', 'true');
mockOptout('true');
let result = invokeGetIdAPI({
he: HASHED_EMAIL,
pixelId: PIXEL_ID
}, consentData);
expect(result).to.be.an('object').that.has.all.keys('callback');
expect(result.callback).to.be.a('function');
localStorage.removeItem('connectIdOptOut');
});

it('Makes an ajax GET request to the production API endpoint with pixelId and he query params', () => {
Expand Down Expand Up @@ -804,6 +808,25 @@ describe('Yahoo ConnectID Submodule', () => {
});
});
});
describe('userHasOptedOut()', () => {
it('should return a function', () => {
expect(connectIdSubmodule.userHasOptedOut).to.be.a('function');
});

it('should return false when local storage key has not been set function', () => {
expect(connectIdSubmodule.userHasOptedOut()).to.be.false;
});

it('should return true when local storage key has been set to "1"', () => {
getLocalStorageStub.returns('1');
expect(connectIdSubmodule.userHasOptedOut()).to.be.true;
});

it('should return false when local storage key has not been set to "1"', () => {
getLocalStorageStub.returns('hello');
expect(connectIdSubmodule.userHasOptedOut()).to.be.false;
});
});
});

describe('decode()', () => {
Expand Down Expand Up @@ -884,28 +907,4 @@ describe('Yahoo ConnectID Submodule', () => {
})).to.be.true;
});
});

describe('userHasOptedOut()', () => {
afterEach(() => {
localStorage.removeItem('connectIdOptOut');
});

it('should return a function', () => {
expect(connectIdSubmodule.userHasOptedOut).to.be.a('function');
});

it('should return false when local storage key has not been set function', () => {
expect(connectIdSubmodule.userHasOptedOut()).to.be.false;
});

it('should return true when local storage key has been set to "1"', () => {
localStorage.setItem('connectIdOptOut', '1');
expect(connectIdSubmodule.userHasOptedOut()).to.be.true;
});

it('should return false when local storage key has not been set to "1"', () => {
localStorage.setItem('connectIdOptOut', 'hello');
expect(connectIdSubmodule.userHasOptedOut()).to.be.false;
});
});
});