-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
prevent window.parent access from iframe #534
Comments
@rogerwang any thoughts on this? This is a particularly dangerous security hole. I wanted to be able to use the iframes as a mini-browser, but given that the frame can access window.parent, someone could delete your entire harddrive just by putting a window.parent.require('fs').rmdir in there. |
For what it's worth, the ideal in my case would be that the parent window can access and mess around with the iframe, but that the iframe could never touch the parent window. If there was a way to remove |
ah, looks like you'd also need to nuke |
There is some bug in NW's handling for cross domain checking. I'll fix it in 0.5.0. After that I believe such need can be fulfilled? |
With the cross domain check in place, would I still be able to access the contents of the frame from the parent? i.e. window.frames["my-iframe"].document.head.appendChild(...); kind of stuff |
Are you suggesting the access should be unidirectional? I can see it when I try to fix it. |
Yeah, that's what I'm hoping for. Being able to access the child from the parent window, but ensuring that the child can't access the parent. This allows me to do whatever I want (i.e. capturing certain keys) within the frame, while making sure that child frame can't delete my harddrive :) |
I would like for postMessage to work bidirectionally. AKA the child iframe can call a postMessage on the parent, and the parent can call postMessage on its child iframe. |
@danyocom Sure. postMessage should remain untouched behavior as the standard. |
One thing I've run into with this is that sites that use framebusting are causing all sorts of trouble. @rogerwang any thoughts on making it such that iframes with nwdisable have window.top and window.parent set to the iframe's window? This would make the frame seem like it's the root and prevent all that nonsense. That being said, I have no idea how hard that would necessarily be - if you have thoughts on some other way I can get around it, I'm happy to hear them! |
It could be done. But it'd be better with another flag attribute, since some app may want to access window.top and window.parent. What do you think? Any suggestion to the name of the flag? |
Yeah another attribute seems fine. Maybe something like |
|
@rogerwang How soon do you think you could patch this functionality in? I've had a little luck myself, but lots of unprompted quitting without any hints as to what's wrong. I'd much rather yield to the maintainer :) Also, I'll second |
I need to look into it a little bit first, but it shouldn't be hard. Could any of you provide some test case so I'll be sure it works for you. |
Yeah, I wouldn't think so, but I just don't know Webkit well enough to know where to look. I know it's at least more nuanced than letting |
The test case is the following:
{
"name": "nwfaketop",
"main": "nwfaketop.html",
"window": {
"width": 600,
"height": 400,
"position": "center",
"toolbar": false,
"title": "Starting nwfaketop...",
"resizable": true
}
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing nwfaketop</title>
</head>
<body>
<b>You should be able to see Twitter in an iframe below:</b>
<iframe src="https://twitter.com" style="width: 80%;" nwdisable nwfaketop>
</iframe>
</body>
</html> Command line: Expected behaviour: Twitter in iframe. Current behaviour: Twitter realizes it's not in the top frame → Twitter takes the whole window. I dunno if Twitter breaks free of the |
With 'nwfaketop' attribute window.parent and window.top would return the iframe object rather than the parent frame and the top frame. See nwjs/nw.js#534
The previous commit passed @Mithgol 's test case. Have fun. |
@rogerwang you are awesome :) Thanks! |
While that patch will work for Twitter (bravo, by the way, I wish I knew the codebase well enough to have done it that quickly!), clicking on a search result from DuckDuckGo will still break out of the frame. Sandboxing the iframe without It would also probably be a good idea to plug If you fix the DuckDuckGo hole, then you can probably just document "If you sandbox an Here's some test-cases for all the methods I've talked about (Thanks for the template @Mithgol):
{
"name": "nwfaketop",
"main": "nwfaketop.html",
"window": {
"width": 600,
"height": 400,
"position": "center",
"toolbar": true,
"title": "Starting nwfaketop...",
"resizable": true
}
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing Anchor Target Access</title>
</head>
<body>
<a href="nwfaketop_targettests.html" target="_top">Test target="_top"</a><br/>
<a href="nwfaketop_targettests.html" target="_parent">Test target="_parent"</a>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing Property Access</title>
<script>
try {
if(window.top !== window)
document.write("window.top Failure<br />");
if(window.parent !== window)
document.write("window.parent Failure<br />");
if(window.frameElement !== null)
document.write("window.frameElement Failure<br />");
var l = 0;
for(var tempWin = window; tempWin != top; tempWin = tempWin.parent) { ++l; }
if(window.location.ancestorOrigins.length !== l)
document.write("window.location.ancestorOrigins.length Failure<br />");
if(document.documentElement.innerText === "")
document.write("Success");
}
catch(e) { document.write("Exception Thrown Failure<br />"); }
</script>
</head>
<body></body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing nwfaketop</title>
</head>
<body>
<b>You should be able to see Twitter in an iframe below:</b><br />
<iframe src="https://twitter.com" style="width: 80%;" nwdisable nwfaketop>
</iframe><br />
<b>You should be able to click on a search result from Google and have it navigate within the iframe below:</b><br />
<iframe src="https://www.google.com/search?q=foobar" style="width: 80%;" nwdisable nwfaketop>
</iframe><br />
<b>You should be able to click on a search result from DuckDuckGo and have it navigate within the iframe below:</b><br />
<iframe src="https://duckduckgo.com/?q=foobar" style="width: 80%;" nwdisable nwfaketop>
</iframe><br />
<b>This tests the following properties: "frameElement", "parent", "top", "location.ancestorOrigins"</b><br />
<iframe src="nwfaketop_windowproptests.html" style="width: 80%;" nwdisable nwfaketop>
</iframe><br />
<b>This tests target="_parent" and target="_top" access. Clicking on the links shouldn't navigate the whole window.</b><br />
<iframe src="nwfaketop_targettests.html" style="width: 80%;" nwdisable nwfaketop>
</iframe><br />
</body>
</html> Command line: Expected behaviour:
Current behaviour:
@Mithgol I can confirm that indeed it does break out of the |
@twrodriguez , cool and it's very helpful! I'll try to fix those before 0.5.1 release. |
@twrodriguez there is a little error in the case under Chrome:
|
You can either move the script tag to the bottom of the body tag, or change it to |
The following should be emulated as a top level window: "_top" navigation target window.frameElement location.ancestorOrigins see the comments in nwjs/nw.js#534
@rogerwang May I ask for the |
@Mithgol It seems should be merged with |
Just built with 0.5.1, works beautifully. Thanks for your quick work @rogerwang! |
More news regarding working with fake-top iframes. There's two more issues I've discovered, one semi-major (security-related), and one minor:
In light of these, I'd like to propose a new event: I'm sure I haven't thought enough about this, so please weigh in with your comments. |
Please see the doc for the new new-win-policy event for new window handling, and make comment if you feel anything need to be changed. https://github.com/rogerwang/node-webkit/wiki/Window#new-win-policy |
The |
parent.postMessage() does not appear to be working bi-directionally with nwfaketop, I thought it was said in these comments that postMessage would still work? Is there a workaround? (sending messages from iframe to parent) |
rogerwang All good, but some links that point to Facebook or Twitter break out of my iframe and kill the user experience. I've added the flag to the iframe for nwdisable nwfaketop but that does not seem to help. Is there a command that I'm missing....thanks |
Is there any way to get the title of the document a iframe is viewing? |
Simple question if I may---is it possible to constrain what an iframe can access of its parent while still listen in on it from the parent? |
Can someone please tell me how to embed an external web app into a node-webkit window without using an iframe? I just want the following in the package.json: { Is that possible? |
main: index.js index.js: <script> window.open('http://mysite.com', '_self'); </script> Atte. Allan Brazute Alves
2014-11-14 18:06 GMT-02:00 Demián Rodriguez notifications@github.com:
|
@EthraZa if I do that, it just displays the text of the script (using 0.11.0) |
@demian85 the following works for me, but in light of the discussion above it doesn't seem to be the safe thing to do:
|
If the target for an anchor tag is something other than "_blank" the "new-win-policy" does not fire. So there is still an issue, it replaces the entire nwjs container (i.e. especially with ctrl+clicking a link) |
On the original topic this is easier than you guys think.
iframe.addEventListener('onload', function() {
|
Not fully sure, but I think one may also need to deal with |
@baconface This doesn't the problem of ctrl-clicking or shift-clicking |
Hello,
I am looking into developing an application using node-webkit. One thing this application will do is load untrusted content (html/js) (served from a webserver running on localhost) into an iframe. I noticed that no matter what sandbox attributes I set on the iframe (but always allowing js), the javascript inside the Iframe can call window.parent.someNodeJsCommand().
Is there a way to prevent the iframe from being able to access window.parent? I do need to communicate between the iframe and the parent application, and was planning on using window.postMessage for that.
Thanks,
Dan
The text was updated successfully, but these errors were encountered: