Fix BaggageBuilder.put() silently accepting empty string keys - #8660
Fix BaggageBuilder.put() silently accepting empty string keys#8660itsmehotpants wants to merge 2 commits into
Conversation
Per the W3C Baggage spec (§3 definition), a baggage-name must be a
non-empty token. An empty string key is therefore invalid and should
be ignored, not stored and later propagated downstream.
The W3CBaggagePropagator.isValidBaggageKey() already correctly
rejects empty keys when *parsing* incoming headers. This change
closes the same gap on the *programmatic* builder path so that
calling Baggage.builder().put("", value).build() is a no-op,
consistent with how null keys are handled today.
- ImmutableBaggage.Builder.put(): add key.isEmpty() guard
- BaggageBuilder.java: document the empty-key contract in Javadoc
- ImmutableBaggageTest: correct put_keyEmpty to assert the right
behaviour; add put_keyEmpty_withMetadata variant
Fixes open-telemetry#8657
|
|
Pull request dashboard statusWaiting on the author · refreshed 2026-08-07 07:35 UTC Respond to 1 review item (e.g. link a commit, explain why not, ask a follow-up):
Status above doesn't look right?
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8660 +/- ##
============================================
+ Coverage 91.46% 91.48% +0.01%
+ Complexity 10457 10456 -1
============================================
Files 1021 1021
Lines 27647 27647
Branches 3242 3242
============================================
+ Hits 25288 25293 +5
+ Misses 1616 1611 -5
Partials 743 743 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| @Override | ||
| public BaggageBuilder put(String key, String value, BaggageEntryMetadata entryMetadata) { | ||
| if ((key == null) || (value == null) || (entryMetadata == null)) { | ||
| if ((key == null) || key.isEmpty() || (value == null) || (entryMetadata == null)) { |
There was a problem hiding this comment.
isEmpty() only checks for an empty string, but doesn't account for whitespace. According to the spec, a key cannot consist solely of whitespace, so maybe isBlank() should be used instead?
|
Hi @itsmehotpants — just a friendly reminder that this pull request is waiting on you. There are still items that need your attention. See the dashboard status comment for the full list. You don't need to push a code change to hand it back — replying to move each discussion forward is enough, whether that's answering a question, explaining why no change is needed, or asking a follow-up. The dashboard then automatically routes it back to reviewers. If you believe this pull request is incorrectly routed as waiting on the author, comment |
Problem
The W3C Baggage spec requires that a
baggage-namebe a non-empty token (§3 definition). An empty string""is therefore an invalid key and should be silently ignored, the same waynullkeys already are.Currently,
ImmutableBaggage.Builder.put()accepts empty string keys and stores them. When theW3CBaggagePropagatorlater serialises theBaggageinto a header, it includes the empty-key entry, which can produce malformedbaggageheaders like:This corrupts propagation for downstream services.
Inconsistency with the propagator
The parsing path (
W3CBaggagePropagator.isValidBaggageKey) already correctly rejects empty/blank keys when reading incoming headers:This PR closes the same gap on the programmatic builder path.
Change
ImmutableBaggage.Builder.put(): addkey.isEmpty()to the existing early-return null-guardBaggageBuilder.java: document the empty-key contract in Javadoc (links W3C spec)ImmutableBaggageTest: correct the existingput_keyEmptytest (it was asserting the buggy behaviour); addput_keyEmpty_withMetadatavariantFixes #8657