From 0df46f56e0f4891eb245cc7d6c4a41e243e95452 Mon Sep 17 00:00:00 2001 From: Benito Visone Date: Mon, 18 Dec 2023 07:09:45 +0100 Subject: [PATCH] Fixes java.lang.IllegalStateException: Missing: traceId with AWS propagation type within AWSExtractor (#203) * add test to exploit issue #201 * add fix for #201 and #202 with corresponding unit tests --- .../java/brave/propagation/aws/AWSExtractor.java | 3 +++ .../brave/propagation/aws/AWSPropagationTest.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/brave/propagation-aws/src/main/java/brave/propagation/aws/AWSExtractor.java b/brave/propagation-aws/src/main/java/brave/propagation/aws/AWSExtractor.java index ffbf9c1b..a5736022 100644 --- a/brave/propagation-aws/src/main/java/brave/propagation/aws/AWSExtractor.java +++ b/brave/propagation-aws/src/main/java/brave/propagation/aws/AWSExtractor.java @@ -124,6 +124,7 @@ public TraceContextOrSamplingFlags extract(R request) { } else if (c >= 'a' && c <= 'f') { traceIdHigh |= c - 'a' + 10; } else { + traceIdHigh = 0L; break OUTER; // invalid format } } @@ -136,6 +137,8 @@ public TraceContextOrSamplingFlags extract(R request) { } else if (c >= 'a' && c <= 'f') { traceId |= c - 'a' + 10; } else { + traceIdHigh = 0L; + traceId = 0L; break OUTER; // invalid format } } diff --git a/brave/propagation-aws/src/test/java/brave/propagation/aws/AWSPropagationTest.java b/brave/propagation-aws/src/test/java/brave/propagation/aws/AWSPropagationTest.java index a013ada3..4975c0f0 100644 --- a/brave/propagation-aws/src/test/java/brave/propagation/aws/AWSPropagationTest.java +++ b/brave/propagation-aws/src/test/java/brave/propagation/aws/AWSPropagationTest.java @@ -296,4 +296,18 @@ public void extract_skips_really_malformed() { assertThat(extractor.extract(carrier).samplingFlags()).isEqualTo(SamplingFlags.EMPTY); } + + @Test + public void extract_malformed_check_not_throws_exception_different_length() { + carrier.put("x-amzn-trace-id", "Root=1-1373cbb-77f4b48ed7ff3eebbd62b5e01;Parent=1a4e96536a3ff131;Sampled=0"); + + assertThat(extractor.extract(carrier).samplingFlags()).isEqualTo(SamplingFlags.EMPTY); + } + + @Test + public void extract_malformed_throws_exception_not_hexadecimal_value() { + carrier.put("x-amzn-trace-id", "Root=1-1z373cbb-77f4b48ed7ff3eebbd62b5e01;Parent=1a4e96536a3ff131;Sampled=0"); + + assertThat(extractor.extract(carrier).samplingFlags()).isEqualTo(SamplingFlags.EMPTY); + } }