I have the following code in my worker to copy the incoming request so I can mutate it:
const url = new URL(fetchEvent.request.url)
url.hostname = 'some.other.hostname'
request = new Request(url, fetchEvent.request)
That works fine for 99.99999% of requests, but a few cause the exception
Invalid pathname when stringifying URL
https://www.my-company.com/%2e/WEB-INF/web.xml
Although it’s probably not a valid request, it is a perfectly well-formed URL:
// Firefox:
new URL('https://www.my-company.com/%2e/WEB-INF/web.xml').toString()
"https://www.my-company.com/WEB-INF/web.xml"
// Cloudflare:
try {
new URL('https://www.my-company.com/%2e/WEB-INF/web.xml').toString()
} catch (e) {
e.message // "Invalid pathname when stringifying URL"
}
Why is Cloudflare complaining?
This only happens in production environments, not when using wrangler dev
.