Skip to content

Commit

Permalink
ConnectIdSystem.js: fix storage bypass (prebid#11964)
Browse files Browse the repository at this point in the history
* Update connectIdSystem.js: fix storage bypass

* Update connectIdSystem_spec.js

* Update connectIdSystem.js

* Update connectIdSystem_spec.js

* Update connectIdSystem.js

* Fix tests

---------

Co-authored-by: Demetrio Girardi <[email protected]>
  • Loading branch information
2 people authored and DecayConstant committed Jul 18, 2024
1 parent 15455b0 commit 8675968
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
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;
});
});
});

0 comments on commit 8675968

Please sign in to comment.