Skip to content

fix(core): invalidate ctx.var for every spelling of a modified header - #14209

Open
nic-6443 wants to merge 1 commit into
apache:masterfrom
nic-6443:fix/request-header-var-cache-invalidation
Open

nic-6443 wants to merge 1 commit into
apache:masterfrom
nic-6443:fix/request-header-var-cache-invalidation

Conversation

@nic-6443

Copy link
Copy Markdown
Member

Description

core.request.set_header / add_header (and clearing a header with nil) are supposed to drop the cached ctx.var value of that header, but the value read through the usual spelling stays stale.

modify_header clears ctx.var["http_" .. str_lower(header_name)], i.e. http_x-foo for X-Foo. ctx.var however caches http_* lookups under whatever key the caller used, and route vars, plugins and most code read the underscore form http_x_foo. So this sequence returns the old value:

local v1 = ctx.var.http_x_foo              -- "old", now cached under "http_x_foo"
core.request.set_header(ctx, "X-Foo", "new")
local v2 = ctx.var.http_x_foo              -- still "old"

The same happens after core.request.set_header(ctx, "X-Foo", nil): a header removed before proxying is still visible to every later phase that reads ctx.var.http_x_foo, and a route whose vars matched on that header has already put it in the cache.

Since nginx treats $http_* names case-insensitively and with - equal to _, any spelling (http_x_foo, http_x-foo, http_X-Foo, ...) can end up as its own cache key, so clearing one more spelling in modify_header would still leave the others. Instead, ctx.var now stores every http_* entry, on read and on write, under the normalized key (lower case, - replaced with _), the same name it already used to fetch the nginx variable. Each header then has a single cache entry, and the existing invalidation in modify_header (which goes through __newindex) clears it whatever spelling the caller passes. Lookups with the normalized spelling still hit the cache directly; other spellings pay one lower/gsub before hitting the shared entry.

A test in t/core/request.t reads the header under three spellings after set, add, clear and a direct ctx.var write; it fails without this change.

No documentation describes this caching, so there is nothing to update in docs/.

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

Copilot AI balanced review requested due to automatic review settings November 16, 2026 10:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants