@@ -51,6 +51,10 @@ const IS_SAMPLED = '1';
5151const NOT_SAMPLED = '0' ;
5252
5353const 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 ;
5458
5559/**
5660 * Implementation of the AWS X-Ray Trace Header propagation protocol. See <a href=
@@ -162,7 +166,9 @@ export class AWSXRayPropagator implements TextMapPropagator {
162166 } else if ( trimmedPart . startsWith ( SAMPLED_FLAG_KEY ) ) {
163167 parsedTraceFlags = AWSXRayPropagator . _parseTraceFlag ( value ) ;
164168 } 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+ }
166172 }
167173 }
168174 if ( parsedTraceFlags === null ) {
@@ -225,6 +231,23 @@ export class AWSXRayPropagator implements TextMapPropagator {
225231 return isValidSpanId ( xrayParentId ) ? xrayParentId : INVALID_SPANID ;
226232 }
227233
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+
228251 private static _parseTraceFlag ( xraySampledFlag : string ) : TraceFlags | null {
229252 if ( xraySampledFlag === NOT_SAMPLED ) {
230253 return TraceFlags . NONE ;
0 commit comments