Skip to content

Fix BaggageBuilder.put() silently accepting empty string keys#8660

Open
itsmehotpants wants to merge 2 commits into
open-telemetry:mainfrom
itsmehotpants:fix/baggage-builder-empty-key-validation
Open

Fix BaggageBuilder.put() silently accepting empty string keys#8660
itsmehotpants wants to merge 2 commits into
open-telemetry:mainfrom
itsmehotpants:fix/baggage-builder-empty-key-validation

Conversation

@itsmehotpants

Copy link
Copy Markdown

Problem

The W3C Baggage spec requires that a baggage-name be a non-empty token (§3 definition). An empty string "" is therefore an invalid key and should be silently ignored, the same way null keys already are.

Currently, ImmutableBaggage.Builder.put() accepts empty string keys and stores them. When the W3CBaggagePropagator later serialises the Baggage into a header, it includes the empty-key entry, which can produce malformed baggage headers like:

baggage: =somevalue, valid-key=other

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:

// W3CBaggagePropagator.java
private static boolean isValidBaggageKey(String name) {
  return name != null && !name.trim().isEmpty() && StringUtils.isPrintableString(name);
}

This PR closes the same gap on the programmatic builder path.

Change

  • ImmutableBaggage.Builder.put(): add key.isEmpty() to the existing early-return null-guard
  • BaggageBuilder.java: document the empty-key contract in Javadoc (links W3C spec)
  • ImmutableBaggageTest: correct the existing put_keyEmpty test (it was asserting the buggy behaviour); add put_keyEmpty_withMetadata variant

Fixes #8657

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
@itsmehotpants
itsmehotpants requested a review from a team as a code owner July 25, 2026 07:58
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 25, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: itsmehotpants / name: itsmehotpants (a669383)

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Jul 25, 2026

Copy link
Copy Markdown

Pull request dashboard status

Status last refreshed: 2026-07-26 18:31:14 UTC.

  • Waiting on: Reviewers
  • Next step: Review the latest changes.

This automated status or its linked feedback items may be incorrect. If something looks wrong, please report it with the result you expected.

@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.48%. Comparing base (6678f8e) to head (0b9a556).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Fix: BaggageBuilder.put() accepts empty string keys instead of ignoring them

1 participant