Update the TraceState key validations to more closely match the w3c spec#1561
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1561 +/- ##
============================================
+ Coverage 86.71% 86.78% +0.06%
- Complexity 1394 1401 +7
============================================
Files 162 162
Lines 5353 5365 +12
Branches 513 519 +6
============================================
+ Hits 4642 4656 +14
+ Misses 525 524 -1
+ Partials 186 185 -1
Continue to review full report at Codecov.
|
| } | ||
| if ((c == '@') && (++atSeenCount > 1)) { | ||
| return false; | ||
| int atPosition = 0; |
There was a problem hiding this comment.
I'm having trouble grokking this code.
There was a problem hiding this comment.
Sorry accidentally hit Add single comment, continuing below
| atSeenCount++; | ||
| if (atSeenCount > 1) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Instead of having atPosition is it ok to do something like this in this branch?
// remaining is vendor id, must be 13 characters or less
if (key.length() - i > 13) {
return false;
}| } | ||
| if (atSeenCount == 1) { | ||
| // vendor id must be 13 characters or less | ||
| if ((i - atPosition) > 13) { |
There was a problem hiding this comment.
I may just be misgrokking, but isn't atPosition 0 for most of the loop rather than the actual atPosition? If my reading is right, my above suggestion would probably solve it but it also means there's a unit test missing for the current buggy state.
There was a problem hiding this comment.
atSeenCount is incremented only when atPosition changes to i. This way, this if is checked only for atPosition > 0.
There was a problem hiding this comment.
Yeah but atPosition is declared inside the loop, which I think is the original bug. So except for when key[i] == '@', atPosition == 0 for the remainder of the loop since it's initialized to 0. Sorry if I'm missing it though I've tried to stare long and this is what I came up with.
There was a problem hiding this comment.
Oh! You are totally right! I miss that bit! We are indeed missing a test here!
There was a problem hiding this comment.
I'm not following the problem. Can you give me a key that we're not handling correctly, so I can add a unit test for the issue?
There was a problem hiding this comment.
Note, I'm rewriting this chunk of code to make it clearer. But, having an example key that we're not handling correctly would be very useful (because I don't see the issue).
There was a problem hiding this comment.
I've pushed the rewrite. Let me know if that helps at all with understanding what's going on in here.
And, please give me a concrete key example case we're not handling correctly if there is one. :)
There was a problem hiding this comment.
I'm pretty sure 12345678901234567890@nr is valid but fails when I added this to your branch. Basically any time the tenant id is longer than 13 characters, since the current logic is resetting the atPosition to the beinning of the string.
My suggestion of not bothering with atPosition still stands :)
@Test
void testValidLongVendorKey() {
TraceState result = EMPTY.toBuilder().set("12345678901234567890@nr", FIRST_VALUE).build();
assertThat(result.get("12345678901234567890@nr")).isEqualTo(FIRST_VALUE);
}
There was a problem hiding this comment.
doh! I know you said it like 5 times, but I totally missed that we were resetting the atPosition inside the loop. 🤦
| } | ||
| if (atSeenCount == 0) { | ||
| // if it's not the vendor format (with an '@' sign), the key must start with a letter. | ||
| return !Character.isDigit(key.charAt(0)); |
There was a problem hiding this comment.
Also, guess we should avoid unicode comparisons like this and stick with simple ones we implement
There was a problem hiding this comment.
that's fair. I'll extract the digit logic into a separate method and us that.
|
I think we should revert this change. We should follow the "be liberal in what you accept" guideline here, otherwise any deployed opentelemetry-java SDK will break tracestate entries that make use of the relaxed grammar in the current draft spec (once it graduates from being a draft). |
If we want to truly "be liberal in what we accept", we should drop all validations of the trace state entries and just pass them through un-molested. See this issue: #876 |
Resolves #1027