Exception when fetching url with IP hostname in workers

deploy worker javascript code

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

/**
 * Respond to the request
 * @param {Request} request
 */
async function handleRequest(request) {
  const url = 'https://[240e:ff:e020:966:0:ff:b042:f296]:443';
  const newRequest = new Request(url, request);
  return fetch(url, newRequest);
}

and visit the worker via browser, It throws

Error 1003 Ray ID: 83c20b59d45f4625 • 2023-12-27 13:52:29 UTC
Direct IP access not allowed
What happened?
You've requested an IP address that is part of the Cloudflare network. A valid Host header must be supplied to reach the desired website.

What can I do?
If you are interested in learning more about Cloudflare, please visit our website.

Cloudflare blocks URL with IP address? I don’t understand why.

As the error explains:

Direct IP access not allowed

Workers can only fetch domains, not IPs (i.e create an A DNS Record for your ip like backend.example.com and fetch that). That wouldn’t work with https anyway, certificate wasn’t issued for the IP.

3 Likes

Hello @Chaika, thank you for answering on my question.




Certificate wasn’t issued for the IP.

IP addresses are eligible to enroll SSL certificate to start a HTTPs server. eg: 223.5.5.5.



Create an A DNS Record for your ip like backend.example.com and fetch that.

If destination server has some SNI verification or failed to fetch SNI certificate for this domain name, destination will reject this connection.



Workers can only fetch domains, not IPs.

That’s what i think what the thing cloudflare makes no sense. Because they can block visiting IP sni and IP hostname incoming flows to cloudflare networks, but no need to block outcoming flows which fetching IP sni or IP hostname.

Sure, except the IP you gave doesn’t have a valid cert for that IP.

That sounds more like you’re trying to misuse fetch then anything.

This was the answer CF gave: A valid Host header must be supplied to reach the desired website - #12 by harris