15

I have a product that's playing a video in Flash (if available), and falls back to HTML5 if Flash isn't available.

I'm not able to find a way to determine if JavaScript is executing within an Iframe with the "sandbox" attribute, which is necessary for my solution because sandboxed iframes disable all plugins. The sandboxed iframe could be as simple as this:

<iframe src="http://www.cross-domain.com/" sandbox="allow-scripts">

To determine if Flash is enabled, I'm using swfobject's method of checking navigator.plugins["Shockwave Flash"].description, which is set even when in a sandboxed iframe. I can load the swf object, but it doesn't play.

To reproduce this issue, visit http://jsfiddle.net/max_winderbaum/9cqkjo45/, open your chrome inspector and click "Run". The script on the cross-domain site will pause in the context of the sandboxed iframe.

According to the W3 spec at http://dev.w3.org/html5/spec-preview/browsers.html#sandboxing-flag-set, there is supposed to be an "active sandboxing flag set" on the document that JavaScript can access (at least that's how I'm reading the spec). There doesn't seem to be any flag set on the iframe's document.

Does anyone have any ideas / solutions on how to detect if JavaScript is executing from within a sandboxed iframe?

7
  • But surely that's the point of the HTML5 fallback? Apr 3, 2015 at 15:37
  • 1
    @theonlygusti If you keep reading, the issue is that I have no idea of when I need to fall back to HTML5. Everything I can get from the browser tells me Flash is enabled, so I load Flash. If I need to load my HTML5 solution, I need to know if Flash is disabled in JavaScript. If you tell me how I can tell if Flash is disabled, it will resolve my issue. Apr 3, 2015 at 15:54
  • What about document.plugins? That might tell you something different than navigator.plugins. The latter is the installed plugins, the former is (supposedly) the plugins in the document, which may differ due to being sandboxed. I haven't checked though. Apr 3, 2015 at 16:34
  • In both a sandboxed iframe and outside of an iframe altogether, document.plugins returns an empty array in Chrome and Firefox. Good idea, though! Apr 3, 2015 at 16:42
  • Duplicate with stackoverflow.com/questions/34043278/… (although neither has an upvoted answer).
    – Tim B
    Dec 3, 2015 at 17:56

2 Answers 2

6
+50

I will consider different kinds of iframes (choose the first case which applies):

Note: Firefox treats data URIs as same-origin, so it's OK. However, Chrome treats them as cross-origin. Then frameElement doesn't work and document.domain is the empty string regardless of whether the iframe is sandboxed or not. You can check whether location.protocol is 'data:' string to detect data URIs.

In general, you might try something like

function isSandboxedIframe() {
  if (window.parent === window) return 'no-iframe';
  try { var f = window.frameElement; } catch(err) { f = null; }
  if(f === null) {
    if(document.domain !== '') return 'unkown'; // Probably 'non-sandboxed'
    if(location.protocol !== 'data:') return 'sandboxed';
    return 'unkown'; // Can be 'sandboxed' on Firefox
  }
  return f.hasAttribute('sandbox') ? 'sandboxed' : 'non-sandboxed';
}
8
  • 1
    This works for me on Firefox, but on Chrome, it looks like trying to access window.frameElement from a sandboxed iframe throws an exception. Of course, catching the exception should be a reasonable diagnostic in itself. Dec 4, 2015 at 17:00
  • @IlmariKaronen Interesting, w3c says browsers should throw but whatwg says they should return null. I will update my answer.
    – Oriol
    Dec 4, 2015 at 20:27
  • Damn, it seemed to work well on Firefox for different domain too, but only because of caching. Sorry Chrome for blaming you for not doing it well. Then this answer becomes somewhat superfluous. I will think if I can improve it someway, or otherwise I may delete it.
    – Oriol
    Dec 5, 2015 at 21:32
  • Because this doesn't work for cross-origin iframes, it's not really a general case as far as what I need it to do Dec 6, 2015 at 17:33
  • OK, now this works for some cross-origin iframes, and a reasonable assumption can be made for the others.
    – Oriol
    Dec 7, 2015 at 2:23
4

A project sandblaster can help you detect if you running being sandboxed.

Sandbox check if itself is framed first and then scans through the attributes of the frame element to detect several information about itself. These includes framed, crossOrigin, sandboxed, sandboxAllowances, unsandboxable, resandboxable, sandboxable.

To detect if itself is sandboxed in our case, it checks if the frame element has an attribute sandbox.

// On below `frameEl` is the detected frame element
try {
  result.sandboxed = frameEl.hasAttribute("sandbox");
}
catch (sandboxErr) {
  result.sandboxed = null;
  if (typeof errback === "function") {
    errback(sandboxErr);
  }
}

I tried to replicate your issue and to test if this solution works, I had to paste the script into the window itself due to the security issue.

<html>
    <head>
    </head>
    <body>

    <script>
        //Paste the contents of the script(https://raw.githubusercontent.com/JamesMGreene/sandblaster/master/dist/sandblaster.js) here

        var result = sandblaster.detect();
        if(result.sandboxed === true) {
            //sandboxed
        }
        debugger;
    </script>
    </body>
</html>

Here is a demo: http://jsfiddle.net/Starx/tzmn4088/ that shows this working.

5
  • Any chance you could add a little on how sandblaster does what it does to this post as well in case the project disappears in the future?
    – Tim B
    Dec 4, 2015 at 9:21
  • @TimB, Sure. I added some which I think is relevant.
    – Starx
    Dec 4, 2015 at 9:40
  • This works great for me, even cross-domain. The key is in the _getEffectiveScriptOrigin() function. Basically, if you're in a sandboxed iframe, document.domain will be the empty string. Otherwise it will be set to something. Dec 6, 2015 at 16:21
  • Here's an example with a debugger statement at the line that detects the sandboxed iframe: jsfiddle.net/max_winderbaum/q9ca0c8g Dec 6, 2015 at 17:30
  • @Omninternet, Nice one.
    – Starx
    Dec 7, 2015 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.