From c7db1204ca5e7e6e8c2a8bae40cb3b97636ec215 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 16 Sep 2024 17:29:07 -0400 Subject: [PATCH] WebContent: Protect the switch-to-window endpoint against null BCs This is one of the few endpoints that does not ensure a top-level BC is open. It's a bit of an implementation-defined endpoint, so let's protect against a non-existent BC explicitly. --- .../Services/WebContent/WebDriverConnection.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index 8199891c51b..c91595d8283 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -449,15 +449,17 @@ Messages::WebDriverClient::SwitchToWindowResponse WebDriverConnection::switch_to // Otherwise, return error with error code no such window. bool found_matching_context = false; - current_top_level_browsing_context()->for_each_in_inclusive_subtree([&](Web::HTML::BrowsingContext& context) { - if (handle != context.top_level_traversable()->window_handle()) - return Web::TraversalDecision::Continue; + if (auto browsing_context = current_top_level_browsing_context()) { + browsing_context->for_each_in_inclusive_subtree([&](Web::HTML::BrowsingContext& context) { + if (handle != context.top_level_traversable()->window_handle()) + return Web::TraversalDecision::Continue; - m_current_browsing_context = context; - found_matching_context = true; + m_current_browsing_context = context; + found_matching_context = true; - return Web::TraversalDecision::Break; - }); + return Web::TraversalDecision::Break; + }); + } if (!found_matching_context) return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found");