cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.7k stars 709 forks source link

🐛 BUG: It's not possible to emulate `resolveOverride` behavior locally #5000

Closed gaastonsr closed 4 months ago

gaastonsr commented 9 months ago

Which Cloudflare product(s) does this pertain to?

Workers Runtime, Wrangler core, Miniflare

What version(s) of the tool(s) are you using?

3.28.1 [wrangler]

What version of Node are you using?

18.17

What operating system and version are you using?

Debian Bookworm

Describe the Bug

Observed behavior

I have a worker that uses resolveOverride: foo.com to communicate with an alternate origin server. When I run the local server npx wrangler dev I expect to be able to simulate that behavior.

Attempt 1. If I run npx wrangler dev and then I access the server using bar.com, then I expect my fetch call to forward the request to my alternate origin defined in resolveOverride but it doesn't. I think resolveOverride is being ignored locally.

Attempt 2. If I run npx wrangler dev --host='foo.com' and then I access the server using bar.com then I expect my fetch call to forward the request to foo.com maintaining the original host header. Instead, my request is forwarded to foo.com but the host is changed to foo.com instead of maintaining bar.com.

Steps to reproduce

Attempt 1:

  1. Run npx wrangler dev having a fetch call with resolveOverride pointing to alternateorigin.com.
  2. Make a request to the local server.
  3. Expect the fetch call to ignore the value of resolveOverride.

Attempt 2:

  1. Run npx wrangler dev --host='alternateorigin.com'
  2. Make a request to the local server.
  3. Expect the request to be forwarded to alternateorigin.com but with the host header changed to alternateorigin.com.

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

No response

kuahyeow commented 7 months ago

I see a slightly different problem. With the following simple script:

/**
 * Welcome to Cloudflare Workers! This is your first worker.
 *
 * - Run `npm run dev` in your terminal to start a development server
 * - Open a browser tab at http://localhost:8787/ to see your worker in action
 * - Run `npm run deploy` to publish your worker
 *
 * Learn more at https://developers.cloudflare.com/workers/
 */

export interface Env {
    // Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
    // MY_KV_NAMESPACE: KVNamespace;
    //
    // Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
    // MY_DURABLE_OBJECT: DurableObjectNamespace;
    //
    // Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
    // MY_BUCKET: R2Bucket;
    //
    // Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
    // MY_SERVICE: Fetcher;
    //
    // Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/
    // MY_QUEUE: Queue;
    GITLAB_URL: string;
}

export default {
    async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
        let resolveOverride = new URL(env.GITLAB_URL).host;
        const req = new Request(request, { cf: { resolveOverride: resolveOverride } });
        console.log({ resolveOverride: resolveOverride, url: req.url, headers: req.headers });

        return fetch(req);
    },
};

I get a 500 error:

[wrangler:err] Error: Network connection lost.
[wrangler:inf] GET /favicon.ico 500 Internal Server Error (32ms)
[wrangler:inf] GET /favicon.ico 500 Internal Server Error (33ms)
[wrangler:inf] GET /favicon.ico 500 Internal Server Error (68ms)
[wrangler:inf] GET /favicon.ico 500 Internal Server Error (76ms)
[wrangler:inf] GET /favicon.ico 500 Internal Server Error (150ms)

and also a warning:

▲ [WARNING] Failed to get worker definitions TypeError: fetch failed

      at fetch (/Users/tkuah/code/router/node_modules/wrangler/wrangler-dist/cli.js:17033:19)
      at async getRegisteredWorkers
  (/Users/tkuah/code/router/node_modules/wrangler/wrangler-dist/cli.js:127225:22)
      at async getBoundRegisteredWorkers
  (/Users/tkuah/code/router/node_modules/wrangler/wrangler-dist/cli.js:127244:29) {
    cause: SocketError: other side closed
        at Socket.onSocketEnd
  (/Users/tkuah/code/router/node_modules/wrangler/wrangler-dist/cli.js:7263:27)
        at Socket.emit (node:events:531:35)
        at Socket.emit (node:domain:488:12)
        at endReadableNT (node:internal/streams/readable:1696:12)
        at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
      code: 'UND_ERR_SOCKET',
      socket: {
        localAddress: '127.0.0.1',
        localPort: 55798,
        remoteAddress: '127.0.0.1',
        remotePort: 6284,
        remoteFamily: 'IPv4',
        timeout: undefined,
        bytesWritten: 356,
        bytesRead: 391
      }
    }
  }
penalosa commented 4 months ago

Thanks for reporting this! Although we do aim to emulate as much of the Cloudflare network stack as possible with wrangler dev, if you need additional Cloudflare features outside of Workers to work properly, wrangler dev --remote should give you the highest-fidelity experience. Could you try that? If that doesn't work, feel free to comment with more details and we can re-open and investigate further.

@kuahyeow That looks like an unrelated issue—could you open a new issue with a reproduction?