|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright 2025 New Relic Corporation. All rights reserved. |
| 4 | + * * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +package com.nr.agent.instrumentation.header.utils; |
| 9 | + |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +import static com.nr.agent.instrumentation.header.utils.W3CTraceParentHeader.W3C_VERSION; |
| 14 | +import static java.util.regex.Pattern.compile; |
| 15 | + |
| 16 | +public class W3CTraceParentValidator { |
| 17 | + |
| 18 | + private static final String INVALID_VERSION = "ff"; |
| 19 | + private static final String INVALID_TRACE_ID = "00000000000000000000000000000000"; // 32 characters |
| 20 | + private static final String INVALID_PARENT_ID = "0000000000000000"; // 16 characters |
| 21 | + private static final Pattern HEXADECIMAL_PATTERN = compile("\\p{XDigit}+"); |
| 22 | + |
| 23 | + private final String traceParentHeader; |
| 24 | + private final String version; |
| 25 | + private final String traceId; |
| 26 | + private final String parentId; |
| 27 | + private final String flags; |
| 28 | + |
| 29 | + private W3CTraceParentValidator(Builder builder) { |
| 30 | + this.traceParentHeader = builder.traceParentHeader; |
| 31 | + this.version = builder.version; |
| 32 | + this.traceId = builder.traceId; |
| 33 | + this.parentId = builder.parentId; |
| 34 | + this.flags = builder.flags; |
| 35 | + } |
| 36 | + |
| 37 | + private boolean isValid() { |
| 38 | + return isValidVersion() && isValidTraceId() && isValidParentId() && isValidFlags(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Version can only be 2 hexadecimal characters, `ff` is not allowed and if it matches our expected version the length must be 55 characters |
| 43 | + */ |
| 44 | + boolean isValidVersion() { |
| 45 | + return version.length() == 2 && isHexadecimal(version.charAt(0)) && isHexadecimal(version.charAt(1)) && !version.equals(INVALID_VERSION) && |
| 46 | + !(version.equals(W3C_VERSION) && traceParentHeaderLengthIsInvalid()); |
| 47 | + } |
| 48 | + |
| 49 | + private boolean traceParentHeaderLengthIsInvalid() { |
| 50 | + return traceParentHeader.length() != 55; |
| 51 | + } |
| 52 | + |
| 53 | + boolean isHexadecimal(char character) { |
| 54 | + return Character.digit(character, 16) != -1; |
| 55 | + } |
| 56 | + |
| 57 | + boolean isHexadecimal(String input) { |
| 58 | + final Matcher matcher = HEXADECIMAL_PATTERN.matcher(input); |
| 59 | + return matcher.matches(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * TraceId must be 32 characters, not all zeros and must be hexadecimal |
| 64 | + */ |
| 65 | + boolean isValidTraceId() { |
| 66 | + return traceId.length() == 32 && !traceId.equals(INVALID_TRACE_ID) && isHexadecimal(traceId); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * ParentId must be 16 characters, not all zeros and must be hexadecimal |
| 71 | + */ |
| 72 | + boolean isValidParentId() { |
| 73 | + return parentId.length() == 16 && !parentId.equals(INVALID_PARENT_ID) && isHexadecimal(parentId); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Flags must be 2 characters and must be hexadecimal |
| 78 | + */ |
| 79 | + boolean isValidFlags() { |
| 80 | + return flags.length() == 2 && isHexadecimal(flags); |
| 81 | + } |
| 82 | + |
| 83 | + static Builder forHeader(String traceParentHeader) { |
| 84 | + return new Builder(traceParentHeader); |
| 85 | + } |
| 86 | + |
| 87 | + static class Builder { |
| 88 | + private final String traceParentHeader; |
| 89 | + private String version; |
| 90 | + private String traceId; |
| 91 | + private String parentId; |
| 92 | + private String flags; |
| 93 | + |
| 94 | + Builder(String traceParentHeader) { |
| 95 | + this.traceParentHeader = traceParentHeader; |
| 96 | + } |
| 97 | + |
| 98 | + public Builder version(String version) { |
| 99 | + this.version = version; |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public Builder traceId(String traceId) { |
| 104 | + this.traceId = traceId; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + public Builder parentId(String parentId) { |
| 109 | + this.parentId = parentId; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public Builder flags(String flags) { |
| 114 | + this.flags = flags; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + W3CTraceParentValidator build() { |
| 119 | + return new W3CTraceParentValidator(this); |
| 120 | + } |
| 121 | + |
| 122 | + public boolean isValid() { |
| 123 | + return build().isValid(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments