javascript - Making a Same Domain iframe Secure -
tl;dr can execute un-trusted scripts on iframe safely?
back story:
i'm trying make secure jsonp requests. lot of older browsers not support web workers means current solution came not optimal.
i figured create <iframe> , load script inside it. script perform jsonp request (creating script tag), post message main page. main page message, execute callback , destroy iframe. i've managed do sort of thing.
function jsonp(url, data, callback) { var iframe = document.createelement("iframe"); iframe.style.display = "none"; document.body.appendchild(iframe); var iframedoc = iframe.contentdocument || iframe.contentwindow.document; sc = document.createelement("script"); sc.textcontent = "(function(p){ cb = function(result){p.postmessage(result,'http://fiddle.jshell.net');};})(parent);"; //sc.textcontent += "alert(cb)"; iframedoc.body.appendchild(sc); var jr = document.createelement("script"); var getparams = ""; // serialize parameters (var in data) { getparams += "&" + + "=" + data[i]; } jr.src = url + "?callback=cb" + getparams; iframedoc.body.appendchild(jr); window.onmessage = function (e) { callback(e.data); document.body.removechild(iframe); } } jsonp("http://jsfiddle.net/echo/jsonp/", { foo: "bar" }, function (result) { alert("result: " + json.stringify(result)); }); the problem since iframes on same domain, injected script still has access external scope through .top or .parent , such.
is there way create iframe can not access data on parent scope?
i want create iframe scripts added through script tags not able access variables on parent window (and dom). tried stuff top=parent=null i'm not sure that's enough, there might other workarounds. tried running for... in loop, function stopped working , unable find out why.
note:
i know optimally webworkers better isolated environment. know jsonp "bad" technique (i had some random guy tell me he'd never use today). i'm trying create secure environment scenarios have perform jsonp queries.
you can't delete references, setting null silently fail , there way reference parent dom.
references frameelement , frameelement.defaultview etc. cannot deleted. attempting either silently fail or throw exception depending on browser.
you caja/cajita though.
Comments
Post a Comment