1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/dom/DOMParser.h"
8 
9 #include "nsNetUtil.h"
10 #include "nsDOMString.h"
11 #include "MainThreadUtils.h"
12 #include "SystemPrincipal.h"
13 #include "nsIScriptGlobalObject.h"
14 #include "nsIStreamListener.h"
15 #include "nsStringStream.h"
16 #include "nsCRT.h"
17 #include "nsStreamUtils.h"
18 #include "nsContentUtils.h"
19 #include "nsDOMJSUtils.h"
20 #include "nsError.h"
21 #include "nsPIDOMWindow.h"
22 #include "mozilla/BasePrincipal.h"
23 #include "mozilla/LoadInfo.h"
24 #include "mozilla/NullPrincipal.h"
25 #include "mozilla/dom/BindingUtils.h"
26 #include "mozilla/dom/Document.h"
27 #include "mozilla/dom/ScriptSettings.h"
28 
29 using namespace mozilla;
30 using namespace mozilla::dom;
31 
DOMParser(nsIGlobalObject * aOwner,nsIPrincipal * aDocPrincipal,nsIURI * aDocumentURI,nsIURI * aBaseURI)32 DOMParser::DOMParser(nsIGlobalObject* aOwner, nsIPrincipal* aDocPrincipal,
33                      nsIURI* aDocumentURI, nsIURI* aBaseURI)
34     : mOwner(aOwner),
35       mPrincipal(aDocPrincipal),
36       mDocumentURI(aDocumentURI),
37       mBaseURI(aBaseURI),
38       mForceEnableXULXBL(false),
39       mForceEnableDTD(false) {
40   MOZ_ASSERT(aDocPrincipal);
41   MOZ_ASSERT(aDocumentURI);
42 }
43 
44 DOMParser::~DOMParser() = default;
45 
46 // QueryInterface implementation for DOMParser
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMParser)47 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMParser)
48   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
49   NS_INTERFACE_MAP_ENTRY(nsISupports)
50 NS_INTERFACE_MAP_END
51 
52 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMParser, mOwner)
53 
54 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMParser)
55 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMParser)
56 
57 already_AddRefed<Document> DOMParser::ParseFromString(const nsAString& aStr,
58                                                       SupportedType aType,
59                                                       ErrorResult& aRv) {
60   if (aType == SupportedType::Text_html) {
61     nsCOMPtr<Document> document = SetUpDocument(DocumentFlavorHTML, aRv);
62     if (NS_WARN_IF(aRv.Failed())) {
63       return nullptr;
64     }
65 
66     // Keep the XULXBL state in sync with the XML case.
67     if (mForceEnableXULXBL) {
68       document->ForceEnableXULXBL();
69     }
70 
71     if (mForceEnableDTD) {
72       document->ForceSkipDTDSecurityChecks();
73     }
74 
75     nsresult rv = nsContentUtils::ParseDocumentHTML(aStr, document, false);
76     if (NS_WARN_IF(NS_FAILED(rv))) {
77       aRv.Throw(rv);
78       return nullptr;
79     }
80 
81     return document.forget();
82   }
83 
84   nsAutoCString utf8str;
85   // Convert from UTF16 to UTF8 using fallible allocations
86   if (!AppendUTF16toUTF8(aStr, utf8str, mozilla::fallible)) {
87     aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
88     return nullptr;
89   }
90 
91   // The new stream holds a reference to the buffer
92   nsCOMPtr<nsIInputStream> stream;
93   nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), utf8str,
94                                       NS_ASSIGNMENT_DEPEND);
95   if (NS_WARN_IF(NS_FAILED(rv))) {
96     aRv.Throw(rv);
97     return nullptr;
98   }
99 
100   return ParseFromStream(stream, u"UTF-8"_ns, utf8str.Length(), aType, aRv);
101 }
102 
ParseFromSafeString(const nsAString & aStr,SupportedType aType,ErrorResult & aRv)103 already_AddRefed<Document> DOMParser::ParseFromSafeString(const nsAString& aStr,
104                                                           SupportedType aType,
105                                                           ErrorResult& aRv) {
106   // Since we disable cross docGroup node adoption, it is safe to create
107   // new document with the system principal, then the new document will be
108   // placed in the same docGroup as the chrome document.
109   nsCOMPtr<nsIPrincipal> docPrincipal = mPrincipal;
110   if (!mPrincipal->IsSystemPrincipal()) {
111     mPrincipal = SystemPrincipal::Create();
112   }
113 
114   RefPtr<Document> ret = ParseFromString(aStr, aType, aRv);
115   mPrincipal = docPrincipal;
116   return ret.forget();
117 }
118 
ParseFromBuffer(const Uint8Array & aBuf,SupportedType aType,ErrorResult & aRv)119 already_AddRefed<Document> DOMParser::ParseFromBuffer(const Uint8Array& aBuf,
120                                                       SupportedType aType,
121                                                       ErrorResult& aRv) {
122   aBuf.ComputeState();
123   return ParseFromBuffer(Span(aBuf.Data(), aBuf.Length()), aType, aRv);
124 }
125 
ParseFromBuffer(Span<const uint8_t> aBuf,SupportedType aType,ErrorResult & aRv)126 already_AddRefed<Document> DOMParser::ParseFromBuffer(Span<const uint8_t> aBuf,
127                                                       SupportedType aType,
128                                                       ErrorResult& aRv) {
129   // The new stream holds a reference to the buffer
130   nsCOMPtr<nsIInputStream> stream;
131   nsresult rv = NS_NewByteInputStream(
132       getter_AddRefs(stream),
133       Span(reinterpret_cast<const char*>(aBuf.Elements()), aBuf.Length()),
134       NS_ASSIGNMENT_DEPEND);
135   if (NS_FAILED(rv)) {
136     aRv.Throw(rv);
137     return nullptr;
138   }
139 
140   return ParseFromStream(stream, VoidString(), aBuf.Length(), aType, aRv);
141 }
142 
ParseFromStream(nsIInputStream * aStream,const nsAString & aCharset,int32_t aContentLength,SupportedType aType,ErrorResult & aRv)143 already_AddRefed<Document> DOMParser::ParseFromStream(nsIInputStream* aStream,
144                                                       const nsAString& aCharset,
145                                                       int32_t aContentLength,
146                                                       SupportedType aType,
147                                                       ErrorResult& aRv) {
148   bool svg = (aType == SupportedType::Image_svg_xml);
149 
150   // For now, we can only create XML documents.
151   // XXXsmaug Should we create an HTMLDocument (in XHTML mode)
152   //         for "application/xhtml+xml"?
153   if (aType != SupportedType::Text_xml &&
154       aType != SupportedType::Application_xml &&
155       aType != SupportedType::Application_xhtml_xml && !svg) {
156     aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
157     return nullptr;
158   }
159 
160   // Put the nsCOMPtr out here so we hold a ref to the stream as needed
161   nsCOMPtr<nsIInputStream> stream = aStream;
162   if (!NS_InputStreamIsBuffered(stream)) {
163     nsCOMPtr<nsIInputStream> bufferedStream;
164     nsresult rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
165                                             stream.forget(), 4096);
166     if (NS_WARN_IF(NS_FAILED(rv))) {
167       aRv.Throw(rv);
168       return nullptr;
169     }
170 
171     stream = bufferedStream;
172   }
173 
174   nsCOMPtr<Document> document =
175       SetUpDocument(svg ? DocumentFlavorSVG : DocumentFlavorLegacyGuess, aRv);
176   if (NS_WARN_IF(aRv.Failed())) {
177     return nullptr;
178   }
179 
180   // Create a fake channel
181   nsCOMPtr<nsIChannel> parserChannel;
182   NS_NewInputStreamChannel(
183       getter_AddRefs(parserChannel), mDocumentURI,
184       nullptr,  // aStream
185       mPrincipal, nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL,
186       nsIContentPolicy::TYPE_OTHER,
187       nsDependentCSubstring(SupportedTypeValues::GetString(aType)));
188   if (NS_WARN_IF(!parserChannel)) {
189     aRv.Throw(NS_ERROR_UNEXPECTED);
190     return nullptr;
191   }
192 
193   if (!DOMStringIsNull(aCharset)) {
194     parserChannel->SetContentCharset(NS_ConvertUTF16toUTF8(aCharset));
195   }
196 
197   // Tell the document to start loading
198   nsCOMPtr<nsIStreamListener> listener;
199 
200   // Keep the XULXBL state in sync with the HTML case
201   if (mForceEnableXULXBL) {
202     document->ForceEnableXULXBL();
203   }
204 
205   if (mForceEnableDTD) {
206     document->ForceSkipDTDSecurityChecks();
207   }
208 
209   // Have to pass false for reset here, else the reset will remove
210   // our event listener.  Should that listener addition move to later
211   // than this call?
212   nsresult rv =
213       document->StartDocumentLoad(kLoadAsData, parserChannel, nullptr, nullptr,
214                                   getter_AddRefs(listener), false);
215 
216   if (NS_FAILED(rv) || !listener) {
217     aRv.Throw(NS_ERROR_FAILURE);
218     return nullptr;
219   }
220 
221   // Now start pumping data to the listener
222   nsresult status;
223 
224   rv = listener->OnStartRequest(parserChannel);
225   if (NS_FAILED(rv)) parserChannel->Cancel(rv);
226   parserChannel->GetStatus(&status);
227 
228   if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(status)) {
229     rv = listener->OnDataAvailable(parserChannel, stream, 0, aContentLength);
230     if (NS_FAILED(rv)) parserChannel->Cancel(rv);
231     parserChannel->GetStatus(&status);
232   }
233 
234   rv = listener->OnStopRequest(parserChannel, status);
235   // Failure returned from OnStopRequest does not affect the final status of
236   // the channel, so we do not need to call Cancel(rv) as we do above.
237 
238   if (NS_FAILED(rv)) {
239     aRv.Throw(NS_ERROR_FAILURE);
240     return nullptr;
241   }
242 
243   return document.forget();
244 }
245 
246 /*static */
Constructor(const GlobalObject & aOwner,ErrorResult & rv)247 already_AddRefed<DOMParser> DOMParser::Constructor(const GlobalObject& aOwner,
248                                                    ErrorResult& rv) {
249   MOZ_ASSERT(NS_IsMainThread());
250   nsCOMPtr<nsIPrincipal> docPrincipal = aOwner.GetSubjectPrincipal();
251   nsCOMPtr<nsIURI> documentURI;
252   nsIURI* baseURI = nullptr;
253   if (docPrincipal->IsSystemPrincipal()) {
254     docPrincipal = NullPrincipal::Create(OriginAttributes());
255     documentURI = docPrincipal->GetURI();
256   } else {
257     // Grab document and base URIs off the window our constructor was
258     // called on. Error out if anything untoward happens.
259     nsCOMPtr<nsPIDOMWindowInner> window =
260         do_QueryInterface(aOwner.GetAsSupports());
261     if (!window) {
262       rv.Throw(NS_ERROR_UNEXPECTED);
263       return nullptr;
264     }
265 
266     baseURI = window->GetDocBaseURI();
267     documentURI = window->GetDocumentURI();
268   }
269 
270   if (!documentURI) {
271     rv.Throw(NS_ERROR_UNEXPECTED);
272     return nullptr;
273   }
274 
275   nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aOwner.GetAsSupports());
276   MOZ_ASSERT(global);
277   RefPtr<DOMParser> domParser =
278       new DOMParser(global, docPrincipal, documentURI, baseURI);
279   return domParser.forget();
280 }
281 
282 // static
CreateWithoutGlobal(ErrorResult & aRv)283 already_AddRefed<DOMParser> DOMParser::CreateWithoutGlobal(ErrorResult& aRv) {
284   nsCOMPtr<nsIPrincipal> docPrincipal =
285       NullPrincipal::Create(OriginAttributes());
286 
287   nsCOMPtr<nsIURI> documentURI = docPrincipal->GetURI();
288   if (!documentURI) {
289     aRv.Throw(NS_ERROR_UNEXPECTED);
290     return nullptr;
291   }
292 
293   RefPtr<DOMParser> domParser =
294       new DOMParser(nullptr, docPrincipal, documentURI, nullptr);
295   return domParser.forget();
296 }
297 
SetUpDocument(DocumentFlavor aFlavor,ErrorResult & aRv)298 already_AddRefed<Document> DOMParser::SetUpDocument(DocumentFlavor aFlavor,
299                                                     ErrorResult& aRv) {
300   // We should really just use mOwner here, but Document gets confused
301   // if we pass it a scriptHandlingObject that doesn't QI to
302   // nsIScriptGlobalObject, and test_isequalnode.js (an xpcshell test without
303   // a window global) breaks. The correct solution is just to wean Document off
304   // of nsIScriptGlobalObject, but that's a yak to shave another day.
305   nsCOMPtr<nsIScriptGlobalObject> scriptHandlingObject =
306       do_QueryInterface(mOwner);
307 
308   // Try to inherit a style backend.
309   NS_ASSERTION(mPrincipal, "Must have principal by now");
310   NS_ASSERTION(mDocumentURI, "Must have document URI by now");
311 
312   nsCOMPtr<Document> doc;
313   nsresult rv = NS_NewDOMDocument(getter_AddRefs(doc), u""_ns, u""_ns, nullptr,
314                                   mDocumentURI, mBaseURI, mPrincipal, true,
315                                   scriptHandlingObject, aFlavor);
316   if (NS_WARN_IF(NS_FAILED(rv))) {
317     aRv.Throw(rv);
318     return nullptr;
319   }
320 
321   return doc.forget();
322 }
323