Skip to content

Commit

Permalink
Fix exception on __pb_locator__ lookup (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi authored Oct 3, 2024
1 parent 1c32e8b commit df66ebd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export function prebidMessenger(publisherURL, win = window) {
}

let target = win.parent;
while (target != null && target !== win.top && !isPrebidWindow(target)) target = target.parent;
if (!isPrebidWindow(target)) target = win.parent;
try {
while (target != null && target !== win.top && !isPrebidWindow(target)) target = target.parent;
if (!isPrebidWindow(target)) target = win.parent;
} catch (e) {}

return function sendMessage(message, onResponse) {
if (prebidDomain == null) {
Expand Down
33 changes: 24 additions & 9 deletions test/spec/messaging_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,36 @@ describe('prebidMessenger',() => {
}
};
})
it('should post to first ancestor that has a __pb_locator__ child', () => {
[target, target.parent].forEach(win => {
win.frames = {
__pb_locator__: {}
};
Object.entries({
throws() { throw new DOMException() },
'does not throw'() { return {} }
}).forEach(([t, getFrames]) => {
describe(`when ancestor ${t}`, () => {
beforeEach(() => {
Object.defineProperty(target.parent.parent, 'frames', {get: getFrames});
})
it('should post to first ancestor that has a __pb_locator__ child', () => {
[target, target.parent].forEach(win => {
win.frames = {
__pb_locator__: {}
};
})
sendMessage('test');
sinon.assert.called(target.postMessage);
});
})
sendMessage('test');
sinon.assert.calledWith(target.postMessage);
});
})
it('should post to immediate parent when no ancestor has __pb_locator__', () => {
win.parent.postMessage = sinon.spy();
delete target.postMessage;
sendMessage('test');
sinon.assert.calledWith(win.parent.postMessage);
sinon.assert.called(win.parent.postMessage);
});
it('should post to first restricted frame if no __pb_locator__ can be found', () => {
Object.defineProperty(target, 'frames', {get() { throw new DOMException() }});
sendMessage('test');
sinon.assert.called(target.postMessage)
})
});

it('should not run callback on response if origin does not mach', ()=> {
Expand Down

0 comments on commit df66ebd

Please sign in to comment.