Repository navigation
[API] Correct space and plus encoding in W3C Baggage - #4765
hidingmyname wants to merge 7 commits into
Conversation
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
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
mateenali66
left a comment
There was a problem hiding this comment.
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.
Co-authored-by: Mateen Anjum <mateenali66@gmail.com>
mateenali66
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
- A sets baggage
expr=1+1. Per the W3C spec,+is a normal character, so A sendsbaggage: expr=1+1. - B (C++) receives it.
FromHeaderstill decodes+as a space, so B reads the value as1 1. - B forwards the baggage to C:
- Before this PR:
ToHeaderturns the space back into+, so C receivesexpr=1+1. B misread the value itself, but C still gets the right one. - With this PR:
ToHeadersends the space as%20, so C receivesexpr=1%201and reads it as1 1. The value is now wrong for every service after B, not just inside B.
- Before this PR:
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
UrlDecodeno longer has the form-style'+'branch, so a literal+is preserved.- The extraction expectation is
bar+4, notbar 4.
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.
mateenali66
left a comment
There was a problem hiding this comment.
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.
Part of #4745
Changes
Correct W3C Baggage encoding and decoding together:
Baggage::ToHeaderemits a space in a key or value as%20, not form-style+. Literal plus signs continue to be emitted as%2B.Baggage::FromHeaderpreserves a literal+in a key or value instead of decoding it as a space. Percent escapes still decode normally:%20is a space, and%2B/%2bis 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 reads1+1and forwardsexpr=1%2B1, so C also reads1+1. Keeping the old decoder with the new encoder would instead turn the incoming plus into a space and forwardexpr=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
%20and%2Bemitted 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
ValidateExtractHeaderno longer expects the incorrect plus-to-space interpretation.PreserveLiteralPlusOnExtractchecks the exactexpr=1+1example, literal plus signs in keys and values, uppercase/lowercase percent-encoded plus, mixed plus/space input, metadata preservation, and canonical re-encoding/round-tripping.SpaceAndPlusRoundTripInKeysAndValueschecks publicSet→ToHeader→FromHeaderbehavior for spaces, plus signs, and both together.BaggagePropagatorTest.PreserveSpaceAndPlusOnExtractAndInjectchecks the same distinctions through public carrier/context extraction and injection, including the precise forwarded wire header and a second extraction.%20encoding/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:
BaggagePropagatorexecutable 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.baggage_test(16) andbaggage_propagator_test(7) pass in both ABI configurations, built directly against installed GoogleTest 1.18 using C++17 (46 successful test executions).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
d9838a56a562209b30a033f844de9b736ce5aef5had 100 successful checks and 3 failures. All 3 failed while building Conan dependencyabseil/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.mdupdated for non-trivial changesPrepared by omp, an AI coding assistant running in the omp harness, on behalf of @hidingmyname.