Investigate using Blob to render non-A4A
Go to https://example.com/ (they have no CSP)
var blob = new Blob([`
<script>
Object.defineProperty(document, 'cookie', {
get: () => '',
set: val => undefined,
});
</script>
<script>console.log(window.top.Object);</script>
`], {type : 'text/html'});
var url = URL.createObjectURL(blob);
var iframe = document.createElement('iframe');
iframe.src = url;
iframe.sandbox = 'allow-scripts';
document.body.appendChild(iframe);
// => Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame.
This might be better than the current NameFrame or SafeFrame options. No extra network connections, no need to parse anything. 😁
I've mentioned this before. A4A is tooooooooo complicated and this could simplify it considerably.
As for the same-origin-policy restrictions, we can polyfill with fakes as needed? NameFrame has a unique origin every time anyways.
An iframe sandbox without 'allow-same-origin' comes with some pretty onerous browser restrictions, like "All XHRs automatically fail" :-(. See e.g. http://stackoverflow.com/questions/14486461/iframe-sandbox-attribute-is-blocking-ajax-calls for some discussion.
Are you imagining a polyfill for XHR which instead sends a postMessage to the parent, which runs a dedicated handler to proxy the request? My instinct is that writing a proxied network stack in javascript would be more painful than NameFrame, but maybe I'm being too pessimistic. Oh, and we would need to inject it into any friendly iframes of the blob iframe as well, recursively.
There's also the issue that any attempts to look at document.cookie or touch localStorage throw DOM exceptions. As in your example code, no-op polyfills for those seem more approachable, but there's still the need to recursively install them in all friendly child iframes.
I would love to take you up on this suggestion, but taking an arbitrary (non-AMP) ad and running it inside this deliberately-broken browser environment seems like a really high barrier.
Are blob based frames same origin as parent by default?
I tried using data URLs when building AMP's 3p frames originally. The main problem there are
- in Firefox they are same origin VS. unique origin in other browsers
document.cookieaccess fails instead of being a blackhole which would be more useful.
So, if the cookie override works, this could be used without the sandbox attribute in non-Firefox browsers.
"All XHRs automatically fail"
You can make "no-cors" requests, apparently. Would that suit the needs? If not, polyfilling XHRs through the parent would be wayyyy to much work.
no-op polyfills for those seem more approachable, but there's still the need to recursively install them in all friendly child iframes.
That's not too bad.
Are blob based frames same origin as parent by default?
Yes, unfortunately.
document.cookie access fails instead of being a blackhole which would be more useful.
Can we file a bug for the browsers?
So, if the cookie override works, this could be used without the sandbox attribute in non-Firefox browsers.
Sweet, it's pretty easy to generate data: urls for arbitrary content. And, conveniently, Firefox blackholes document.cookie when it's restricted.
Just to clarify: you could try using data for this in all browsers except Firefox. Lets discuss investing time in this right now, though.
It is really sad that Fx is different here. Not sure a change is worth it, if we have to special case it.
It is really sad that Fx is different here. Not sure a change is worth it, if we have to special case it.
Why not just restrict to no "allow-same-origin" and use a data: URI everywhere? You get essentially the same results as Blob without "allow-same-origin".
In general any sandbox on ads is problematic because Chrome is the only browser that supports opting out of child windows (like target=_blank) inheriting the restrictions.
So, you are saying to use no sandbox, except in Firefox? All child windows would then have no allow-same-origin and thus be broken for sure (even if the ad did work, which is unlikely).
They need allow-scripts at the least, which'll put them in no-same-origin mode. But even if we were to give the data: URIs no sandbox, we still have cookie restrictions (can't have cookies on null origin, except FF which blackholes).
@jridgewell But you did demonstrate that manual blackholing works, right? Would probably also need to be done for other storage types.
Yup, to both.
@jridgewell who is this request to? @ampproject/a4a team or are you going to do it yourself?
@ampproject/a4a. Or, if they could give me the requirements for the ad creatives, I could do it myself.
Sorry, I thought it was clear that this was impossible with current browser tech. We need to render ad creatives which might (1) issue XHRs, (2) touch document.cookie, (3) touch localStorage, (4) do all of (1-3) inside of nested same-domain or cross-domain iframes.
NameFrame is an implementation of the API "Render the following string of HTML inside a cross-domain iframe", where the HTML can be an arbitrary web page. I just don't see a simpler implementation. Asking browsers to offer one seems like the only feasible plan, but of obviously it's slow, and we still need today's code paths until all browsers support whatever new API is everywhere.
@jridgewell Is this still relevant or can we close?