include/punk/punk_proxy.h

filter: Statementstmt 97.0% 290 / 300 (1 uncoverable) Branchbran 83.3% 287 / 378 (44 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.3% 577 / 678 (45 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_sendfile.h ↑ Summary include/punk/punk_multipart.h »
1 #ifndef PUNK_PROXY_H
2 #define PUNK_PROXY_H
3  
4 /* punk_proxy.h - reverse-proxy trust, in C.
5 *
6 * The `proxy` keyword freezes a trust policy at to_app; pp_resolve runs once
7 * at the top of punk_serve, before routing, and OVERWRITES REMOTE_ADDR with
8 * the real client. Everything downstream - rate_limit (punk_ratelimit.h),
9 * $c->block_ip, the access log, $c->req->address - then reads the right
10 * address without any of them being changed. The mount path already rewrites
11 * env keys in place (punk_serve.h), so this is an established move.
12 *
13 * Why it exists: rate_limit keys on REMOTE_ADDR, and because Hyperman's
14 * arena makes a counter exact across the whole worker pool rather than per
15 * worker, a limiter behind a proxy puts EVERY client in one bucket - a
16 * 100/min rule throttles the whole site at 100/min. block_ip, keyed the same
17 * way, bans the load balancer.
18 *
19 * The hop rule, which is what almost every implementation gets wrong:
20 * X-Forwarded-For reads `client, proxy1, proxy2` and each hop APPENDS the
21 * address it received the connection FROM. The socket peer is the last
22 * proxy and never appears in the header it forwarded. So with N trusted
23 * proxies the client sits at index N-1 counting from the RIGHT. Taking the
24 * leftmost entry is the spoofable version: the client writes that one.
25 *
26 * Everything here is prefixed pp_ / PP_. Needs nothing but punk_compat.h;
27 * included before punk_serve.h, which calls pp_resolve.
28 */
29  
30 #include <string.h>
31  
32 /* A chain longer than this is pathological. We keep the RIGHTMOST entries,
33 * which is the end the hop walk starts from; 'all' mode scans forward for
34 * the leftmost separately and is unaffected. */
35 #define PP_MAX_HOPS 32
36  
37 /* Addresses are held as 16 bytes, v4 stored v4-mapped (::ffff:a.b.c.d), so
38 * one compare serves both families and a v4 CIDR written by an operator
39 * still matches the v4-mapped form a dual-stack listener hands us. */
40 typedef struct {
41 unsigned char addr[16];
42 int bits; /* prefix length in the 128-bit space */
43 } pp_cidr;
44  
45 typedef struct {
46 int hops; /* >0 fixed count; 0 = use cidrs; -1 = all */
47 pp_cidr *cidrs;
48 int ncidrs;
49 char *for_key; /* pre-built env keys: HTTP_X_FORWARDED_FOR */
50 STRLEN for_len;
51 char *proto_key;
52 STRLEN proto_len;
53 char *host_key;
54 STRLEN host_len;
55 char *port_key;
56 STRLEN port_len;
57 } pp_policy;
58  
59 static void pp_free(pTHX_ pp_policy *p); /* the error path in pp_compile */
60  
61 /* ---- address parsing ----------------------------------------------------
62 * Hand-rolled rather than inet_pton: this runs on attacker-controlled bytes
63 * that become a shared-memory key, the grammar wanted here is stricter than
64 * the libc one (no octal, no shortened v4), and it keeps the Windows story
65 * free of ws2tcpip.h. */
66  
67 /* Strict dotted quad: four decimal octets, no leading zeros (a leading zero
68 * is octal to some resolvers and decimal to others, which is exactly the
69 * ambiguity an address filter must not inherit), nothing trailing. */
70 110 static int pp_parse_v4(const char *s, STRLEN len, unsigned char out[4]) {
71 110 STRLEN i = 0;
72 int oct;
73 453 ▶ for (oct = 0; oct < 4; oct++) {
Branch: 1 branch
BranchTrueFalseTotal
for (oct = 0; oct < 4; oct++) { 369 84 453
74 369 int v = 0, nd = 0;
75 369 ▶ if (oct) {
Branch: 1 branch
BranchTrueFalseTotal
if (oct) { 259 110 369
76 259 ▶ if (i >= len || s[i] != '.') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i >= len || s[i] != '.') return 0; 257 2 259
if (i >= len || s[i] != '.') return 0; 1 256 257
77 256 i++;
78 }
79 857 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') { 773 84 857
while (i < len && s[i] >= '0' && s[i] <= '9') { 515 258 773
while (i < len && s[i] >= '0' && s[i] <= '9') { 501 14 515
80 501 ▶ if (nd == 0 && s[i] == '0' && i + 1 < len /* # uncoverable condition right count:5 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (nd == 0 && s[i] == '0' && i + 1 < len /* # uncoverable condition right count:5 class:ignore_covered_err */ 353 148 501
if (nd == 0 && s[i] == '0' && i + 1 < len /* # uncoverable condition right count:5 class:ignore_covered_err */ 84 269 353
if (nd == 0 && s[i] == '0' && i + 1 < len /* # uncoverable condition right count:5 class:ignore_covered_err */ 72 12 84
81 72 ▶ && s[i + 1] >= '0' && s[i + 1] <= '9')
Branch: 2 branches
BranchTrueFalseTotal
&& s[i + 1] >= '0' && s[i + 1] <= '9') 2 70 72
&& s[i + 1] >= '0' && s[i + 1] <= '9') 2 0 2
82 2 return 0; /* leading zero */
83 499 v = v * 10 + (s[i] - '0');
84 499 ▶ if (v > 255) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > 255) return 0; 8 491 499
85 491 nd++; i++;
86 /*
87 # uncoverable branch true
88 # uncoverable condition left count:1 class:ignore_covered_err */
89 491 ▶ if (nd > 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nd > 3) return 0; 0 491 491
90 }
91 356 ▶ if (!nd) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nd) return 0; 13 343 356
92 343 out[oct] = (unsigned char)v;
93 }
94 84 return i == len;
95 }
96  
97 /* RFC 4291 text form, including `::` compression and an embedded v4 tail. */
98 24 static int pp_parse_v6(const char *s, STRLEN len, unsigned char out[16]) {
99 unsigned char b[16];
100 24 int gi = 0, dc = -1;
101 24 STRLEN i = 0;
102  
103 24 memset(b, 0, sizeof(b));
104 /*
105 # uncoverable branch true
106 # uncoverable condition left count:1 class:ignore_covered_err */
107 24 ▶ if (len < 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) return 0; 0 24 24
108 24 ▶ if (s[0] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] == ':') { 22 2 24
109 /*
110 # uncoverable branch true
111 # uncoverable condition left count:1 class:ignore_covered_err */
112 2 ▶ if (s[1] != ':') return 0; /* a lone leading ':' */
Branch: 1 branch
BranchTrueFalseTotal
if (s[1] != ':') return 0; /* a lone leading ':' */ 0 2 2
113 2 dc = 0; i = 2;
114 /*
115 # uncoverable branch true
116 # uncoverable condition left count:1 class:ignore_covered_err */
117 2 ▶ if (i == len) { memcpy(out, b, 16); return 1; } /* "::" */
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) { memcpy(out, b, 16); return 1; } /* "::" */ 2 0 2
118 }
119  
120 18 for (;;) {
121 42 STRLEN j = i;
122 42 int hex = 0, nd = 0, isv4 = 0;
123 /*
124 # uncoverable branch true
125 # uncoverable condition left count:1 class:ignore_covered_err */
126 42 ▶ if (gi >= 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gi >= 8) return 0; 0 42 42
127 185 ▶ while (j < len && s[j] != ':') {
Branch: 2 branches
BranchTrueFalseTotal
while (j < len && s[j] != ':') { 173 12 185
while (j < len && s[j] != ':') { 149 24 173
128 149 ▶ if (s[j] == '.') { isv4 = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (s[j] == '.') { isv4 = 1; break; } 6 143 149
129 143 j++;
130 }
131 42 ▶ if (isv4) {
Branch: 1 branch
BranchTrueFalseTotal
if (isv4) { 6 36 42
132 unsigned char v4[4];
133 6 STRLEN k = i;
134 53 ▶ while (k < len && s[k] != ':') k++; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (k < len && s[k] != ':') k++; /* # uncoverable condition right count:2 class:ignore_covered_err */ 47 6 53
while (k < len && s[k] != ':') k++; /* # uncoverable condition right count:2 class:ignore_covered_err */ 47 0 47
135 /*
136 # uncoverable branch true class:ignore_covered_err
137 # uncoverable condition left count:1 class:ignore_covered_err */
138 10 ▶ if (k != len) return 0; /* the v4 tail must be last */
Branch: 1 branch
BranchTrueFalseTotal
if (k != len) return 0; /* the v4 tail must be last */ 0 6 6
139 /*
140 # uncoverable branch true
141 # uncoverable condition left count:1 class:ignore_covered_err */
142 6 ▶ if (gi > 6) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gi > 6) return 0; 0 6 6
143 6 ▶ if (!pp_parse_v4(s + i, len - i, v4)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_v4(s + i, len - i, v4)) return 0; 4 2 6
144 2 memcpy(b + gi * 2, v4, 4);
145 2 gi += 2;
146 2 break;
147 }
148 126 ▶ while (i < j) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < j) { 94 32 126
149 94 int c = (unsigned char)s[i], v;
150 94 ▶ if (c >= '0' && c <= '9') v = c - '0'; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') v = c - '0'; /* # uncoverable condition right count:1 class:ignore_covered_err */ 94 0 94
if (c >= '0' && c <= '9') v = c - '0'; /* # uncoverable condition right count:1 class:ignore_covered_err */ 57 37 94
151 37 ▶ else if (c >= 'a' && c <= 'f') v = c - 'a' + 10; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'a' && c <= 'f') v = c - 'a' + 10; /* # uncoverable condition right count:1 class:ignore_covered_err */ 37 0 37
else if (c >= 'a' && c <= 'f') v = c - 'a' + 10; /* # uncoverable condition right count:1 class:ignore_covered_err */ 34 3 37
152 /*
153 # uncoverable branch true
154 # uncoverable condition left count:1 class:ignore_covered_err
155 # uncoverable condition right count:1 class:ignore_covered_err
156 # uncoverable condition left count:2 class:ignore_covered_err */
157 3 ▶ else if (c >= 'A' && c <= 'F') v = c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'A' && c <= 'F') v = c - 'A' + 10; 3 0 3
else if (c >= 'A' && c <= 'F') v = c - 'A' + 10; 0 3 3
158 3 else return 0;
159 91 hex = (hex << 4) | v;
160 91 nd++;
161 91 ▶ if (nd > 4) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nd > 4) return 0; 1 90 91
162 90 i++;
163 }
164 32 ▶ if (!nd) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nd) return 0; 1 31 32
165 31 b[gi * 2] = (unsigned char)(hex >> 8);
166 31 b[gi * 2 + 1] = (unsigned char)(hex & 0xff);
167 31 gi++;
168 31 ▶ if (i == len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) break; 9 22 31
169 22 i++; /* the ':' */
170 22 ▶ if (i < len && s[i] == ':') { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == ':') { /* # uncoverable condition right count:1 class:ignore_covered_err */ 22 0 22
if (i < len && s[i] == ':') { /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 7 22
171 15 ▶ if (dc >= 0) return 0; /* a second '::' */
Branch: 1 branch
BranchTrueFalseTotal
if (dc >= 0) return 0; /* a second '::' */ 1 14 15
172 14 dc = gi;
173 14 i++;
174 14 ▶ if (i == len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) break; 3 11 14
175 /*
176 # uncoverable branch true
177 # uncoverable condition left count:1 class:ignore_covered_err */
178 7 ▶ } else if (i == len) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (i == len) { 0 7 7
179 0 return 0; /* a trailing single ':' */ /* # uncoverable statement */
180 }
181 }
182  
183 /*
184 # uncoverable branch true
185 # uncoverable condition left count:1 class:ignore_covered_err */
186 14 ▶ if (dc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (dc < 0) { 0 14 14
187 /*
188 # uncoverable statement
189 # uncoverable branch true
190 # uncoverable branch false
191 # uncoverable condition left count:1
192 # uncoverable condition right count:1 */
193 0 ▶ if (gi != 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gi != 8) return 0; 0 0 0
194 0 memcpy(out, b, 16); /* # uncoverable statement */
195 0 return 1; /* # uncoverable statement */
196 }
197 /*
198 # uncoverable branch true
199 # uncoverable condition left count:1 class:ignore_covered_err */
200 14 ▶ if (gi >= 8) return 0; /* '::' must cover >= 1 group */
Branch: 1 branch
BranchTrueFalseTotal
if (gi >= 8) return 0; /* '::' must cover >= 1 group */ 0 14 14
201 14 memset(out, 0, 16);
202 14 memcpy(out, b, (size_t)dc * 2);
203 14 memcpy(out + 16 - (size_t)(gi - dc) * 2, b + dc * 2,
204 14 (size_t)(gi - dc) * 2);
205 14 return 1;
206 }
207  
208 /* One address in either family into 16 bytes. Surrounding whitespace is
209 * trimmed; a bracketed `[::1]:443` and a `1.2.3.4:5678` port suffix are
210 * accepted and dropped, because real proxies emit both and rejecting them
211 * would silently fall back to the peer. *is_v4 reports which family was
212 * written, which is what a CIDR's prefix length is relative to.
213 *
214 * `sp`/`sl` report the span actually parsed - the address WITHOUT the
215 * brackets, port or padding. Callers that go on to store the value must use
216 * that span and not the raw token, or REMOTE_ADDR ends up as
217 * "1.2.3.4:5678" and the same client hashes to two rate-limit keys. */
218 106 static int pp_parse_addr(const char *s, STRLEN len, unsigned char out[16],
219 int *is_v4, const char **sp, STRLEN *sl) {
220 unsigned char v4[4];
221 106 int dots = 0, colons = 0;
222 STRLEN i;
223  
224 /*
225 # uncoverable branch true count:2
226 # uncoverable branch true class:ignore_covered_err
227 # uncoverable condition left count:1 class:ignore_covered_err
228 # uncoverable condition left count:2 class:ignore_covered_err
229 # uncoverable condition left count:3 class:ignore_covered_err */
230 106 ▶ while (len && (*s == ' ' || *s == '\t')) { s++; len--; }
Branch: 3 branches
BranchTrueFalseTotal
while (len && (*s == ' ' || *s == '\t')) { s++; len--; } 104 2 106
while (len && (*s == ' ' || *s == '\t')) { s++; len--; } 0 104 104
while (len && (*s == ' ' || *s == '\t')) { s++; len--; } 0 104 104
231 /*
232 # uncoverable branch true count:2
233 # uncoverable branch true class:ignore_covered_err
234 # uncoverable condition left count:1 class:ignore_covered_err
235 # uncoverable condition left count:2 class:ignore_covered_err
236 # uncoverable condition left count:3 class:ignore_covered_err */
237 106 ▶ while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--;
Branch: 3 branches
BranchTrueFalseTotal
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--; 104 2 106
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--; 0 104 104
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--; 0 104 104
238  
239 106 ▶ if (len > 1 && *s == '[') { /* [addr] or [addr]:port */
Branch: 2 branches
BranchTrueFalseTotal
if (len > 1 && *s == '[') { /* [addr] or [addr]:port */ 104 2 106
if (len > 1 && *s == '[') { /* [addr] or [addr]:port */ 2 102 104
240 2 const char *close = (const char *)memchr(s, ']', len);
241 /*
242 # uncoverable branch true
243 # uncoverable condition left count:1 class:ignore_covered_err */
244 2 ▶ if (!close) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!close) return 0; 0 2 2
245 2 s++;
246 2 len = (STRLEN)(close - s);
247 }
248 106 ▶ if (!len || len > 45) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!len || len > 45) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 104 2 106
if (!len || len > 45) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 104 104
249  
250 1020 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 916 104 1020
251 916 ▶ if (s[i] == '.') dots++;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '.') dots++; 258 658 916
252 658 ▶ else if (s[i] == ':') colons++;
Branch: 1 branch
BranchTrueFalseTotal
else if (s[i] == ':') colons++; 45 613 658
253 }
254 104 ▶ if (dots == 3 && colons == 1) { /* v4:port */
Branch: 2 branches
BranchTrueFalseTotal
if (dots == 3 && colons == 1) { /* v4:port */ 84 20 104
if (dots == 3 && colons == 1) { /* v4:port */ 1 83 84
255 5 ▶ while (len && s[len - 1] != ':') len--; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (len && s[len - 1] != ':') len--; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
while (len && s[len - 1] != ':') len--; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 1 5
256 /*
257 # uncoverable branch false
258 # uncoverable condition right count:1 class:ignore_covered_err */
259 1 ▶ if (len) len--;
Branch: 1 branch
BranchTrueFalseTotal
if (len) len--; 1 0 1
260 }
261 /*
262 # uncoverable branch true
263 # uncoverable condition left count:1 class:ignore_covered_err */
264 104 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 0 104 104
265  
266 104 ▶ if (sp) *sp = s;
Branch: 1 branch
BranchTrueFalseTotal
if (sp) *sp = s; 60 44 104
267 104 ▶ if (sl) *sl = len;
Branch: 1 branch
BranchTrueFalseTotal
if (sl) *sl = len; 60 44 104
268  
269 104 ▶ if (pp_parse_v4(s, len, v4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp_parse_v4(s, len, v4)) { 80 24 104
270 80 memset(out, 0, 10);
271 80 out[10] = 0xff; out[11] = 0xff;
272 80 memcpy(out + 12, v4, 4);
273 /*
274 # uncoverable branch false
275 # uncoverable condition right count:1 class:ignore_covered_err */
276 80 ▶ if (is_v4) *is_v4 = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (is_v4) *is_v4 = 1; 80 0 80
277 80 return 1;
278 }
279 /*
280 # uncoverable branch false
281 # uncoverable condition right count:1 class:ignore_covered_err */
282 24 ▶ if (is_v4) *is_v4 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (is_v4) *is_v4 = 0; 24 0 24
283 24 return pp_parse_v6(s, len, out);
284 }
285  
286 /* `addr` or `addr/prefixlen`. A v4 prefix is taken in the v4 space and
287 * lifted into the mapped range, so 10.0.0.0/8 is stored as /104 and an
288 * operator never has to know that. Returns 0 on anything malformed, which
289 * the keyword turns into a boot croak - a mistyped CIDR must not become a
290 * policy that quietly trusts nothing. */
291 17 static int pp_parse_cidr(const char *s, STRLEN len, pp_cidr *out) {
292 17 const char *slash = (const char *)memchr(s, '/', len);
293 17 ▶ STRLEN alen = slash ? (STRLEN)(slash - s) : len;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN alen = slash ? (STRLEN)(slash - s) : len; 15 2 17
294 17 int is_v4 = 0, bits;
295  
296 17 ▶ if (!pp_parse_addr(s, alen, out->addr, &is_v4, NULL, NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_addr(s, alen, out->addr, &is_v4, NULL, NULL)) return 0; 1 16 17
297  
298 16 ▶ if (!slash) {
Branch: 1 branch
BranchTrueFalseTotal
if (!slash) { 1 15 16
299 1 out->bits = 128; /* a bare address is a host */
300 1 return 1;
301 }
302 {
303 15 STRLEN i = alen + 1;
304 15 int v = 0, nd = 0;
305 /*
306 # uncoverable condition right count:2 class:ignore_covered_err
307 # uncoverable condition right count:3 class:ignore_covered_err */
308 32 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') { 17 15 32
while (i < len && s[i] >= '0' && s[i] <= '9') { 17 0 17
while (i < len && s[i] >= '0' && s[i] <= '9') { 17 0 17
309 17 v = v * 10 + (s[i] - '0');
310 /*
311 # uncoverable branch true
312 # uncoverable condition left count:1 class:ignore_covered_err */
313 17 ▶ if (v > 128) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > 128) return 0; 0 17 17
314 17 nd++; i++;
315 /*
316 # uncoverable branch true
317 # uncoverable condition left count:1 class:ignore_covered_err */
318 17 ▶ if (nd > 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nd > 3) return 0; 0 17 17
319 }
320 /*
321 # uncoverable branch true
322 # uncoverable condition left count:1 class:ignore_covered_err
323 # uncoverable condition left count:2 class:ignore_covered_err */
324 15 ▶ if (!nd || i != len) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!nd || i != len) return 0; 15 0 15
if (!nd || i != len) return 0; 0 15 15
325 15 bits = v;
326 }
327 15 ▶ if (is_v4) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_v4) { 12 3 15
328 12 ▶ if (bits > 32) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (bits > 32) return 0; 1 11 12
329 11 bits += 96; /* into the mapped range */
330 }
331 14 out->bits = bits;
332 14 return 1;
333 }
334  
335 60 static int pp_cidr_match(const pp_cidr *c, const unsigned char a[16]) {
336 60 int full = c->bits >> 3, rem = c->bits & 7;
337 60 ▶ if (full && memcmp(c->addr, a, (size_t)full) != 0) return 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (full && memcmp(c->addr, a, (size_t)full) != 0) return 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 60 0 60
if (full && memcmp(c->addr, a, (size_t)full) != 0) return 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 33 27 60
338 27 ▶ if (rem) {
Branch: 1 branch
BranchTrueFalseTotal
if (rem) { 1 26 27
339 1 unsigned char m = (unsigned char)(0xff << (8 - rem));
340 /*
341 # uncoverable branch true
342 # uncoverable condition left count:1 class:ignore_covered_err */
343 1 ▶ if ((c->addr[full] & m) != (a[full] & m)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((c->addr[full] & m) != (a[full] & m)) return 0; 0 1 1
344 }
345 27 return 1;
346 }
347  
348 47 static int pp_trusted(const pp_policy *p, const unsigned char a[16]) {
349 int i;
350 80 ▶ for (i = 0; i < p->ncidrs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p->ncidrs; i++) 60 20 80
351 60 ▶ if (pp_cidr_match(&p->cidrs[i], a)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pp_cidr_match(&p->cidrs[i], a)) return 1; 27 33 60
352 20 return 0;
353 }
354  
355 /* ---- the header walk ---------------------------------------------------- */
356  
357 /* The leftmost comma-separated entry, trimmed. */
358 20 static const char *pp_first_token(const char *s, STRLEN len, STRLEN *outlen) {
359 20 const char *comma = (const char *)memchr(s, ',', len);
360 20 ▶ STRLEN l = comma ? (STRLEN)(comma - s) : len;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN l = comma ? (STRLEN)(comma - s) : len; 2 18 20
361 /*
362 # uncoverable branch true count:2
363 # uncoverable branch true class:ignore_covered_err
364 # uncoverable condition left count:1 class:ignore_covered_err
365 # uncoverable condition right count:1 class:ignore_covered_err
366 # uncoverable condition left count:2 class:ignore_covered_err
367 # uncoverable condition left count:3 class:ignore_covered_err */
368 20 ▶ while (l && (*s == ' ' || *s == '\t')) { s++; l--; }
Branch: 3 branches
BranchTrueFalseTotal
while (l && (*s == ' ' || *s == '\t')) { s++; l--; } 20 0 20
while (l && (*s == ' ' || *s == '\t')) { s++; l--; } 0 20 20
while (l && (*s == ' ' || *s == '\t')) { s++; l--; } 0 20 20
369 /*
370 # uncoverable branch true count:2
371 # uncoverable branch true class:ignore_covered_err
372 # uncoverable condition left count:1 class:ignore_covered_err
373 # uncoverable condition right count:1 class:ignore_covered_err
374 # uncoverable condition left count:2 class:ignore_covered_err
375 # uncoverable condition left count:3 class:ignore_covered_err */
376 20 ▶ while (l && (s[l - 1] == ' ' || s[l - 1] == '\t')) l--;
Branch: 3 branches
BranchTrueFalseTotal
while (l && (s[l - 1] == ' ' || s[l - 1] == '\t')) l--; 20 0 20
while (l && (s[l - 1] == ' ' || s[l - 1] == '\t')) l--; 0 20 20
while (l && (s[l - 1] == ' ' || s[l - 1] == '\t')) l--; 0 20 20
377 20 *outlen = l;
378 20 return s;
379 }
380  
381 /* The rightmost entries, trimmed, tok[0] being the last one on the line.
382 * Empty entries are KEPT rather than skipped: dropping them would shift the
383 * hop count, and a malformed entry must end the walk, not shorten it. */
384 54 static int pp_collect_right(const char *s, STRLEN len,
385 const char **tok, STRLEN *tlen, int max) {
386 54 int n = 0;
387 54 STRLEN end = len;
388 56 for (;;) {
389 110 STRLEN start = end;
390 const char *p;
391 STRLEN l;
392 110 ▶ if (n >= max) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= max) break; 1 109 110
393 1070 ▶ while (start > 0 && s[start - 1] != ',') start--;
Branch: 2 branches
BranchTrueFalseTotal
while (start > 0 && s[start - 1] != ',') start--; 1017 53 1070
while (start > 0 && s[start - 1] != ',') start--; 961 56 1017
394 109 p = s + start;
395 109 l = end - start;
396 167 ▶ while (l && (*p == ' ' || *p == '\t')) { p++; l--; }
Branch: 3 branches
BranchTrueFalseTotal
while (l && (*p == ' ' || *p == '\t')) { p++; l--; } 166 1 167
while (l && (*p == ' ' || *p == '\t')) { p++; l--; } 57 109 166
while (l && (*p == ' ' || *p == '\t')) { p++; l--; } 1 108 109
397 111 ▶ while (l && (p[l - 1] == ' ' || p[l - 1] == '\t')) l--; /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
while (l && (p[l - 1] == ' ' || p[l - 1] == '\t')) l--; /* # uncoverable condition left count:3 class:ignore_covered_err */ 110 1 111
while (l && (p[l - 1] == ' ' || p[l - 1] == '\t')) l--; /* # uncoverable condition left count:3 class:ignore_covered_err */ 2 108 110
while (l && (p[l - 1] == ' ' || p[l - 1] == '\t')) l--; /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 108 108
398 109 tok[n] = p;
399 109 tlen[n] = l;
400 109 n++;
401 109 ▶ if (start == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (start == 0) break; 53 56 109
402 56 end = start - 1;
403 }
404 54 return n;
405 }
406  
407 /* The real client, as a pointer into `xff` or into `peer`. Never returns
408 * NULL: with nothing trustworthy to say, the answer is the socket peer,
409 * which is the address that is true by construction. */
410 58 static const char *pp_xff_client(const char *xff, STRLEN len,
411 const pp_policy *p,
412 const char *peer, STRLEN plen,
413 STRLEN *outlen) {
414 const char *tok[PP_MAX_HOPS];
415 STRLEN tlen[PP_MAX_HOPS];
416 unsigned char a[16];
417 const char *sp;
418 STRLEN sl;
419 int n, i, isv4;
420  
421 58 *outlen = plen;
422 58 ▶ if (!xff || !len) return peer;
Branch: 2 branches
BranchTrueFalseTotal
if (!xff || !len) return peer; 57 1 58
if (!xff || !len) return peer; 2 55 57
423  
424 55 ▶ if (p->hops < 0) { /* trust => 'all' */
Branch: 1 branch
BranchTrueFalseTotal
if (p->hops < 0) { /* trust => 'all' */ 1 54 55
425 STRLEN l;
426 1 const char *t = pp_first_token(xff, len, &l);
427 /*
428 # uncoverable branch false
429 # uncoverable condition right count:1 class:ignore_covered_err
430 # uncoverable condition right count:2 class:ignore_covered_err */
431 1 ▶ if (l && pp_parse_addr(t, l, a, &isv4, &sp, &sl)) {
Branch: 2 branches
BranchTrueFalseTotal
if (l && pp_parse_addr(t, l, a, &isv4, &sp, &sl)) { 1 0 1
if (l && pp_parse_addr(t, l, a, &isv4, &sp, &sl)) { 1 0 1
432 1 *outlen = sl;
433 1 return sp;
434 }
435 0 return peer; /* # uncoverable statement */
436 }
437  
438 54 n = pp_collect_right(xff, len, tok, tlen, PP_MAX_HOPS);
439 /*
440 # uncoverable branch true
441 # uncoverable condition left count:1 class:ignore_covered_err */
442 54 ▶ if (n <= 0) return peer;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return peer; 0 54 54
443  
444 54 ▶ if (p->hops > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->hops > 0) { 40 14 54
445 /* N trusted proxies: the client is at index N-1 from the right. A
446 * chain shorter than declared is a misconfiguration or a client
447 * that sent nothing - either way the peer is the honest answer, and
448 * reaching for the leftmost entry here is precisely the bug. */
449 40 i = p->hops - 1;
450 40 ▶ if (i >= n) return peer;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= n) return peer; 2 38 40
451 38 ▶ if (!pp_parse_addr(tok[i], tlen[i], a, &isv4, &sp, &sl)) return peer;
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_addr(tok[i], tlen[i], a, &isv4, &sp, &sl)) return peer; 9 29 38
452 29 *outlen = sl;
453 29 return sp;
454 }
455  
456 /* CIDR mode. The peer must itself be a trusted proxy, or the header is
457 * not ours to believe at all. */
458 14 ▶ if (!(pp_parse_addr(peer, plen, a, &isv4, NULL, NULL) && pp_trusted(p, a))) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(pp_parse_addr(peer, plen, a, &isv4, NULL, NULL) && pp_trusted(p, a))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 14 0 14
if (!(pp_parse_addr(peer, plen, a, &isv4, NULL, NULL) && pp_trusted(p, a))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 13 14
459 1 return peer;
460 22 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 21 1 22
461 /*
462 # uncoverable branch true
463 # uncoverable condition left count:1 class:ignore_covered_err */
464 21 ▶ if (!pp_parse_addr(tok[i], tlen[i], a, &isv4, &sp, &sl)) return peer;
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_addr(tok[i], tlen[i], a, &isv4, &sp, &sl)) return peer; 0 21 21
465 21 ▶ if (!pp_trusted(p, a)) { *outlen = sl; return sp; }
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_trusted(p, a)) { *outlen = sl; return sp; } 12 9 21
466 }
467 /* every entry was a trusted proxy: the leftmost is the client */
468 /*
469 # uncoverable branch true
470 # uncoverable condition left count:1 class:ignore_covered_err */
471 1 ▶ if (!pp_parse_addr(tok[n - 1], tlen[n - 1], a, &isv4, &sp, &sl))
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_addr(tok[n - 1], tlen[n - 1], a, &isv4, &sp, &sl)) 0 1 1
472 0 return peer; /* # uncoverable statement */
473 1 *outlen = sl;
474 1 return sp;
475 }
476  
477 /* ---- the boot-time build ------------------------------------------------- */
478  
479 /* A header name into the PSGI env key it arrives as: uppercased, '-' to '_',
480 * HTTP_ prefixed. Done once at to_app so a request is a hash lookup on a
481 * string we already own. */
482 184 static char *pp_env_key(pTHX_ const char *name, STRLEN nlen, STRLEN *outlen) {
483 char *k;
484 STRLEN i;
485 184 Newx(k, nlen + 6, char);
486 184 memcpy(k, "HTTP_", 5);
487 3129 ▶ for (i = 0; i < nlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlen; i++) { 2945 184 3129
488 2945 char c = name[i];
489 2945 ▶ k[5 + i] = (c == '-') ? '_' : (char)toUPPER((unsigned char)c);
Branch: 2 branches
BranchTrueFalseTotal
k[5 + i] = (c == '-') ? '_' : (char)toUPPER((unsigned char)c); 2577 368 2945
k[5 + i] = (c == '-') ? '_' : (char)toUPPER((unsigned char)c); 2023 554 2577
490 }
491 184 k[5 + nlen] = '\0';
492 184 *outlen = nlen + 5;
493 184 return k;
494 }
495  
496 184 static char *pp_key_opt(pTHX_ HV *cfg, const char *opt, const char *dflt,
497 STRLEN *outlen) {
498 184 SV **v = hv_fetch(cfg, opt, (I32)strlen(opt), 0);
499 STRLEN nl;
500 const char *n;
501 /*
502 # uncoverable branch true
503 # uncoverable condition left count:1 class:ignore_covered_err
504 # uncoverable condition left count:2 class:ignore_covered_err
505 # uncoverable condition right count:2 class:ignore_covered_err
506 # uncoverable condition left count:3 class:ignore_covered_err */
507 184 ▶ if (v && *v && !SvOK(*v)) return NULL; /* undef switches it off */
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && !SvOK(*v)) return NULL; /* undef switches it off */ 1 183 184
if (v && *v && !SvOK(*v)) return NULL; /* undef switches it off */ 1 0 1
if (v && *v && !SvOK(*v)) return NULL; /* undef switches it off */ 0 1 1
508 185 ▶ n = (v && *v) ? SvPV_const(*v, nl) : (nl = strlen(dflt), dflt); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
n = (v && *v) ? SvPV_const(*v, nl) : (nl = strlen(dflt), dflt); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 183 184
n = (v && *v) ? SvPV_const(*v, nl) : (nl = strlen(dflt), dflt); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
509 /*
510 # uncoverable branch true
511 # uncoverable condition left count:1 class:ignore_covered_err */
512 184 ▶ if (!nl) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return NULL; 0 184 184
513 184 return pp_env_key(aTHX_ n, nl, outlen);
514 }
515  
516 /* Freeze the keyword's config into the policy a request reads. Croaks on
517 * anything malformed - a mistyped CIDR or a nonsense hop count must fail at
518 * boot, where it is one line of output, not per request where it is a
519 * silently wrong address in a rate-limit key. `appenv` is $app->env. */
520 54 static pp_policy *pp_compile(pTHX_ HV *cfg, const char *appenv) {
521 pp_policy *p;
522 54 SV **t = hv_fetchs(cfg, "trust", 0);
523  
524 54 Newxz(p, 1, pp_policy);
525 54 p->hops = 1; /* bare `proxy` = one hop */
526  
527 /*
528 # uncoverable condition right count:2 class:ignore_covered_err
529 # uncoverable condition right count:3 class:ignore_covered_err */
530 54 ▶ if (t && *t && SvOK(*t)) {
Branch: 3 branches
BranchTrueFalseTotal
if (t && *t && SvOK(*t)) { 49 5 54
if (t && *t && SvOK(*t)) { 49 0 49
if (t && *t && SvOK(*t)) { 49 0 49
531 49 ▶ if (SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV) { 16 33 49
if (SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV) { 15 1 16
532 15 AV *av = (AV *)SvRV(*t);
533 15 SSize_t i, n = av_len(av) + 1;
534 15 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 1 14 15
535 1 pp_free(aTHX_ p);
536 1 croak("Punk: proxy: trust => [] trusts nothing - drop the "
537 "keyword instead, or name the proxy networks");
538 }
539 14 Newxz(p->cidrs, (int)n, pp_cidr);
540 29 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17 12 29
541 17 SV **e = av_fetch(av, i, 0);
542 STRLEN sl;
543 const char *s;
544 /*
545 # uncoverable branch true
546 # uncoverable condition left count:1 class:ignore_covered_err
547 # uncoverable condition left count:2 class:ignore_covered_err
548 # uncoverable condition left count:3 class:ignore_covered_err */
549 17 ▶ if (!(e && *e && SvOK(*e))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) { 17 0 17
if (!(e && *e && SvOK(*e))) { 17 0 17
if (!(e && *e && SvOK(*e))) { 0 17 17
550 0 pp_free(aTHX_ p); /* # uncoverable statement */
551 0 croak("Punk: proxy: trust list entry %d is undef", /* # uncoverable statement */
552 (int)i); /* # uncoverable statement */
553 }
554 17 s = SvPV_const(*e, sl);
555 17 ▶ if (!pp_parse_cidr(s, sl, &p->cidrs[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_cidr(s, sl, &p->cidrs[i])) { 2 15 17
556 2 SV *msg = sv_2mortal(newSVpvf(
557 "Punk: proxy: '%.*s' is not an address or CIDR",
558 (int)sl, s));
559 2 pp_free(aTHX_ p);
560 2 croak("%s", SvPV_nolen(msg));
561 }
562 }
563 12 p->ncidrs = (int)n;
564 12 p->hops = 0;
565 }
566 34 ▶ else if (!SvROK(*t) && !looks_like_number(*t)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!SvROK(*t) && !looks_like_number(*t)) { 33 1 34
else if (!SvROK(*t) && !looks_like_number(*t)) { 5 28 33
567 STRLEN sl;
568 5 const char *s = SvPV_const(*t, sl);
569 5 ▶ if (sl == 3 && memEQ(s, "all", 3)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (sl == 3 && memEQ(s, "all", 3)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 1 5
if (sl == 3 && memEQ(s, "all", 3)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
570 4 ▶ if (!(appenv && strEQ(appenv, "development"))) { /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(appenv && strEQ(appenv, "development"))) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 4 0 4
if (!(appenv && strEQ(appenv, "development"))) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 3 4
571 1 pp_free(aTHX_ p);
572 1 croak("Punk: proxy: trust => 'all' believes any "
573 "X-Forwarded-For from anyone, so with no proxy in "
574 "front it is a total bypass - it is allowed only "
575 "under PUNK_ENV=development. Name a hop count or "
576 "the proxy networks instead");
577 }
578 3 p->hops = -1;
579 } else {
580 1 SV *msg = sv_2mortal(newSVpvf(
581 "Punk: proxy: trust must be a hop count, an arrayref of "
582 "CIDRs, or 'all' - not '%.*s'", (int)sl, s));
583 1 pp_free(aTHX_ p);
584 1 croak("%s", SvPV_nolen(msg));
585 }
586 }
587 29 ▶ else if (SvROK(*t)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(*t)) { 1 28 29
588 1 pp_free(aTHX_ p);
589 1 croak("Punk: proxy: trust must be a hop count, an arrayref of "
590 "CIDRs, or 'all'");
591 }
592 else {
593 28 IV n = SvIV(*t);
594 28 ▶ if (n < 1 || n > PP_MAX_HOPS) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n < 1 || n > PP_MAX_HOPS) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 26 2 28
if (n < 1 || n > PP_MAX_HOPS) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 26 26
595 2 pp_free(aTHX_ p);
596 2 croak("Punk: proxy: trust => %" IVdf " is not a usable hop "
597 "count (1..%d)", n, PP_MAX_HOPS);
598 }
599 26 p->hops = (int)n;
600 }
601 }
602  
603 46 p->for_key = pp_key_opt(aTHX_ cfg, "for_header",
604 "X-Forwarded-For", &p->for_len);
605 46 p->proto_key = pp_key_opt(aTHX_ cfg, "proto_header",
606 "X-Forwarded-Proto", &p->proto_len);
607 46 p->host_key = pp_key_opt(aTHX_ cfg, "host_header",
608 "X-Forwarded-Host", &p->host_len);
609 46 p->port_key = pp_key_opt(aTHX_ cfg, "port_header",
610 "X-Forwarded-Port", &p->port_len);
611 46 return p;
612 }
613  
614 /* ---- the per-request resolution ----------------------------------------- */
615  
616 54 static void pp_free(pTHX_ pp_policy *p) {
617 /*
618 # uncoverable branch true
619 # uncoverable condition left count:1 class:ignore_covered_err */
620 54 ▶ if (!p) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return; 0 54 54
621 54 Safefree(p->cidrs);
622 54 Safefree(p->for_key);
623 54 Safefree(p->proto_key);
624 54 Safefree(p->host_key);
625 54 Safefree(p->port_key);
626 54 Safefree(p);
627 }
628  
629 /* Called once per request from the top of punk_serve, and only when a
630 * policy was declared - an app without `proxy` pays one predictable
631 * branch and nothing else. */
632 40 static void pp_resolve(pTHX_ const pp_policy *p, HV *env) {
633 SV **e;
634 SV *peersv;
635 const char *peer;
636 40 STRLEN plen = 0;
637  
638 40 e = hv_fetchs(env, "REMOTE_ADDR", 0);
639 /*
640 # uncoverable condition right count:1 class:ignore_covered_err
641 # uncoverable condition right count:2 class:ignore_covered_err
642 # uncoverable condition right count:3 class:ignore_covered_err */
643 40 ▶ peersv = (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
peersv = (e && *e && SvOK(*e)) ? *e : NULL; 40 0 40
peersv = (e && *e && SvOK(*e)) ? *e : NULL; 40 0 40
peersv = (e && *e && SvOK(*e)) ? *e : NULL; 40 0 40
644 80 ▶ peer = peersv ? SvPV_const(peersv, plen) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
peer = peersv ? SvPV_const(peersv, plen) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 40 0 40
645  
646 /* the socket peer, kept before anything is rewritten, so an edge ban
647 * still has the address the connection actually came from */
648 40 ▶ (void)hv_stores(env, "punk.peer_addr",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(env, "punk.peer_addr", 40 0 40
649 peersv ? newSVsv(peersv) : newSVpvs(""));
650  
651 /* ONE trust decision, and every rewrite below is behind it. A hop count
652 * or `all` trusts whoever connected, by declaration. A network list
653 * trusts a peer inside it and nobody else: the forwarded scheme, host
654 * and port from any other peer are a stranger's claims about a request
655 * that never went near the proxy. */
656 40 ▶ if (p->hops == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->hops == 0) { 14 26 40
657 unsigned char a[16];
658 int isv4;
659 26 if (!(pp_parse_addr(peer, plen, a, &isv4, NULL, NULL)
660 12 && pp_trusted(p, a)))
661 9 return;
662 }
663  
664 31 ▶ e = p->for_key ? hv_fetch(env, p->for_key, (I32)p->for_len, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
e = p->for_key ? hv_fetch(env, p->for_key, (I32)p->for_len, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 31 0 31
665 /*
666 # uncoverable condition right count:2 class:ignore_covered_err
667 # uncoverable condition right count:3 class:ignore_covered_err */
668 31 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 22 9 31
if (e && *e && SvOK(*e)) { 22 0 22
if (e && *e && SvOK(*e)) { 22 0 22
669 STRLEN xl, clen;
670 22 const char *x = SvPV_const(*e, xl);
671 22 const char *client = pp_xff_client(x, xl, p, peer, plen, &clen);
672 /*
673 # uncoverable branch false
674 # uncoverable condition right count:1 class:ignore_covered_err
675 # uncoverable condition right count:2 class:ignore_covered_err
676 # uncoverable condition right count:3 class:ignore_covered_err */
677 22 ▶ if (clen != plen || (clen && memNE(client, peer, clen))) {
Branch: 3 branches
BranchTrueFalseTotal
if (clen != plen || (clen && memNE(client, peer, clen))) { 2 20 22
if (clen != plen || (clen && memNE(client, peer, clen))) { 2 0 2
if (clen != plen || (clen && memNE(client, peer, clen))) { 2 0 2
678 /* copy before the store: `peer` points into the SV it frees */
679 22 SV *newsv = newSVpvn(client, clen);
680 22 (void)hv_stores(env, "REMOTE_ADDR", newsv);
681 /* the port described the proxy's socket and is now a lie */
682 22 (void)hv_delete(env, "REMOTE_PORT", 11, G_DISCARD);
683 }
684 }
685  
686 /*
687 # uncoverable branch false
688 # uncoverable condition right count:1 class:ignore_covered_err */
689 31 ▶ if (p->proto_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->proto_key) { 31 0 31
690 31 e = hv_fetch(env, p->proto_key, (I32)p->proto_len, 0);
691 /*
692 # uncoverable condition right count:2 class:ignore_covered_err
693 # uncoverable condition right count:3 class:ignore_covered_err */
694 31 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 9 22 31
if (e && *e && SvOK(*e)) { 9 0 9
if (e && *e && SvOK(*e)) { 9 0 9
695 STRLEN vl, l;
696 9 const char *v = SvPV_const(*e, vl);
697 9 const char *t = pp_first_token(v, vl, &l);
698 9 ▶ if (l == 5 && foldEQ(t, "https", 5)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && foldEQ(t, "https", 5)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 1 9
if (l == 5 && foldEQ(t, "https", 5)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 0 8
699 8 (void)hv_stores(env, "psgi.url_scheme", newSVpvs("https"));
700 8 (void)hv_stores(env, "HTTPS", newSVpvs("on"));
701 /*
702 # uncoverable branch true
703 # uncoverable condition left count:1 class:ignore_covered_err
704 # uncoverable condition left count:2 class:ignore_covered_err
705 # uncoverable condition right count:2 class:ignore_covered_err */
706 1 ▶ } else if (l == 4 && foldEQ(t, "http", 4)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (l == 4 && foldEQ(t, "http", 4)) { 0 1 1
} else if (l == 4 && foldEQ(t, "http", 4)) { 0 0 0
707 0 (void)hv_stores(env, "psgi.url_scheme", newSVpvs("http")); /* # uncoverable statement */
708 0 (void)hv_delete(env, "HTTPS", 5, G_DISCARD); /* # uncoverable statement */
709 }
710 }
711 }
712  
713 /*
714 # uncoverable branch false
715 # uncoverable condition right count:1 class:ignore_covered_err */
716 31 ▶ if (p->host_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->host_key) { 31 0 31
717 31 e = hv_fetch(env, p->host_key, (I32)p->host_len, 0);
718 /*
719 # uncoverable condition right count:2 class:ignore_covered_err
720 # uncoverable condition right count:3 class:ignore_covered_err */
721 31 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 5 26 31
if (e && *e && SvOK(*e)) { 5 0 5
if (e && *e && SvOK(*e)) { 5 0 5
722 STRLEN vl, l;
723 5 const char *v = SvPV_const(*e, vl);
724 5 const char *t = pp_first_token(v, vl, &l);
725 /*
726 # uncoverable branch false
727 # uncoverable condition right count:1 class:ignore_covered_err */
728 5 ▶ if (l) (void)hv_stores(env, "HTTP_HOST", newSVpvn(t, l));
Branch: 1 branch
BranchTrueFalseTotal
if (l) (void)hv_stores(env, "HTTP_HOST", newSVpvn(t, l)); 5 0 5
729 }
730 }
731  
732 /*
733 # uncoverable branch false
734 # uncoverable condition right count:1 class:ignore_covered_err */
735 31 ▶ if (p->port_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->port_key) { 31 0 31
736 31 e = hv_fetch(env, p->port_key, (I32)p->port_len, 0);
737 /*
738 # uncoverable condition right count:2 class:ignore_covered_err
739 # uncoverable condition right count:3 class:ignore_covered_err */
740 31 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 5 26 31
if (e && *e && SvOK(*e)) { 5 0 5
if (e && *e && SvOK(*e)) { 5 0 5
741 STRLEN vl, l;
742 5 const char *v = SvPV_const(*e, vl);
743 5 const char *t = pp_first_token(v, vl, &l);
744 STRLEN i;
745 /*
746 # uncoverable condition right count:1 class:ignore_covered_err
747 # uncoverable condition right count:2 class:ignore_covered_err */
748 5 ▶ int ok = l > 0 && l <= 5;
Branch: 2 branches
BranchTrueFalseTotal
int ok = l > 0 && l <= 5; 5 0 5
int ok = l > 0 && l <= 5; 5 0 5
749 20 ▶ for (i = 0; ok && i < l; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; ok && i < l; i++) 19 1 20
for (i = 0; ok && i < l; i++) 15 4 19
750 15 ▶ if (t[i] < '0' || t[i] > '9') ok = 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (t[i] < '0' || t[i] > '9') ok = 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 15 0 15
if (t[i] < '0' || t[i] > '9') ok = 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 14 15
751 5 ▶ if (ok) (void)hv_stores(env, "SERVER_PORT", newSVpvn(t, l));
Branch: 1 branch
BranchTrueFalseTotal
if (ok) (void)hv_stores(env, "SERVER_PORT", newSVpvn(t, l)); 4 1 5
752 }
753 }
754 }
755  
756 #endif /* PUNK_PROXY_H */
« include/punk/punk_sendfile.h ↑ Summary include/punk/punk_multipart.h »