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

Prevent renderAd from overwriting the website DOM document #602

Closed
kasparsd opened this issue Sep 6, 2016 · 6 comments
Closed

Prevent renderAd from overwriting the website DOM document #602

kasparsd opened this issue Sep 6, 2016 · 6 comments
Assignees
Labels

Comments

@kasparsd
Copy link
Contributor

kasparsd commented Sep 6, 2016

By accidentally changing document to w in the suggested ad creative line item, one is able to completely override the DOM of the parent document (the website) with the ad output.

Would it make sense to add a doc !== document check here to prevent that from happening?

That is even suggested in the description of the renderAd() method:

/**
 * This function will render the ad (based on params) in the given iframe document passed through. Note that doc SHOULD NOT be the parent document page as we can't doc.write() asynchrounsly
 * @param  {object} doc document
 * @param  {string} id bid id to locate the ad
 * @alias module:$$PREBID_GLOBAL$$.renderAd
 */
$$PREBID_GLOBAL$$.renderAd = function (doc, id) {
@mkendall07
Copy link
Member

Ah, that is a problem is the creative uses window.document
We'll update that right away - thanks.

@mkendall07 mkendall07 added the bug label Sep 6, 2016
@mkendall07 mkendall07 self-assigned this Sep 6, 2016
@mkendall07
Copy link
Member

@kasparsd
I'm not sure what you mean by accidentally changing document to w - since these are 2 separate objects. The window object get's assigned to it's parent, which arguably should not be happening, but the creative document passed is in fact the creative document. Are you suggesting that it causes issues when someone alters this code?

@kasparsd
Copy link
Contributor Author

kasparsd commented Sep 7, 2016

@mkendall07 I wasn't clear about it, sorry.

The core issue here is that pbjs.renderAd() expects doc to be the creative document and doesn't protect the website document from being written to.

Currently, passing the website document to pbjs.renderAd() will result in the ad replacing the whole website because we're using document.write() to insert either the ad HTML or the iframe! I assume this is not desired and shouldn't be possible.

@kasparsd kasparsd changed the title Prevent adops from rendering the ad in the parent document Prevent renderAd from overwriting the website DOM document Sep 7, 2016
@brs14ku
Copy link

brs14ku commented Sep 30, 2016

Sorry to reopen, but I'm unsure of what is actually expected for the document parameter now. Anything I pass other than the actual document gives me a ERROR: Error trying to write ad Id :5247b9619e4e776 to the page:e.write is not a function. I've tried this with my iframe id, the dfp slot container div id and more. I did notice the docs say This function is usually used in the ad server’s creative. though I'm unsure of how that could be done outside of using creative templates. Any guidance here would be greatly appreciated. Perhaps we could update the readme to explain what is expected here?

@mkendall07
Copy link
Member

@brs14ku
It should be the iframe's document object. 2 different examples are shown here https://github.com/prebid/Prebid.js/blob/master/integrationExamples/gpt/creative_rendering.html

@prebidtappx
Copy link
Contributor

prebidtappx commented Jan 13, 2022

Try this...

var adUnits = [{
  code: 'mrec', //This have to be equal of an id of div in your page
  mediaTypes: {
    banner: {
      sizes: [300,50]
    }
  },
  bids: [{
    bidder: 'bidderName like Tappx',
      params: {
         //the params of the bidder...
      }
   }]
}
//...code...
pbjs.requestBids({
  timeout: 2000,
  bidsBackHandler: function (bids) {
    for (const bid in bids) {
      //This is manual rewrite of renderAd - trick
      let highestCpmBids = pbjs.getHighestCpmBids(bid);
      var placement = document.getElementById(bid)
      placement.write = function(adm) {this.innerHTML=adm}
      placement.close = function() {console.log('closeRender')}
      //This is the renderAd of Prebid
      pbjs.renderAd(placement, highestCpmBids[0].adId)
    }
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants