Pssst… are your wp_http_validate_url() related tests failing? Check your DNS.

Recently, while working on WordPress core changes I started to find several failures running the wordpress-develop repository PHP unit tests (npm run test:php).

I noticed that all the tests failing (see the failures list at the end of the post) were related to the use of external URLs and those URLs were bing validated using with wp_http_validate_url(). So I went to the source code of the test cases failing and I found that the tests failing were using URLs with the https://example.com domain. Let’s see one of the examples:

	public function test_wp_http_validate_url_should_validate( $url, $cb_safe_ports = false, $external_host = false ) {
		if ( $external_host ) {
			add_filter( 'http_request_host_is_external', '__return_true' );
		}

		if ( $cb_safe_ports ) {
			add_filter( 'http_allowed_safe_ports', array( $this, $cb_safe_ports ) );
		}

		$this->assertSame( $url, wp_http_validate_url( $url ) );
	}

	public function data_wp_http_validate_url_should_validate() {
		return array(
			'no port specified'                 => array(
				'url' => 'http://example.com/caniload.php',
			),
			'an external request when allowed'  => array(
				'url'           => 'http://172.20.0.123/caniload.php',
				'cb_safe_ports' => false,
				'external_host' => true,
			),
			'a port considered safe by default' => array(
				'url' => 'https://example.com:8080/caniload.php',
			),
			'a port considered safe by filter'  => array(
				'url'           => 'https://example.com:81/caniload.php',
				'cb_safe_ports' => 'callback_custom_safe_ports',
			),
		);
	}

That caught my attention, I knew that https://example.com is en example daomin reserved by the Internet Assigned Numbers Authority (IANA) and I was really surprised about the fact that navigating example.com I was getting this error in my browser.

For some reason, my internet service provider DNS service was failing to resolve the example.com. Curiously example.org was resolved as expected.

Having found that I added some public DNS address to my wifi internet connection. I added some Cloudflare public DNS ( 1.1.1.1 and 1.0.0.1 ), then disconnected and connected again to use the new DNS.

After that I run the tests again using npm run test:php and I worked, I didn’t received any other test failure. Be sure to destroy the docker containers before running the tests again.

So in case that you find this type of failures while running these tests I encourage you to try this workaround.


Leave a Reply

Your email address will not be published. Required fields are marked *