From 0bf2a8699bdda02c928b87978bf603508077050e Mon Sep 17 00:00:00 2001 From: "He-Pin(kerr)" Date: Fri, 27 Nov 2026 04:34:01 +0800 Subject: [PATCH] fix: reject attempts to reparent the HTTP/2 priority tree root Motivation: The internal root is a fixed sentinel, not a repositionable stream node. Modification: Reject root updates consistently with removal of the root, and exercise both root and self-dependency contracts in PriorityTreeSpec. Result: Invalid local operations leave the tree unchanged. Default scheduling and wire handling are unchanged. References: Netty 4.2 DefaultHttp2RemoteFlowControllerTest local programming contracts; RFC 9113 Sections 5.3.2 and 6.3. --- .../http/impl/engine/http2/PriorityTree.scala | 4 +++- .../impl/engine/http2/PriorityTreeSpec.scala | 21 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/PriorityTree.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/PriorityTree.scala index d495ce4294..4a78be71c5 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/PriorityTree.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/PriorityTree.scala @@ -63,9 +63,11 @@ private[http2] object PriorityTree { private class PriorityTreeImpl(nodes: TreeMap[Int, PriorityInfo]) extends PriorityTree { def rootNode: PriorityNode = node(0) - def insertOrUpdate(streamId: Int, streamDependency: Int, weight: Int, exclusive: Boolean): PriorityTree = + def insertOrUpdate(streamId: Int, streamDependency: Int, weight: Int, exclusive: Boolean): PriorityTree = { + require(streamId != 0, "Cannot update root node") if (nodes.isDefinedAt(streamId)) update(streamId, streamDependency, weight, exclusive) else insert(streamId, streamDependency, weight, exclusive) + } private def insert(streamId: Int, streamDependency: Int, weight: Int, exclusive: Boolean): PriorityTreeImpl = { require(!nodes.isDefinedAt(streamId), s"Cannot insert node twice: $streamId") diff --git a/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/PriorityTreeSpec.scala b/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/PriorityTreeSpec.scala index 5a09c93e99..60421ae208 100644 --- a/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/PriorityTreeSpec.scala +++ b/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/PriorityTreeSpec.scala @@ -212,8 +212,25 @@ class PriorityTreeSpec extends AnyWordSpec with Matchers { "prune nodes" in pending - "fail when updating the root node" in pending - "fail when introducing a dependency on itself" in pending + "fail when updating the root node" in { + val tree = PriorityTree().insertOrUpdate(1, 0, 16, exclusive = false) + + intercept[IllegalArgumentException] { + tree.insertOrUpdate(0, 1, 16, exclusive = true) + } + } + + "fail when introducing a dependency on itself" in { + val empty = PriorityTree() + intercept[IllegalArgumentException] { + empty.insertOrUpdate(1, 1, 16, exclusive = false) + } + + val tree = empty.insertOrUpdate(1, 0, 16, exclusive = false) + intercept[IllegalArgumentException] { + tree.insertOrUpdate(1, 1, 16, exclusive = false) + } + } } def printLike(resultingTree: String): Matcher[PriorityTree] =