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

[miniflare] fix: ensure redirects handled correctly with dispatchFetch() #5191

Merged
merged 1 commit into from
Mar 8, 2024

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Mar 7, 2024

What this PR solves / how to test

Fixes #5018.

Previously, if your Worker returned a redirect response, calling dispatchFetch(url) would send another request to the original url rather than the redirect. This happened because...

  1. We always used this.#runtimeDispatcher for fetch()ing, which only sends request to the runtime. This meant if a Worker returned a redirect to a different origin, that origin would be ignored and the request would still go to the runtime.
  2. The MF-Original-URL header was not cleared/modified during redirects. This meant if a Worker returned a relative/absolute redirect to the same runtime origin, the path would not be updated in the request received by the user.

This change ensures redirects are followed correctly.

  • If your Worker returns a relative redirect or an absolute redirect with the same origin as the original url, the request will be sent to the Worker.
  • If your Worker instead returns an absolute redirect with a different origin, the request will be sent to the Internet.
  • If a redirected request to a different origin returns an absolute redirect with the same origin as the original url, the request will also be sent to the Worker.

Tests have been added to cover the different types of redirect. 👍

Author has addressed the following

  • Tests
    • TODO (before merge)
    • Included
    • Not necessary because:
  • Changeset (Changeset guidelines)
    • TODO (before merge)
    • Included
    • Not necessary because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Not necessary because: redirects working is expected behaviour 😅

@mrbbot mrbbot requested a review from a team as a code owner March 7, 2024 10:57
Copy link

changeset-bot bot commented Mar 7, 2024

🦋 Changeset detected

Latest commit: da7ad8f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
miniflare Patch
@cloudflare/pages-shared Patch
@cloudflare/vitest-pool-workers Patch
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Mar 7, 2024

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8186988941/npm-package-wrangler-5191

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/5191/npm-package-wrangler-5191

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8186988941/npm-package-wrangler-5191 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8186988941/npm-package-create-cloudflare-5191 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8186988941/npm-package-cloudflare-kv-asset-handler-5191
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8186988941/npm-package-miniflare-5191
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8186988941/npm-package-cloudflare-pages-shared-5191
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8186988941/npm-package-cloudflare-vitest-pool-workers-5191

Note that these links will no longer work once the GitHub Actions artifact expires.


wrangler@3.31.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240223.1
workerd 1.20240304.0 1.20240304.0
workerd --version 1.20240304.0 2024-03-04

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

Copy link

codecov bot commented Mar 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.37%. Comparing base (84eeee5) to head (da7ad8f).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5191      +/-   ##
==========================================
+ Coverage   70.35%   70.37%   +0.02%     
==========================================
  Files         298      298              
  Lines       15567    15567              
  Branches     4007     4007              
==========================================
+ Hits        10952    10956       +4     
+ Misses       4615     4611       -4     

see 5 files with indirect coverage changes

@mrbbot mrbbot merged commit 27fb22b into main Mar 8, 2024
15 checks passed
@mrbbot mrbbot deleted the bcoll/fix-dispatch-fetch-redirects branch March 8, 2024 14:03
@workers-devprod workers-devprod mentioned this pull request Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

🐛 BUG: does Miniflare.dispatchFetch handle redirect Response from worker script?
2 participants