include/otel_baggage.h

filter: Statementstmt 100% 70 / 70 Branchbran 71.1% 74 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.7% 144 / 174 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_expo.h ↑ Summary xs/meter.xs »
1 /* otel_baggage.h - W3C Baggage.
2 *
3 * baggage: key1=value1;prop=x,key2=value2
4 *
5 * Application state travelling with the trace. Percent-encoded values, three
6 * size limits (180 entries, 4096 bytes an entry, 8192 bytes total), and
7 * per-entry properties carried through opaquely.
8 *
9 * BAGGAGE IS NOT AUTOMATICALLY ATTACHED TO SPANS, and that is a security
10 * decision rather than an omission.
11 *
12 * Baggage arrives in a request header, which on any public endpoint means an
13 * attacker chooses its contents. Copying it into every span's attributes -
14 * which is the obvious convenience, and which some SDKs offer by default -
15 * hands that attacker two things at once: a cardinality bomb, because they
16 * pick both the key names and the values, and a data-leak path, because
17 * whatever they put there is now stored in a telemetry backend that is
18 * usually less guarded than the application and is often a third party.
19 *
20 * So the capability exists and is off. An application that wants baggage on
21 * its spans asks for it, by name, for the keys it expects.
22 */
23  
24 #ifndef OTEL_BAGGAGE_H
25 #define OTEL_BAGGAGE_H
26  
27 #include "otel_ctx.h"
28  
29 #define OTEL_BAGGAGE_MAX_ENTRIES 180
30 #define OTEL_BAGGAGE_MAX_ENTRY 4096
31 #define OTEL_BAGGAGE_MAX_TOTAL 8192
32  
33 /* Percent-encode a baggage key or value into `out`. Everything outside
34 * unreserved ASCII is escaped, which covers the delimiters (`,` `;` `=`) and
35 * every control byte - so a value taken from a request and re-emitted cannot
36 * forge an entry or split the header. Bounded; returns 0 if it will not
37 * fit. */
38 370 static STRLEN otel_pct_encode(const char *s, STRLEN len, char *out,
39 STRLEN cap) {
40 static const char H[] = "0123456789ABCDEF";
41 370 STRLEN i, o = 0;
42 1623 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 1253 370 1623
43 1253 unsigned char c = (unsigned char)s[i];
44 1253 ▶ int safe = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
Branch: 3 branches
BranchTrueFalseTotal
int safe = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 0 200 200
int safe = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 2 1051 1053
int safe = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 0 2 2
45 1051 ▶ || (c >= '0' && c <= '9')
Branch: 2 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') 1044 7 1051
|| (c >= '0' && c <= '9') 2 1042 1044
46 2506 ▶ || c == '-' || c == '.' || c == '_' || c == '~';
Branch: 5 branches
BranchTrueFalseTotal
|| c == '-' || c == '.' || c == '_' || c == '~'; 200 1053 1253
|| c == '-' || c == '.' || c == '_' || c == '~'; 8 1 9
|| c == '-' || c == '.' || c == '_' || c == '~'; 8 0 8
|| c == '-' || c == '.' || c == '_' || c == '~'; 8 0 8
|| c == '-' || c == '.' || c == '_' || c == '~'; 0 8 8
47 1253 ▶ if (safe) {
Branch: 1 branch
BranchTrueFalseTotal
if (safe) { 1245 8 1253
48 1245 ▶ if (o + 1 > cap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (o + 1 > cap) return 0; 0 1245 1245
49 1245 out[o++] = (char)c;
50 }
51 else {
52 8 ▶ if (o + 3 > cap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (o + 3 > cap) return 0; 0 8 8
53 8 out[o++] = '%';
54 8 out[o++] = H[c >> 4];
55 8 out[o++] = H[c & 0xf];
56 }
57 }
58 370 return o;
59 }
60  
61 /* Parse a baggage header into a hashref of { key => value }, decoding both.
62 * Properties (the `;prop` tail) are preserved on the value as received, since
63 * they are opaque to us and dropping them would lose somebody's metadata.
64 *
65 * Over-limit entries are DROPPED rather than truncated: a truncated value is
66 * a different value, and silently changing an application's data is worse
67 * than not carrying it. */
68 6 static HV *otel_baggage_parse(pTHX_ const char *s, STRLEN len) {
69 6 HV *out = newHV();
70 6 STRLEN i = 0, total = 0;
71 6 int entries = 0;
72 6 ▶ if (!s || !len) return out;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !len) return out; 6 0 6
if (!s || !len) return out; 1 5 6
73  
74 11 ▶ while (i < len && entries < OTEL_BAGGAGE_MAX_ENTRIES
Branch: 1 branch
BranchTrueFalseTotal
while (i < len && entries < OTEL_BAGGAGE_MAX_ENTRIES 6 0 6
75 17 ▶ && total < OTEL_BAGGAGE_MAX_TOTAL) {
Branch: 2 branches
BranchTrueFalseTotal
&& total < OTEL_BAGGAGE_MAX_TOTAL) { 6 5 11
&& total < OTEL_BAGGAGE_MAX_TOTAL) { 6 0 6
76 STRLEN start, end, eq;
77 char kbuf[OTEL_BAGGAGE_MAX_ENTRY], vbuf[OTEL_BAGGAGE_MAX_ENTRY];
78 STRLEN kl, vl;
79  
80 7 ▶ while (i < len && (s[i] == ' ' || s[i] == '\t' || s[i] == ',')) i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == '\t' || s[i] == ',')) i++; 7 0 7
while (i < len && (s[i] == ' ' || s[i] == '\t' || s[i] == ',')) i++; 0 7 7
while (i < len && (s[i] == ' ' || s[i] == '\t' || s[i] == ',')) i++; 0 7 7
while (i < len && (s[i] == ' ' || s[i] == '\t' || s[i] == ',')) i++; 1 6 7
81 6 ▶ if (i >= len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) break; 0 6 6
82 6 start = i;
83 5066 ▶ while (i < len && s[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && s[i] != ',') i++; 5061 5 5066
while (i < len && s[i] != ',') i++; 5060 1 5061
84 6 end = i;
85 6 ▶ while (end > start && (s[end - 1] == ' ' || s[end - 1] == '\t')) end--;
Branch: 3 branches
BranchTrueFalseTotal
while (end > start && (s[end - 1] == ' ' || s[end - 1] == '\t')) end--; 6 0 6
while (end > start && (s[end - 1] == ' ' || s[end - 1] == '\t')) end--; 0 6 6
while (end > start && (s[end - 1] == ' ' || s[end - 1] == '\t')) end--; 0 6 6
86 8 ▶ if (end <= start) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (end <= start) continue; 0 6 6
87 6 ▶ if (end - start > OTEL_BAGGAGE_MAX_ENTRY) continue; /* drop it */
Branch: 1 branch
BranchTrueFalseTotal
if (end - start > OTEL_BAGGAGE_MAX_ENTRY) continue; /* drop it */ 1 5 6
88  
89 23 ▶ for (eq = start; eq < end && s[eq] != '='; eq++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (eq = start; eq < end && s[eq] != '='; eq++) ; 22 1 23
for (eq = start; eq < end && s[eq] != '='; eq++) ; 18 4 22
90 5 ▶ if (eq >= end) continue; /* no '=' */
Branch: 1 branch
BranchTrueFalseTotal
if (eq >= end) continue; /* no '=' */ 1 4 5
91  
92 4 kl = otel_pct_decode(s + start, eq - start, kbuf, sizeof kbuf);
93 4 vl = otel_pct_decode(s + eq + 1, end - eq - 1, vbuf, sizeof vbuf);
94 4 ▶ if (!kl) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!kl) continue; 0 4 4
95 {
96 STRLEN j;
97 4 int ok = 1;
98 14 ▶ for (j = 0; j < kl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < kl; j++) { 10 4 14
99 10 unsigned char c = (unsigned char)kbuf[j];
100 10 ▶ if (c < 0x21 || c > 0x7e) { ok = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (c < 0x21 || c > 0x7e) { ok = 0; break; } 10 0 10
if (c < 0x21 || c > 0x7e) { ok = 0; break; } 0 10 10
101 }
102 4 ▶ if (!ok) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) continue; 0 4 4
103 }
104 4 total += (end - start);
105 4 (void)hv_store(out, kbuf, (I32)kl, newSVpvn(vbuf, vl), 0);
106 4 entries++;
107 }
108 5 return out;
109 }
110  
111 /* Render a hashref as a baggage header. Keys are sorted, for the same reason
112 * everything else in this dist sorts: a header that reorders itself between
113 * runs cannot be diffed or tested. Returns a mortal SV. */
114 5 static SV *otel_baggage_format(pTHX_ HV *in) {
115 5 SV *out = sv_2mortal(newSVpvs(""));
116 AV *keys;
117 HE *he;
118 5 SSize_t i, n = 0;
119 5 int entries = 0;
120 5 ▶ if (!in) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!in) return out; 0 5 5
121  
122 5 keys = (AV *)sv_2mortal((SV *)newAV());
123 5 hv_iterinit(in);
124 310 ▶ while ((he = hv_iternext(in))) { av_push(keys, newSVsv(hv_iterkeysv(he))); n++; }
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(in))) { av_push(keys, newSVsv(hv_iterkeysv(he))); n++; } 305 5 310
125 5 ▶ if (n > 1) sortsv(AvARRAY(keys), (STRLEN)n, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) sortsv(AvARRAY(keys), (STRLEN)n, Perl_sv_cmp); 2 3 5
126  
127 190 ▶ for (i = 0; i < n && entries < OTEL_BAGGAGE_MAX_ENTRIES; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && entries < OTEL_BAGGAGE_MAX_ENTRIES; i++) { 186 4 190
for (i = 0; i < n && entries < OTEL_BAGGAGE_MAX_ENTRIES; i++) { 185 1 186
128 185 SV **kp = av_fetch(keys, i, 0);
129 HE *e;
130 STRLEN kl, vl, ek, ev;
131 const char *k, *v;
132 char kb[OTEL_BAGGAGE_MAX_ENTRY], vb[OTEL_BAGGAGE_MAX_ENTRY];
133 185 ▶ if (!(kp && *kp)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(kp && *kp)) continue; 185 0 185
if (!(kp && *kp)) continue; 0 185 185
134 185 k = SvPV_const(*kp, kl);
135 185 e = hv_fetch_ent(in, *kp, 0, 0);
136 370 ▶ v = (e && HeVAL(e) && SvOK(HeVAL(e))) ? SvPV_const(HeVAL(e), vl)
Branch: 2 branches
BranchTrueFalseTotal
v = (e && HeVAL(e) && SvOK(HeVAL(e))) ? SvPV_const(HeVAL(e), vl) 185 0 185
v = (e && HeVAL(e) && SvOK(HeVAL(e))) ? SvPV_const(HeVAL(e), vl) 185 0 185
137 185 ▶ : (vl = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
: (vl = 0, ""); 185 0 185
138 185 ek = otel_pct_encode(k, kl, kb, sizeof kb);
139 185 ev = otel_pct_encode(v, vl, vb, sizeof vb);
140 185 ▶ if (!ek) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ek) continue; 0 185 185
141 185 ▶ if (SvCUR(out) + ek + ev + 2 > OTEL_BAGGAGE_MAX_TOTAL) break;
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out) + ek + ev + 2 > OTEL_BAGGAGE_MAX_TOTAL) break; 0 185 185
142 185 ▶ if (SvCUR(out)) sv_catpvs(out, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out)) sv_catpvs(out, ","); 180 5 185
143 185 sv_catpvn(out, kb, ek);
144 185 sv_catpvs(out, "=");
145 185 sv_catpvn(out, vb, ev);
146 185 entries++;
147 }
148 5 return out;
149 }
150  
151 #endif /* OTEL_BAGGAGE_H */
« include/otel_expo.h ↑ Summary xs/meter.xs »