Skip to content

[API] Correct space and plus encoding in W3C Baggage - #4765

Open
hidingmyname wants to merge 7 commits into
open-telemetry:mainfrom
hidingmyname:baggage-encode-space-percent-4745
Open

hidingmyname wants to merge 7 commits into
open-telemetry:mainfrom
hidingmyname:baggage-encode-space-percent-4745

Conversation

@hidingmyname

@hidingmyname hidingmyname commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Part of #4745

Changes

Correct W3C Baggage encoding and decoding together:

  • Baggage::ToHeader emits a space in a key or value as %20, not form-style +. Literal plus signs continue to be emitted as %2B.
  • Baggage::FromHeader preserves a literal + in a key or value instead of decoding it as a space. Percent escapes still decode normally: %20 is a space, and %2B/%2b is a plus sign. Metadata remains in its existing wire form.

This addresses the Go A → C++ B → Go C example from the review. For incoming baggage: expr=1+1, B now reads 1+1 and forwards expr=1%2B1, so C also reads 1+1. Keeping the old decoder with the new encoder would instead turn the incoming plus into a space and forward expr=1%201.

Compatibility tradeoff

Older C++ senders encoded spaces as +. A newer C++ receiver now reads those bytes as literal plus signs, just as conforming non-C++ receivers already do. It cannot distinguish a legacy encoded space from an intentional plus. This deliberately changes old-C++ → new-C++ behavior rather than retaining the ambiguous form-style decoder. Applications that need to send spaces to newer receivers should use a sender that emits %20.

The other direction remains readable: older C++ receivers already decode both %20 and %2B emitted by newer C++ senders. This is not a claim that every mixed-version direction is unchanged, nor that mixed-header definitions in one process are ABI-safe.

CHANGELOG.md, the public API getting-started documentation, and the public method comments explain the paired behavior and the compatibility cost.

Regression coverage

  • ValidateExtractHeader no longer expects the incorrect plus-to-space interpretation.
  • PreserveLiteralPlusOnExtract checks the exact expr=1+1 example, literal plus signs in keys and values, uppercase/lowercase percent-encoded plus, mixed plus/space input, metadata preservation, and canonical re-encoding/round-tripping.
  • SpaceAndPlusRoundTripInKeysAndValues checks public Set → ToHeader → FromHeader behavior for spaces, plus signs, and both together.
  • BaggagePropagatorTest.PreserveSpaceAndPlusOnExtractAndInject checks the same distinctions through public carrier/context extraction and injection, including the precise forwarded wire header and a second extraction.
  • Existing %20 encoding/decoding coverage remains.

ABI policy: still unresolved

Both corrections change observable behavior of inline API-header code. docs/abi-policy.md, “Definitions in API headers,” requires the experimental ABI for such changes unless maintainers accept a critical correctness/security exception. The question in #4765 (comment) remains open. This update does not decide that exception, change ABI policy, or introduce an arbitrary ABI guard. The existing ungated proposal remains pending that maintainer decision.

Validation status

The updated code has now been validated locally:

  • The standalone public BaggagePropagator executable reproduces the exact corruption before the change (1 1, expr=1%201) and passes after it (1+1, expr=1%2B1), including space/plus metadata round trips; compiled with C++14 in ABI 1 and ABI 2 namespace configurations.
  • Complete baggage_test (16) and baggage_propagator_test (7) pass in both ABI configurations, built directly against installed GoogleTest 1.18 using C++17 (46 successful test executions).
  • Composite (2), global (3) and environment-carrier (17) propagation tests pass (22 more tests).
  • The changed C++ files are formatted and checked with clang-format 18.1.8, matching the CI major version.

These are direct compiler/GTest runs, not a full CMake/Conan build or full SDK validation. The older remote head's three failing jobs stop inside Conan abseil/CMake dependency configuration before baggage tests, as described below. ABI1/2 test success does not resolve ABI policy.

The previously checked remote head d9838a56a562209b30a033f844de9b736ce5aef5 had 100 successful checks and 3 failures. All 3 failed while building Conan dependency abseil/20250127.0, in a CMake 4.5 compiler ABI probe (CMAKE_CXX_COMPILER not set, after EnableLanguage), rather than in baggage compilation or tests. No unrelated Conan CI changes or remote reruns are included.

  • CHANGELOG.md updated for non-trivial changes
  • Regression tests added
  • Revised direct-compiler tests, public smoke and formatting validated (full SDK/CMake/Conan build not run)
  • Inline API/ABI exception or experimental-ABI decision accepted by maintainers

