-
Notifications
You must be signed in to change notification settings - Fork 38
fix: enforce w3c trace context value validation #777
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
Changes from 2 commits
885b7a9
9000c57
05e00c8
18c4b1c
ae8e923
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,11 +18,17 @@ | |
|
|
||
| import com.google.cloud.logging.HttpRequest.RequestMethod; | ||
| import com.google.common.base.MoreObjects; | ||
| import com.google.common.base.Strings; | ||
| import java.util.Objects; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** Class to hold context attributes including information about {@see HttpRequest} and tracing. */ | ||
| public class Context { | ||
| // validate W3C trace context value on load according to the existing version format. | ||
| // see https://www.w3.org/TR/trace-context/#traceparent-header-field-values for details. | ||
| private static final Pattern W3C_TRACE_CONTEXT_FORMAT = | ||
| Pattern.compile( | ||
| "^00-(?!00000000000000000000000000000000)[0-9abcdef]{32}-(?!000000000000)[0-9abcdef]{12}-[0-9abcdef]{2}$"); | ||
| private final HttpRequest request; | ||
| private final String traceId; | ||
| private final String spanId; | ||
|
|
@@ -142,26 +148,15 @@ public Builder loadCloudTraceContext(String cloudTrace) { | |
| */ | ||
| public Builder loadW3CTraceParentContext(String traceParent) throws IllegalArgumentException { | ||
| if (traceParent != null) { | ||
| String[] fields = traceParent.split("-"); | ||
| if (fields.length > 3) { | ||
| String versionFormat = fields[0]; | ||
| if (!versionFormat.equals("00")) { | ||
| throw new IllegalArgumentException("Not supporting versionFormat other than \"00\""); | ||
| } | ||
| } else { | ||
| Matcher validator = W3C_TRACE_CONTEXT_FORMAT.matcher(traceParent.toLowerCase()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not know whether we should be so strict about it. A chance that two different trace ids will be produced that differ only by a specific letter register seems low to me. But this way we can avoid exceptions that interrupt the metadata collection in the library.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use then toUpperCase() to be safe?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My vote would be to just ignore any My interpretation is that header parsing is meant to be farily strict, we're not supposed to alter the fields to make the data fit our expectations. But I'll leave the decision up to you.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with you about the standard. My concern is that ignoring it (or throwing and catching exception on the same account) would be very hard to troubleshoot for application developers. To be a little more liberal on this might prove to be closer to the spirit of the shared library. |
||
| if (!validator.matches()) { | ||
| throw new IllegalArgumentException( | ||
| "Invalid format of the header value. Expected \"00-traceid-spanid-arguments\""); | ||
| } | ||
| String traceId = fields[1]; | ||
| if (!traceId.isEmpty()) { | ||
| setTraceId(traceId); | ||
| } | ||
| if (!Strings.isNullOrEmpty(traceId)) { | ||
| String spanId = fields[2]; | ||
| if (!spanId.isEmpty()) { | ||
| setSpanId(spanId); | ||
| } | ||
| "Invalid format of the header value. The value does not match W3C Trace Context version \"00\""); | ||
| } | ||
| String[] fields = traceParent.split("-"); | ||
| setTraceId(fields[1]); | ||
| setSpanId(fields[2]); | ||
| // fields[3] contains flag(s) | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2021 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.logging; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
| import org.junit.runners.Parameterized.Parameters; | ||
|
|
||
| @RunWith(Parameterized.class) | ||
| public class InvalidContextTest { | ||
| @Parameters | ||
| public static Collection<String> data() { | ||
| final String[] INVALID_W3C_TRACE_CONTEXTS = { | ||
| "", | ||
| "abc/efg", | ||
| "01-something", | ||
| "00-123456789012345678901234567890", | ||
| "00-12345678901234567890123456789012", | ||
| "00-12345678901234567890123456789012345", | ||
| "00-12345678901234567890123456789012-12345678901", | ||
| "00-12345678901234567890123456789012-123456789012", | ||
| "00-12345678901234567890123456789012-1234567890123", | ||
| "00-12345678901234567890123456789012-123456789012-1", | ||
| "00-12345678901234567890123456789012-123456789012-123" | ||
| }; | ||
| return Arrays.asList(INVALID_W3C_TRACE_CONTEXTS); | ||
| } | ||
|
|
||
| private final String traceContext; | ||
|
|
||
| public InvalidContextTest(String traceContext) { | ||
| this.traceContext = traceContext; | ||
| } | ||
|
|
||
| @Test(expected = IllegalArgumentException.class) | ||
| public void testAssertionInvalidContext() { | ||
| Context.Builder builder = Context.newBuilder(); | ||
| builder.loadW3CTraceParentContext(traceContext); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.