From c854a6b2e91f9194f93a780f33ed9b4a796d134a Mon Sep 17 00:00:00 2001 From: Igor Rozum Date: Wed, 25 Nov 2026 11:24:18 -0500 Subject: [PATCH 1/2] Tests: Add tests for circular dependencies in WP_Dependencies::all_deps() --- tests/phpunit/tests/dependencies.php | 84 ++++++++++++++++++++ tests/phpunit/tests/dependencies/scripts.php | 36 +++++++++ 2 files changed, 120 insertions(+) diff --git a/tests/phpunit/tests/dependencies.php b/tests/phpunit/tests/dependencies.php index 57dd62b2bd000..dde2c33079632 100644 --- a/tests/phpunit/tests/dependencies.php +++ b/tests/phpunit/tests/dependencies.php @@ -244,4 +244,88 @@ public function test_get_etag_styles( $load, $hash_source_string, $expected ) { $this->assertSame( $expected, $result, "Expected MD hash: $expected for $hash_source_string, but got: $result." ); } + + /** + * Tests that a handle depending on itself does not recurse infinitely. + * + * @ticket 46089 + * + * @covers WP_Dependencies::all_deps + */ + public function test_all_deps_handles_self_dependency() { + $dependencies = new WP_Dependencies(); + $dependencies->add( 'one', '', array( 'one' ) ); + + $this->assertTrue( $dependencies->all_deps( 'one' ) ); + $this->assertSame( array( 'one' ), $dependencies->to_do ); + } + + /** + * Tests that two handles depending on each other do not recurse infinitely. + * + * @ticket 46089 + * + * @covers WP_Dependencies::all_deps + */ + public function test_all_deps_handles_circular_dependency() { + $dependencies = new WP_Dependencies(); + $dependencies->add( 'one', '', array( 'two' ) ); + $dependencies->add( 'two', '', array( 'one' ) ); + + $this->assertTrue( $dependencies->all_deps( 'one' ) ); + $this->assertSame( array( 'two', 'one' ), $dependencies->to_do ); + } + + /** + * Tests that a circular dependency spanning several handles does not recurse infinitely. + * + * @ticket 46089 + * + * @covers WP_Dependencies::all_deps + */ + public function test_all_deps_handles_indirect_circular_dependency() { + $dependencies = new WP_Dependencies(); + $dependencies->add( 'one', '', array( 'two' ) ); + $dependencies->add( 'two', '', array( 'three' ) ); + $dependencies->add( 'three', '', array( 'one' ) ); + + $this->assertTrue( $dependencies->all_deps( 'one' ) ); + $this->assertSame( array( 'three', 'two', 'one' ), $dependencies->to_do ); + } + + /** + * Tests that a dependency shared by several handles is still resolved once. + * + * @ticket 46089 + * + * @covers WP_Dependencies::all_deps + */ + public function test_all_deps_still_resolves_shared_dependencies() { + $dependencies = new WP_Dependencies(); + $dependencies->add( 'one', '', array( 'two', 'three' ) ); + $dependencies->add( 'two', '', array( 'three' ) ); + $dependencies->add( 'three', '' ); + + $this->assertTrue( $dependencies->all_deps( 'one' ) ); + $this->assertSame( array( 'three', 'two', 'one' ), $dependencies->to_do ); + } + + /** + * Tests that a circular dependency with a missing dependency terminates without queueing the handle. + * + * @ticket 46089 + * + * @covers WP_Dependencies::all_deps + */ + public function test_all_deps_circular_dependency_with_missing_dependency_fails() { + $this->setExpectedIncorrectUsage( 'WP_Dependencies::add' ); + + $dependencies = new WP_Dependencies(); + $dependencies->add( 'one', '', array( 'two' ) ); + $dependencies->add( 'two', '', array( 'one', 'missing' ) ); + + $dependencies->all_deps( 'one' ); + + $this->assertSame( array(), $dependencies->to_do ); + } } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 75e919355c76c..127deb2c5b2c4 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -2774,6 +2774,42 @@ public function test_wp_set_script_translations() { $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); } + /** + * Tests that setting translations on `wp-i18n` does not make it depend on itself recursively. + * + * @ticket 46089 + * + * @covers ::wp_set_script_translations + */ + public function test_wp_set_script_translations_on_wp_i18n_does_not_recurse_infinitely() { + wp_register_script( 'wp-i18n', '/wp-i18n.js', array(), null ); + wp_set_script_translations( 'wp-i18n' ); + wp_enqueue_script( 'wp-i18n' ); + + $output = get_echo( 'wp_print_scripts' ); + + $this->assertSame( 1, substr_count( $output, '' ) ); + } + + /** + * Tests that a dependency cycle created through `wp-i18n` does not recurse infinitely. + * + * @ticket 46089 + * + * @covers ::wp_set_script_translations + */ + public function test_wp_set_script_translations_with_dependency_cycle_through_wp_i18n() { + wp_register_script( 'wp-hooks', '/wp-hooks.js', array(), null ); + wp_register_script( 'wp-i18n', '/wp-i18n.js', array( 'wp-hooks' ), null ); + wp_set_script_translations( 'wp-hooks' ); + wp_enqueue_script( 'wp-hooks' ); + + $output = get_echo( 'wp_print_scripts' ); + + $this->assertSame( 1, substr_count( $output, '' ) ); + $this->assertSame( 1, substr_count( $output, '' ) ); + } + /** * @ticket 63944 */ From 80d5913489865e0228e678e78cd560c91fe93aaf Mon Sep 17 00:00:00 2001 From: Igor Rozum Date: Wed, 25 Nov 2026 11:24:18 -0500 Subject: [PATCH 2/2] Script Loader: Prevent infinite recursion on circular dependencies in WP_Dependencies::all_deps() --- src/wp-includes/class-wp-dependencies.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-dependencies.php b/src/wp-includes/class-wp-dependencies.php index 204ade1d6f0f0..e3032158c0340 100644 --- a/src/wp-includes/class-wp-dependencies.php +++ b/src/wp-includes/class-wp-dependencies.php @@ -119,6 +119,14 @@ class WP_Dependencies { */ private $dependencies_with_missing_dependencies = array(); + /** + * Handles whose dependencies are currently being resolved by all_deps(), used to detect circular dependencies. + * + * @since 7.3.0 + * @var array + */ + private $handles_being_resolved = array(); + /** * Processes the items and dependencies. * @@ -179,11 +187,13 @@ public function do_item( $handle, $group = false ) { * Determines dependencies. * * Recursively builds an array of items to process taking - * dependencies into account. Does NOT catch infinite loops. + * dependencies into account. Circular dependencies are tolerated: a handle that + * appears again in its own dependency chain is treated as already resolved. * * @since 2.1.0 * @since 2.6.0 Moved from `WP_Scripts`. * @since 2.8.0 Added the `$group` parameter. + * @since 7.3.0 Circular dependencies no longer cause infinite recursion. * * @param string|string[] $handles Item handle (string) or item handles (array of strings). * @param bool $recursion Optional. Internal flag that function is calling itself. @@ -207,6 +217,10 @@ public function all_deps( $handles, $recursion = false, $group = false ) { continue; } + if ( isset( $this->handles_being_resolved[ $handle ] ) ) { // Circular dependency, resolved further up the stack. + continue; + } + $moved = $this->set_group( $handle, $recursion, $group ); $new_group = $this->groups[ $handle ]; @@ -214,6 +228,8 @@ public function all_deps( $handles, $recursion = false, $group = false ) { continue; } + $this->handles_being_resolved[ $handle ] = true; + $keep_going = true; $missing_dependencies = array(); if ( isset( $this->registered[ $handle ] ) && count( $this->registered[ $handle ]->deps ) > 0 ) { @@ -235,6 +251,8 @@ public function all_deps( $handles, $recursion = false, $group = false ) { $keep_going = false; // Item requires dependencies that don't exist. } + unset( $this->handles_being_resolved[ $handle ] ); + if ( ! $keep_going ) { // Either item or its dependencies don't exist. if ( $recursion ) { return false; // Abort this branch.