Last Comment Bug 770684 - (CVE-2012-3975) DOMParser does subresource loads when used from privileged code
: DOMParser does subresource loads when used from privileged code
: regression, sec-moderate
Product: Core
Classification: Components
Component: DOM: Core & HTML (show other bugs)
: 16 Branch
: x86 Windows XP
: -- critical with 1 vote (vote)
: ---
Assigned To: Olli Pettay [:smaug]
: Andrew Overholt [:overholt]
Depends on:
Blocks: 102699 388597
  Show dependency treegraph
Reported: 2012-07-03 14:44 PDT by vsemozhetbyt
Modified: 2012-10-21 20:48 PDT (History)
10 users (show)
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---

v1 (8.19 KB, patch)
2012-07-08 14:49 PDT, Olli Pettay [:smaug]
no flags Details | Diff | Splinter Review
v2 (8.22 KB, patch)
2012-07-08 16:00 PDT, Olli Pettay [:smaug]
no flags Details | Diff | Splinter Review
simple approach (1.35 KB, patch)
2012-07-09 05:10 PDT, Olli Pettay [:smaug]
bzbarsky: review+
akeybl: approval‑mozilla‑beta+
Details | Diff | Splinter Review

Description vsemozhetbyt 2012-07-03 14:44:43 PDT
If DOMParser parses HTML string to DOM, should not it omit not only script processing but also recourse loading (as the pure XHR does with responseType = "document")?

I run this code from JavaScript Editor (Scratchpad, in browser context):

var parser = new DOMParser();
var xhr = new XMLHttpRequest();
xhr.mozBackgroundRequest = true;"GET", "", true);
xhr.timeout = 10000; |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
xhr.onload = function() {
	var doc = parser.parseFromString(this.responseText, "text/html");
	var title = doc.evaluate("//title", doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	if (title) {
	else {
		alert("Parsing error");
xhr.ontimeout = function() {
xhr.onerror = function() {
	alert("HTTP error");

HttpFox ( shows additional recourse loading. Here some HTTP logs for three sites:

Started, Time, Sent, Received, Method, Result, Type, URL

00:00:01.221	1.197	603		316		GET	200		text/html
00:00:02.492	0.171	332		(34312)	GET	(Cache)	text/css
00:00:02.548	0.143	321		(824)	GET	(Cache)	text/css
00:00:02.618	0.464	968		201		GET	303		Redirect to: /dcso6de4r0000082npfcmh4rf_4b1e/njs.gif?dcsredirect=108&dcstlh=1298194945&dcstlv=1298194945&dcsuri=/nojavascript&WT.js=No&
00:00:03.383	0.622	1050	327		GET	200		image/gif

00:00:02.218	0.163	893	6678	GET	200		text/html
00:00:02.470	0.582	844	21059	GET	200		text/css
00:00:02.527	0.487	859	8094	GET	200		image/png
00:00:02.581	0.461	864	613		GET	200		image/gif
00:00:02.639	0.377	865	613		GET	200		image/gif
00:00:02.698	0.112	853	(165)	GET	(Cache)	image/gif
00:00:02.756	0.324	352	231		GET	200		image/gif

00:00:18.897	0.283	1433	243		GET	200		text/html
00:00:19.236	0.339	335		(28286)	GET	(Cache)	text/css
00:00:19.290	0.448	597		188		GET	302		Redirect to:;tile=2;sz=1008x150,1008x200,1008x30,9x1;p=t;p=top;ct=com;bpx=1;ab=a;ka=0;ord=717635184750?
00:00:19.344	0.261	346		(96)	GET	(Cache)	text/css
00:00:19.396	0.340	582		188		GET	302		Redirect to:;tile=5;sz=1008x60,1008x66,7x1;p=ns;ct=com;bpx=1;ab=a;ka=0;ord=717635184750?
00:00:19.449	0.288	580		188		GET	302		Redirect to:;tile=3;sz=300x250,11x1;p=tr;p=tc;ct=com;bpx=1;ab=a;ka=0;ord=717635184750?
00:00:19.504	0.146	425		220		GET	200		image/gif
00:00:19.899	0.418	328		(43)	GET	(Cache)	image/gif
00:00:19.957	0.362	328		(43)	GET	(Cache)	image/gif
00:00:20.015	0.305	328	(	43)		GET	(Cache)	image/gif

Why DOMParser needs these files for just DOM parsing?
Comment 1 Olli Pettay [:smaug] 2012-07-03 14:48:58 PDT
It shouldn't load those files.

(Btw, why do you parse responseText and not just load HTML using XHR?)
Comment 2 vsemozhetbyt 2012-07-03 14:55:03 PDT
Olli Pettay

Because I have a problem with some sites which I describe in this bug:

And I have been advised to use DOMParser in my extension untill this bug will be fixed.
Comment 3 vsemozhetbyt 2012-07-03 14:56:32 PDT
Sorry, wrong link. See:
Comment 4 Kyle Huey [:khuey] (Exited; not receiving bugmail, email if necessary) 2012-07-03 14:56:52 PDT
I'm not convinced that this is a bug.  If you parse the markup as HTML, you can adopt the nodes into the document and they should behave like normal HTML nodes.  It seems weird to only kick off the loads on adoption.
Comment 5 vsemozhetbyt 2012-07-03 15:01:18 PDT
Kyle Huey

But DOMParser does not load all the images and so on. Just some.
Comment 6 vsemozhetbyt 2012-07-03 15:05:21 PDT
Kyle Huey

And I think we must discern page rendering and DOM parsing. For background parsing we don't need any image loading or styles.
Comment 7 Boris Zbarsky [:bz] (still a bit busy) 2012-07-08 00:10:05 PDT
If I run the code from comment 0 in the scratchpad, I get an exception because is undefined, as expected.  I assume that the "in browser context" bit hides some crucial part of the steps to reproduce?  I'd really appreciate actually being told what that is.

Kyle, DOMParser is not supposed to do subresource loads.  See bug 421228.  ;)
Comment 8 vsemozhetbyt 2012-07-08 00:25:59 PDT
2 Boris Zbarsky

See the part "A Word about Scopes", the last paragraph.
Comment 9 Boris Zbarsky [:bz] (still a bit busy) 2012-07-08 00:34:23 PDT
Ah, looks like the "" pref needs to be set to true.

This is a bad bug in the patch for bug 102699.  Before that patch, the only codepath that could lead to parsing looked like this, in order:

1)  Create a document with the DOMParser's mOriginalPrincipal.
2)  Call EnableXULXBL() on the document if needed
3)  Call StartDocumentLoad()
4)  Set the document's base URI
5)  Reset the document's principal to mPrincipal.
6)  Feed data into the parser.

