Skip to content

fix: add guard against the ArrayIndexOutOfBoundsException in BaggageCodec… #7239

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 @@ -44,13 +44,17 @@
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b == ESCAPE_CHAR) {
try {
int u = digit16(bytes[++i]);
int l = digit16(bytes[++i]);
buffer.write((char) ((u << 4) + l));
} catch (ArrayIndexOutOfBoundsException e) { // FIXME
throw new IllegalArgumentException("Invalid URL encoding: ", e);
if (i + 1 >= bytes.length) {
return buffer.toByteArray();

Check warning on line 48 in api/all/src/main/java/io/opentelemetry/api/baggage/propagation/BaggageCodec.java

View check run for this annotation

Codecov / codecov/patch

api/all/src/main/java/io/opentelemetry/api/baggage/propagation/BaggageCodec.java#L48

Added line #L48 was not covered by tests
}
int u = digit16(bytes[++i]);

if (i + 1 >= bytes.length) {
return buffer.toByteArray();
}
int l = digit16(bytes[++i]);

buffer.write((char) ((u << 4) + l));
} else {
buffer.write(b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package io.opentelemetry.api.baggage.propagation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatNoException;

import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -36,7 +36,6 @@ void shouldDecodePercentEncodedValues(String percentEncoded, String expectedDeco

@Test
void shouldThrowIfMalformedData() {
Copy link
Contributor

@breedx-splk breedx-splk Apr 2, 2025

Choose a reason for hiding this comment

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

Let's rename this because the behavior has changed. 👍🏻

assertThatThrownBy(() -> BaggageCodec.decode("%1", StandardCharsets.UTF_8))
.isInstanceOf(IllegalArgumentException.class);
assertThatNoException().isThrownBy(() -> BaggageCodec.decode("%1", StandardCharsets.UTF_8));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also add a few chars and make sure that they come through in the output? In other words, lets assert on the result in addition to no exception being thrown. Thanks!

}
}
Loading