Skip to content

feat(miniflare): add CF-Worker header to outgoing fetch requests#12186

Merged
penalosa merged 3 commits intomainfrom
devin/1769516338-cf-worker-header
Jan 30, 2026
Merged

feat(miniflare): add CF-Worker header to outgoing fetch requests#12186
penalosa merged 3 commits intomainfrom
devin/1769516338-cf-worker-header

Conversation

@penalosa
Copy link
Copy Markdown
Contributor

@penalosa penalosa commented Jan 27, 2026

Fixes #4367.

Adds the CF-Worker header to outgoing fetch requests in Miniflare's local development environment to match production behavior. In production, Cloudflare adds this header to subrequests made by Workers, with the value set to the zone/domain.

The implementation reuses the existing strip-cf-connecting-ip wrapper worker that already intercepts outbound requests. A new optional zone option is exposed to allow specifying the header value. Wrangler now passes the zone value (extracted from the configured routes/host) to Miniflare automatically.

Header value behavior:

  • If zone option is provided: uses that value (e.g., example.com)
  • If zone is not provided but name is: uses ${name}.example.com (e.g., my-worker.example.com)
  • If neither is provided: uses worker.example.com

Updates since last revision:

  • Wrangler now passes the zone value to Miniflare via ConfigBundle.zone, extracted from dev.origin.hostname (which comes from routes configuration)

Human review checklist:

  • Verify that event.config.dev?.origin?.hostname is the correct source for the zone value in Wrangler
  • Consider if there are edge cases where the origin hostname might differ from the actual zone

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: This aligns local behavior with existing production behavior that is already documented.

Link to Devin run: https://app.devin.ai/sessions/3c5448d105c44a5fbbc723f94bbf35b8
Devin PR requested by @penalosa


Open with Devin

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 27, 2026

🦋 Changeset detected

Latest commit: fd5fb0f

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

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

@devin-ai-integration
Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI' or '@devin'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jan 27, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@12186

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@12186

miniflare

npm i https://pkg.pr.new/miniflare@12186

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@12186

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@12186

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@12186

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@12186

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@12186

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@12186

wrangler

npm i https://pkg.pr.new/wrangler@12186

commit: fd5fb0f

@penalosa
Copy link
Copy Markdown
Contributor Author

The Cf-worker header should be set to the zone. Miniflare should expose an (optional) option to pass in a zone, which Wrangler will use. Additionally, Miniflare should default cf-worker to ${worker-name}.example.com if no zone is passed in

@penalosa
Copy link
Copy Markdown
Contributor Author

Devin, please make this change

@penalosa penalosa force-pushed the devin/1769516338-cf-worker-header branch from 5825c4c to 232e8d0 Compare January 28, 2026 17:57
@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Jan 28, 2026

Claude finished @penalosa's task —— View job


Changeset Review

Todo List:

  • Read changeset guidelines from .changeset/README.md
  • Read and validate .changeset/cf-worker-header.md
  • Check version type appropriateness
  • Verify changelog quality
  • Check for markdown headers (h1/h2/h3)
  • Verify no analytics concerns

All changesets look good

The changeset .changeset/cf-worker-header.md is properly formatted:

  • Version type: minor is appropriate for this new configuration option
  • Changelog quality: Clear description with explanation of the new zone option and default behavior
  • No markdown headers: No h1/h2/h3 headers present
  • Package coverage: Correctly targets miniflare package
  • Format: Follows the title + body format with good detail

@penalosa
Copy link
Copy Markdown
Contributor Author

Devin, it doesn't look like Wrangler passes the zone value in this PR

@penalosa penalosa marked this pull request as ready for review January 29, 2026 14:41
@penalosa penalosa requested a review from a team as a code owner January 29, 2026 14:41
@penalosa penalosa force-pushed the devin/1769516338-cf-worker-header branch from 32dc9de to f95ae25 Compare January 29, 2026 14:41
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional flags.

Open in Devin Review

export default {
fetch(request) {
fetch(request, env: Env) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit pick: Not needed if we're using satisfies ExportedHandler<Env>

Suggested change
fetch(request, env: Env) {
fetch(request, env) {

@github-project-automation github-project-automation Bot moved this from Untriaged to Approved in workers-sdk Jan 29, 2026
devin-ai-integration Bot and others added 3 commits January 29, 2026 18:21
Adds the CF-Worker header to outgoing fetch requests in local development
to match production behavior. The header value is set to the worker's name.

Fixes #4367

Co-Authored-By: smacleod@cloudflare.com <samuel@macleod.space>
- Add optional 'zone' option to Miniflare worker options
- CF-Worker header uses zone value when provided
- Default to ${worker-name}.example.com when zone not specified
- Update tests to verify new behavior

Co-Authored-By: smacleod@cloudflare.com <samuel@macleod.space>
Co-Authored-By: smacleod@cloudflare.com <samuel@macleod.space>
@penalosa penalosa force-pushed the devin/1769516338-cf-worker-header branch from f95ae25 to fd5fb0f Compare January 29, 2026 18:21
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

View issue and 6 additional flags in Devin Review.

Open in Devin Review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 CF-Worker header not added when stripCfConnectingIp is disabled

The CF-Worker header is only added to outbound requests when stripCfConnectingIp is true (the default). When a user explicitly sets stripCfConnectingIp: false, the CF-Worker header is not added, which doesn't match production behavior where CF-Worker is always added to subrequests.

Click to expand

Root Cause

The CF-Worker header logic is coupled with the strip-cf-connecting-ip worker at packages/miniflare/src/plugins/core/index.ts:851-853:

globalOutbound: options.stripCfConnectingIp
    ? { name: getStripCfConnectingIpName(workerIndex) }
    : getGlobalOutbound(workerIndex, options),

When stripCfConnectingIp is false, the strip-cf-connecting-ip worker is bypassed entirely, and the CF-Worker header (set at line 9 in strip-cf-connecting-ip.worker.ts) is never added.

Impact

Users who disable CF-Connecting-IP stripping for testing purposes will also lose the CF-Worker header on outbound requests, which may cause their local development environment to behave differently from production in unexpected ways.

Actual vs Expected

  • Actual: When stripCfConnectingIp: false, no CF-Worker header is added
  • Expected: CF-Worker header should be added regardless of the stripCfConnectingIp setting to match production behavior

(Refers to lines 851-853)

Recommendation: Consider decoupling the CF-Worker header logic from the CF-Connecting-IP stripping, or rename the worker to reflect its combined purpose and add a separate option for controlling the CF-Worker header.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine. stripCfConnectingIp really means "this miniflare instance is a proxy and shouldn't do weird things to requests" which applies to both stripCfConnectingIp and adding a CF Worker header

@penalosa penalosa merged commit 0c9625a into main Jan 30, 2026
39 checks passed
@penalosa penalosa deleted the devin/1769516338-cf-worker-header branch January 30, 2026 15:55
@github-project-automation github-project-automation Bot moved this from Approved to Done in workers-sdk Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Miniflare] cf-worker header in local development

3 participants