include/punk/punk_host.h

filter: Statementstmt 98.4% 182 / 190 (5 uncoverable) Branchbran 74.8% 255 / 358 (30 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.0% 437 / 548 (35 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_wsconn.h ↑ Summary include/punk/punk_health.h »
1 /* punk_host.h - the application's origin, and which request hosts may stand
2 * in for it.
3 *
4 * host 'https://example.com';
5 * host 'https://example.com', allow => [ '*.example.com', 'shop.tld' ];
6 *
7 * `host` is one canonical origin, which is the whole story for most
8 * applications. One serving several tenants by Host header has an origin
9 * per request, and the request's Host is attacker-supplied bytes: anything
10 * that reflects it - a sitemap handed to search engines, a redirect, an
11 * absolute link in a mail - is a host-header injection waiting for a
12 * request that sets it.
13 *
14 * The allowlist is what turns that header into configuration. $c->origin is
15 * the request's scheme and host ONLY when the host is the canonical one or
16 * matches an allow entry, and the canonical origin otherwise. The raw header
17 * never comes back, so a consumer cannot be handed a hostname nobody
18 * configured.
19 */
20  
21 #ifndef PUNK_HOST_H
22 #define PUNK_HOST_H
23  
24 /* name[:port] -> name length, and the port if there is one */
25 440236 static void pk_host_split(const char *p, STRLEN l, STRLEN *nl,
26 const char **port, STRLEN *portl) {
27 STRLEN i;
28 5282977 ▶ for (i = 0; i < l; i++) if (p[i] == ':') break;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < l; i++) if (p[i] == ':') break; 39 4842741 4842780
for (i = 0; i < l; i++) if (p[i] == ':') break; 4842780 440197 5282977
29 440236 *nl = i;
30 440236 ▶ if (i < l) { *port = p + i + 1; *portl = l - i - 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (i < l) { *port = p + i + 1; *portl = l - i - 1; } 39 440197 440236
31 440197 else { *port = NULL; *portl = 0; }
32 440236 }
33  
34 /* An allow entry, already lowercased: labels of [a-z0-9-] joined by '.', an
35 * optional leading "*." and no other '*', an optional ":port". Nothing else -
36 * an entry is matched with memEQ, not a pattern language, so a typo fails at
37 * the keyword rather than never matching. */
38 22 static int pk_host_entry_ok(const char *p, STRLEN l) {
39 22 STRLEN i = 0, nl;
40 const char *port;
41 STRLEN portl;
42 /*
43 # uncoverable condition right count:1 class:ignore_covered_err
44 # uncoverable condition right count:3 class:ignore_covered_err */
45 22 ▶ if (l >= 2 && p[0] == '*' && p[1] == '.') i = 2;
Branch: 3 branches
BranchTrueFalseTotal
if (l >= 2 && p[0] == '*' && p[1] == '.') i = 2; 22 0 22
if (l >= 2 && p[0] == '*' && p[1] == '.') i = 2; 5 17 22
if (l >= 2 && p[0] == '*' && p[1] == '.') i = 2; 5 0 5
46 22 pk_host_split(p, l, &nl, &port, &portl);
47 /*
48 # uncoverable branch true
49 # uncoverable condition left count:1 class:ignore_covered_err */
50 22 ▶ if (nl <= i) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nl <= i) return 0; 0 22 22
51 226 ▶ for (; i < nl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < nl; i++) { 206 20 226
52 206 const unsigned char c = (unsigned char)p[i];
53 /*
54 # uncoverable condition right count:2 class:ignore_covered_err
55 # uncoverable condition left count:3 class:ignore_covered_err
56 # uncoverable condition left count:4 class:ignore_covered_err
57 # uncoverable condition right count:4 class:ignore_covered_err
58 # uncoverable condition left count:5 class:ignore_covered_err */
59 206 ▶ if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
Branch: 4 branches
BranchTrueFalseTotal
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 183 23 206
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 0 183 183
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 0 23 23
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 0 0 0
60 206 ▶ || c == '-' || c == '.') continue;
Branch: 2 branches
BranchTrueFalseTotal
|| c == '-' || c == '.') continue; 23 0 23
|| c == '-' || c == '.') continue; 21 2 23
61 2 return 0;
62 }
63 20 ▶ if (port) {
Branch: 1 branch
BranchTrueFalseTotal
if (port) { 5 15 20
64 5 ▶ if (portl == 0 || portl > 5) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (portl == 0 || portl > 5) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 5 0 5
if (portl == 0 || portl > 5) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 3 5
65 11 ▶ for (i = 0; i < portl; i++) if (!isDIGIT(port[i])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < portl; i++) if (!isDIGIT(port[i])) return 0; 1 8 9
for (i = 0; i < portl; i++) if (!isDIGIT(port[i])) return 0; 9 2 11
66 }
67 17 return 1;
68 }
69  
70 /* Does a request host (lowercased, split) satisfy an entry? An entry naming
71 * a port means that port; one without accepts any. A leading "*." matches
72 * any host with at least one label in front of the suffix. */
73 96 static int pk_host_matches(const char *ent, STRLEN el,
74 const char *host, STRLEN hl,
75 const char *port, STRLEN portl) {
76 STRLEN enl;
77 const char *eport;
78 STRLEN eportl;
79 96 pk_host_split(ent, el, &enl, &eport, &eportl);
80 96 ▶ if (eport && !(port && portl == eportl && memEQ(port, eport, portl))) /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (eport && !(port && portl == eportl && memEQ(port, eport, portl))) /* # uncoverable condition left count:3 class:ignore_covered_err */ 20 76 96
if (eport && !(port && portl == eportl && memEQ(port, eport, portl))) /* # uncoverable condition left count:3 class:ignore_covered_err */ 6 14 20
if (eport && !(port && portl == eportl && memEQ(port, eport, portl))) /* # uncoverable condition left count:3 class:ignore_covered_err */ 6 0 6
if (eport && !(port && portl == eportl && memEQ(port, eport, portl))) /* # uncoverable condition left count:3 class:ignore_covered_err */ 3 3 6
81 17 return 0;
82 /*
83 # uncoverable condition right count:1 class:ignore_covered_err
84 # uncoverable condition right count:3 class:ignore_covered_err */
85 79 ▶ if (enl >= 2 && ent[0] == '*' && ent[1] == '.') {
Branch: 3 branches
BranchTrueFalseTotal
if (enl >= 2 && ent[0] == '*' && ent[1] == '.') { 79 0 79
if (enl >= 2 && ent[0] == '*' && ent[1] == '.') { 41 38 79
if (enl >= 2 && ent[0] == '*' && ent[1] == '.') { 41 0 41
86 41 STRLEN sl = enl - 1; /* ".example.com" */
87 41 ▶ return hl > sl && memEQ(host + hl - sl, ent + 1, sl);
Branch: 2 branches
BranchTrueFalseTotal
return hl > sl && memEQ(host + hl - sl, ent + 1, sl); 20 21 41
return hl > sl && memEQ(host + hl - sl, ent + 1, sl); 13 7 20
88 }
89 38 ▶ return hl == enl && memEQ(host, ent, hl); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return hl == enl && memEQ(host, ent, hl); /* # uncoverable condition right count:2 class:ignore_covered_err */ 14 24 38
return hl == enl && memEQ(host, ent, hl); /* # uncoverable condition right count:2 class:ignore_covered_err */ 14 0 14
90 }
91  
92 /* An arrayref of allow entries, validated and lowercased into a new AV (+1).
93 * `what` names the caller in the croak ("host allow", "websocket origin"), so
94 * one rule about what a hostname is has one implementation and every keyword
95 * that takes a list of them refuses the same typos. */
96 18 static AV *pk_host_allow_av(pTHX_ SV *v, const char *what) {
97 AV *in, *out;
98 SSize_t ai, an;
99 /*
100 # uncoverable condition left count:1 class:ignore_covered_err
101 # uncoverable condition left count:3 class:ignore_covered_err */
102 18 ▶ if (!(v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)) 18 0 18
if (!(v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)) 17 1 18
if (!(v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)) 0 17 17
103 1 croak("Punk: %s needs an arrayref of hostnames", what);
104 17 in = (AV *)SvRV(v);
105 17 out = newAV();
106 17 an = av_len(in) + 1;
107 34 ▶ for (ai = 0; ai < an; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < an; ai++) { 23 11 34
108 23 SV **e = av_fetch(in, ai, 0);
109 STRLEN el, j;
110 const char *ep;
111 SV *low;
112 char *lp;
113 /*
114 # uncoverable condition left count:1 class:ignore_covered_err
115 # uncoverable condition left count:2 class:ignore_covered_err
116 # uncoverable condition left count:3 class:ignore_covered_err */
117 23 ▶ if (!(e && *e && SvOK(*e) && SvCUR(*e))) {
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e) && SvCUR(*e))) { 23 0 23
if (!(e && *e && SvOK(*e) && SvCUR(*e))) { 23 0 23
if (!(e && *e && SvOK(*e) && SvCUR(*e))) { 23 0 23
if (!(e && *e && SvOK(*e) && SvCUR(*e))) { 1 22 23
118 1 SvREFCNT_dec((SV *)out);
119 1 croak("Punk: %s entries are hostnames", what);
120 }
121 22 ep = SvPV_const(*e, el);
122 22 low = newSVpvn(ep, el);
123 22 lp = SvPVX(low);
124 287 ▶ for (j = 0; j < el; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < el; j++) 265 22 287
125 265 ▶ if (lp[j] >= 'A' && lp[j] <= 'Z') lp[j] += 32;
Branch: 2 branches
BranchTrueFalseTotal
if (lp[j] >= 'A' && lp[j] <= 'Z') lp[j] += 32; 211 54 265
if (lp[j] >= 'A' && lp[j] <= 'Z') lp[j] += 32; 6 205 211
126 22 ▶ if (!pk_host_entry_ok(lp, el)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_host_entry_ok(lp, el)) { 5 17 22
127 5 sv_2mortal(low); /* freed with the caller's frame */
128 5 SvREFCNT_dec((SV *)out);
129 5 croak("Punk: %s entry '%.*s' is not a hostname - labels of "
130 "[a-z0-9-], an optional leading *. and an optional :port",
131 what, (int)el, ep); /* the bytes as they were given */
132 }
133 17 av_push(out, low);
134 }
135 11 return out;
136 }
137  
138 /* scheme://host[:port] of a stored origin, path dropped: the part that is
139 * an origin in the browser's sense, which is what $c->origin promises. */
140 /* Where the host sits in a stored origin, and where the origin ends. No
141 * allocation: this runs on every request that asks for its origin. */
142 220103 static STRLEN pk_origin_span(const char *o, STRLEN ol, STRLEN *hostoff,
143 STRLEN *hostlen) {
144 220103 STRLEN i = 0, start, end;
145 /*
146 # uncoverable condition right count:1 class:ignore_covered_err
147 # uncoverable condition left count:3 class:ignore_covered_err
148 # uncoverable condition left count:4 class:ignore_covered_err */
149 1320618 ▶ while (i + 2 < ol && !(o[i] == ':' && o[i+1] == '/' && o[i+2] == '/')) i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i + 2 < ol && !(o[i] == ':' && o[i+1] == '/' && o[i+2] == '/')) i++; 1320618 0 1320618
while (i + 2 < ol && !(o[i] == ':' && o[i+1] == '/' && o[i+2] == '/')) i++; 1100515 220103 1320618
while (i + 2 < ol && !(o[i] == ':' && o[i+1] == '/' && o[i+2] == '/')) i++; 0 220103 220103
while (i + 2 < ol && !(o[i] == ':' && o[i+1] == '/' && o[i+2] == '/')) i++; 0 220103 220103
150 220103 ▶ start = (i + 2 < ol) ? i + 3 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
start = (i + 2 < ol) ? i + 3 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 220103 0 220103
151 220103 end = start;
152 2641227 ▶ while (end < ol && o[end] != '/') end++;
Branch: 2 branches
BranchTrueFalseTotal
while (end < ol && o[end] != '/') end++; 2421131 220096 2641227
while (end < ol && o[end] != '/') end++; 2421124 7 2421131
153 220103 ▶ if (hostoff) *hostoff = start;
Branch: 1 branch
BranchTrueFalseTotal
if (hostoff) *hostoff = start; 220071 32 220103
154 220103 ▶ if (hostlen) *hostlen = end - start;
Branch: 1 branch
BranchTrueFalseTotal
if (hostlen) *hostlen = end - start; 220071 32 220103
155 220103 return end;
156 }
157  
158 58 static SV *pk_origin_bare(pTHX_ const char *o, STRLEN ol, STRLEN *hostoff,
159 STRLEN *hostlen) {
160 58 return newSVpvn(o, pk_origin_span(o, ol, hostoff, hostlen));
161 }
162  
163 /* The origin a request may be told it has.
164 *
165 * Returns a new SV (+1), or NULL when the application declared no `host` -
166 * in which case there is nothing safe to say, and the caller says undef.
167 * *state is 0 when the request's Host was unknown or malformed and the
168 * canonical origin is being returned instead; 1 when it is the canonical
169 * host itself; 2 when it matched an allow entry. A consumer that renders
170 * per tenant wants 2; one that only needs a trustworthy absolute URL wants
171 * the string. */
172 220064 static SV *pk_origin_of(pTHX_ SV *c, int *state) {
173 220064 AV *av = pcx_av(aTHX_ c);
174 220064 SV *appsv = pcx_get(aTHX_ av, PCX_APP);
175 220064 SV *envsv = pcx_get(aTHX_ av, PCX_ENV);
176 220064 HV *h, *env = NULL;
177 SV *canon, *allow;
178 const char *cp;
179 STRLEN cl, choff, chlen;
180 220064 SV *hostsv = NULL, *schemesv = NULL;
181  
182 220064 *state = 0;
183 /*
184 # uncoverable branch true class:ignore_covered_err
185 # uncoverable condition left count:1 class:ignore_covered_err
186 # uncoverable condition left count:2 class:ignore_covered_err
187 # uncoverable condition left count:3 class:ignore_covered_err */
188 220064 ▶ if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV)) 220064 0 220064
if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV)) 220064 0 220064
if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV)) 0 220064 220064
189 0 return NULL; /* # uncoverable statement class:ignore_covered_err */
190 220064 h = (HV *)SvRV(appsv);
191 220064 canon = app_get(aTHX_ h, "host");
192 /*
193 # uncoverable condition left count:2 class:ignore_covered_err
194 # uncoverable condition left count:3 class:ignore_covered_err */
195 220064 ▶ if (!(canon && SvOK(canon) && SvCUR(canon))) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(canon && SvOK(canon) && SvCUR(canon))) return NULL; 220057 7 220064
if (!(canon && SvOK(canon) && SvCUR(canon))) return NULL; 220057 0 220057
if (!(canon && SvOK(canon) && SvCUR(canon))) return NULL; 0 220057 220057
196 220057 cp = SvPV_const(canon, cl);
197  
198 /*
199 # uncoverable branch false class:ignore_covered_err
200 # uncoverable condition right count:1 class:ignore_covered_err
201 # uncoverable condition right count:2 class:ignore_covered_err
202 # uncoverable condition right count:3 class:ignore_covered_err */
203 220057 ▶ if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 220057 0 220057
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 220057 0 220057
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 220057 0 220057
204 SV **e;
205 220057 env = (HV *)SvRV(envsv);
206 220057 e = hv_fetchs(env, "HTTP_HOST", 0);
207 /*
208 # uncoverable condition right count:2 class:ignore_covered_err
209 # uncoverable condition right count:3 class:ignore_covered_err */
210 220057 ▶ if (e && *e && SvOK(*e) && SvCUR(*e)) hostsv = *e;
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e) && SvCUR(*e)) hostsv = *e; 220053 4 220057
if (e && *e && SvOK(*e) && SvCUR(*e)) hostsv = *e; 220053 0 220053
if (e && *e && SvOK(*e) && SvCUR(*e)) hostsv = *e; 220053 0 220053
if (e && *e && SvOK(*e) && SvCUR(*e)) hostsv = *e; 220051 2 220053
211 220057 e = hv_fetchs(env, "psgi.url_scheme", 0);
212 /*
213 # uncoverable condition right count:2 class:ignore_covered_err
214 # uncoverable condition right count:3 class:ignore_covered_err */
215 220057 ▶ if (e && *e && SvOK(*e)) schemesv = *e;
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) schemesv = *e; 220053 4 220057
if (e && *e && SvOK(*e)) schemesv = *e; 220053 0 220053
if (e && *e && SvOK(*e)) schemesv = *e; 220053 0 220053
216 }
217  
218 220057 ▶ if (hostsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (hostsv) { 220051 6 220057
219 STRLEN rl, i, nl;
220 220051 const char *rp = SvPV_const(hostsv, rl);
221 220051 SV *norm = sv_2mortal(newSVpvn(rp, rl));
222 220051 char *np = SvPVX(norm);
223 const char *port;
224 STRLEN portl;
225 220051 int valid = 1, ok = 0;
226  
227 /* lowercase, and refuse anything that is not a hostname: the
228 * header is attacker-supplied, and a byte that is not [a-z0-9.-]
229 * or a :port has no business in an origin */
230 2640774 ▶ for (i = 0; i < rl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rl; i++) { 2420727 220047 2640774
231 2420727 unsigned char ch = (unsigned char)np[i];
232 2420727 ▶ if (ch >= 'A' && ch <= 'Z') { np[i] = (char)(ch + 32); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= 'A' && ch <= 'Z') { np[i] = (char)(ch + 32); continue; } 2200605 220122 2420727
if (ch >= 'A' && ch <= 'Z') { np[i] = (char)(ch + 32); continue; } 23 2200582 2200605
233 /*
234 # uncoverable condition right count:2 class:ignore_covered_err
235 # uncoverable condition right count:4 class:ignore_covered_err
236 # uncoverable condition left count:5 class:ignore_covered_err */
237 2420704 ▶ if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
Branch: 4 branches
BranchTrueFalseTotal
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') 2200582 220122 2420704
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') 0 2200582 2200582
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') 36 220086 220122
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') 8 28 36
238 2420704 ▶ || ch == '-' || ch == '.' || ch == ':') continue;
Branch: 3 branches
BranchTrueFalseTotal
|| ch == '-' || ch == '.' || ch == ':') continue; 220094 0 220094
|| ch == '-' || ch == '.' || ch == ':') continue; 12 220082 220094
|| ch == '-' || ch == '.' || ch == ':') continue; 8 4 12
239 4 valid = 0; break;
240 }
241 220051 pk_host_split(np, rl, &nl, &port, &portl);
242 /*
243 # uncoverable branch true
244 # uncoverable condition left count:1 class:ignore_covered_err
245 # uncoverable condition left count:2 class:ignore_covered_err */
246 220051 ▶ if (valid && nl == 0) valid = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (valid && nl == 0) valid = 0; 220047 4 220051
if (valid && nl == 0) valid = 0; 0 220047 220047
247 220051 ▶ if (valid && port) {
Branch: 2 branches
BranchTrueFalseTotal
if (valid && port) { 220047 4 220051
if (valid && port) { 8 220039 220047
248 /*
249 # uncoverable branch true
250 # uncoverable condition left count:1 class:ignore_covered_err
251 # uncoverable condition left count:2 class:ignore_covered_err */
252 8 ▶ if (portl == 0 || portl > 5) valid = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (portl == 0 || portl > 5) valid = 0; 8 0 8
if (portl == 0 || portl > 5) valid = 0; 0 8 8
253 38 ▶ for (i = 0; valid && i < portl; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; valid && i < portl; i++) 36 2 38
for (i = 0; valid && i < portl; i++) 30 6 36
254 30 ▶ if (!isDIGIT(port[i])) valid = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!isDIGIT(port[i])) valid = 0; 2 28 30
255 /* five digits still has to be a port */
256 /*
257 # uncoverable branch true
258 # uncoverable condition left count:1 class:ignore_covered_err
259 # uncoverable condition left count:2 class:ignore_covered_err
260 # uncoverable condition left count:3 class:ignore_covered_err
261 # uncoverable condition right count:3 class:ignore_covered_err */
262 8 ▶ if (valid && portl == 5 && memcmp(port, "65535", 5) > 0) valid = 0;
Branch: 3 branches
BranchTrueFalseTotal
if (valid && portl == 5 && memcmp(port, "65535", 5) > 0) valid = 0; 6 2 8
if (valid && portl == 5 && memcmp(port, "65535", 5) > 0) valid = 0; 0 6 6
if (valid && portl == 5 && memcmp(port, "65535", 5) > 0) valid = 0; 0 0 0
263 }
264 /* a second ':' would have ended up inside the port and failed */
265  
266 220051 ▶ if (valid) {
Branch: 1 branch
BranchTrueFalseTotal
if (valid) { 220045 6 220051
267 /* the canonical host is always its own origin */
268 220045 (void)pk_origin_span(cp, cl, &choff, &chlen);
269 {
270 STRLEN cnl; const char *cport; STRLEN cportl;
271 220045 pk_host_split(cp + choff, chlen, &cnl, &cport, &cportl);
272 220045 ▶ if (cnl == nl && memEQ(cp + choff, np, nl)) ok = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (cnl == nl && memEQ(cp + choff, np, nl)) ok = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 220006 39 220045
if (cnl == nl && memEQ(cp + choff, np, nl)) ok = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 220006 0 220006
273 }
274 220045 ▶ allow = ok ? NULL : app_get(aTHX_ h, "host_allow");
Branch: 1 branch
BranchTrueFalseTotal
allow = ok ? NULL : app_get(aTHX_ h, "host_allow"); 39 220006 220045
275 /*
276 # uncoverable condition right count:3 class:ignore_covered_err
277 # uncoverable condition right count:4 class:ignore_covered_err */
278 220045 ▶ if (!ok && allow && SvROK(allow)
Branch: 3 branches
BranchTrueFalseTotal
if (!ok && allow && SvROK(allow) 39 220006 220045
if (!ok && allow && SvROK(allow) 38 1 39
if (!ok && allow && SvROK(allow) 38 0 38
279 38 ▶ && SvTYPE(SvRV(allow)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(allow)) == SVt_PVAV) { 38 0 38
280 38 AV *list = (AV *)SvRV(allow);
281 38 SSize_t ai, an = av_len(list) + 1;
282 114 ▶ for (ai = 0; ai < an && !ok; ai++) {
Branch: 2 branches
BranchTrueFalseTotal
for (ai = 0; ai < an && !ok; ai++) { 92 22 114
for (ai = 0; ai < an && !ok; ai++) { 76 16 92
283 76 SV **e = av_fetch(list, ai, 0);
284 STRLEN el;
285 const char *ep;
286 /*
287 # uncoverable branch true
288 # uncoverable condition left count:1 class:ignore_covered_err
289 # uncoverable condition left count:2 class:ignore_covered_err
290 # uncoverable condition left count:3 class:ignore_covered_err */
291 76 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 76 0 76
if (!(e && *e && SvOK(*e))) continue; 76 0 76
if (!(e && *e && SvOK(*e))) continue; 0 76 76
292 76 ep = SvPV_const(*e, el);
293 76 ▶ if (pk_host_matches(ep, el, np, nl, port, portl)) ok = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (pk_host_matches(ep, el, np, nl, port, portl)) ok = 2; 22 54 76
294 }
295 }
296 }
297  
298 220051 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 220028 23 220051
299 SV *out;
300 220028 STRLEN sl = 0;
301 440056 ▶ const char *sp = schemesv ? SvPV_const(schemesv, sl) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *sp = schemesv ? SvPV_const(schemesv, sl) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 220028 0 220028
302 /*
303 # uncoverable condition right count:1 class:ignore_covered_err
304 # uncoverable condition right count:3 class:ignore_covered_err */
305 220028 ▶ if (sp && sl == 5 && memEQ(sp, "https", 5)) out = newSVpvs("https://");
Branch: 3 branches
BranchTrueFalseTotal
if (sp && sl == 5 && memEQ(sp, "https", 5)) out = newSVpvs("https://"); 220028 0 220028
if (sp && sl == 5 && memEQ(sp, "https", 5)) out = newSVpvs("https://"); 220024 4 220028
if (sp && sl == 5 && memEQ(sp, "https", 5)) out = newSVpvs("https://"); 220024 0 220024
306 /*
307 # uncoverable condition right count:1 class:ignore_covered_err
308 # uncoverable condition right count:3 class:ignore_covered_err */
309 4 ▶ else if (sp && sl == 4 && memEQ(sp, "http", 4)) out = newSVpvs("http://");
Branch: 3 branches
BranchTrueFalseTotal
else if (sp && sl == 4 && memEQ(sp, "http", 4)) out = newSVpvs("http://"); 4 0 4
else if (sp && sl == 4 && memEQ(sp, "http", 4)) out = newSVpvs("http://"); 2 2 4
else if (sp && sl == 4 && memEQ(sp, "http", 4)) out = newSVpvs("http://"); 2 0 2
310 else {
311 /* no usable scheme on the request: the canonical's */
312 2 STRLEN k = 0;
313 /*
314 # uncoverable condition right count:1 class:ignore_covered_err
315 # uncoverable condition left count:3 class:ignore_covered_err
316 # uncoverable condition left count:4 class:ignore_covered_err */
317 12 ▶ while (k + 2 < cl && !(cp[k] == ':' && cp[k+1] == '/'
Branch: 3 branches
BranchTrueFalseTotal
while (k + 2 < cl && !(cp[k] == ':' && cp[k+1] == '/' 12 0 12
while (k + 2 < cl && !(cp[k] == ':' && cp[k+1] == '/' 10 2 12
while (k + 2 < cl && !(cp[k] == ':' && cp[k+1] == '/' 0 2 2
318 12 ▶ && cp[k+2] == '/')) k++;
Branch: 1 branch
BranchTrueFalseTotal
&& cp[k+2] == '/')) k++; 0 2 2
319 2 out = newSVpvn(cp, k + 3);
320 }
321 220028 sv_catpvn(out, np, rl);
322 220028 *state = ok;
323 220028 return out;
324 }
325 }
326  
327 /* unknown, malformed or absent: the canonical origin, never the header */
328 29 return pk_origin_bare(aTHX_ cp, cl, NULL, NULL);
329 }
330  
331 /* May a page at this Origin talk to this application?
332 *
333 * A WebSocket upgrade is a GET that carries the user's cookies, gets no
334 * preflight, and is not covered by the same-origin policy: the browser sends
335 * it and delivers the answer whatever the page's origin. CORS cannot stand in
336 * for this - CORS refuses nothing, it omits headers and leaves the decision
337 * to the browser, which the browser does not make for ws://. So the check
338 * belongs here, and the default has to be safe without configuration.
339 *
340 * What counts as this application's own origin, in order:
341 *
342 * - the request's own Host. That is what "same origin" means, it needs no
343 * configuration, and it is sound against the attack: a page cannot set
344 * Host, the browser sends the target's. Compared on host and port and NOT
345 * on scheme, because an application behind a TLS terminator that was
346 * never declared to `proxy` sees psgi.url_scheme 'http' under an https
347 * Origin, and refusing every socket there is a worse failure than
348 * accepting a plaintext page on a host an attacker would already have to
349 * own to serve.
350 * - the canonical `host`, scheme included: that one was declared.
351 * - its `allow` entries, the other hosts this application answers as. The
352 * pages it serves are the pages that may talk back to it.
353 * - `extra`, the route's own `origin` list.
354 *
355 * Returns 1 for allowed, 0 for refused. "null" - a sandboxed frame, a data:
356 * URL, a page reached through a redirect - has no "://" and is refused with
357 * everything else that is not an origin.
358 */
359 25 static int pk_origin_ok(pTHX_ SV *c, const char *o, STRLEN ol, AV *extra) {
360 25 AV *av = pcx_av(aTHX_ c);
361 25 SV *appsv = pcx_get(aTHX_ av, PCX_APP);
362 25 SV *envsv = pcx_get(aTHX_ av, PCX_ENV);
363 STRLEN i, j, hoff, hl, nl, portl;
364 const char *port;
365 SV *low;
366 char *lp;
367  
368 136 ▶ for (i = 0; i + 2 < ol; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < ol; i++) 135 1 136
369 /*
370 # uncoverable condition right count:2 class:ignore_covered_err
371 # uncoverable condition right count:3 class:ignore_covered_err */
372 135 ▶ if (o[i] == ':' && o[i+1] == '/' && o[i+2] == '/') break;
Branch: 3 branches
BranchTrueFalseTotal
if (o[i] == ':' && o[i+1] == '/' && o[i+2] == '/') break; 24 111 135
if (o[i] == ':' && o[i+1] == '/' && o[i+2] == '/') break; 24 0 24
if (o[i] == ':' && o[i+1] == '/' && o[i+2] == '/') break; 24 0 24
373 25 ▶ if (i + 2 >= ol) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i + 2 >= ol) return 0; 1 24 25
374 24 hoff = i + 3;
375 /*
376 # uncoverable branch true
377 # uncoverable condition left count:1 class:ignore_covered_err */
378 24 ▶ if (hoff >= ol) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hoff >= ol) return 0; 0 24 24
379 24 hl = ol - hoff;
380  
381 /* the same rule pk_origin_of applies to a Host header: a byte that is not
382 * [a-z0-9.-] or a :port has no business in an origin, and a '/' here
383 * means a URL was sent where an origin was asked for */
384 24 low = sv_2mortal(newSVpvn(o + hoff, hl));
385 24 lp = SvPVX(low);
386 308 ▶ for (j = 0; j < hl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < hl; j++) { 286 22 308
387 286 unsigned char ch = (unsigned char)lp[j];
388 286 ▶ if (ch >= 'A' && ch <= 'Z') { lp[j] = (char)(ch + 32); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= 'A' && ch <= 'Z') { lp[j] = (char)(ch + 32); continue; } 235 51 286
if (ch >= 'A' && ch <= 'Z') { lp[j] = (char)(ch + 32); continue; } 9 226 235
389 /*
390 # uncoverable condition right count:2 class:ignore_covered_err
391 # uncoverable condition right count:4 class:ignore_covered_err
392 # uncoverable condition left count:5 class:ignore_covered_err */
393 277 ▶ if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
Branch: 4 branches
BranchTrueFalseTotal
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') 226 51 277
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') 0 226 226
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') 27 24 51
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') 4 23 27
394 277 ▶ || ch == '-' || ch == '.' || ch == ':') continue;
Branch: 3 branches
BranchTrueFalseTotal
|| ch == '-' || ch == '.' || ch == ':') continue; 28 0 28
|| ch == '-' || ch == '.' || ch == ':') continue; 6 22 28
|| ch == '-' || ch == '.' || ch == ':') continue; 4 2 6
395 2 return 0;
396 }
397 22 pk_host_split(lp, hl, &nl, &port, &portl);
398 /*
399 # uncoverable branch true
400 # uncoverable condition left count:1 class:ignore_covered_err */
401 22 ▶ if (nl == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nl == 0) return 0; 0 22 22
402 22 ▶ if (port) {
Branch: 1 branch
BranchTrueFalseTotal
if (port) { 4 18 22
403 /*
404 # uncoverable branch true
405 # uncoverable condition left count:1 class:ignore_covered_err
406 # uncoverable condition left count:2 class:ignore_covered_err */
407 4 ▶ if (portl == 0 || portl > 5) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (portl == 0 || portl > 5) return 0; 4 0 4
if (portl == 0 || portl > 5) return 0; 0 4 4
408 /*
409 # uncoverable branch true class:ignore_covered_err
410 # uncoverable condition left count:1 class:ignore_covered_err */
411 21 ▶ for (j = 0; j < portl; j++) if (!isDIGIT(port[j])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < portl; j++) if (!isDIGIT(port[j])) return 0; 0 17 17
for (j = 0; j < portl; j++) if (!isDIGIT(port[j])) return 0; 17 4 21
412 }
413  
414 /*
415 # uncoverable branch false
416 # uncoverable condition right count:1 class:ignore_covered_err
417 # uncoverable condition right count:2 class:ignore_covered_err
418 # uncoverable condition right count:3 class:ignore_covered_err */
419 22 ▶ if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 22 0 22
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 22 0 22
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 22 0 22
420 22 SV **e = hv_fetchs((HV *)SvRV(envsv), "HTTP_HOST", 0);
421 /*
422 # uncoverable branch false
423 # uncoverable condition right count:1 class:ignore_covered_err
424 # uncoverable condition right count:2 class:ignore_covered_err
425 # uncoverable condition right count:3 class:ignore_covered_err
426 # uncoverable condition right count:4 class:ignore_covered_err */
427 22 ▶ if (e && *e && SvOK(*e) && SvCUR(*e)) {
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e) && SvCUR(*e)) { 22 0 22
if (e && *e && SvOK(*e) && SvCUR(*e)) { 22 0 22
if (e && *e && SvOK(*e) && SvCUR(*e)) { 22 0 22
if (e && *e && SvOK(*e) && SvCUR(*e)) { 22 0 22
428 STRLEN rl;
429 22 const char *rp = SvPV_const(*e, rl);
430 22 ▶ if (rl == hl) {
Branch: 1 branch
BranchTrueFalseTotal
if (rl == hl) { 7 15 22
431 78 ▶ for (j = 0; j < rl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rl; j++) { 71 7 78
432 71 unsigned char a = (unsigned char)rp[j];
433 /*
434 # uncoverable branch true
435 # uncoverable condition left count:1 class:ignore_covered_err
436 # uncoverable condition left count:2 class:ignore_covered_err */
437 71 ▶ if (a >= 'A' && a <= 'Z') a = (unsigned char)(a + 32);
Branch: 2 branches
BranchTrueFalseTotal
if (a >= 'A' && a <= 'Z') a = (unsigned char)(a + 32); 55 16 71
if (a >= 'A' && a <= 'Z') a = (unsigned char)(a + 32); 0 55 55
438 /*
439 # uncoverable branch true
440 # uncoverable condition left count:1 class:ignore_covered_err */
441 71 ▶ if (a != (unsigned char)lp[j]) break;
Branch: 1 branch
BranchTrueFalseTotal
if (a != (unsigned char)lp[j]) break; 0 71 71
442 }
443 /*
444 # uncoverable branch false
445 # uncoverable condition right count:1 class:ignore_covered_err */
446 7 ▶ if (j == rl) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == rl) return 1; 7 0 7
447 }
448 }
449 }
450  
451 /*
452 # uncoverable branch false
453 # uncoverable condition right count:1 class:ignore_covered_err
454 # uncoverable condition right count:2 class:ignore_covered_err
455 # uncoverable condition right count:3 class:ignore_covered_err */
456 15 ▶ if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { 15 0 15
if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { 15 0 15
if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { 15 0 15
457 15 HV *h = (HV *)SvRV(appsv);
458 15 SV *canon = app_get(aTHX_ h, "host");
459 SV *allow;
460 /*
461 # uncoverable condition right count:2 class:ignore_covered_err
462 # uncoverable condition right count:3 class:ignore_covered_err */
463 15 ▶ if (canon && SvOK(canon) && SvCUR(canon)) {
Branch: 3 branches
BranchTrueFalseTotal
if (canon && SvOK(canon) && SvCUR(canon)) { 3 12 15
if (canon && SvOK(canon) && SvCUR(canon)) { 3 0 3
if (canon && SvOK(canon) && SvCUR(canon)) { 3 0 3
464 STRLEN cl;
465 3 const char *cp = SvPV_const(canon, cl);
466 3 SV *bare = sv_2mortal(pk_origin_bare(aTHX_ cp, cl, NULL, NULL));
467 STRLEN bl;
468 3 const char *bp = SvPV_const(bare, bl);
469 /*
470 # uncoverable branch true
471 # uncoverable condition left count:1 class:ignore_covered_err */
472 3 ▶ if (bl == ol) {
Branch: 1 branch
BranchTrueFalseTotal
if (bl == ol) { 0 3 3
473 /*
474 # uncoverable statement
475 # uncoverable branch true
476 # uncoverable branch false
477 # uncoverable condition left count:1
478 # uncoverable condition right count:1 */
479 0 ▶ for (j = 0; j < bl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < bl; j++) { 0 0 0
480 0 unsigned char a = (unsigned char)bp[j]; /* # uncoverable statement */
481 0 unsigned char b = (unsigned char)o[j]; /* # uncoverable statement */
482 /*
483 # uncoverable statement
484 # uncoverable branch true
485 # uncoverable branch false
486 # uncoverable condition left count:1
487 # uncoverable condition right count:1
488 # uncoverable condition left count:2
489 # uncoverable condition right count:2 */
490 0 ▶ if (a >= 'A' && a <= 'Z') a = (unsigned char)(a + 32);
Branch: 2 branches
BranchTrueFalseTotal
if (a >= 'A' && a <= 'Z') a = (unsigned char)(a + 32); 0 0 0
if (a >= 'A' && a <= 'Z') a = (unsigned char)(a + 32); 0 0 0
491 /*
492 # uncoverable statement
493 # uncoverable branch true
494 # uncoverable branch false
495 # uncoverable condition left count:1
496 # uncoverable condition right count:1
497 # uncoverable condition left count:2
498 # uncoverable condition right count:2 */
499 0 ▶ if (b >= 'A' && b <= 'Z') b = (unsigned char)(b + 32);
Branch: 2 branches
BranchTrueFalseTotal
if (b >= 'A' && b <= 'Z') b = (unsigned char)(b + 32); 0 0 0
if (b >= 'A' && b <= 'Z') b = (unsigned char)(b + 32); 0 0 0
500 /*
501 # uncoverable statement
502 # uncoverable branch true
503 # uncoverable branch false
504 # uncoverable condition left count:1
505 # uncoverable condition right count:1 */
506 0 ▶ if (a != b) break;
Branch: 1 branch
BranchTrueFalseTotal
if (a != b) break; 0 0 0
507 }
508 /*
509 # uncoverable statement
510 # uncoverable branch true
511 # uncoverable branch false
512 # uncoverable condition left count:1
513 # uncoverable condition right count:1 */
514 0 ▶ if (j == bl) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == bl) return 1; 0 0 0
515 }
516 }
517 15 allow = app_get(aTHX_ h, "host_allow");
518 /*
519 # uncoverable condition right count:2 class:ignore_covered_err
520 # uncoverable condition right count:3 class:ignore_covered_err */
521 15 ▶ if (allow && SvROK(allow) && SvTYPE(SvRV(allow)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (allow && SvROK(allow) && SvTYPE(SvRV(allow)) == SVt_PVAV) { 3 12 15
if (allow && SvROK(allow) && SvTYPE(SvRV(allow)) == SVt_PVAV) { 3 0 3
if (allow && SvROK(allow) && SvTYPE(SvRV(allow)) == SVt_PVAV) { 3 0 3
522 3 AV *list = (AV *)SvRV(allow);
523 3 SSize_t ai, an = av_len(list) + 1;
524 5 ▶ for (ai = 0; ai < an; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < an; ai++) { 3 2 5
525 3 SV **e = av_fetch(list, ai, 0);
526 STRLEN el;
527 const char *ep;
528 /*
529 # uncoverable branch true
530 # uncoverable condition left count:1 class:ignore_covered_err
531 # uncoverable condition left count:2 class:ignore_covered_err
532 # uncoverable condition left count:3 class:ignore_covered_err */
533 3 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 3 0 3
if (!(e && *e && SvOK(*e))) continue; 3 0 3
if (!(e && *e && SvOK(*e))) continue; 0 3 3
534 3 ep = SvPV_const(*e, el);
535 3 ▶ if (pk_host_matches(ep, el, lp, nl, port, portl)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pk_host_matches(ep, el, lp, nl, port, portl)) return 1; 1 2 3
536 }
537 }
538 }
539  
540 14 ▶ if (extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra) { 8 6 14
541 8 SSize_t ai, an = av_len(extra) + 1;
542 21 ▶ for (ai = 0; ai < an; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < an; ai++) { 17 4 21
543 17 SV **e = av_fetch(extra, ai, 0);
544 STRLEN el;
545 const char *ep;
546 /*
547 # uncoverable branch true
548 # uncoverable condition left count:1 class:ignore_covered_err
549 # uncoverable condition left count:2 class:ignore_covered_err
550 # uncoverable condition left count:3 class:ignore_covered_err */
551 17 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 17 0 17
if (!(e && *e && SvOK(*e))) continue; 17 0 17
if (!(e && *e && SvOK(*e))) continue; 0 17 17
552 17 ep = SvPV_const(*e, el);
553 17 ▶ if (pk_host_matches(ep, el, lp, nl, port, portl)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pk_host_matches(ep, el, lp, nl, port, portl)) return 1; 4 13 17
554 }
555 }
556 10 return 0;
557 }
558  
559 #endif /* PUNK_HOST_H */
« include/punk/punk_wsconn.h ↑ Summary include/punk/punk_health.h »