Skip to content

Truly independent web browser

Meta: Patch nghttp2 to ignore connection-specific response fields

sideshowbarker14fd15bf18ef
diff --git a/Meta/CMake/vcpkg/overlay-ports/nghttp2/ignore-connection-specific-response-fields.patch b/Meta/CMake/vcpkg/overlay-ports/nghttp2/ignore-connection-specific-response-fields.patch
new file mode 100644
index 00000000000..77648aeb487
--- /dev/null
+++ b/Meta/CMake/vcpkg/overlay-ports/nghttp2/ignore-connection-specific-response-fields.patch
@@ -0,0 +1,24 @@
+--- a/lib/nghttp2_http.c
++++ b/lib/nghttp2_http.c
+@@ -377,16 +377,18 @@
+ }
+ break;
+ }
+- /* disallowed header fields */
++ /* Connection-specific header fields aren't allowed in HTTP/2, but
++ some servers send them in responses anyway. Ignore them rather than
++ failing the stream, as web browsers do. */
+ case NGHTTP2_TOKEN_CONNECTION:
+ case NGHTTP2_TOKEN_KEEP_ALIVE:
+ case NGHTTP2_TOKEN_PROXY_CONNECTION:
+ case NGHTTP2_TOKEN_TRANSFER_ENCODING:
+ case NGHTTP2_TOKEN_UPGRADE:
+- return NGHTTP2_ERR_HTTP_HEADER;
++ return NGHTTP2_ERR_IGN_HTTP_HEADER;
+ case NGHTTP2_TOKEN_TE:
+ if (!lstrieq("trailers", nv->value->base, nv->value->len)) {
+- return NGHTTP2_ERR_HTTP_HEADER;
++ return NGHTTP2_ERR_IGN_HTTP_HEADER;
+ }
+ break;
+ default:
diff --git a/Meta/CMake/vcpkg/overlay-ports/nghttp2/portfile.cmake b/Meta/CMake/vcpkg/overlay-ports/nghttp2/portfile.cmake
new file mode 100644
index 00000000000..950892267de
--- /dev/null
+++ b/Meta/CMake/vcpkg/overlay-ports/nghttp2/portfile.cmake
@@ -0,0 +1,10 @@
+# FIXME: Drop this patch once nghttp2 offers an option for it: https://github.com/nghttp2/nghttp2/issues/2989
+# NB: This is vcpkg's own nghttp2 port with one more patch. To update nghttp2, update the version in vcpkg.json here.
+set(ladybird_nghttp2_patch "${CMAKE_CURRENT_LIST_DIR}/ignore-connection-specific-response-fields.patch")
+set(CURRENT_PORT_DIR "${VCPKG_ROOT_DIR}/ports/nghttp2")
+
+macro(vcpkg_from_github)
+ _vcpkg_from_github(${ARGN} PATCHES "${ladybird_nghttp2_patch}")
+endmacro()
+
+include("${CURRENT_PORT_DIR}/portfile.cmake")
diff --git a/Meta/CMake/vcpkg/overlay-ports/nghttp2/vcpkg.json b/Meta/CMake/vcpkg/overlay-ports/nghttp2/vcpkg.json
new file mode 100644
index 00000000000..dcccae2aed8
--- /dev/null
+++ b/Meta/CMake/vcpkg/overlay-ports/nghttp2/vcpkg.json
@@ -0,0 +1,14 @@
+{
+ "name": "nghttp2",
+ "version": "1.70.0",
+ "port-version": 1,
+ "description": "Implementation of the Hypertext Transfer Protocol version 2 in C",
+ "homepage": "https://github.com/nghttp2/nghttp2",
+ "license": "MIT",
+ "dependencies": [
+ {
+ "name": "vcpkg-cmake",
+ "host": true
+ }
+ ]
+}
diff --git a/Tests/RequestServer/CMakeLists.txt b/Tests/RequestServer/CMakeLists.txt
index bcb2e0670c8..effd2a20fbc 100644
--- a/Tests/RequestServer/CMakeLists.txt
+++ b/Tests/RequestServer/CMakeLists.txt
@@ -17,6 +17,10 @@ if (LINUX)
list(APPEND TEST_SOURCES TestResourceMapSandbox.cpp)
endif()
+if (NOT WIN32)
+ list(APPEND TEST_SOURCES TestHTTP2ConnectionSpecificFields.cpp)
+endif()
+
target_sources(TestRequestServer PRIVATE ${TEST_SOURCES})
-target_link_libraries(TestRequestServer PRIVATE requestserverservice OpenSSL::Crypto OpenSSL::SSL CURL::libcurl)
+target_link_libraries(TestRequestServer PRIVATE requestserverservice OpenSSL::Crypto OpenSSL::SSL CURL::libcurl LibThreading)
target_include_directories(TestRequestServer PRIVATE ${LADYBIRD_SOURCE_DIR}/Services)
diff --git a/Tests/RequestServer/TestHTTP2ConnectionSpecificFields.cpp b/Tests/RequestServer/TestHTTP2ConnectionSpecificFields.cpp
new file mode 100644
index 00000000000..68bff09ed39
--- /dev/null
+++ b/Tests/RequestServer/TestHTTP2ConnectionSpecificFields.cpp
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/ByteBuffer.h>
+#include <AK/Vector.h>
+#include <LibTest/TestCase.h>
+#include <LibThreading/Thread.h>
+#include <RequestServer/CURL.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <poll.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+namespace {
+
+constexpr auto client_preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"sv;
+constexpr u8 frame_type_headers = 0x1;
+constexpr u8 frame_type_settings = 0x4;
+constexpr u8 flag_end_stream = 0x1;
+constexpr u8 flag_ack = 0x1;
+constexpr u8 flag_end_headers = 0x4;
+
+void append_frame(ByteBuffer& buffer, u8 type, u8 flags, u32 stream_id, ReadonlyBytes payload)
+{
+ u8 header[9] = {
+ static_cast<u8>(payload.size() >> 16), static_cast<u8>(payload.size() >> 8), static_cast<u8>(payload.size()),
+ type, flags,
+ static_cast<u8>(stream_id >> 24), static_cast<u8>(stream_id >> 16), static_cast<u8>(stream_id >> 8), static_cast<u8>(stream_id)
+ };
+ buffer.append(header, sizeof(header));
+ buffer.append(payload);
+}
+
+// HPACK: a literal field without indexing, with a literal name (RFC 7541, section 6.2.2).
+void append_literal_field(ByteBuffer& block, StringView name, StringView value)
+{
+ block.append(0x00);
+ block.append(static_cast<u8>(name.length()));
+ block.append(name.bytes());
+ block.append(static_cast<u8>(value.length()));
+ block.append(value.bytes());
+}
+
+// A cleartext HTTP/2 server for a single connection, which answers the client's first request with the given fields.
+class HTTP2Server {
+public:
+ explicit HTTP2Server(Vector<std::pair<StringView, StringView>> response_fields)
+ : m_response_fields(move(response_fields))
+ {
+ m_listen_fd = ::socket(AF_INET, SOCK_STREAM, 0);
+ VERIFY(m_listen_fd >= 0);
+ sockaddr_in address {};
+ address.sin_family = AF_INET;
+ address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ VERIFY(::bind(m_listen_fd, reinterpret_cast<sockaddr*>(&address), sizeof(address)) == 0);
+ VERIFY(::listen(m_listen_fd, 1) == 0);
+ socklen_t length = sizeof(address);
+ VERIFY(::getsockname(m_listen_fd, reinterpret_cast<sockaddr*>(&address), &length) == 0);
+ m_port = ntohs(address.sin_port);
+
+ m_thread = Threading::Thread::construct("HTTP2Server"sv, [this] {
+ serve();
+ return static_cast<intptr_t>(0);
+ });
+ m_thread->start();
+ }
+
+ ~HTTP2Server()
+ {
+ (void)m_thread->join();
+ ::close(m_listen_fd);
+ }
+
+ u16 port() const { return m_port; }
+
+private:
+ void serve()
+ {
+ // Give up if no client connects in time (a proxy that takes the request elsewhere, e.g.), so that the test fails
+ // rather than hangs when it joins this thread.
+ pollfd listen_poll { m_listen_fd, POLLIN, 0 };
+ if (::poll(&listen_poll, 1, 10'000) <= 0)
+ return;
+ auto fd = ::accept(m_listen_fd, nullptr, nullptr);
+ if (fd < 0)
+ return;
+
+ // Read the client's preface and frames until its request (a HEADERS frame on stream 1) has arrived.
+ ByteBuffer received;
+ size_t offset = 0;
+ bool has_request = false;
+ while (!has_request) {
+ u8 chunk[4096];
+ auto count = ::read(fd, chunk, sizeof(chunk));
+ if (count <= 0)
+ break;
+ received.append(chunk, count);
+ if (offset == 0) {
+ if (received.size() < client_preface.length())
+ continue;
+ offset = client_preface.length();
+ }
+ while (received.size() >= offset + 9) {
+ auto length = (received[offset] << 16) | (received[offset + 1] << 8) | received[offset + 2];
+ if (received.size() < offset + 9 + length)
+ break;
+ if (received[offset + 3] == frame_type_headers)
+ has_request = true;
+ offset += 9 + length;
+ }
+ }
+
+ ByteBuffer response;
+ append_frame(response, frame_type_settings, 0, 0, {});
+ append_frame(response, frame_type_settings, flag_ack, 0, {});
+ ByteBuffer block;
+ block.append(0x88); // ":status: 200", from the static table.
+ for (auto const& [name, value] : m_response_fields)
+ append_literal_field(block, name, value);
+ append_frame(response, frame_type_headers, flag_end_headers | flag_end_stream, 1, block);
+ VERIFY(::write(fd, response.data(), response.size()) == static_cast<ssize_t>(response.size()));
+
+ // Hold the connection open until the client is done with it.
+ u8 chunk[4096];
+ while (::read(fd, chunk, sizeof(chunk)) > 0) { }
+ ::close(fd);
+ }
+
+ Vector<std::pair<StringView, StringView>> m_response_fields;
+ int m_listen_fd { -1 };
+ u16 m_port { 0 };
+ RefPtr<Threading::Thread> m_thread;
+};
+
+size_t collect_header(char* data, size_t size, size_t count, void* user_data)
+{
+ auto& headers = *static_cast<Vector<ByteString>*>(user_data);
+ headers.append(ByteString { data, size * count }.trim_whitespace());
+ return size * count;
+}
+
+}
+
+// A connection-specific field isn't allowed in an HTTP/2 response, but some servers send one. The response is still
+// delivered, without that field, rather than failing.
+TEST_CASE(http2_response_with_connection_specific_fields_is_delivered)
+{
+ MUST(RequestServer::initialize_libcurl());
+
+ HTTP2Server server({ { "connection"sv, "close"sv }, { "keep-alive"sv, "timeout=5"sv }, { "x-test"sv, "delivered"sv } });
+
+ auto* easy = curl_easy_init();
+ VERIFY(easy);
+ auto url = ByteString::formatted("http://127.0.0.1:{}/", server.port());
+ Vector<ByteString> headers;
+ curl_easy_setopt(easy, CURLOPT_URL, url.characters());
+ curl_easy_setopt(easy, CURLOPT_NOPROXY, "*");
+ curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
+ curl_easy_setopt(easy, CURLOPT_HEADERFUNCTION, collect_header);
+ curl_easy_setopt(easy, CURLOPT_HEADERDATA, &headers);
+ curl_easy_setopt(easy, CURLOPT_TIMEOUT, 10L);
+
+ auto result = curl_easy_perform(easy);
+ long status = 0;
+ curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &status);
+ curl_easy_cleanup(easy);
+
+ EXPECT_EQ(result, CURLE_OK);
+ EXPECT_EQ(status, 200);
+ EXPECT(headers.contains_slow("x-test: delivered"sv));
+ EXPECT(!headers.contains_slow("connection: close"sv));
+}
diff --git a/vcpkg.json b/vcpkg.json
index b35f8f51944..f33768ad4af 100644
--- a/vcpkg.json
+++ b/vcpkg.json
@@ -387,7 +387,7 @@
},
{
"name": "nghttp2",
- "version": "1.70.0#0"
+ "version": "1.70.0#1"
},
{
"name": "openssl",