Prepared by omp, an AI coding assistant running in the omp harness, on behalf of @hidingmyname.

Baggage::ToHeader encoded a space as '+' (form-style), which the W3C
Baggage specification does not define; other implementations read it as
a literal '+'. Encode spaces as %20 instead. Existing C++ readers already
decode %20, so this is safe across versions.

Decoding of '+' as space is intentionally left unchanged.

Part of open-telemetry#4745
@hidingmyname
hidingmyname requested a review from a team as a code owner November 5, 2026 08:08
@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.69%. Comparing base (b013f71) to head (8412e51).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4765      +/-   ##
==========================================
- Coverage   86.70%   86.69%   -0.00%     
==========================================
  Files         530      530              
  Lines       21204    21202       -2     
==========================================
- Hits        18382    18380       -2     
  Misses       2822     2822              
Files with missing lines Coverage Δ
api/include/opentelemetry/baggage/baggage.h 98.74% <ø> (-0.03%) ⬇️

... and 1 file with indirect coverage changes

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

@mateenali66 mateenali66 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks for the fix.

ValidateInjectHeader and SpaceIsPercentEncoded fail with the + branch put back and pass at 7aa97087, and + still decodes as a space.

See minor comment on the CHANGELOG link.

Comment thread CHANGELOG.md Resolved

@mateenali66 mateenali66 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fix.

The code and tests are fine, but my earlier approval did not cover the ABI policy.

See the question on ToHeader being an inline function in api/include.

{
ret.push_back('+');
}
else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Dropping the space branch changes the observable behavior of an inline function in api/include.

docs/abi-policy.md, "Definitions in API headers", puts such changes in the experimental ABI, with correctness fixes as a possible exception.

#4717 changed API behavior without a guard, so the exception may apply here.

A mixed-version process emits either + or %20, and this reader decodes both.

Is the ungated change acceptable, @marcalff @lalitb?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The decoder is now included in the proposed correction following #4765 (comment): %20 represents a space, while a literal + is preserved rather than decoded as a space. Keeping the old decoder with only the new encoder corrupts expr=1+1 when C++ forwards it: it becomes expr=1%201. With the paired correction it remains 1+1 in C++ and is forwarded as expr=1%2B1.

That means the earlier compatibility argument needs qualification. Older C++ readers already understand %20 and %2B, but newer C++ readers will interpret old C++ senders' form-style + spaces as literal plus signs. This tradeoff is now explicit in the changelog/docs and covered by public Baggage/propagator regression cases. It is not safe to claim unchanged mixed-version behavior in both directions.

The ABI question itself is still unresolved. Both changes alter inline definitions and observable behavior in api/include; docs/abi-policy.md explicitly covers that, including the possibility of a critical correctness exception. Wire-format compatibility does not remove the mixed-header-definition/ODR concern in one process. Commit 8412e51 keeps the current ungated proposal without changing policy or asserting an approved exception. A maintainer decision is still required between accepting the correctness exception and restricting the paired correction to the experimental ABI; this reply does not mark that decision resolved.

Prepared by omp, an AI coding assistant running in the omp harness, on behalf of @hidingmyname.

@marcalff marcalff self-assigned this Nov 16, 2026

@denizariyan denizariyan Nov 21, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The decode side is intentionally unchanged: FromHeader still decodes + as a space, so headers from older C++ senders keep reading the same way. Whether to stop treating + as a space is left as a maintainer decision; I'm happy to follow up with a separate PR for that if wanted.

I think changing only the encode side makes things worse when C++ sits in the middle of a call chain.

Example: Service A (Go) -> Service B (C++) -> Service C (Go).

  1. A sets baggage expr=1+1. Per the W3C spec, + is a normal character, so A sends baggage: expr=1+1.
  2. B (C++) receives it. FromHeader still decodes + as a space, so B reads the value as 1 1.
  3. B forwards the baggage to C:
    • Before this PR: ToHeader turns the space back into +, so C receives expr=1+1. B misread the value itself, but C still gets the right one.
    • With this PR: ToHeader sends the space as %20, so C receives expr=1%201 and reads it as 1 1. The value is now wrong for every service after B, not just inside B.

