Skip to content
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

fix: add type checking in propagators #904

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export class B3Propagator implements HttpTextPropagator {
const traceIdHeader = getter(carrier, X_B3_TRACE_ID);
const spanIdHeader = getter(carrier, X_B3_SPAN_ID);
const sampledHeader = getter(carrier, X_B3_SAMPLED);
if (!traceIdHeader || !spanIdHeader) return context;
const traceId = Array.isArray(traceIdHeader)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root of this issue was that Array.isArray turns our unknown into an any which made the type system not complain like we would have expected.

? traceIdHeader[0]
: traceIdHeader;
Expand All @@ -75,6 +74,9 @@ export class B3Propagator implements HttpTextPropagator {
? sampledHeader[0]
: sampledHeader;

if (typeof traceId !== 'string' || typeof spanId !== 'string')
return context;

if (isValidTraceId(traceId) && isValidSpanId(spanId)) {
return setExtractedSpanContext(context, {
traceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class HttpTraceContext implements HttpTextPropagator {
const traceParent = Array.isArray(traceParentHeader)
? traceParentHeader[0]
: traceParentHeader;
if (typeof traceParent !== 'string') return context;
const spanContext = parseTraceParent(traceParent);
if (!spanContext) return context;

Expand All @@ -96,7 +97,9 @@ export class HttpTraceContext implements HttpTextPropagator {
const state = Array.isArray(traceStateHeader)
? traceStateHeader.join(',')
: traceStateHeader;
spanContext.traceState = new TraceState(state as string);
spanContext.traceState = new TraceState(
typeof state === 'string' ? state : undefined
);
}
return setExtractedSpanContext(context, spanContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ export class JaegerHttpTracePropagator implements HttpTextPropagator {

extract(context: Context, carrier: unknown, getter: GetterFunction): Context {
const uberTraceIdHeader = getter(carrier, this._jaegerTraceHeader);
if (!uberTraceIdHeader) return context;
const uberTraceId = Array.isArray(uberTraceIdHeader)
? uberTraceIdHeader[0]
: uberTraceIdHeader;

if (typeof uberTraceId !== 'string') return context;

const spanContext = deserializeSpanContext(uberTraceId);
if (!spanContext) return context;

Expand Down