From f3213e75a39e95874a0e02f6f433f9216c62a2be Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Sun, 15 Nov 2026 16:54:51 +0300 Subject: [PATCH] fix(runtime): raise ResultTooDeepError when a result exceeds Chrome's CBOR depth limit --- CHANGELOG.md | 2 ++ lib/ferrum/errors.rb | 9 +++++++++ lib/ferrum/frame/runtime.rb | 10 ++++++++++ sig/ferrum/errors.rbs | 4 ++++ sig/ferrum/frame/runtime.rbs | 2 ++ spec/frame/runtime_spec.rb | 6 ++++++ 6 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25cd9de7..f80aba86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ `go_to` instead ### Fixed +- `evaluate` of a result nested about 100 levels deep raised an opaque `BrowserError` about Chrome's CBOR stack limit, + it raises `ResultTooDeepError` now, a `BrowserError` that says to return less or use `evaluate_handle` - `worker.close` never closed a dedicated worker, Chrome refuses to close it from outside, it closes itself now - `page.on(:request, &:continue)` never continued the request, it called `continue(index, total)` and the error was dropped. Symbol procs and lambdas taking one argument get only the object now, for `:request`, `:response`, `:auth` diff --git a/lib/ferrum/errors.rb b/lib/ferrum/errors.rb index 24932917..d2c47262 100644 --- a/lib/ferrum/errors.rb +++ b/lib/ferrum/errors.rb @@ -242,6 +242,15 @@ def data end end + # Raised when an evaluated script returns a value nested too deeply for + # Chrome to serialize, around 100 levels. + class ResultTooDeepError < BrowserError + def initialize(response) + super(response.merge("message" => "The result is nested too deeply to be serialized, return less of it or " \ + "use evaluate_handle")) + end + end + # Raised when the browser reports that a DOM node no longer exists. class NodeNotFoundError < BrowserError; end diff --git a/lib/ferrum/frame/runtime.rb b/lib/ferrum/frame/runtime.rb index c28e2298..323bd5e1 100644 --- a/lib/ferrum/frame/runtime.rb +++ b/lib/ferrum/frame/runtime.rb @@ -361,6 +361,8 @@ def call_function(declaration, values, target, serialization, timeout) functionDeclaration: declaration, arguments: prepare_args(values), **target) rescue TimeoutError raise ScriptTimeoutError + rescue BrowserError => e + raise_deep_error(e) end # A promise, or a plain object that may be one, such as the promises of @@ -380,6 +382,14 @@ def await_promise(promise, serialization, timeout) response["result"] rescue TimeoutError raise ScriptTimeoutError + rescue BrowserError => e + raise_deep_error(e) + end + + def raise_deep_error(error) + raise error unless error.message.to_s.include?("CBOR: stack limit exceeded") + + raise ResultTooDeepError, error.response end def call_target(on) diff --git a/sig/ferrum/errors.rbs b/sig/ferrum/errors.rbs index 1f0f4a70..8bdfc000 100644 --- a/sig/ferrum/errors.rbs +++ b/sig/ferrum/errors.rbs @@ -96,6 +96,10 @@ module Ferrum def data: () -> untyped end + class ResultTooDeepError < BrowserError + def initialize: (Hash[String, untyped] response) -> void + end + class NodeNotFoundError < BrowserError end diff --git a/sig/ferrum/frame/runtime.rbs b/sig/ferrum/frame/runtime.rbs index 2ad9cb23..776c58e7 100644 --- a/sig/ferrum/frame/runtime.rbs +++ b/sig/ferrum/frame/runtime.rbs @@ -50,6 +50,8 @@ module Ferrum def handle_remote_object: (untyped result) -> untyped + def raise_deep_error: (BrowserError error) -> bot + def handle_response: (Hash[String, untyped] response) -> untyped def deserialize: (Hash[String, untyped] serialized, ?Hash[Integer, Hash[String, untyped]] references, ?Array[Integer] path, ?Hash[Integer, untyped] converted) -> untyped diff --git a/spec/frame/runtime_spec.rb b/spec/frame/runtime_spec.rb index 2bce37a5..938caccf 100644 --- a/spec/frame/runtime_spec.rb +++ b/spec/frame/runtime_spec.rb @@ -231,6 +231,12 @@ end end + it "raises ResultTooDeepError for a result nested over Chrome's limit" do + code = "(() => { let o = {}; let c = o; for (let i = 0; i < 200; i++) { c.a = {}; c = c.a } return o })()" + + expect { browser.evaluate(code) }.to raise_error(Ferrum::ResultTooDeepError, /nested too deeply/) + end + context "with a cyclic structure" do let(:cyclic) { Ferrum::CyclicObject.instance }