sandblaster.js
A client-side JavaScript library to detect if your code is running inside of a sandbox
ed iframe
... and, if desired, might just be able to change that fact!
Background
What is a sandbox
ed iframe
?
The sandbox
attribute of the iframe
element (new in HTML5, supported in IE10+ and all other evergreen browsers) provides web developers with a way tighten the restrictions on framed content beyond what Content Security Policy (CSP) provides for unsandbox
ed cross-origin iframe
s. With the sandbox
attribute, you can instruct the browser to load a specific frame's content in a low-privilege environment, starting with the least privilege possible and then whitelisting the necessary subset of capabilities.
It is also very important to note, however, that the sandbox
attribute takes away some privileges from the framed content that CANNOT be whitelisted "back in". For example, any framed page running in a sandbox absolutely cannot run native plugins (e.g. Flash, Silverlight, Java, etc.). This decision was made because native plugins run unmanaged code that the browser cannot offer any further security verifications on, and are frequently sourced from third parties.
See Also
Getting Started
Install
Sandblaster.js is published to NPM for ease of installation:
npm install sandblaster
Please keep in mind, however, that it only works in browser contexts, not in Node.js.
Load
Then, load up the "sandblaster.js" script on your HTML page:
<script src="node_modules/sandblaster/dist/sandblaster.min.js"></script>
Usage
Analysis
Detect and analyze, as best as possible, the state of the current window
as it relates to being within a frame, sandbox
ed, and/or cross-origin:
var result = sandblaster.detect();
For full details on this analysis result object, see The Analysis Result below.
Action
Rebellion: Unsandbox
ing
If it is possible for your window
state, you can also attempt to remove the sandbox
ing:
var succeeded = sandblaster.unsandbox();
NOTE: Executing this function will also return true
if the window
is not within a frame at all. The basic concept of its return value is that it should be true
if the window
is not sandbox
ed at the end of the call.
Submission: Resandbox
ing & sandbox
ing
If you unsandbox
ed the window
and later decide you want to resandbox
it exactly as it was before:
var succeeded = sandblaster.resandbox();
Or, if the existing window
state will allow it, you can also add and/or update the sandbox
configuration to whatever acceptable value you want:
var succeeded = sandblaster.sandbox({
scripts: true,
sameDomain: true,
forms: false,
pointerLock: false,
popups: false,
topNavigation: false
});
If the window
is already in a sandbox
ed iframe
, any non-Boolean values in the allowances object will defer to their current value in the DOM.
Resetting: Reloading the iframe
If it is possible for your window
state, you can also attempt to reload the iframe
with its current attributes:
var succeeded = sandblaster.reload();
This is useful for those who want to make native plugins work after removing sandboxing (e.g. on JSFiddle, CodePen, etc.):
var result = sandblaster.detect();
if (result.sandboxed && sandblaster.unsandbox()) {
sandblaster.reload();
}
NOTE: If this function succeeds, your following code may never get a chance to execute as the framed page will begin reloading.
The Anaylsis Result
A live visualization of every known possible analysis result can be seen at jamesmgreene.github.io/sandblaster/test-iframes.html.
The result object structure is as follows:
var result = sandblaster.detect();