| 1 |
|
| /* otel_ctx.h - an extracted trace context, and the rules every parser here |
| 2 |
|
| * shares. |
| 3 |
|
| * |
| 4 |
|
| * EVERY BYTE THESE PARSERS SEE CAME FROM THE CLIENT. A traceparent is a |
| 5 |
|
| * request header, which means it is attacker-controlled input arriving on the |
| 6 |
|
| * hot path of every request. So: |
| 7 |
|
| * |
| 8 |
|
| * - Nothing here allocates based on a length the client supplied. Every |
| 9 |
|
| * parse is fixed-width or bounded by a constant. |
| 10 |
|
| * - Nothing here croaks. A malformed header is ABSENT, not an error: a |
| 11 |
|
| * 500 because somebody sent a bad trace header would be a denial of |
| 12 |
|
| * service with extra steps. |
| 13 |
|
| * - Anything reflected back out is validated on the way OUT as well as in. |
| 14 |
|
| * That is the lesson of CVE-2026-75628 and the Punk markdown 301: bytes |
| 15 |
|
| * do not become safe by having been seen once already. |
| 16 |
|
| * |
| 17 |
|
| * INVALID IS ABSENT. An unparseable, wrong-length or all-zero id yields no |
| 18 |
|
| * context at all rather than a partial one. A span that claims a parent which |
| 19 |
|
| * cannot exist hangs off nothing for ever, in every UI; a root span is |
| 20 |
|
| * correct and legible. |
| 21 |
|
| */ |
| 22 |
|
| |
| 23 |
|
| #ifndef OTEL_CTX_H |
| 24 |
|
| #define OTEL_CTX_H |
| 25 |
|
| |
| 26 |
|
| #include "otel_id.h" |
| 27 |
|
| |
| 28 |
|
| typedef struct { |
| 29 |
|
| unsigned char trace_id[16]; |
| 30 |
|
| unsigned char span_id[8]; |
| 31 |
|
| int flags; /* bit 0 = sampled; other bits preserved as received */ |
| 32 |
|
| int valid; /* 0 = no usable context was found */ |
| 33 |
|
| int debug; /* B3 / Jaeger debug, which IMPLIES sampled */ |
| 34 |
|
| } otel_ctx; |
| 35 |
|
| |
| 36 |
91 |
| static void otel_ctx_clear(otel_ctx *c) { |
| 37 |
91 |
| Zero(c, 1, otel_ctx); |
| 38 |
91 |
| } |
| 39 |
|
| |
| 40 |
|
| /* One hex nibble, or -1. */ |
| 41 |
1438 |
| static int otel_nib(char ch) { |
| 42 |
1438 |
▶ | if (ch >= '0' && ch <= '9') return ch - '0'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ch >= '0' && ch <= '9') return ch - '0'; |
1438 |
0 |
1438 |
| if (ch >= '0' && ch <= '9') return ch - '0'; |
912 |
526 |
1438 |
|
| 43 |
526 |
▶ | if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; |
520 |
6 |
526 |
| if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; |
518 |
2 |
520 |
|
| 44 |
8 |
▶ | if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; |
8 |
0 |
8 |
| if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; |
6 |
2 |
8 |
|
| 45 |
2 |
| return -1; |
| 46 |
|
| } |
| 47 |
|
| |
| 48 |
|
| /* Parse exactly `want` bytes of hex. No length is taken from the input. */ |
| 49 |
80 |
| static int otel_hexn(const char *s, STRLEN len, STRLEN want, |
| 50 |
|
| unsigned char *out) { |
| 51 |
|
| STRLEN i; |
| 52 |
80 |
▶ | if (len != want * 2) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len != want * 2) return 0; |
0 |
80 |
80 |
|
| 53 |
789 |
▶ | for (i = 0; i < want; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < want; i++) { |
710 |
79 |
789 |
|
| 54 |
710 |
| int hi = otel_nib(s[i * 2]), lo = otel_nib(s[i * 2 + 1]); |
| 55 |
710 |
▶ | if (hi < 0 || lo < 0) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (hi < 0 || lo < 0) return 0; |
709 |
1 |
710 |
| if (hi < 0 || lo < 0) return 0; |
0 |
709 |
709 |
|
| 56 |
709 |
| out[i] = (unsigned char)((hi << 4) | lo); |
| 57 |
|
| } |
| 58 |
79 |
| return 1; |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
|
| /* A trace id that may arrive 64-bit (16 hex) or 128-bit (32 hex), as B3 and |
| 62 |
|
| * Jaeger both allow. |
| 63 |
|
| * |
| 64 |
|
| * A short id is LEFT-padded with zero bytes. The side matters and is easy to |
| 65 |
|
| * get backwards: right-padding produces a well-formed id of an entirely |
| 66 |
|
| * different value, which is worse than a rejection because it looks fine and |
| 67 |
|
| * joins to nothing. */ |
| 68 |
16 |
| static int otel_trace_id_flex(const char *s, STRLEN len, unsigned char *out) { |
| 69 |
16 |
▶ | if (len == 32) return otel_hexn(s, len, 16, out) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 32) return otel_hexn(s, len, 16, out) |
15 |
1 |
16 |
|
| 70 |
15 |
▶ | && !otel_all_zero(out, 16); |
Branch: 2 branches
| Branch | True | False | Total |
| && !otel_all_zero(out, 16); |
15 |
0 |
15 |
| && !otel_all_zero(out, 16); |
15 |
0 |
15 |
|
| 71 |
1 |
▶ | if (len == 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 16) { |
1 |
0 |
1 |
|
| 72 |
1 |
| Zero(out, 8, unsigned char); /* the HIGH half is the pad */ |
| 73 |
1 |
| return otel_hexn(s, len, 8, out + 8) |
| 74 |
1 |
▶ | && !otel_all_zero(out, 16); |
Branch: 2 branches
| Branch | True | False | Total |
| && !otel_all_zero(out, 16); |
1 |
0 |
1 |
| && !otel_all_zero(out, 16); |
1 |
0 |
1 |
|
| 75 |
|
| } |
| 76 |
0 |
| return 0; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| /* Likewise a span id, which is always 8 bytes but is sometimes written short |
| 80 |
|
| * by a careless sender. */ |
| 81 |
16 |
| static int otel_span_id_flex(const char *s, STRLEN len, unsigned char *out) { |
| 82 |
16 |
▶ | if (len == 16) return otel_hexn(s, len, 8, out) && !otel_all_zero(out, 8); |
Branch: 3 branches
| Branch | True | False | Total |
| if (len == 16) return otel_hexn(s, len, 8, out) && !otel_all_zero(out, 8); |
16 |
0 |
16 |
| if (len == 16) return otel_hexn(s, len, 8, out) && !otel_all_zero(out, 8); |
16 |
0 |
16 |
| if (len == 16) return otel_hexn(s, len, 8, out) && !otel_all_zero(out, 8); |
16 |
0 |
16 |
|
| 83 |
0 |
▶ | if (len > 0 && len < 16 && (len % 2) == 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (len > 0 && len < 16 && (len % 2) == 0) { |
0 |
0 |
0 |
| if (len > 0 && len < 16 && (len % 2) == 0) { |
0 |
0 |
0 |
| if (len > 0 && len < 16 && (len % 2) == 0) { |
0 |
0 |
0 |
|
| 84 |
0 |
| STRLEN pad = 8 - len / 2; |
| 85 |
0 |
| Zero(out, pad, unsigned char); |
| 86 |
0 |
| return otel_hexn(s, len, len / 2, out + pad) |
| 87 |
0 |
▶ | && !otel_all_zero(out, 8); |
Branch: 2 branches
| Branch | True | False | Total |
| && !otel_all_zero(out, 8); |
0 |
0 |
0 |
| && !otel_all_zero(out, 8); |
0 |
0 |
0 |
|
| 88 |
|
| } |
| 89 |
0 |
| return 0; |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| #endif /* OTEL_CTX_H */ |