`_headers` drops the headers of earlier blocks when a path appears twice

Open Beginner friendly
#17,139 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
80/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
backend, tooling

Research direction

Start in packages/workers-shared/utils/configuration/constructConfiguration.ts: compare what parseHeaders returns for both blocks with what constructHeaders keeps once it assigns rules[rule.path], then make repeated paths accumulate their header entries instead of being overwritten. The existing configuration tests in the same package are the place to add the two-block repro, and the _redirects duplicate-rule warning shows the message style if you add one. Run the package's test suite, and check createMetadataObject and the Vite/Miniflare consumers still agree. Done means both blocks' headers appear on the response.

Written by the indexing model from the issue text.

Description

When the same path appears in more than one block of a _headers file, only the last block is applied. The headers from the earlier blocks are dropped with no warning.

Repro:

mkdir public && echo hi > public/index.html
printf '/*\n  X-Frame-Options: DENY\n  X-Content-Type-Options: nosniff\n\n/*\n  Cache-Control: public, max-age=3600\n' > public/_headers
echo '{ "name": "t", "compatibility_date": "2025-01-01", "assets": { "directory": "./public" } }' > wrangler.jsonc
npx wrangler dev
curl -i http://localhost:8787/

Expected: the response has x-frame-options, x-content-type-options and cache-control. The docs say a request that matches multiple rules inherits the headers of all of them, and repeated header names inside a single block are already combined.

Actual: only cache-control is returned; the two security headers are gone. _redirects reports Ignoring duplicate rule for path ... for the same situation, _headers says nothing.

Cause: constructHeaders in packages/workers-shared/utils/configuration/constructConfiguration.ts stores rules in an object keyed by path (rules[rule.path] = configuredRule), so a second block for the same path replaces the first. parseHeaders returns both blocks. Pages (createMetadataObject), the Vite plugin and Miniflare all go through constructHeaders.

Reproducible on main at de6b171 and with wrangler 4.167.0.

Dominant language
TypeScript
Stars
4.7k
Forks
1.7k
Avg merge
1d 19h
Merged PRs (30d)
292

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workers-sdk

All issues in cloudflare/workers-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.