That sequence of steps was pretty clearly documented (at least in terms of the whole principal dance) and _very_ critical.

When that bug was fixed, the XML codepath stayed as above, but HTML codepath was written more like this:

1)  Create a document with the DOMParser's mOriginalPrincipal.
2)  Feed data into the parser.
3)  Call EnableXULXBL() on the document if needed
4)  Set the document's base URI
5)  Reset the document's principal to mPrincipal.

But the whole point of resetting to mPrincipal is that it MUST happen before any data goes in.  Otherwise you're parsing with the system principal.  Also, this is never calling StartDocumentLoad, so afaict it's not setting up whatever state that would normally set up (e.g. the document URI) the same way as the XML path.  And it's calling EnableXULXBL() too late, of course.  Not like this matters much for text/html.

This bug means that using DOMParser on text/html is pretty unsafe from chrome: It allows whatever string you're parsing to poke any URI it wants, including ones that web content normally can't access.  (On a Unix system it allows at minimum a DoS attack by reading from file:///dev/tty.)

Henri, Olli, can one of you grab this?  Or should I try to?
Comment 10 Alex Keybl [:akeybl] 2012-07-08 09:11:40 PDT
Our final beta build is tomorrow EOD pacific time. Is there any reason to consider blocking that build on this FF12 regression?
Comment 11 Boris Zbarsky [:bz] (still a bit busy) 2012-07-08 09:42:49 PDT
No, though this might be worth taking in a chemspill if we do one for some reason: every extension using DOMParser is basically insecure at the moment.
Comment 12 Olli Pettay [:smaug] 2012-07-08 10:51:28 PDT
I think Henri will be back from vacation real soon.
Comment 13 Olli Pettay [:smaug] 2012-07-08 11:06:34 PDT
What is the critical part of this bug?
The created document is a data document, so it itself shouldn't load anything.
HTML parser may speculatively load something. (It shouldn't enable speculative loads for data documents)
Or what am I missing here.
Comment 14 Olli Pettay [:smaug] 2012-07-08 11:11:03 PDT
Though, I really don't understand why anything is loaded. Document is a data document, so
the data document content policy should prevent all the loads.
Comment 15 Boris Zbarsky [:bz] (still a bit busy) 2012-07-08 11:39:13 PDT
Content policy is never consulted for the system principal.  See the CHECK_PRINCIPAL bit in NS_CheckContentLoadPolicy.  So the document is in fact loading things here.
Comment 16 Olli Pettay [:smaug] 2012-07-08 11:44:01 PDT
bug 388597 added a bizarre inconsistency to content policy handling.
(Another option would be to not have data document cp at all, but prevent such loads in some other way.)
Comment 17 Olli Pettay [:smaug] 2012-07-08 12:36:33 PDT
I guess I've looked at the code enough to write a patch.
Comment 18 vsemozhetbyt 2012-07-08 14:11:33 PDT
//every extension using DOMParser is basically insecure at the moment

I use it only because of the bug 765620. I just have given a simple testcase in the last comment there. May be somebody can confirm the bug.
Comment 19 Olli Pettay [:smaug] 2012-07-08 14:49:32 PDT
Created attachment 640106 [details] [diff] [review]

Like this?
Comment 20 Olli Pettay [:smaug] 2012-07-08 15:52:19 PDT
Comment on attachment 640106 [details] [diff] [review]

Unfortunately this seems to leak. We're not supposed to call
StartDocumentLoad in this case for HTML documents.
Comment 21 Olli Pettay [:smaug] 2012-07-08 16:00:50 PDT
Created attachment 640113 [details] [diff] [review]
Comment 22 Olli Pettay [:smaug] 2012-07-09 05:10:24 PDT
Created attachment 640187 [details] [diff] [review]
simple approach
Comment 23 Boris Zbarsky [:bz] (still a bit busy) 2012-07-09 10:57:38 PDT
Comment on attachment 640187 [details] [diff] [review]
simple approach

Comment 24 Olli Pettay [:smaug] 2012-07-09 11:42:43 PDT
Comment 25 Alex Keybl [:akeybl] 2012-07-10 10:34:08 PDT
Tracking for FF14, for possible 14.0.1 consideration.
Comment 26 vsemozhetbyt 2012-07-10 11:32:29 PDT
Thank you, people.
Comment 27 Alex Keybl [:akeybl] 2012-07-26 17:31:34 PDT
(In reply to Olli Pettay [:smaug] (limited connectivity July 26-29) from comment #24)

Is this low risk enough to uplift to FF15 on mozilla-beta?
Comment 28 Henri Sivonen (:hsivonen) 2012-07-26 22:36:57 PDT
(In reply to Alex Keybl [:akeybl] from comment #27)
> (In reply to Olli Pettay [:smaug] (limited connectivity July 26-29) from
> comment #24)
> >
> Is this low risk enough to uplift to FF15 on mozilla-beta?

Seems so to me. (Looks like smaug is away right now.)
Comment 29 Olli Pettay [:smaug] 2012-07-27 04:52:23 PDT
Comment on attachment 640187 [details] [diff] [review]
simple approach

[Approval Request Comment]
Bug caused by (feature/regressing bug #): bug 102699, bug 388597
User impact if declined: security issues 
Testing completed (on m-c, etc.): Landed m-c 2012-07-09 
Risk to taking this patch (and alternatives if risky): Should be very low risk
String or UUID changes made by this patch: NA
Comment 30 Alex Keybl [:akeybl] 2012-07-30 12:28:35 PDT
Comment on attachment 640187 [details] [diff] [review]
simple approach

[Triage Comment]
Low risk sg:moderate fix - let's take this for the next beta (going to build tomorrow). Please land as soon as possible.
Comment 32 Anthony Hughes (:ashughes) [GFX][QA][Mentor] 2012-09-18 14:48:10 PDT
Reporter, can you see if this is fixed for you now? Can you try the latest Firefox 15 release, 16 beta, and 17 aurora? Thanks.
Comment 33 vsemozhetbyt 2012-09-18 14:56:03 PDT
Yes, it is fixed in these versions. Thank you.
Comment 34 Anthony Hughes (:ashughes) [GFX][QA][Mentor] 2012-10-16 15:07:06 PDT
Thanks vvsemozhetbyt@. Marking this bug verified based on comment 33.

Note You need to log in before you can comment on or make changes to this bug.