The way to avoid this is to also stop decoding + as a space (line 354). The cost is that spaces sent by older C++ versions (as +) would be read as + by newer C++ services. But non-C++ receivers already read them that way today, so this only affects C++ to C++ calls between different versions and fixes the issue at once for newer versions.

IMO the better trade off is to change both sides in a single PR/release.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed: the encode-only change can turn B's existing misinterpretation into a persistent downstream corruption. The proposed update now changes both sides together: spaces are encoded as %20, and a literal + is no longer decoded as a space.

For your exact example, incoming expr=1+1 now remains 1+1 inside C++ B and is forwarded as expr=1%2B1, so a conforming receiver C reads 1+1. Incoming %20 still becomes a space, and %2B/%2b still becomes a plus. The same rules apply to keys and values; metadata remains in wire form.

I changed the old extraction expectation that deliberately treated bar+4 as bar 4 and added public Baggage tests plus carrier/context BaggagePropagator extraction/injection coverage, including the forwarded header for expr=1+1, round-trips, key/value plus signs and spaces, and unchanged metadata. The changelog and public docs now explain the real tradeoff: spaces emitted as + by older C++ senders become literal plus signs at newer C++ receivers. There is no way to distinguish those legacy spaces from intentional plus signs; non-C++ receivers already face that behavior. Older C++ receivers can read the newer sender's %20 and %2B.

This supersedes the earlier encode-only description. The separate inline-API ABI-policy decision in #4765 (comment) remains unresolved; I have not assumed an exception or introduced an arbitrary ABI guard. The paired correction is pushed in 8412e51. The exact public-propagator example fails before and passes after in C++14; 16 baggage + 7 propagator tests pass in each of ABI1/ABI2 configurations, with 22 more propagation tests passing. The changed C++ files pass clang-format 18.1.8. These direct tests do not settle the ABI-policy question or claim full SDK/CMake validation.

Prepared by omp, an AI coding assistant running in the omp harness, on behalf of @hidingmyname.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@denizariyan

Agreed, my approval and the #4745 triage missed the forwarding case.

B decodes the header and calls ToHeader(), built at d9838a56:

A sends            main forwards      this PR forwards   both sides changed
k=1+1              k=1+1              k=1%201            k=1%2B1
k=hello%20world    k=hello+world      k=hello%20world    k=hello%20world

Main and this PR each corrupt one of the two values for every service after B, and only changing both sides forwards both.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The paired correction is already present in the current head, 8412e51e6dc721a9c7a7e20b91f220d07bba9295; the forwarding table above was built from the earlier encode-only d9838a56.

The two requested edits are included:

With that head, expr=1+1 extracts as 1+1 and forwards as expr=1%2B1; hello%20world remains a space value and forwards with %20. The public forwarding regression failed on d9838a56 and passed on 8412e51, as recorded in the earlier reply. All 16 baggage and 7 baggage-propagator tests also passed in the current-head macOS job; that job's failure is in the SDK metrics multi-reader gauge timestamp assertions, not baggage.

The inline-API ABI-policy question for @marcalff / @lalitb remains unresolved. This reply only identifies the implemented semantic correction; it does not assume an ABI exception or mark the review thread resolved.

Posted by the omp AI coding assistant in the omp harness on behalf of @hidingmyname.

Pair percent-space encoding with literal-plus-preserving extraction to avoid corrupting expr=1+1 on forwarding. Cover public baggage and propagator round trips and document legacy C++ sender compatibility. Inline API ABI-policy approval remains pending.
@hidingmyname hidingmyname changed the title [API] Percent-encode spaces in Baggage::ToHeader [API] Correct space and plus encoding in W3C Baggage Nov 23, 2026

@mateenali66 mateenali66 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My approval missed the forwarding case, see the reply to @denizariyan on baggage.h.

With UrlDecode still turning + into a space, a C++ hop now forwards 1+1 as 1%201.

Please also drop the '+' branch in UrlDecode, and expect bar+4 at baggage_test.cc:204.

Whether the API change can go in ungated is still the open question for @marcalff and @lalitb.

This branch has not been deployed

No deployments
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.

5 participants