Skip to content

Fix TraceState key validation limits to match W3C spec #7575

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

final class ArrayBasedTraceStateBuilder implements TraceStateBuilder {

private static final int MAX_VENDOR_ID_SIZE = 13;
private static final int MAX_VENDOR_ID_SIZE = 14;

// Needs to be in this class to avoid initialization deadlock because super class depends on
// subclass (the auto-value generate class).
Expand All @@ -24,7 +24,7 @@ final class ArrayBasedTraceStateBuilder implements TraceStateBuilder {
private static final int MAX_ENTRIES = 32;
private static final int KEY_MAX_SIZE = 256;
private static final int VALUE_MAX_SIZE = 256;
private static final int MAX_TENANT_ID_SIZE = 240;
private static final int MAX_TENANT_ID_SIZE = 241;

// Later calls to put must be at the front of trace state. We append to the list and then reverse
// when finished.
Expand Down Expand Up @@ -157,11 +157,11 @@ private static boolean isKeyValid(@Nullable String key) {
return false;
}
isMultiTenantVendorKey = true;
// tenant id (the part to the left of the '@' sign) must be 240 characters or less
// tenant id (the part to the left of the '@' sign) must be 241 characters or less
if (i > MAX_TENANT_ID_SIZE) {
return false;
}
// vendor id (the part to the right of the '@' sign) must be 1-13 characters long
// vendor id (the part to the right of the '@' sign) must be 1-14 characters long
int remainingKeyChars = key.length() - i - 1;
if (remainingKeyChars > MAX_VENDOR_ID_SIZE || remainingKeyChars == 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,39 @@ void testValidAtSignVendorNamePrefix() {
}

@Test
void testVendorIdLongerThan13Characters() {
assertThat(TraceState.builder().put("1@nrabcdefghijkl", FIRST_VALUE).build())
void testVendorIdWith14Characters() {
String key = "1@nrabcdefghijkl";
Copy link
Member

Choose a reason for hiding this comment

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

this is 16 chars

Copy link
Author

@priettt priettt Aug 18, 2025

Choose a reason for hiding this comment

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

Multi-tenant keys are formed with: a tenant-id, the "@" symbol, and a system-id (called vendor-id in the otel-java codebase).

This test verifies behavior for a system-id of 14 chars, so what follows the @ should be that long

assertThat(TraceState.builder().put(key, FIRST_VALUE).build().get(key)).isEqualTo(FIRST_VALUE);
}

@Test
void testVendorIdLongerThan14Characters() {
assertThat(TraceState.builder().put("1@nrabcdefghijklm", FIRST_VALUE).build())
.isEqualTo(TraceState.getDefault());
}

@Test
void testVendorIdLongerThan13Characters_longTenantId() {
assertThat(TraceState.builder().put("12345678901234567890@nrabcdefghijkl", FIRST_VALUE).build())
void testVendorIdLongerThan14Characters_longTenantId() {
assertThat(
TraceState.builder().put("12345678901234567890@nrabcdefghijklm", FIRST_VALUE).build())
.isEqualTo(TraceState.getDefault());
}

@Test
void tenantIdLongerThan240Characters() {
void tenantIdWith241Characters() {
char[] chars = new char[241];
Arrays.fill(chars, 'a');
String tenantId = new String(chars);
assertThat(
TraceState.builder().put(tenantId + "@nr", FIRST_VALUE).build().get(tenantId + "@nr"))
.isEqualTo(FIRST_VALUE);
}

@Test
void tenantIdLongerThan241Characters() {
char[] chars = new char[242];
Arrays.fill(chars, 'a');
String tenantId = new String(chars);
assertThat(TraceState.builder().put(tenantId + "@nr", FIRST_VALUE).build())
.isEqualTo(TraceState.getDefault());
}
Expand Down
Loading