From 802ef7732fddbbbb4d809bd063e4576b0fdf694e Mon Sep 17 00:00:00 2001 From: Olivier Date: Tue, 14 May 2024 15:48:51 +0200 Subject: [PATCH] Utils: add isSafeFrameWindow(), canAccessWindowTop() --- src/utils.js | 23 +++++++++++++++++++++++ test/spec/utils_spec.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/utils.js b/src/utils.js index 3225eac162e..46dd06a6a41 100644 --- a/src/utils.js +++ b/src/utils.js @@ -41,6 +41,7 @@ export const internal = { createTrackPixelIframeHtml, getWindowSelf, getWindowTop, + canAccessWindowTop, getWindowLocation, insertUserSyncIframe, insertElement, @@ -180,6 +181,16 @@ export function getWindowLocation() { return window.location; } +export function canAccessWindowTop() { + try { + if (internal.getWindowTop().location.href) { + return true; + } + } catch (e) { + return false; + } +} + /** * Wrappers to console.(log | info | warn | error). Takes N arguments, the same as the native methods */ @@ -620,6 +631,18 @@ export function inIframe() { } } +/** + * https://iabtechlab.com/wp-content/uploads/2016/03/SafeFrames_v1.1_final.pdf + */ +export function isSafeFrameWindow() { + if (!inIframe()) { + return false; + } + + const ws = internal.getWindowSelf(); + return !!(ws.$sf && ws.$sf.ext); +} + export function isSafariBrowser() { return /^((?!chrome|android|crios|fxios).)*safari/i.test(navigator.userAgent); } diff --git a/test/spec/utils_spec.js b/test/spec/utils_spec.js index 8ea68aadecc..9236b9d6ac1 100644 --- a/test/spec/utils_spec.js +++ b/test/spec/utils_spec.js @@ -21,6 +21,46 @@ describe('Utils', function () { type_array = 'Array', type_function = 'Function'; + describe('canAccessWindowTop', function () { + it('should return true if window.top is accessible', function () { + assert.equal(utils.canAccessWindowTop(), true); + }); + + it('should return false if window.top is not accessible', function () { + const stubGetWindowTop = sinon.stub(utils.internal, 'getWindowTop').throws(); + assert.equal(utils.canAccessWindowTop(), false); + stubGetWindowTop.restore(); + }); + }); + + describe('isSafeFrameWindow', function () { + // SafeFrames implementation + // https://iabtechlab.com/wp-content/uploads/2016/03/SafeFrames_v1.1_final.pdf + const $sf = { + ext: { + geom: function() {} + } + }; + + afterEach(function() { + delete window.$sf; + }) + + it('should return true if window.$sf is accessible', function () { + window.$sf = $sf; + assert.equal(utils.isSafeFrameWindow(), true); + }); + + it('should return false if window.$sf is missimplemented', function () { + window.$sf = {}; + assert.equal(utils.isSafeFrameWindow(), false); + }); + + it('should return false if window.$sf is missing', function () { + assert.equal(utils.isSafeFrameWindow(), false); + }); + }); + describe('getBidIdParameter', function () { it('should return value of the key in input object', function () { var obj = {