{"id":541,"date":"2024-02-19T18:17:01","date_gmt":"2024-02-19T18:17:01","guid":{"rendered":"https:\/\/mebenedetto.com\/blog\/?p=541"},"modified":"2024-04-13T00:52:08","modified_gmt":"2024-04-13T00:52:08","slug":"pssst-are-your-wp_http_validate_url-related-tests-failing-check-your-dns","status":"publish","type":"post","link":"https:\/\/mebenedetto.com\/blog\/pssst-are-your-wp_http_validate_url-related-tests-failing-check-your-dns\/","title":{"rendered":"Pssst&#8230; are your wp_http_validate_url() related tests failing? Check your DNS."},"content":{"rendered":"\n<p>Recently, while working on WordPress core changes I started to find several failures running the <a href=\"https:\/\/github.com\/WordPress\/wordpress-develop\">wordpress-develop<\/a>  repository PHP unit tests (<code>npm run test:php<\/code>).<\/p>\n\n\n\n<p>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 <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_http_validate_url\/\" data-type=\"link\" data-id=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_http_validate_url\/\">wp_http_validate_url()<\/a>. So I went to the source code of the test cases failing and I found that the tests failing were using  URLs with the <a href=\"https:\/\/example.com\">https:\/\/example.com<\/a> domain. Let&#8217;s see one of the examples:<\/p>\n\n\n\n<pre title=\"Example of a test related to wp_http_validate_url\" class=\"wp-block-code alignwide has-small-font-size\"><code lang=\"php\" class=\"language-php line-numbers\">\tpublic function test_wp_http_validate_url_should_validate( $url, $cb_safe_ports = false, $external_host = false ) {\n\t\tif ( $external_host ) {\n\t\t\tadd_filter( 'http_request_host_is_external', '__return_true' );\n\t\t}\n\n\t\tif ( $cb_safe_ports ) {\n\t\t\tadd_filter( 'http_allowed_safe_ports', array( $this, $cb_safe_ports ) );\n\t\t}\n\n\t\t$this-&gt;assertSame( $url, wp_http_validate_url( $url ) );\n\t}\n\n\tpublic function data_wp_http_validate_url_should_validate() {\n\t\treturn array(\n\t\t\t'no port specified'                 =&gt; array(\n\t\t\t\t'url' =&gt; 'http:\/\/example.com\/caniload.php',\n\t\t\t),\n\t\t\t'an external request when allowed'  =&gt; array(\n\t\t\t\t'url'           =&gt; 'http:\/\/172.20.0.123\/caniload.php',\n\t\t\t\t'cb_safe_ports' =&gt; false,\n\t\t\t\t'external_host' =&gt; true,\n\t\t\t),\n\t\t\t'a port considered safe by default' =&gt; array(\n\t\t\t\t'url' =&gt; 'https:\/\/example.com:8080\/caniload.php',\n\t\t\t),\n\t\t\t'a port considered safe by filter'  =&gt; array(\n\t\t\t\t'url'           =&gt; 'https:\/\/example.com:81\/caniload.php',\n\t\t\t\t'cb_safe_ports' =&gt; 'callback_custom_safe_ports',\n\t\t\t),\n\t\t);\n\t}<\/code><\/pre>\n\n\n\n<p>That caught my attention, I knew that <a href=\"https:\/\/example.com\">https:\/\/example.com<\/a> is en <a href=\"https:\/\/www.iana.org\/help\/example-domains\">example daomin reserved<\/a> by the Internet Assigned Numbers Authority (<a href=\"https:\/\/www.iana.org\/\">IANA<\/a>) and I was really surprised about the fact that navigating example.com I was getting this error in my browser.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"499\" src=\"https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-1024x499.png\" alt=\"\" class=\"wp-image-547\" srcset=\"https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-1024x499.png 1024w, https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-300x146.png 300w, https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-768x374.png 768w, https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-1536x748.png 1536w, https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-2048x998.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For some reason, my internet service provider DNS service was failing to resolve the example.com. Curiously example.org was resolved as expected.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"499\" src=\"https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-1-1024x499.png\" alt=\"\" class=\"wp-image-548\" srcset=\"https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-1-1024x499.png 1024w, https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-1-300x146.png 300w, https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-1-768x374.png 768w, https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-1-1536x748.png 1536w, https:\/\/mebenedetto.com\/blog\/wp-content\/uploads\/2024\/02\/image-1-2048x998.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Having found that I added some public DNS address to my wifi internet connection. I added some Cloudflare public DNS ( <code>1.1.1.1<\/code> and <code>1.0.0.1 <\/code>), then disconnected and connected again to use the new DNS.<\/p>\n\n\n\n<p>After that I run the tests again using <code>npm run test:php<\/code> and I worked, I didn&#8217;t received any other test failure. Be sure to destroy the docker containers before running the tests again.<\/p>\n\n\n\n<p>So in case that you find this type of failures while running these tests I encourage you to try this workaround.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":548,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[35],"class_list":["post-541","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/posts\/541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/comments?post=541"}],"version-history":[{"count":14,"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/posts\/541\/revisions"}],"predecessor-version":[{"id":558,"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/posts\/541\/revisions\/558"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/media\/548"}],"wp:attachment":[{"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/media?parent=541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/categories?post=541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mebenedetto.com\/blog\/wp-json\/wp\/v2\/tags?post=541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}