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?
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
.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.
Here is a demo: http://jsfiddle.net/Starx/tzmn4088/ that shows this working.
I will consider different kinds of iframes (choose the first case which applies):
Iframes with the sandboxed scripts browsing context flag
That is, iframes with a
sandbox
attribute which doesn't contain theallow-scripts
keyword.This flag blocks script execution. In particular, you can't use a script to check if the iframe is sandboxed.
Same-origin iframes without the sandboxed origin browsing context flag
That is, same-origin iframes with no
sandbox
attribute, or same-origin iframes with asandbox
attribute which contains theallow-same-origin
andallow-scripts
keywords.In this case, you can use the
frameElement
global property to access the frame element (returnsnull
when not used inside an iframe).Once you have a reference to the iframe, you can use
hasAttribute
orgetAttribute
to check itssandboxed
attribute. There is also thesandboxed
property, which should return aDOMSettableTokenList
(old browsers might return a string according to an old spec).Cross-origin iframes without the sandboxed origin browsing context flag
That is, cross-origin iframes with no
sandbox
attribute, or cross-origin iframes with asandbox
attribute which contains theallow-same-origin
andallow-scripts
keywords.In this case, the use of
frameElement
is blocked:SecurityError
exception. This is implemented by Chrome.null
. This is implemented by Firefox.allow-same-origin
in a cross-origin iframe isn't much useful, consider assuming the iframe isn't sandboxed.Iframes with the sandboxed origin browsing context flag
That is, iframes with a
sandbox
attribute which doesn't contain theallow-same-origin
keyword but contains theallow-scripts
keyword.As in the previous case, the use of
frameElement
is blocked.However, you can detect this case because
document.domain
will be the empty string.Note: Firefox treats data URIs as same-origin, so it's OK. However, Chrome treats them as cross-origin. Then
frameElement
doesn't work anddocument.domain
is the empty string regardless of whether the iframe is sandboxed or not. You can check whetherlocation.protocol
is'data:'
string to detect data URIs.In general, you might try something like