Truly independent web browser
LibWeb: Bound recursive document writes
sideshowbarker1254deeb0bc2
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex c5087f51b9e..a78d2d6421b 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -1532,6 +1532,19 @@ WebIDL::ExceptionOr<void> Document::run_the_document_write_steps(Utf16View text,if (m_active_parser_was_aborted)return {};+ // AD-HOC: The document write steps don't bound recursive parser invocations. Limit nested writes so a script+ // can't keep writing itself indefinitely, and suppress further writes until the outermost call returns.+ static constexpr size_t maximum_write_depth = 21;+ if (m_document_write_depth >= maximum_write_depth || m_document_write_limit_reached) {+ m_document_write_limit_reached = true;+ return {};+ }+ ++m_document_write_depth;+ ScopeGuard restore_write_depth = [&] {+ if (--m_document_write_depth == 0)+ m_document_write_limit_reached = false;+ };+// 9. If the insertion point is undefined, then:if (!(m_parser && m_parser->tokenizer().is_insertion_point_defined())) {// 1. If document's unload counter is greater than 0 or document's ignore-destructive-writes counter is greater than 0, then return.diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex df6faa81534..bbb27674262 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -2164,6 +2164,8 @@ private:GC::Ptr<Element> m_current_script;u32 m_ignore_destructive_writes_counter { 0 };+ size_t m_document_write_depth { 0 };+ bool m_document_write_limit_reached { false };// https://html.spec.whatwg.org/multipage/browsing-the-web.html#unload-counteru32 m_unload_counter { 0 };diff --git a/Tests/LibWeb/Text/expected/HTML/document-write-recursion.txt b/Tests/LibWeb/Text/expected/HTML/document-write-recursion.txtnew file mode 100644index 00000000000..bb99ea32eab--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/document-write-recursion.txt@@ -0,0 +1,15 @@+write: first chain ran 22 scripts+write: unwinding markers: 1+write: second chain ran 21 scripts+write: 50 subsequent writes+write: other document accepted writes: true+writeln: first chain ran 22 scripts+writeln: unwinding markers: 1+writeln: second chain ran 21 scripts+writeln: 50 subsequent writes+writeln: other document accepted writes: true+alternating: first chain ran 22 scripts+alternating: unwinding markers: 1+alternating: second chain ran 21 scripts+alternating: 50 subsequent writes+alternating: other document accepted writes: truediff --git a/Tests/LibWeb/Text/input/HTML/document-write-recursion.html b/Tests/LibWeb/Text/input/HTML/document-write-recursion.htmlnew file mode 100644index 00000000000..01034c1513a--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/document-write-recursion.html@@ -0,0 +1,53 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ for (const method of ["write", "writeln", "alternating"]) {+ const other = document.createElement("iframe");+ document.body.appendChild(other);+ window.otherDocument = other.contentDocument;+ window.otherDocument.open();+ const iframe = document.createElement("iframe");+ const loaded = new Promise(resolve => (iframe.onload = resolve));+ iframe.srcdoc = `<!DOCTYPE html><body>+ <script>+ window.calls = 0;+ <\/script>+ <script id="recursive">+ ++window.calls;+ // Bound the test even if recursive writes aren't limited.+ if (window.calls < 64) {+ const call = window.calls;+ const method = ${JSON.stringify(method)};+ const writer = method === "alternating" ? (window.calls % 2 ? "write" : "writeln") : method;+ document[writer](document.currentScript.outerHTML);+ document[writer]("<i></i>");+ if (call === 22)+ parent.otherDocument.write("<u></u>");+ }+ <\/script>+ <script>+ window.firstCalls = window.calls;+ window.firstMarkers = document.querySelectorAll("i").length;+ window.calls = 0;+ document.write(document.querySelector("#recursive").outerHTML);+ window.secondCalls = window.calls;+ for (let i = 0; i < 50; ++i)+ document.write("<b></b>");+ <\/script>`;+ document.body.appendChild(iframe);+ await loaded;+ const child = iframe.contentWindow;+ println(`${method}: first chain ran ${child.firstCalls} scripts`);+ println(`${method}: unwinding markers: ${child.firstMarkers}`);+ println(`${method}: second chain ran ${child.secondCalls} scripts`);+ println(`${method}: ${child.document.querySelectorAll("b").length} subsequent writes`);+ println(`${method}: other document accepted writes: ${otherDocument.querySelector("u") !== null}`);+ otherDocument.close();+ delete window.otherDocument;+ other.remove();+ iframe.remove();+ }+ done();+ });+</script>