amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Instance of amp-ad 3p frame with same type all indicate they are master frame

Open keithwrightbos opened this issue 9 years ago • 12 comments

Short description of your issue:

Attempting to use window.context.master to store state between 3p frames to allow for incrementing frame count to be sent on ad request. However, each frame seems to indicate it is the master due to window.parent.frames['frame_doubleclick_master'] consistently throwing DOMException due to cross origin access (see https://github.com/ampproject/amphtml/blob/master/3p/integration.js#L359)

How do we reproduce the issue?

  1. Emulating mobile on chrome, go to page with multiple doubleclick amp-ad instances using delayed fetch (e.g. https://amp-andro4all-com.cdn.ampproject.org/v/s/amp.andro4all.com/2017/01/nintendo-fecha-llegada-super-mario-run-android?amp_js_v=9#origin=https%3A%2F%2Fwww.google.com.ar&exp=a4a%3A0&channelid=0&cid=1&dialog=0&prerenderSize=1&paddingTop=54&history=1&p2r=0&horizontalScrolling=0&csi=0&storage=1&viewerUrl=https%3A%2F%2Fwww.google.com.ar%2Famp%2Fs%2Famp.andro4all.com%2F2017%2F01%2Fnintendo-fecha-llegada-super-mario-run-android&cap=swipe)
  2. In console, go to remote.html frame (name should start with "{"attributes":{"type":"doubleclick","slot":"...)
  3. execute window.parent.frames['frame_doubleclick_master'] which will throw with error: Uncaught DOMException: Blocked a frame with origin "https://altb.marfeel.com" from accessing a cross-origin frame.

What browsers are affected?

All

Which AMP version is affected?

Unknown whether this is a new issue given this is our first attempt at using window.context.master

keithwrightbos avatar Mar 13 '17 17:03 keithwrightbos

@ampproject/a4a FYI

keithwrightbos avatar Mar 13 '17 17:03 keithwrightbos

@keithwrightbos the right way to get a master frame is to call

window.context.master

inside the iframe window.

lannka avatar Mar 15 '17 05:03 lannka

I realize that and I do get a window object in that case. The problem is that every slot thinks its the master window because the underlying method in which master is assigned to context as mentioned in the description throws a DOMException. As a result, my attempts to store state across slots using window does not work.

keithwrightbos avatar Mar 15 '17 10:03 keithwrightbos

The technique used to make a frame into the master frame seems completely bogus to me: at https://github.com/ampproject/amphtml/blob/master/3p/integration.js#L371 the window renames itself to the master name (comment: "Yaihh."), but that will have no effect on what its name looks like when inspected from the window.parent's point of view.

That's why we get the DOM exception error that Keith mentioned: (some) browsers throw an exception whenever the named window does not exist, and with this code, it never does.

michaelkleber avatar Mar 15 '17 11:03 michaelkleber

Agreed. Fix is to not rely on name and instead iterate through the frames finding any that are friendly and assigning a variable (e.g. amp_master = true). @lannka do you have bandwidth to work on this? If not, I can assign to someone on my team.

keithwrightbos avatar Mar 15 '17 11:03 keithwrightbos

It should be working. can you try go to one of the iframe window, and do window.context.master. (this will elect itself as master frame)

Then go to another iframe with the same domain, and compare window.context.master.name with window.name. They should be different, because master is not itself.

window.context.master.name should give you 'frame_adense_master' and window.name should give you the original json object.

lannka avatar Mar 15 '17 16:03 lannka

I just tested this on https://cdn.ampproject.org/c/s/mobile.nytimes.com/2017/03/15/us/politics/budget-epa-state-department-cuts.amp.html by (scrolling down, so all ads load)

  1. changing the context to the master ad (it's name is a crazy {"host":"d-964447504926939108.ampproject in Dev Tools).
  2. Change some property, window.test = 'testing'.
  3. Go into the next ad context, check the value: context.master.test

jridgewell avatar Mar 15 '17 23:03 jridgewell

Interesting. It does appear to work when I test now on production pages as mentioned though I'm not sure why. @michaelkleber Thoughts?

keithwrightbos avatar Mar 16 '17 10:03 keithwrightbos

You're right, I do see this working on Chrome! I'm astonished to see window.name as a two-way communications channel.

But not on iOS, as far as I can tell (though inspecting the state of cross-domain iframes on Safari isn't working ideally for me). Are there any automated tests indicating that this works across browsers?

michaelkleber avatar Mar 16 '17 10:03 michaelkleber

@michaelkleber indeed :-). but the two-way communication is for same origin frames, so it's just an easier API instead of writing a loop as @keithwrightbos has suggested.

We don't have good functional tests for it yet. To test it really quick, I can try to create a concept-prove example for Safari.

Also, + @nekodo (the original author). Do you have idea about browser compatibility of this technique?

lannka avatar Mar 16 '17 17:03 lannka

Tested on Safari on my Mac and it does look like name works as is. I recommend we try using the context.master as is and can re-open as needed.

keithwrightbos avatar Mar 16 '17 22:03 keithwrightbos

Thanks for verifying.

lannka avatar Mar 16 '17 22:03 lannka