-
Notifications
You must be signed in to change notification settings - Fork 752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[baggage] baggage item value encoding fix - custom encoder #5292
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/OpenTelemetry.Api/Context/Propagation/W3CBaggageValueEncoder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
|
||
#nullable enable | ||
|
||
namespace OpenTelemetry.Context.Propagation; | ||
|
||
// Encodes baggage values according to the https://www.w3.org/TR/baggage/#value spec. | ||
// This is a modified code of WebUtility.Encode, which handles space char differently - | ||
// it is percent-encoded, instead of being converted to '+'. | ||
// Additionally, allowed characters from baggage-octet range from the spec are not percent-encoded. | ||
// Encoding baggage value with this encoder yields minimal, spec-compliant representation. | ||
|
||
internal static class W3CBaggageValueEncoder | ||
{ | ||
// originated from https://github.com/dotnet/runtime/blob/9429e432f39786e1bcd1c080833e5d7691946591/src/libraries/System.Private.CoreLib/src/System/Net/WebUtility.cs#L368 | ||
|
||
// Modified to percent-encode space char instead of converting it to '+'. | ||
public static string? Encode(string? value) | ||
{ | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
return value; | ||
} | ||
|
||
int safeCount = 0; | ||
for (int i = 0; i < value!.Length; i++) | ||
{ | ||
char ch = value[i]; | ||
if (!RequiresPercentEncoding(ch)) | ||
{ | ||
safeCount++; | ||
} | ||
} | ||
|
||
if (safeCount == value.Length) | ||
{ | ||
// Nothing to expand | ||
return value; | ||
} | ||
|
||
int byteCount = Encoding.UTF8.GetByteCount(value); | ||
int unsafeByteCount = byteCount - safeCount; | ||
int byteIndex = unsafeByteCount * 2; | ||
|
||
// comment originated from https://github.com/dotnet/runtime/blob/9429e432f39786e1bcd1c080833e5d7691946591/src/libraries/System.Private.CoreLib/src/System/Net/WebUtility.cs#L405 | ||
|
||
// Instead of allocating one array of length `byteCount` to store | ||
// the UTF-8 encoded bytes, and then a second array of length | ||
// `3 * byteCount - 2 * unexpandedCount` | ||
// to store the URL-encoded UTF-8 bytes, we allocate a single array of | ||
// the latter and encode the data in place, saving the first allocation. | ||
// We store the UTF-8 bytes to the end of this array, and then URL encode to the | ||
// beginning of the array. | ||
byte[] newBytes = new byte[byteCount + byteIndex]; | ||
Encoding.UTF8.GetBytes(value, 0, value.Length, newBytes, byteIndex); | ||
|
||
GetEncodedBytes(newBytes, byteIndex, byteCount, newBytes); | ||
return Encoding.UTF8.GetString(newBytes); | ||
} | ||
|
||
private static bool RequiresPercentEncoding(char ch) | ||
{ | ||
// The percent char MUST be percent-encoded. | ||
return ch == '%' || !IsInBaggageOctetRange(ch); | ||
} | ||
|
||
private static bool IsInBaggageOctetRange(char ch) | ||
{ | ||
// from the spec: | ||
// Any characters outside of the baggage-octet range of characters MUST be percent-encoded. | ||
// baggage-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E | ||
return ch == '!' || // 0x21 | ||
(ch >= '#' && ch <= '+') || // 0x23-0x2B | ||
(ch >= '-' && ch <= ':') || // 0x2D-0x3A | ||
(ch >= '<' && ch <= '[') || // 0x3C-0x5B | ||
(ch >= ']' && ch <= '~'); // 0x5D-0x7E | ||
} | ||
|
||
// originated from https://github.com/dotnet/runtime/blob/9429e432f39786e1bcd1c080833e5d7691946591/src/libraries/System.Private.CoreLib/src/System/Net/WebUtility.cs#L328 | ||
// Modified to percent-encode the space char. | ||
private static void GetEncodedBytes(byte[] originalBytes, int offset, int count, byte[] expandedBytes) | ||
{ | ||
int pos = 0; | ||
int end = offset + count; | ||
for (int i = offset; i < end; i++) | ||
{ | ||
byte b = originalBytes[i]; | ||
char ch = (char)b; | ||
if (RequiresPercentEncoding(ch)) | ||
{ | ||
expandedBytes[pos++] = (byte)'%'; | ||
expandedBytes[pos++] = (byte)ToCharUpper(b >> 4); | ||
expandedBytes[pos++] = (byte)ToCharUpper(b); | ||
} | ||
else | ||
{ | ||
expandedBytes[pos++] = b; | ||
} | ||
} | ||
} | ||
|
||
// originated from https://github.com/dotnet/runtime/blob/9429e432f39786e1bcd1c080833e5d7691946591/src/libraries/Common/src/System/HexConverter.cs#L203 | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static char ToCharUpper(int value) | ||
{ | ||
value &= 0xF; | ||
value += '0'; | ||
|
||
if (value > '9') | ||
{ | ||
value += 'A' - ('9' + 1); | ||
} | ||
|
||
return (char)value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Baggage item keys should meet
token
requirement from the specification, and not be encoded.I can address that in a separate PR.