Skip to content

client: WithHTTPHeaders: improve doc, add test coverage, and error on duplicate headers#52204

Merged
thaJeztah merged 2 commits intomoby:masterfrom
thaJeztah:stable_customheaders
Mar 30, 2026
Merged

client: WithHTTPHeaders: improve doc, add test coverage, and error on duplicate headers#52204
thaJeztah merged 2 commits intomoby:masterfrom
thaJeztah:stable_customheaders

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

@thaJeztah thaJeztah commented Mar 23, 2026

The behavior of this option was under-documented, and there's an issue with duplicate headers leading to unexpected behavior; changing that is probably not suitable for a patch release, so let's start with adding test-coverage, and adding a TODO for a future release.

- What I did

- How I did it

- How to verify it

- Human readable description for the release notes

Go SDK: client.WithHTTPHeaders now detects if duplicate headers are set, and produces an error. Previously, duplicate errors would be randomized, resulting in undefined behavior.

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah added status/2-code-review kind/refactor PR's that refactor, or clean-up code module/client labels Mar 23, 2026
Comment thread client/client_options.go
Comment on lines +206 to +215
c.customHTTPHeaders = make(map[string]string)
for k, v := range headers {
k = http.CanonicalHeaderKey(k)
_, ok := c.customHTTPHeaders[k]
if ok {
// TODO(thaJeztah): consider returning an error on duplicates
continue
}
c.customHTTPHeaders[k] = v
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is preserving the current behavior, but removing duplicates early; before this patch, duplicates would be silently discarded when we set the headers (headers.Set normalises the header-name, so overwriting earlier values);

moby/client/request.go

Lines 310 to 314 in 086a6eb

// Add CLI Config's HTTP Headers BEFORE we set the Docker headers
// then the user can't change OUR headers
for k, v := range cli.customHTTPHeaders {
req.Header.Set(k, v)
}

@thaJeztah thaJeztah force-pushed the stable_customheaders branch from d733957 to 61e54bb Compare March 24, 2026 22:22
@thaJeztah thaJeztah added this to the 29.3.2 milestone Mar 25, 2026
Comment thread client/client_options.go Outdated
k = http.CanonicalHeaderKey(k)
_, ok := c.customHTTPHeaders[k]
if ok {
// TODO(thaJeztah): consider returning an error on duplicates
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.

So this doesn't change the current behavior and they are still discarded silently right?

So

Accept: application/json
Accept: text/plain

would silently still produce unexpected results.

I think we should just bite the bullet and return an error in this PR already.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, didn't want to do it if it's a patch release; can do a quick follow-up as well to make it more visible

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

do your example; it would be random when using mixed casing; ACCEPT and Accept (eg) in the same map provided here. Probably very corner case if someone would set that

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Updated; added an error if duplicates are detected.

The behavior of this option was under-documented, and there's an
issue with duplicate headers leading to unexpected behavior;
changing that is probably not suitable for a patch release, so
let's start with adding test-coverage, and adding a TODO for
a future release.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Previously, WithHTTPHeaders would silently discard duplicate headers
(after normalizing). Produce an error instead to prevent unexpected
behavior.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah force-pushed the stable_customheaders branch from 61e54bb to 28aeae2 Compare March 29, 2026 11:42
@thaJeztah thaJeztah added impact/changelog area/go-sdk impact/go-sdk Noteworthy (compatibility changes) in the Go SDK labels Mar 29, 2026
@thaJeztah thaJeztah changed the title client: WithHTTPHeaders: improve doc and add test coverage client: WithHTTPHeaders: improve doc, add test coverage, and error on duplicate headers Mar 29, 2026
@thaJeztah thaJeztah requested a review from vvoland March 30, 2026 08:28
@thaJeztah thaJeztah merged commit afb0cac into moby:master Mar 30, 2026
182 of 185 checks passed
@thaJeztah thaJeztah deleted the stable_customheaders branch March 30, 2026 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/go-sdk impact/changelog impact/go-sdk Noteworthy (compatibility changes) in the Go SDK kind/refactor PR's that refactor, or clean-up code module/client status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants