include/punk/punk_host.h

filter: Statementstmt 95.7% 182 / 190 Branchbran 71.2% 255 / 358 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.7% 437 / 548 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_oamount.h ↑ Summary include/punk/punk_dispatch.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 440233 static void pk_host_split(const char *p, STRLEN l, STRLEN *nl,
26 const char **port, STRLEN *portl) {
27 STRLEN i;
28 5282944 ▶ 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 4842711 4842750
for (i = 0; i < l; i++) if (p[i] == ':') break; 4842750 440194 5282944
29 440233 *nl = i;
30 440233 ▶ 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 440194 440233
31 440194 else { *port = NULL; *portl = 0; }
32 440233 }
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 19 static int pk_host_entry_ok(const char *p, STRLEN l) {
39 19 STRLEN i = 0, nl;
40 const char *port;
41 STRLEN portl;
42 19 ▶ if (l >= 2 && p[0] == '*' && p[1] == '.') i = 2;
Branch: 3 branches
BranchTrueFalseTotal
if (l >= 2 && p[0] == '*' && p[1] == '.') i = 2; 19 0 19
if (l >= 2 && p[0] == '*' && p[1] == '.') i = 2; 5 14 19
if (l >= 2 && p[0] == '*' && p[1] == '.') i = 2; 5 0 5
43 19 pk_host_split(p, l, &nl, &port, &portl);
44 19 ▶ if (nl <= i) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nl <= i) return 0; 0 19 19
45 193 ▶ for (; i < nl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < nl; i++) { 176 17 193
46 176 const unsigned char c = (unsigned char)p[i];
47 176 ▶ if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
Branch: 4 branches
BranchTrueFalseTotal
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 156 20 176
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 0 156 156
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 0 20 20
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 0 0 0
48 176 ▶ || c == '-' || c == '.') continue;
Branch: 2 branches
BranchTrueFalseTotal
|| c == '-' || c == '.') continue; 20 0 20
|| c == '-' || c == '.') continue; 18 2 20
49 2 return 0;
50 }
51 17 ▶ if (port) {
Branch: 1 branch
BranchTrueFalseTotal
if (port) { 5 12 17
52 5 ▶ if (portl == 0 || portl > 5) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (portl == 0 || portl > 5) return 0; 5 0 5
if (portl == 0 || portl > 5) return 0; 2 3 5
53 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
54 }
55 14 return 1;
56 }
57  
58 /* Does a request host (lowercased, split) satisfy an entry? An entry naming
59 * a port means that port; one without accepts any. A leading "*." matches
60 * any host with at least one label in front of the suffix. */
61 96 static int pk_host_matches(const char *ent, STRLEN el,
62 const char *host, STRLEN hl,
63 const char *port, STRLEN portl) {
64 STRLEN enl;
65 const char *eport;
66 STRLEN eportl;
67 96 pk_host_split(ent, el, &enl, &eport, &eportl);
68 96 ▶ if (eport && !(port && portl == eportl && memEQ(port, eport, portl)))
Branch: 4 branches
BranchTrueFalseTotal
if (eport && !(port && portl == eportl && memEQ(port, eport, portl))) 20 76 96
if (eport && !(port && portl == eportl && memEQ(port, eport, portl))) 6 14 20
if (eport && !(port && portl == eportl && memEQ(port, eport, portl))) 6 0 6
if (eport && !(port && portl == eportl && memEQ(port, eport, portl))) 3 3 6
69 17 return 0;
70 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
71 41 STRLEN sl = enl - 1; /* ".example.com" */
72 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
73 }
74 38 ▶ return hl == enl && memEQ(host, ent, hl);
Branch: 2 branches
BranchTrueFalseTotal
return hl == enl && memEQ(host, ent, hl); 14 24 38
return hl == enl && memEQ(host, ent, hl); 14 0 14
75 }
76  
77 /* An arrayref of allow entries, validated and lowercased into a new AV (+1).
78 * `what` names the caller in the croak ("host allow", "websocket origin"), so
79 * one rule about what a hostname is has one implementation and every keyword
80 * that takes a list of them refuses the same typos. */
81 16 static AV *pk_host_allow_av(pTHX_ SV *v, const char *what) {
82 AV *in, *out;
83 SSize_t ai, an;
84 16 ▶ if (!(v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)) 16 0 16
if (!(v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)) 15 1 16
if (!(v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)) 0 15 15
85 1 croak("Punk: %s needs an arrayref of hostnames", what);
86 15 in = (AV *)SvRV(v);
87 15 out = newAV();
88 15 an = av_len(in) + 1;
89 29 ▶ for (ai = 0; ai < an; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < an; ai++) { 20 9 29
90 20 SV **e = av_fetch(in, ai, 0);
91 STRLEN el, j;
92 const char *ep;
93 SV *low;
94 char *lp;
95 20 ▶ if (!(e && *e && SvOK(*e) && SvCUR(*e))) {
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e) && SvCUR(*e))) { 20 0 20
if (!(e && *e && SvOK(*e) && SvCUR(*e))) { 20 0 20
if (!(e && *e && SvOK(*e) && SvCUR(*e))) { 20 0 20
if (!(e && *e && SvOK(*e) && SvCUR(*e))) { 1 19 20
96 1 SvREFCNT_dec((SV *)out);
97 1 croak("Punk: %s entries are hostnames", what);
98 }
99 19 ep = SvPV_const(*e, el);
100 19 low = newSVpvn(ep, el);
101 19 lp = SvPVX(low);
102 254 ▶ for (j = 0; j < el; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < el; j++) 235 19 254
103 235 ▶ if (lp[j] >= 'A' && lp[j] <= 'Z') lp[j] += 32;
Branch: 2 branches
BranchTrueFalseTotal
if (lp[j] >= 'A' && lp[j] <= 'Z') lp[j] += 32; 184 51 235
if (lp[j] >= 'A' && lp[j] <= 'Z') lp[j] += 32; 6 178 184
104 19 ▶ if (!pk_host_entry_ok(lp, el)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_host_entry_ok(lp, el)) { 5 14 19
105 5 sv_2mortal(low); /* freed with the caller's frame */
106 5 SvREFCNT_dec((SV *)out);
107 5 croak("Punk: %s entry '%.*s' is not a hostname - labels of "
108 "[a-z0-9-], an optional leading *. and an optional :port",
109 what, (int)el, ep); /* the bytes as they were given */
110 }
111 14 av_push(out, low);
112 }
113 9 return out;
114 }
115  
116 /* scheme://host[:port] of a stored origin, path dropped: the part that is
117 * an origin in the browser's sense, which is what $c->origin promises. */
118 /* Where the host sits in a stored origin, and where the origin ends. No
119 * allocation: this runs on every request that asks for its origin. */
120 220098 static STRLEN pk_origin_span(const char *o, STRLEN ol, STRLEN *hostoff,
121 STRLEN *hostlen) {
122 220098 STRLEN i = 0, start, end;
123 1320588 ▶ 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++; 1320588 0 1320588
while (i + 2 < ol && !(o[i] == ':' && o[i+1] == '/' && o[i+2] == '/')) i++; 1100490 220098 1320588
while (i + 2 < ol && !(o[i] == ':' && o[i+1] == '/' && o[i+2] == '/')) i++; 0 220098 220098
while (i + 2 < ol && !(o[i] == ':' && o[i+1] == '/' && o[i+2] == '/')) i++; 0 220098 220098
124 220098 ▶ start = (i + 2 < ol) ? i + 3 : 0;
Branch: 1 branch
BranchTrueFalseTotal
start = (i + 2 < ol) ? i + 3 : 0; 220098 0 220098
125 220098 end = start;
126 2641167 ▶ while (end < ol && o[end] != '/') end++;
Branch: 2 branches
BranchTrueFalseTotal
while (end < ol && o[end] != '/') end++; 2421076 220091 2641167
while (end < ol && o[end] != '/') end++; 2421069 7 2421076
127 220098 ▶ if (hostoff) *hostoff = start;
Branch: 1 branch
BranchTrueFalseTotal
if (hostoff) *hostoff = start; 220066 32 220098
128 220098 ▶ if (hostlen) *hostlen = end - start;
Branch: 1 branch
BranchTrueFalseTotal
if (hostlen) *hostlen = end - start; 220066 32 220098
129 220098 return end;
130 }
131  
132 53 static SV *pk_origin_bare(pTHX_ const char *o, STRLEN ol, STRLEN *hostoff,
133 STRLEN *hostlen) {
134 53 return newSVpvn(o, pk_origin_span(o, ol, hostoff, hostlen));
135 }
136  
137 /* The origin a request may be told it has.
138 *
139 * Returns a new SV (+1), or NULL when the application declared no `host` -
140 * in which case there is nothing safe to say, and the caller says undef.
141 * *state is 0 when the request's Host was unknown or malformed and the
142 * canonical origin is being returned instead; 1 when it is the canonical
143 * host itself; 2 when it matched an allow entry. A consumer that renders
144 * per tenant wants 2; one that only needs a trustworthy absolute URL wants
145 * the string. */
146 220064 static SV *pk_origin_of(pTHX_ SV *c, int *state) {
147 220064 AV *av = pcx_av(aTHX_ c);
148 220064 SV *appsv = pcx_get(aTHX_ av, PCX_APP);
149 220064 SV *envsv = pcx_get(aTHX_ av, PCX_ENV);
150 220064 HV *h, *env = NULL;
151 SV *canon, *allow;
152 const char *cp;
153 STRLEN cl, choff, chlen;
154 220064 SV *hostsv = NULL, *schemesv = NULL;
155  
156 220064 *state = 0;
157 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
158 0 return NULL;
159 220064 h = (HV *)SvRV(appsv);
160 220064 canon = app_get(aTHX_ h, "host");
161 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
162 220057 cp = SvPV_const(canon, cl);
163  
164 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
165 SV **e;
166 220057 env = (HV *)SvRV(envsv);
167 220057 e = hv_fetchs(env, "HTTP_HOST", 0);
168 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
169 220057 e = hv_fetchs(env, "psgi.url_scheme", 0);
170 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
171 }
172  
173 220057 ▶ if (hostsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (hostsv) { 220051 6 220057
174 STRLEN rl, i, nl;
175 220051 const char *rp = SvPV_const(hostsv, rl);
176 220051 SV *norm = sv_2mortal(newSVpvn(rp, rl));
177 220051 char *np = SvPVX(norm);
178 const char *port;
179 STRLEN portl;
180 220051 int valid = 1, ok = 0;
181  
182 /* lowercase, and refuse anything that is not a hostname: the
183 * header is attacker-supplied, and a byte that is not [a-z0-9.-]
184 * or a :port has no business in an origin */
185 2640774 ▶ for (i = 0; i < rl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rl; i++) { 2420727 220047 2640774
186 2420727 unsigned char ch = (unsigned char)np[i];
187 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
188 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
189 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
190 4 valid = 0; break;
191 }
192 220051 pk_host_split(np, rl, &nl, &port, &portl);
193 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
194 220051 ▶ if (valid && port) {
Branch: 2 branches
BranchTrueFalseTotal
if (valid && port) { 220047 4 220051
if (valid && port) { 8 220039 220047
195 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
196 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
197 30 ▶ if (!isDIGIT(port[i])) valid = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!isDIGIT(port[i])) valid = 0; 2 28 30
198 /* five digits still has to be a port */
199 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
200 }
201 /* a second ':' would have ended up inside the port and failed */
202  
203 220051 ▶ if (valid) {
Branch: 1 branch
BranchTrueFalseTotal
if (valid) { 220045 6 220051
204 /* the canonical host is always its own origin */
205 220045 (void)pk_origin_span(cp, cl, &choff, &chlen);
206 {
207 STRLEN cnl; const char *cport; STRLEN cportl;
208 220045 pk_host_split(cp + choff, chlen, &cnl, &cport, &cportl);
209 220045 ▶ if (cnl == nl && memEQ(cp + choff, np, nl)) ok = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (cnl == nl && memEQ(cp + choff, np, nl)) ok = 1; 220006 39 220045
if (cnl == nl && memEQ(cp + choff, np, nl)) ok = 1; 220006 0 220006
210 }
211 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
212 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
213 38 ▶ && SvTYPE(SvRV(allow)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(allow)) == SVt_PVAV) { 38 0 38
214 38 AV *list = (AV *)SvRV(allow);
215 38 SSize_t ai, an = av_len(list) + 1;
216 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
217 76 SV **e = av_fetch(list, ai, 0);
218 STRLEN el;
219 const char *ep;
220 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
221 76 ep = SvPV_const(*e, el);
222 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
223 }
224 }
225 }
226  
227 220051 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 220028 23 220051
228 SV *out;
229 220028 STRLEN sl = 0;
230 440056 ▶ const char *sp = schemesv ? SvPV_const(schemesv, sl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *sp = schemesv ? SvPV_const(schemesv, sl) : NULL; 220028 0 220028
231 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
232 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
233 else {
234 /* no usable scheme on the request: the canonical's */
235 2 STRLEN k = 0;
236 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
237 12 ▶ && cp[k+2] == '/')) k++;
Branch: 1 branch
BranchTrueFalseTotal
&& cp[k+2] == '/')) k++; 0 2 2
238 2 out = newSVpvn(cp, k + 3);
239 }
240 220028 sv_catpvn(out, np, rl);
241 220028 *state = ok;
242 220028 return out;
243 }
244 }
245  
246 /* unknown, malformed or absent: the canonical origin, never the header */
247 29 return pk_origin_bare(aTHX_ cp, cl, NULL, NULL);
248 }
249  
250 /* May a page at this Origin talk to this application?
251 *
252 * A WebSocket upgrade is a GET that carries the user's cookies, gets no
253 * preflight, and is not covered by the same-origin policy: the browser sends
254 * it and delivers the answer whatever the page's origin. CORS cannot stand in
255 * for this - CORS refuses nothing, it omits headers and leaves the decision
256 * to the browser, which the browser does not make for ws://. So the check
257 * belongs here, and the default has to be safe without configuration.
258 *
259 * What counts as this application's own origin, in order:
260 *
261 * - the request's own Host. That is what "same origin" means, it needs no
262 * configuration, and it is sound against the attack: a page cannot set
263 * Host, the browser sends the target's. Compared on host and port and NOT
264 * on scheme, because an application behind a TLS terminator that was
265 * never declared to `proxy` sees psgi.url_scheme 'http' under an https
266 * Origin, and refusing every socket there is a worse failure than
267 * accepting a plaintext page on a host an attacker would already have to
268 * own to serve.
269 * - the canonical `host`, scheme included: that one was declared.
270 * - its `allow` entries, the other hosts this application answers as. The
271 * pages it serves are the pages that may talk back to it.
272 * - `extra`, the route's own `origin` list.
273 *
274 * Returns 1 for allowed, 0 for refused. "null" - a sandboxed frame, a data:
275 * URL, a page reached through a redirect - has no "://" and is refused with
276 * everything else that is not an origin.
277 */
278 25 static int pk_origin_ok(pTHX_ SV *c, const char *o, STRLEN ol, AV *extra) {
279 25 AV *av = pcx_av(aTHX_ c);
280 25 SV *appsv = pcx_get(aTHX_ av, PCX_APP);
281 25 SV *envsv = pcx_get(aTHX_ av, PCX_ENV);
282 STRLEN i, j, hoff, hl, nl, portl;
283 const char *port;
284 SV *low;
285 char *lp;
286  
287 136 ▶ for (i = 0; i + 2 < ol; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < ol; i++) 135 1 136
288 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
289 25 ▶ if (i + 2 >= ol) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i + 2 >= ol) return 0; 1 24 25
290 24 hoff = i + 3;
291 24 ▶ if (hoff >= ol) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hoff >= ol) return 0; 0 24 24
292 24 hl = ol - hoff;
293  
294 /* the same rule pk_origin_of applies to a Host header: a byte that is not
295 * [a-z0-9.-] or a :port has no business in an origin, and a '/' here
296 * means a URL was sent where an origin was asked for */
297 24 low = sv_2mortal(newSVpvn(o + hoff, hl));
298 24 lp = SvPVX(low);
299 308 ▶ for (j = 0; j < hl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < hl; j++) { 286 22 308
300 286 unsigned char ch = (unsigned char)lp[j];
301 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
302 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
303 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
304 2 return 0;
305 }
306 22 pk_host_split(lp, hl, &nl, &port, &portl);
307 22 ▶ if (nl == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nl == 0) return 0; 0 22 22
308 22 ▶ if (port) {
Branch: 1 branch
BranchTrueFalseTotal
if (port) { 4 18 22
309 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
310 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
311 }
312  
313 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
314 22 SV **e = hv_fetchs((HV *)SvRV(envsv), "HTTP_HOST", 0);
315 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
316 STRLEN rl;
317 22 const char *rp = SvPV_const(*e, rl);
318 22 ▶ if (rl == hl) {
Branch: 1 branch
BranchTrueFalseTotal
if (rl == hl) { 7 15 22
319 78 ▶ for (j = 0; j < rl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rl; j++) { 71 7 78
320 71 unsigned char a = (unsigned char)rp[j];
321 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
322 71 ▶ if (a != (unsigned char)lp[j]) break;
Branch: 1 branch
BranchTrueFalseTotal
if (a != (unsigned char)lp[j]) break; 0 71 71
323 }
324 7 ▶ if (j == rl) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == rl) return 1; 7 0 7
325 }
326 }
327 }
328  
329 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
330 15 HV *h = (HV *)SvRV(appsv);
331 15 SV *canon = app_get(aTHX_ h, "host");
332 SV *allow;
333 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
334 STRLEN cl;
335 3 const char *cp = SvPV_const(canon, cl);
336 3 SV *bare = sv_2mortal(pk_origin_bare(aTHX_ cp, cl, NULL, NULL));
337 STRLEN bl;
338 3 const char *bp = SvPV_const(bare, bl);
339 3 ▶ if (bl == ol) {
Branch: 1 branch
BranchTrueFalseTotal
if (bl == ol) { 0 3 3
340 0 ▶ for (j = 0; j < bl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < bl; j++) { 0 0 0
341 0 unsigned char a = (unsigned char)bp[j];
342 0 unsigned char b = (unsigned char)o[j];
343 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
344 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
345 0 ▶ if (a != b) break;
Branch: 1 branch
BranchTrueFalseTotal
if (a != b) break; 0 0 0
346 }
347 0 ▶ if (j == bl) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == bl) return 1; 0 0 0
348 }
349 }
350 15 allow = app_get(aTHX_ h, "host_allow");
351 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
352 3 AV *list = (AV *)SvRV(allow);
353 3 SSize_t ai, an = av_len(list) + 1;
354 5 ▶ for (ai = 0; ai < an; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < an; ai++) { 3 2 5
355 3 SV **e = av_fetch(list, ai, 0);
356 STRLEN el;
357 const char *ep;
358 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
359 3 ep = SvPV_const(*e, el);
360 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
361 }
362 }
363 }
364  
365 14 ▶ if (extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra) { 8 6 14
366 8 SSize_t ai, an = av_len(extra) + 1;
367 21 ▶ for (ai = 0; ai < an; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < an; ai++) { 17 4 21
368 17 SV **e = av_fetch(extra, ai, 0);
369 STRLEN el;
370 const char *ep;
371 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
372 17 ep = SvPV_const(*e, el);
373 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
374 }
375 }
376 10 return 0;
377 }
378  
379 #endif /* PUNK_HOST_H */
« include/punk/punk_oamount.h ↑ Summary include/punk/punk_dispatch.h »