Skip to content

fix(core/etcd): nil-deref on response without header field#13361

Merged
nic-6443 merged 2 commits into
apache:masterfrom
nic-6443:fix-etcd-nil-header
May 13, 2026
Merged

fix(core/etcd): nil-deref on response without header field#13361
nic-6443 merged 2 commits into
apache:masterfrom
nic-6443:fix-etcd-nil-header

Conversation

@nic-6443
Copy link
Copy Markdown
Member

Several functions in apisix/core/etcd.lua access res.body.header.revision without checking whether header exists. When the response body lacks an etcd v3 header field — e.g. a reverse proxy returns its own JSON error, or the upstream returns a non-etcd response — this raises attempt to index field 'header' (a nil value).

Since get_format is called from config_etcd.lua during init_worker_by_lua, a malformed response during bootstrap crashes the worker init phase entirely (CrashLoopBackoff in K8s deployments).

This PR adds a get_header_revision() helper that returns nil, err when header or revision is missing, and uses it in all 6 affected call sites in etcd.lua. It also guards the direct res.body.header.revision access in config_etcd.lua's watch init retry loop to log and retry instead of crashing.

get_format() and other etcd wrapper functions access
res.body.header.revision without nil-guard. When the etcd response
body lacks a header field (e.g. proxy/LB injects its own JSON error,
or the upstream returns a non-etcd error), this raises:

    attempt to index field 'header' (a nil value)

Add a get_header_revision() helper that returns nil,err when header or
revision is missing, and use it in all 6 call sites in etcd.lua.
Also guard config_etcd.lua watch init loop to retry instead of crash.
Copilot AI review requested due to automatic review settings May 12, 2026 08:29
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels May 12, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@nic-6443 nic-6443 merged commit 6c988bc into apache:master May 13, 2026
20 of 22 checks passed
@nic-6443 nic-6443 deleted the fix-etcd-nil-header branch May 13, 2026 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants