Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch API is inconsistent with the browser/web API for URL starting with whitespaces #1957

Closed
SamyPesse opened this issue Apr 4, 2024 · 1 comment · Fixed by #2114
Closed
Labels
api bug Something isn't working spec-compliance

Comments

@SamyPesse
Copy link

I'm opening this issue to report a difference in implementation between fetch in the browser and in Cloudflare for an edge case. We are not really expecting a fix, as I'm not sure the browser is doing the right thing there; but I've suggested one at the end.

If you fetch a URL that starts with a whitespace, like:

const res = await fetch(' https://raw.githubusercontent.com/cloudflare/workerd/main/README.md');
console.log(await res.text());

It will throw an error TypeError: Fetch API cannot load in Cloudflare Workers, while this code will work in the browser (at least Chrome).

We encountered a situation in which we needed to fetch a user-inputted URL, but our validation for the URL was not great.

A workaround we implemented was to wrap the url in a new URL(): fetch(new URL(' https://raw.githubusercontent.com/cloudflare/workerd/main/README.md')) works in Cloudflare.

A solution in workerd could be to normalize / trim these urls in the fetch function, the same way new URL is doing it.

@jasnell
Copy link
Member

jasnell commented Apr 4, 2024

Currently, our implementation of fetch(...) does not use the whatwg URL parsing rules for backwards compatibility reasons. It's been on my todo list for a while to get that fixed but it will require a compatibility flag as it'll be a breaking change. Will see about upping the priority of making that change

/cc @irvinebroque @kentonv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api bug Something isn't working spec-compliance
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants