Skip to content

Commit

Permalink
fix: use error-prone reference to crypto module (prebid#11882)
Browse files Browse the repository at this point in the history
  • Loading branch information
chernodub authored Jul 2, 2024
1 parent 85d38aa commit 97687dd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion modules/yandexIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class YandexUidGenerator {
}

_getRandomGenerator() {
if (crypto) {
if (window.crypto) {
return () => {
const buffer = new Uint32Array(1);
crypto.getRandomValues(buffer);
Expand Down
7 changes: 6 additions & 1 deletion test/spec/modules/yandexIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,17 @@ describe('YandexId module', () => {

describe('crypto', () => {
it('uses Math.random when crypto is not available', () => {
sandbox.stub(window, 'crypto').value(undefined);
const cryptoTmp = window.crypto;

// @ts-expect-error -- Crypto is always defined in modern JS. TS yells when trying to delete non-nullable property.
delete window.crypto;

yandexIdSubmodule.getId(CORRECT_SUBMODULE_CONFIG);

expect(randomStub.calledOnce).to.be.true;
expect(getCryptoRandomValuesStub.called).to.be.false;

window.crypto = cryptoTmp;
});

it('uses crypto when it is available', () => {
Expand Down

0 comments on commit 97687dd

Please sign in to comment.