@@ -51,6 +51,10 @@ const IS_SAMPLED = '1';
51
51
const NOT_SAMPLED = '0' ;
52
52
53
53
const LINEAGE_KEY = "Lineage" ;
54
+ const LINEAGE_DELIMITER = ":" ;
55
+ const LINEAGE_HASH_LENGTH = 8 ;
56
+ const LINEAGE_MAX_REQUEST_COUNTER = 255 ;
57
+ const LINEAGE_MAX_LOOP_COUNTER = 32767 ;
54
58
55
59
/**
56
60
* Implementation of the AWS X-Ray Trace Header propagation protocol. See <a href=
@@ -162,7 +166,9 @@ export class AWSXRayPropagator implements TextMapPropagator {
162
166
} else if ( trimmedPart . startsWith ( SAMPLED_FLAG_KEY ) ) {
163
167
parsedTraceFlags = AWSXRayPropagator . _parseTraceFlag ( value ) ;
164
168
} else if ( trimmedPart . startsWith ( LINEAGE_KEY ) ) {
165
- baggage = baggage . setEntry ( LINEAGE_KEY , { value} ) ;
169
+ if ( AWSXRayPropagator . _isValidLineageV2Header ( value ) ) {
170
+ baggage = baggage . setEntry ( LINEAGE_KEY , { value} ) ;
171
+ }
166
172
}
167
173
}
168
174
if ( parsedTraceFlags === null ) {
@@ -225,6 +231,23 @@ export class AWSXRayPropagator implements TextMapPropagator {
225
231
return isValidSpanId ( xrayParentId ) ? xrayParentId : INVALID_SPANID ;
226
232
}
227
233
234
+ private static _isValidLineageV2Header ( xrayLineageHeader : string ) : boolean {
235
+ const lineageSubstrings = xrayLineageHeader . split ( LINEAGE_DELIMITER ) ;
236
+ if ( lineageSubstrings . length != 3 ) {
237
+ return false ;
238
+ }
239
+
240
+ const requestCounter = parseInt ( lineageSubstrings [ 0 ] ) ;
241
+ const hashedResourceId = lineageSubstrings [ 1 ] ;
242
+ const loopCounter = parseInt ( lineageSubstrings [ 2 ] ) ;
243
+
244
+ const isValidKey = hashedResourceId . length == LINEAGE_HASH_LENGTH && ! ! hashedResourceId . match ( / ^ [ 0 - 9 a - f A - F ] + $ / ) ;
245
+ const isValidRequestCounter = requestCounter >= 0 && requestCounter <= LINEAGE_MAX_REQUEST_COUNTER ;
246
+ const isValidLoopCounter = loopCounter >= 0 && loopCounter <= LINEAGE_MAX_LOOP_COUNTER ;
247
+
248
+ return isValidKey && isValidRequestCounter && isValidLoopCounter ;
249
+ }
250
+
228
251
private static _parseTraceFlag ( xraySampledFlag : string ) : TraceFlags | null {
229
252
if ( xraySampledFlag === NOT_SAMPLED ) {
230
253
return TraceFlags . NONE ;
0 commit comments