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 ignoring port #1320

Closed
RihanArfan opened this issue Dec 18, 2020 · 13 comments · Fixed by #992 or #1336
Closed

Fetch ignoring port #1320

RihanArfan opened this issue Dec 18, 2020 · 13 comments · Fixed by #992 or #1336
Labels
blocked Blocked on other work internal Requires support from the Cloudflare Platform

Comments

@RihanArfan
Copy link

🐛 Bug Report

I'm having an extremely strange issue where Fetch() completely ignores any custom port when actually running in the deployed Worker but from the quick editor, it works as expected.

I've gotten the Worker as simplified as much as possible to try and debug this issue. To explain the test, I have Nginx running on 443 and another application on 4567.

Steps to reproduce

addEventListener('fetch', event => {
  event.respondWith(rewriteURL(event.request))
})
 
async function rewriteURL(request) {
  let urlWithPort = new URL("https://test.file.properties:4567/download/file")
  urlWithPort.port = 9055
  //return new Response(urlWithPort) - shows the port 9055 in the URL, as it should.
  const response = await fetch(urlWithPort, request)
  return response
}

What did you expect to see?

  • When I test this code from the quick editor, I get 502 (expected) because I specified port 9055 and it's using that as I told it to
  • When I test this code by visiting the deployed URL, I get an Nginx 404 page (so it's completely ignoring port 9055)
  • If you change urlWithPort.port to 4567 (or remove it), the quick editor will successfully return it from the application running on port 4567 (expect some JSON error about processing the request because the application expects some headers and parameters)

I'm expecting the deployed Worker to use the custom specified port when doing Fetch() just like it does on the quick editor.

--
Wrangler wasn't used to create this worker, so I didn't specify any environment. I've entirely used the online quick editor.

@stale
Copy link

stale bot commented Jan 9, 2022

This issue has been automatically marked as stale because it has not had recent activity in the last 180 days. It will be closed if no further activity occurs in the next week. Please feel free to comment if you'd like it to remain open, and thank you for your contributions.

@RihanArfan
Copy link
Author

Please keep this open

@petebacondarwin petebacondarwin transferred this issue from cloudflare/wrangler-legacy Jun 21, 2022
@petebacondarwin petebacondarwin self-assigned this Jun 21, 2022
@petebacondarwin petebacondarwin added the internal Requires support from the Cloudflare Platform label Jun 24, 2022
petebacondarwin added a commit to petebacondarwin/wrangler2 that referenced this issue Jun 28, 2022
In Workers published to the Edge (rather than previews) there is a bug where a custom port on a downstream fetch request is ignored, defaulting to the standard port.
For example, `https://my.example.com:668` will actually send the request to `https://my.example.com:443`.

This does not happen when using `wrangler dev` (both in remote and local mode), but to ensure that developers are aware of it this change displays a runtime warning in the console when the bug is hit.

Closes cloudflare#1320
petebacondarwin added a commit to petebacondarwin/wrangler2 that referenced this issue Jun 28, 2022
In Workers published to the Edge (rather than previews) there is a bug where a custom port on a downstream fetch request is ignored, defaulting to the standard port.
For example, `https://my.example.com:668` will actually send the request to `https://my.example.com:443`.

This does not happen when using `wrangler dev` (both in remote and local mode), but to ensure that developers are aware of it this change displays a runtime warning in the console when the bug is hit.

Closes cloudflare#1320
petebacondarwin added a commit that referenced this issue Jun 28, 2022
…rt (#992)

In Workers published to the Edge (rather than previews) there is a bug where a custom port on a downstream fetch request is ignored, defaulting to the standard port.
For example, `https://my.example.com:668` will actually send the request to `https://my.example.com:443`.

This does not happen when using `wrangler dev` (both in remote and local mode), but to ensure that developers are aware of it this change displays a runtime warning in the console when the bug is hit.

Closes #1320
@threepointone threepointone reopened this Jun 28, 2022
@threepointone
Copy link
Contributor

Let's leave this open till we ship a fix in the runtime

@jfsiii
Copy link

jfsiii commented Jul 5, 2022

@threepointone can you reopen this? I think the release notes closed it

@threepointone
Copy link
Contributor

Whoops, reopening

@threepointone threepointone reopened this Jul 5, 2022
@davidkarlsson
Copy link

davidkarlsson commented Jul 19, 2022

I think maybe this fix broke running the wrangler dev server if you are using Yarn 3 PnP? From version 2.0.16 and onwards I now get the following error when I try to start the dev server with yarn wrangler dev:

X [ERROR] Could not read from file: D:\Code\Cloudflare\Test\.yarn\cache\wrangler-npm-2.0.16-25a529adf2-67d437092c.zip\node_modules\wrangler\templates\checked-fetch.js


X [ERROR] Build failed with 1 error:

  error: Could not read from file:
  D:\Code\Cloudflare\Test\.yarn\cache\wrangler-npm-2.0.16-25a529adf2-67d437092c.zip\node_modules\wrangler\templates\checked-fetch.js

@threepointone
Copy link
Contributor

Good catch! Fix incoming.

threepointone added a commit that referenced this issue Jul 19, 2022
In #992, we added a dev-only helper that would warn when using `fetch()` in a manner that wouldn't work as expected (because of a bug we currently have in the runtime). We did this by injecting a file that would override usages of `fetch()`. When using pnp style package managers like yarn, this file can't be resolved correctly. So to fix that, we extract it into the temporary destination directory that we use to build the worker (much like a similar fix we did in #1154)

Reported at #1320 (comment)
threepointone added a commit that referenced this issue Jul 19, 2022
In #992, we added a dev-only helper that would warn when using `fetch()` in a manner that wouldn't work as expected (because of a bug we currently have in the runtime). We did this by injecting a file that would override usages of `fetch()`. When using pnp style package managers like yarn, this file can't be resolved correctly. So to fix that, we extract it into the temporary destination directory that we use to build the worker (much like a similar fix we did in #1154)

Reported at #1320 (comment)
@caass
Copy link
Contributor

caass commented Oct 16, 2022

@jfsiii has this been fixed?

@Contextualist
Copy link

Contextualist commented Jan 4, 2023

Recently, fetch() seems to start respecting custom port in deployment. Maybe they have fixed this in the runtime. Can anybody confirm? Custom port is respected only when fetch's URL shares the same second-level domain with the worker's custom domain.

@penalosa
Copy link
Contributor

We're going to close this for now as it's dependent on internal Cloudflare changes. For now, fetch ignoring custom ports is considered behaviour as designed, but that may be revisited in the future. Of note—you should be able to make fetch requests to a custom port within your own Cloudflare zone.

@codenoid
Copy link

"you should be able to make fetch requests to a custom port within your own Cloudflare zone"

sorry, but wdym by that?

image

@parasdaryanani
Copy link

For whomever this may help:

I recently had this issue and ended up using AWS API Gateway as a http proxy to forward requests to the custom port origin. It took only a few clicks and meant that I could continue using workers as usual. It costs pennies per month and its Edge optimised as well.

@lrapoport-cf
Copy link
Contributor

hi all 👋 we see this issue has been re-opened. however, we have discussed again internally and can confirm this behavior is a consequence of the interaction between the workers runtime and the rest of cloudflare's systems and isn't related to wrangler, so we are going to re-close the issue. however, there is an open internal ticket to track this: EW-1281. we note that there is not currently a timeline on this, and it's not on the immediate roadmap for the relevant team.

@codenoid in terms of a fetch to a custom port on your own cloudflare zone (see https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones for reference), this won't work in wrangler dev, but will work when your worker is deployed to the cloudflare network 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Blocked on other work internal Requires support from the Cloudflare Platform
Projects
Archived in project